ludc
2024-12-05 c6aa8e0dae3c87100d51e962229e05752d937092
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.vci.web.controller;
 
import com.vci.dto.OrgDepartmentDTO;
import com.vci.constant.FrameWorkLangCodeConstant;
import com.vci.pagemodel.OrgDepartmentVO;
import com.vci.starter.web.annotation.controller.VciUnCheckRight;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.*;
import com.vci.starter.web.util.ControllerUtil;
import com.vci.starter.web.util.LangBaseUtil;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.web.service.OrgDeptQueryServiceI;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * 部门的查询控制器
 * @author weidy
 * @date 2020/3/4
 */
@RestController
@RequestMapping("/departmentQueryController")
public class OrgDepartmentQueryController {
 
    /**
     * 部门的查询服务
     */
    @Autowired
    private OrgDeptQueryServiceI deptQueryService;
 
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 部门的树形参照,部门管理也用的这个查询接口
     * @param treeQueryObject 树形数据的查询对象,包括查询条件,上级主键,是否多选等,(extandParamsMap中添加"showAllDepartmentNode"为"true"时,并且parentOid为空,返回结果中会包含“所有部门”这个节点)
     * @return 部门的树形参照,已经转换了上下级关系
     * @throws VciBaseException 查询出错的时候会抛出异常,如果是老的项目里不抛出异常
     */
    @RequestMapping(value = "/refTree",method = RequestMethod.GET)
    public BaseResult<Tree> refTree(TreeQueryObject treeQueryObject) throws VciBaseException{
        try {
            List<Tree> deptTreeList = deptQueryService.refTreeDept(treeQueryObject);
            return  BaseResult.tree(deptTreeList);
        }catch (Exception e) {
            e.printStackTrace();
            String exceptionMessage = "部门树查询时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e);
            logger.error(exceptionMessage);
            return BaseResult.fail(exceptionMessage);
        }
    }
 
    /**
     * 部门列表查询,带分页
     * @param queryObject
     * @return 返回的list不是tree结构的
     */
    @RequestMapping(value = "/refDataGrid",method = RequestMethod.GET)
    public BaseResult<OrgDepartmentVO> refDataGrid(BaseQueryObject queryObject){
        DataGrid<OrgDepartmentVO> dataGrid=deptQueryService.gridDepts(queryObject.getConditionMap(), queryObject.getPageHelper());
        return BaseResult.dataGrid(dataGrid);
    }
 
    /**
     * 保存部门用户关联信息,平台自带查重功能
     * @param userOIds 用户id
     * @param deptId 部门oid
     * @return
     */
    @RequestMapping(value = "/saveUsersDepts",method = RequestMethod.POST)
    public BaseResult saveUsersDepts(String[] userOIds, String deptId){
        //不能同时为空
        if(Func.isEmpty(userOIds) && Func.isBlank(deptId)){
            return BaseResult.fail("参数【用户主键】和【部门主键】,不能同时为空!");
        }
        try {
            return deptQueryService.saveUsersDept(userOIds,deptId) ? BaseResult.success("部门分配成功!"):BaseResult.fail("部门分配失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = "关联部门时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e);
            logger.error(exceptionMessage);
            return BaseResult.fail(exceptionMessage);
        }
    }
 
    /**
     * 新增单条部门
     * @param orgDepartmentDTO
     * @return
     */
    @RequestMapping(value = "/addDept",method = RequestMethod.POST)
    public BaseResult addDept(@RequestBody OrgDepartmentDTO orgDepartmentDTO){
        try {
            return deptQueryService.addDept(orgDepartmentDTO) ? BaseResult.success("部门添加成功!"):BaseResult.fail("部门添加失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = "添加部门时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e);
            logger.error(exceptionMessage);
            return BaseResult.fail(exceptionMessage);
        }
    }
 
    /**
     * 修改部门信息
     * @param orgDepartmentDTO
     * @return
     */
    @RequestMapping(value = "/updateDept",method = RequestMethod.PUT)
    public BaseResult updateRole(@RequestBody OrgDepartmentDTO orgDepartmentDTO){
        try {
            return deptQueryService.updateDept(orgDepartmentDTO) ? BaseResult.success("部门修改成功!"):BaseResult.fail("部门修改失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = "修改部门时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e);
            logger.error(exceptionMessage);
            return BaseResult.fail(exceptionMessage);
        }
    }
 
    /**
     * 删除部门
     * @param ids 要删除的部门主键
     * @return
     */
    @RequestMapping(value = "/deleteDept",method = RequestMethod.DELETE)
    public BaseResult deleteDept(String[] ids){
        try {
            return deptQueryService.deleteDept(ids) ? BaseResult.success("删除部门成功!"):BaseResult.fail("删除部门失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = "添加部门时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e);
            logger.error(exceptionMessage);
            return BaseResult.fail(exceptionMessage);
        }
    }
 
    /**
     * 下载人员导入模板
     * @param downloadFileName
     * @param response
     */
    @RequestMapping(value = "/downloadImportTemplate",method = RequestMethod.GET)
    public void downloadImportTemplate(String downloadFileName, HttpServletResponse response){
        try {
            String excelPath = deptQueryService.downloadImportTemplate(downloadFileName);
            ControllerUtil.writeFileToResponse(response,excelPath);
        } catch (Exception e) {
            String msg = "下载部门导入模板时出现错误,原因:" + LangBaseUtil.getErrorMsg(e);
            try {
                e.printStackTrace();
                ControllerUtil.writeDataToResponse(response,"error_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss") + ".txt", StringUtils.isNotBlank(msg)?msg.getBytes():new byte[0],null);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
 
    /**
     * 部门导入
     * @param file
     * @return
     */
    @RequestMapping(value = "/importDept",method = RequestMethod.POST)
    @VciUnCheckRight
    public BaseResult importUser(MultipartFile file){
        String excelFileName = LocalFileUtil.getDefaultTempFolder() + File.separator + LocalFileUtil.getFileNameForIE(file.getOriginalFilename());
        File file1 = new File(excelFileName);
        try {
            file.transferTo(new File(excelFileName));
            if (file != null) {
                return deptQueryService.importDept(file1);
            } else {
                return BaseResult.fail(FrameWorkLangCodeConstant.IMPORT_FAIL, new String[]{"无导入的文件"});
            }
        }catch (Throwable e) {
            throw new VciBaseException(e.getMessage(),new String[0],e);
        }finally {
            file1.delete();
        }
    }
 
 
    /**
     * 根据用户主键获取关联的部门
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 部门的显示对象
     */
    @RequestMapping(value = "/listDeptByUserOid",method = RequestMethod.GET)
    public BaseResult listDeptByUserOid(String userOid, Map<String, String> queryMap){
        List<OrgDepartmentVO> listData=deptQueryService.listDeptByUserOid(userOid, queryMap);
        return BaseResult.dataList(listData);
    }
}