package com.finance.web.controller.api; import com.alibaba.excel.EasyExcel; import com.finance.common.basic.PageDTO; import com.finance.common.core.domain.R; import com.finance.common.core.domain.entity.SysUser; import com.finance.common.exception.ServiceException; import com.finance.common.utils.BeanUtils; import com.finance.common.utils.CollUtils; import com.finance.common.utils.EasyExcelUtil; import com.finance.common.utils.SecurityUtils; import com.finance.system.dto.update.DeptFocusDTO; import com.finance.system.dto.update.DeptUpdateDTO; import com.finance.system.handler.CustomCellWriteHandler; import com.finance.system.handler.TitleHandler; import com.finance.system.query.DeptQuery; import com.finance.system.service.ISysUserService; import com.finance.system.service.TbDeptService; import com.finance.system.vo.DeptVO; import com.finance.web.controller.excel.DeptExcel; import com.finance.web.controller.lisenter.DeptImportListener; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import java.io.InputStream; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import java.util.Optional; import javax.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** *

* 部门管理表 前端控制器 *

* * @author mitao * @since 2024-03-13 */ @Slf4j @RestController @RequestMapping("/dept") @RequiredArgsConstructor @Api(tags = "部门管理相关接口") public class TbDeptController { private final HttpServletResponse response; private final TbDeptService tbDeptService; private final ISysUserService sysUserService; /** * 导入模板下载 */ @GetMapping("/download") @ApiOperation("模板下载") public void download() { try { //ArrayList list = new ArrayList<>(); //ExcelUtil.exportExcel(list, "部门导入模板", "部门导入模板", TbDept.class, "部门导入模板", response); // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("部门导入模板", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); Map selectedMap = new HashMap<>(); selectedMap.put(3, new String[]{"省本级", "市", "开发区", "县"}); selectedMap.put(4, new String[]{"是", "否"}); EasyExcel.write(response.getOutputStream(), DeptExcel.class).sheet("模板") .registerWriteHandler(EasyExcelUtil.getStyleStrategy()) .registerWriteHandler(new CustomCellWriteHandler()) .registerWriteHandler(new TitleHandler(selectedMap)) .doWrite(CollUtils.emptyList()); } catch (Exception e) { log.error("模板下载异常", e); throw new ServiceException("模板下载失败,请联系管理员!"); } } /** * 导入 * * @param file 文件 * @return R */ @PostMapping("/import") @ApiOperation("导入") @Transactional(rollbackFor = Exception.class) public R importExcel(@RequestPart("file") MultipartFile file) { try { //tbDeptService.importExcel(file); InputStream inputStream = file.getInputStream(); EasyExcel.read(inputStream, DeptExcel.class, new DeptImportListener(sysUserService)) .sheet().doRead(); inputStream.close(); } catch (Exception e) { if (e instanceof ServiceException) { return R.fail(e.getMessage()); } log.error("导入失败", e); throw new RuntimeException(e.getMessage()); } return R.ok(); } /** * 分页条件查询 * * @param query 查询条件 * @return R> */ @PostMapping("/page") @ApiOperation("分页条件查询") public R> page(@Validated @RequestBody DeptQuery query) { try { return R.ok(tbDeptService.queryPage(query)); } catch (Exception e) { if (e instanceof ServiceException) { return R.fail(e.getMessage()); } log.error("分页条件查询异常", e); return R.fail(); } } /** * 根据id查询部门详情 * * @param userId 部门id * @return DeptVO */ @GetMapping("/get-details") @ApiOperation("根据id查询部门详情") public R getDetails(@RequestParam Long userId) { try { Optional sysUser = sysUserService.lambdaQuery().eq(SysUser::getUserId, userId) .oneOpt(); if (sysUser.isPresent()) { return R.ok(BeanUtils.copyBean(sysUser.get(), DeptVO.class)); } } catch (Exception e) { if (e instanceof ServiceException) { return R.fail(e.getMessage()); } log.error("根据id查询部门详情异常", e); return R.fail(); } return R.ok(new DeptVO()); } /** * 编辑 * * @param dto 部门更新实体 * @return 响应成功 */ @PostMapping("/edit") @ApiOperation("编辑") public R edit(@RequestBody DeptUpdateDTO dto) { try { dto.setPassword(SecurityUtils.encryptPassword(dto.getPassword())); sysUserService.lambdaUpdate().set(SysUser::getAreaAlias, dto.getAreaAlias()) .set(SysUser::getPersonInCharge, dto.getPersonInCharge()) .set(SysUser::getPhoneNumber, dto.getPhoneNumber()) .set(SysUser::getUserName, dto.getUserName()) .set(SysUser::getPassword, dto.getPassword()) .eq(SysUser::getUserId, dto.getUserId()) .update(); } catch (Exception e) { if (e instanceof ServiceException) { return R.fail(e.getMessage()); } log.error("编辑异常", e); return R.fail(); } return R.ok(); } /** * 重点关注 * * @param dto 部门更新实体 * @return 响应成功 */ @PostMapping("/focus") @ApiOperation("重点关注") public R focus(@RequestBody DeptFocusDTO dto) { tbDeptService.focus(dto); return R.ok(); } /** * 部门未上传季度数据提示 * * @return Void */ @PostMapping("/reporting-message") @ApiOperation(value = "部门未上传季度数据提示", notes = "code=200不展示,code=500 展示返回的msg") public R reportingMessage() { return tbDeptService.reportingMessage(); } }