TOrganController.java 13.5 KB
Newer Older
1 2
package cn.wisenergy.chnmuseum.party.web.controller;

wzp's avatar
wzp committed
3 4 5
import cn.wisenergy.chnmuseum.party.common.log.MethodLog;
import cn.wisenergy.chnmuseum.party.common.log.OperModule;
import cn.wisenergy.chnmuseum.party.common.log.OperType;
6
import cn.wisenergy.chnmuseum.party.common.util.DateUtil80;
7
import cn.wisenergy.chnmuseum.party.common.util.ImportExcelUtil;
liqin's avatar
liqin committed
8
import cn.wisenergy.chnmuseum.party.common.vo.GenericPageParam;
wzp's avatar
wzp committed
9
import cn.wisenergy.chnmuseum.party.model.TArea;
liqin's avatar
liqin committed
10
import cn.wisenergy.chnmuseum.party.model.TOrgan;
11
import cn.wisenergy.chnmuseum.party.model.TUser;
wzp's avatar
wzp committed
12
import cn.wisenergy.chnmuseum.party.service.TAreaService;
liqin's avatar
liqin committed
13 14
import cn.wisenergy.chnmuseum.party.service.TOrganService;
import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
15
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
wzp's avatar
wzp committed
16
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
17 18 19 20 21 22 23 24
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
25
import org.apache.shiro.authz.annotation.RequiresAuthentication;
26
import org.springframework.web.bind.annotation.*;
27
import org.springframework.web.multipart.MultipartFile;
28 29

import javax.annotation.Resource;
wzp's avatar
wzp committed
30
import java.util.*;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

/**
 * <pre>
 * 机构 前端控制器
 * </pre>
 *
 * @author Danny Lee
 * @since 2021-03-22
 */
@Slf4j
@RestController
@RequestMapping("/organ")
@Api(tags = {"机构操作接口"})
public class TOrganController extends BaseController {

    @Resource
    private TOrganService tOrganService;

wzp's avatar
wzp committed
49 50 51
    @Resource
    private TAreaService tAreaService;

52
    @PostMapping("/add")
wzp's avatar
wzp committed
53
    @RequiresAuthentication  //@RequiresPermissions("/organ/add")
54
    @ApiOperation(value = "添加机构", notes = "添加机构")
wzp's avatar
wzp committed
55
    @MethodLog(operModule = OperModule.ORG, operType = OperType.ADD)
56
    public Map<String, Object> add(TOrgan organ) {
wzp's avatar
wzp committed
57 58 59 60 61 62
        QueryWrapper<TOrgan> ew = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(organ.getName())) {
            organ.setName(organ.getName().trim());
            ew.eq("is_deleted", 0);
            ew.eq("name", organ.getName());
            List<TOrgan> list = this.tOrganService.list(ew);
wzp's avatar
wzp committed
63
            if (list != null && list.size() > 0 && list.get(0) != null) {
wzp's avatar
wzp committed
64 65 66 67 68 69
                HashMap<String, Object> resultMap = new HashMap<>();
                resultMap.put("resultCode", "500");
                resultMap.put("message", "机构名称不能重复!");
                return resultMap;
            }
        }
70 71
        organ.setCreateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        organ.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
wzp's avatar
wzp committed
72
        String organCode = getOrganCode(organ.getParentId());
wzp's avatar
wzp committed
73 74 75 76 77 78 79
        organ.setLevel(organCode.length() / 3);
        if (organ.getLevel() > 3) {
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("resultCode", "500");
            resultMap.put("message", "不能添加4级机构!");
            return resultMap;
        }
wzp's avatar
wzp committed
80
        organ.setCode(organCode);
81 82 83 84 85 86 87 88 89 90 91 92
        // 保存业务节点信息
        boolean result = tOrganService.save(organ);
        // 返回操作结果
        if (result) {
            return getSuccessResult();
        } else {
            // 保存失败
            return getFailResult();
        }
    }

    @PutMapping("/update")
