mitao
2024-04-02 4403a428edc18482c5aa561e603b26ebe13d328f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.ruoyi.web.controller.api;
 
 
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.SecurityUtils;
import com.ruoyi.system.dto.update.DeptFocusDTO;
import com.ruoyi.system.dto.update.DeptUpdateDTO;
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Optional;
 
/**
 * <p>
 * 部门管理表 前端控制器
 * </p>
 *
 * @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<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");
            EasyExcel.write(response.getOutputStream(), DeptExcel.class).sheet("模板").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<Void> 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<PageVO<DeptVO>>
     */
    @PostMapping("/page")
    @ApiOperation("分页条件查询")
    public R<PageDTO<DeptVO>> page(@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<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 响应成功
     */
    @PostMapping("/edit")
    @ApiOperation("编辑")
    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 响应成功
     */
    @PostMapping("/focus")
    @ApiOperation("重点关注")
    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();
    }
}