From 604d00f67ddcf25a9d079ceddd1c668b2a4e5288 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期五, 19 四月 2024 15:08:22 +0800
Subject: [PATCH] bug修复

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbDeptController.java |  180 +++++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 135 insertions(+), 45 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbDeptController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbDeptController.java
index d9b60b6..6910a20 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbDeptController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbDeptController.java
@@ -1,26 +1,45 @@
 package com.ruoyi.web.controller.api;
 
 
-import com.ruoyi.common.basic.PageVO;
-import com.ruoyi.common.core.domain.AjaxResult;
-import com.ruoyi.common.exception.GlobalException;
-import com.ruoyi.common.utils.ExcelUtil;
-import com.ruoyi.common.utils.bean.BeanUtils;
-import com.ruoyi.system.domain.TbDept;
+import com.alibaba.excel.EasyExcel;
+import com.ruoyi.common.basic.PageDTO;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.BeanUtils;
+import com.ruoyi.common.utils.CollUtils;
+import com.ruoyi.common.utils.EasyExcelUtil;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.system.dto.update.DeptFocusDTO;
 import com.ruoyi.system.dto.update.DeptUpdateDTO;
+import com.ruoyi.system.handler.CustomCellWriteHandler;
+import com.ruoyi.system.handler.TitleHandler;
 import com.ruoyi.system.query.DeptQuery;
+import com.ruoyi.system.service.ISysUserService;
 import com.ruoyi.system.service.TbDeptService;
 import com.ruoyi.system.vo.DeptVO;
+import com.ruoyi.web.controller.excel.DeptExcel;
+import com.ruoyi.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.web.bind.annotation.*;
+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;
-
-import javax.servlet.http.HttpServletResponse;
-import java.util.ArrayList;
 
 /**
  * <p>
@@ -30,15 +49,16 @@
  * @author mitao
  * @since 2024-03-13
  */
-@Api(tags = "部门管理相关接口")
 @Slf4j
 @RestController
 @RequestMapping("/dept")
 @RequiredArgsConstructor
+@Api(tags = "部门管理相关接口")
 public class TbDeptController {
 
     private final HttpServletResponse response;
     private final TbDeptService tbDeptService;
+    private final ISysUserService sysUserService;
 
     /**
      * 导入模板下载
@@ -47,79 +67,149 @@
     @ApiOperation("模板下载")
     public void download() {
         try {
-            ArrayList<TbDept> list = new ArrayList<>();
-            ExcelUtil.exportExcel(list, "部门导入模板", "部门导入模板", TbDept.class, "部门导入模板", response);
+            //ArrayList<TbDept> 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<Integer, String[]> 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 GlobalException("模板下载失败,请联系管理员!");
+            log.error("模板下载异常", e);
+            throw new ServiceException("模板下载失败,请联系管理员!");
         }
     }
 
     /**
      * 导入
+     *
      * @param file 文件
-     * @return AjaxResult
+     * @return R
      */
     @PostMapping("/import")
     @ApiOperation("导入")
-    public AjaxResult importExcel(@RequestPart("file")MultipartFile file) {
+    @Transactional(rollbackFor = Exception.class)
+    public R<Void> importExcel(@RequestPart("file") MultipartFile file) {
         try {
-            tbDeptService.importExcel(file);
+            //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 AjaxResult.success();
+        return R.ok();
     }
 
     /**
      * 分页条件查询
+     *
      * @param query 查询条件
-     * @return AjaxResult<PageVO<DeptVO>>
+     * @return R<PageVO < DeptVO>>
      */
     @PostMapping("/page")
     @ApiOperation("分页条件查询")
-    public AjaxResult<PageVO<DeptVO>> page(@RequestBody DeptQuery query) {
-        return AjaxResult.success(tbDeptService.queryPage(query));
+    public R<PageDTO<DeptVO>> 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();
+        }
     }
 
-    @GetMapping("/{id}")
+    /**
+     * 根据id查询部门详情
+     *
+     * @param userId 部门id
+     * @return DeptVO
+     */
+    @GetMapping("/get-details")
     @ApiOperation("根据id查询部门详情")
-    public AjaxResult<DeptVO> getById(@PathVariable Integer id) {
-        TbDept dept = tbDeptService.getById(id);
-        DeptVO deptVO = new DeptVO();
-        BeanUtils.copyBeanProp(deptVO, dept);
-        return AjaxResult.success(deptVO);
+    public R<DeptVO> getDetails(@RequestParam Long userId) {
+        try {
+            Optional<SysUser> 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
+     *
+     * @param dto 部门更新实体
+     * @return 响应成功
      */
     @PostMapping("/edit")
     @ApiOperation("编辑")
-    public AjaxResult edit(@RequestBody DeptUpdateDTO dto){
-        TbDept tbDept = new TbDept();
-        BeanUtils.copyBeanProp(tbDept,dto);
-        tbDeptService.updateById(tbDept);
-        return AjaxResult.success();
+    public R<Void> 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
+     *
+     * @param dto 部门更新实体
+     * @return 响应成功
      */
     @PostMapping("/focus")
     @ApiOperation("重点关注")
-    public AjaxResult focus(@RequestBody DeptFocusDTO dto){
-        TbDept tbDept = new TbDept();
-        BeanUtils.copyBeanProp(tbDept,dto);
-        tbDeptService.lambdaUpdate()
-                .eq(TbDept::getId, dto.getId())
-                .set(TbDept::getFocussed, dto.getFocussed())
-                .update();
-        return AjaxResult.success();
+    public R<Object> 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<Void> reportingMessage() {
+        return tbDeptService.reportingMessage();
     }
 }
 

--
Gitblit v1.7.1