wzp's avatar
wzp committed
93
    @RequiresAuthentication  //@RequiresPermissions("/organ/update")
94
    @ApiOperation(value = "update", notes = "修改机构信息")
wzp's avatar
wzp committed
95
    @MethodLog(operModule = OperModule.ORG, operType = OperType.UPDATE)
96
    public Map<String, Object> updateTOrgan(TOrgan tOrgan) {
wzp's avatar
wzp committed
97 98
        try {
            TOrgan byId = tOrganService.selectById(tOrgan.getId());
wzp's avatar
wzp committed
99 100 101 102
            if (StringUtils.isNotBlank(tOrgan.getParentId())) {
                if (!byId.getParentId().equals(tOrgan.getParentId())) {
                    tOrgan.setCode(getOrganCode(tOrgan.getParentId()));
                }
wzp's avatar
wzp committed
103 104 105
            }
            if (StringUtils.isNotBlank(tOrgan.getCode())) {
                if (byId.getChildren().size() > 0) {
wzp's avatar
wzp committed
106 107 108 109 110 111 112 113 114 115 116 117 118
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("resultCode", "500");
                    map.put("message", "机构存在下级机构,不能修改机构级别!");
                    map.put("data", "");
                    return map;
                }
                tOrgan.setLevel(tOrgan.getCode().length() / 3);
                if (tOrgan.getLevel() > 3) {
                    HashMap<String, Object> resultMap = new HashMap<>();
                    resultMap.put("resultCode", "500");
                    resultMap.put("message", "不能修改机构为4级!");
                    return resultMap;
                }
wzp's avatar
wzp committed
119
            }
wzp's avatar
wzp committed
120
            tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
liqin's avatar
liqin committed
121
            boolean flag;
wzp's avatar
wzp committed
122 123 124 125
            flag = tOrganService.updateById(tOrgan);
            if (!flag) {
                return getFailResult();
            }
126
            return getSuccessResult();
wzp's avatar
wzp committed
127 128
        } catch (Exception e) {
            e.printStackTrace();
129
        }
wzp's avatar
wzp committed
130

131 132 133 134
        return getFailResult();
    }

    @DeleteMapping("/delete")
wzp's avatar
wzp committed
135
    @RequiresAuthentication  //@RequiresPermissions("/organ/delete")
136
    @ApiOperation(value = "根据ID删除机构", notes = "根据ID删除机构")
wzp's avatar
wzp committed
137 138 139
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "标识ID", paramType = "query", dataType = "String")
    })
wzp's avatar
wzp committed
140
    @MethodLog(operModule = OperModule.ORG, operType = OperType.DELETE)
wzp's avatar
wzp committed
141
    public Map<String, Object> deleteTOrgan(String id) {
liqin's avatar
liqin committed
142
        boolean result;
wzp's avatar
wzp committed
143
        try {
wzp's avatar
wzp committed
144 145 146
            TOrgan tOrgan = tOrganService.getById(id);
            tOrgan.setIsDeleted(true);
            tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
wzp's avatar
wzp committed
147
            result = tOrganService.removeById(tOrgan);
wzp's avatar
wzp committed
148 149 150
            if (!result) {
                return getFailResult();
            }
151
            return getSuccessResult();
wzp's avatar
wzp committed
152 153
        } catch (Exception e) {
            e.printStackTrace();
154 155 156 157 158
        }
        return getFailResult();
    }

    @GetMapping("/getList")
wzp's avatar
wzp committed
159
    @RequiresAuthentication  //@RequiresPermissions("/organ/getList")
160
    @ApiOperation(value = "获取机构全部列表(无分页)", notes = "获取机构全部列表(无分页)")
wzp's avatar
wzp committed
161
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
162
    public Map<String, Object> getTOrganList() {
liqin's avatar
liqin committed
163
        TUser user;
164 165
        try {
            user = getcurUser();
wzp's avatar
wzp committed
166
            TOrgan tOrgan = new TOrgan();
liqin's avatar
liqin committed
167
            if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
wzp's avatar
wzp committed
168 169 170
                //设置用户数据权限
                tOrgan.setCode(user.getOrgCode());
            }
wzp's avatar
wzp committed
171 172
            List<TOrgan> list = tOrganService.getAllList(tOrgan);
            return getResult(list);
173 174 175
        } catch (Exception e) {
            e.printStackTrace();
        }
wzp's avatar
wzp committed
176
        return getFailResult();
177 178 179 180 181 182 183 184 185 186
    }

    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
    @PostMapping("/getPageList")
wzp's avatar
wzp committed
187
    @RequiresAuthentication  //@RequiresPermissions("/organ/getPageList")
188
    @ApiOperation(value = "获取机构分页列表", notes = "获取机构分页列表")
wzp's avatar
wzp committed
189
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
190
    public Map<String, Object> getTOrganPageList(GenericPageParam genericPageParam) {
191
        TUser user = getcurUser();
liqin's avatar
liqin committed
192
        Page<TOrgan> page;
wzp's avatar
wzp committed
193 194 195 196 197 198 199 200 201 202 203
        try {
            LambdaQueryWrapper<TOrgan> queryWrapper = new LambdaQueryWrapper<>();
            // 对名称或编码模糊查询
            if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
                queryWrapper.like(TOrgan::getName, genericPageParam.getNameOrCode());
            }
            // 根据创建时间区间检索
            if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
                queryWrapper.ge(TOrgan::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
                        .le(TOrgan::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
            }
liqin's avatar
liqin committed
204
            if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
wzp's avatar
wzp committed
205 206 207
                //设置用户数据权限
                queryWrapper.likeRight(TOrgan::getCode, user.getOrgCode());
            }
wzp's avatar
wzp committed
208
            queryWrapper.eq(TOrgan::getIsDeleted, false);
wzp's avatar
wzp committed
209 210 211 212 213 214
            // 设置排序规则
            queryWrapper.orderByDesc(TOrgan::getCreateTime);
            page = this.tOrganService.page(getPage(), queryWrapper);
            return getResult(page);
        } catch (Exception e) {
            e.printStackTrace();
215
        }
wzp's avatar
wzp committed
216
        return getFailResult();
217 218 219 220
    }

    @ApiOperation(value = "获取机构详情", notes = "获取机构详情")
    @GetMapping("/getById")
wzp's avatar
wzp committed
221
    @RequiresAuthentication  //@RequiresPermissions("/organ/getById")
wzp's avatar
wzp committed
222
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
223
    public Map<String, Object> getById(String id) {
liqin's avatar
liqin committed
224
        TOrgan tOrgan;
wzp's avatar
wzp committed
225 226 227 228 229 230 231
        try {
            tOrgan = tOrganService.selectById(id);
            return getResult(tOrgan);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
232 233 234
    }


wzp's avatar
wzp committed
235 236 237 238 239
    @GetMapping("/getTree")
    @RequiresAuthentication  //@RequiresPermissions("/organ/getTree")
    @ApiOperation(value = "获取机构树", notes = "获取机构树")
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
    public Map<String, Object> getTree(String name) {
240
        TUser user = getcurUser();
liqin's avatar
liqin committed
241
        List<TOrgan> list;
wzp's avatar
wzp committed
242
        UpdateWrapper<TOrgan> wrapper = new UpdateWrapper<>();
wzp's avatar
wzp committed
243
        try {
liqin's avatar
liqin committed
244
            if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
wzp's avatar
wzp committed
245 246
                //设置用户数据权限
                wrapper.likeRight("code", user.getOrgCode());
liqin's avatar
liqin committed
247
            } else {
wzp's avatar
wzp committed
248 249
                user.setOrgCode("");
            }
wzp's avatar
wzp committed
250
            if (StringUtils.isBlank(name)) {
wzp's avatar
wzp committed
251
                list = tOrganService.getTree(user.getOrgCode());
wzp's avatar
wzp committed
252
            } else {
wzp's avatar
wzp committed
253 254
                wrapper.like("name", name);
                list = tOrganService.list(wrapper);
wzp's avatar
wzp committed
255 256 257 258
            }
            return getResult(list);
        } catch (Exception e) {
            e.printStackTrace();
259
        }
wzp's avatar
wzp committed
260 261
        return getFailResult();
    }
262

wzp's avatar
wzp committed
263
    @GetMapping("/getAreaTree")
wzp's avatar
wzp committed
264
    @RequiresAuthentication  //@RequiresPermissions("/organ/getAreaTree")
wzp's avatar
wzp committed
265
    @ApiOperation(value = "获取区域树", notes = "获取区域树")
wzp's avatar
wzp committed
266
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
wzp's avatar
wzp committed
267
    public Map<String, Object> getAreaTree() {
liqin's avatar
liqin committed
268
        List<TArea> list;
wzp's avatar
wzp committed
269 270 271 272 273 274 275
        try {
            list = tAreaService.getAreaTree();
            return getResult(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
wzp's avatar
wzp committed
276 277
    }

278 279 280 281

    // 导入EXCEL
    @ApiOperation(value = "导入EXCEL", notes = "导入EXCEL", httpMethod = "POST")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
wzp's avatar
wzp committed
282
    @RequiresAuthentication  //@RequiresPermissions("/organ/import")
wzp's avatar
wzp committed
283
    @MethodLog(operModule = OperModule.ORG, operType = OperType.IMPORT)
wzp's avatar
wzp committed
284
    public Map<String, Object> upload(MultipartFile file) {
liqin's avatar
liqin committed
285
        Map<String, Object> resultMap = new LinkedHashMap<>();
286
        try {
liqin's avatar
liqin committed
287
            boolean flag;
288
            //使用工具类从文件中读取数据
liqin's avatar
liqin committed
289
            final List<Map<String, String>> excelList = ImportExcelUtil.readExcel(file.getOriginalFilename(), file.getInputStream());
290 291
            flag = tOrganService.batchUpload(excelList);
            if (!flag) {
wzp's avatar
wzp committed
292
                resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
293
                resultMap.put("message", "导入失败!");
wzp's avatar
wzp committed
294
                return resultMap;
295
            }
wzp's avatar
wzp committed
296
            resultMap.put("resultCode", "200");
wzp's avatar
wzp committed
297
            resultMap.put("message", "导入成功!");
wzp's avatar
wzp committed
298
            return resultMap;
299
        } catch (Exception e) {
wzp's avatar
wzp committed
300
            resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
301
            resultMap.put("message", e.getMessage());
wzp's avatar
wzp committed
302
            return resultMap;
303 304 305
        }
    }

wzp's avatar
wzp committed
306 307
    private String getOrganCode(String parentId) {
        TOrgan max = tOrganService.selectCodeMax(parentId);
wzp's avatar
wzp committed
308 309 310
        TOrgan byId = tOrganService.getById(parentId);
        if (max == null) {
            if (byId != null) {
liqin's avatar
liqin committed
311 312
                return byId.getCode() + "001";
            } else {
wzp's avatar
wzp committed
313 314
                return "001";
            }
wzp's avatar
wzp committed
315
        }
wzp's avatar
wzp committed
316
        String s = max.getCode().substring(max.getCode().length() - 3);
wzp's avatar
wzp committed
317
        String s1 = max.getCode().substring(0, max.getCode().length() - 3);
liqin's avatar
liqin committed
318
        int integer = Integer.parseInt(s) + 1;
wzp's avatar
wzp committed
319
        if (integer < 10) {
liqin's avatar
liqin committed
320
            s1 = s1 + "00" + integer;
wzp's avatar
wzp committed
321
        } else if (integer < 100) {
liqin's avatar
liqin committed
322
            s1 = s1 + "0" + integer;
wzp's avatar
wzp committed
323
        } else {
liqin's avatar
liqin committed
324
            s1 = s1 + integer;
wzp's avatar
wzp committed
325 326 327 328
        }
        return s1;
    }

329 330
}