TOrganController.java 14.4 KB
Newer Older
liqin's avatar
liqin committed
1
package cn.chnmuseum.party.web.controller;
2

liqin's avatar
liqin committed
3 4 5 6 7 8 9 10 11 12 13 14
import cn.chnmuseum.party.common.log.MethodLog;
import cn.chnmuseum.party.common.log.OperModule;
import cn.chnmuseum.party.common.log.OperType;
import cn.chnmuseum.party.common.util.DateUtil80;
import cn.chnmuseum.party.common.util.ImportExcelUtil;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.TArea;
import cn.chnmuseum.party.model.TOrgan;
import cn.chnmuseum.party.model.TUser;
import cn.chnmuseum.party.service.TAreaService;
import cn.chnmuseum.party.service.TOrganService;
import cn.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;
30 31 32 33
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/**
 * <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
52 53 54
    @Resource
    private TAreaService tAreaService;

55
    @PostMapping("/add")
wzp's avatar
wzp committed
56
    @RequiresAuthentication  //@RequiresPermissions("/organ/add")
57
    @ApiOperation(value = "添加机构", notes = "添加机构")
wzp's avatar
wzp committed
58
    @MethodLog(operModule = OperModule.ORG, operType = OperType.ADD)
59
    public Map<String, Object> add(TOrgan organ) {
wzp's avatar
wzp committed
60
        HashMap<String, Object> resultMap = new HashMap<>();
wzp's avatar
wzp committed
61
        QueryWrapper<TOrgan> ew = new QueryWrapper<>();
62
        if (StringUtils.isBlank(organ.getParentId())) {
wzp's avatar
wzp committed
63 64
            organ.setParentId("0");
        }
wzp's avatar
wzp committed
65
        if (StringUtils.isNoneBlank(organ.getName().trim())) {
wzp's avatar
wzp committed
66 67 68 69
            organ.setName(organ.getName().trim());
            ew.eq("is_deleted", 0);
            ew.eq("name", organ.getName());
            List<TOrgan> list = this.tOrganService.list(ew);
70
            if (list != null && list.size() > 0 && list.get(0) != null) {
wzp's avatar
wzp committed
71
                resultMap.put("resultCode", "400");
wzp's avatar
wzp committed
72 73 74
                resultMap.put("message", "机构名称不能重复!");
                return resultMap;
            }
75
        } else {
wzp's avatar
wzp committed
76 77 78 79
            resultMap.put("resultCode", "400");
            resultMap.put("message", "机构名称不能为空!");
            return resultMap;

wzp's avatar
wzp committed
80
        }
81 82
        organ.setCreateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        organ.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
wzp's avatar
wzp committed
83
        String organCode = getOrganCode(organ.getParentId());
wzp's avatar
wzp committed
84 85
        organ.setLevel(organCode.length() / 3);
        if (organ.getLevel() > 3) {
wzp's avatar
wzp committed
86
            resultMap.put("resultCode", "400");
wzp's avatar
wzp committed
87 88 89
            resultMap.put("message", "不能添加4级机构!");
            return resultMap;
        }
wzp's avatar
wzp committed
90
        organ.setCode(organCode);
91 92 93 94 95 96 97 98 99 100 101 102
        // 保存业务节点信息
        boolean result = tOrganService.save(organ);
        // 返回操作结果
        if (result) {
            return getSuccessResult();
        } else {
            // 保存失败
            return getFailResult();
        }
    }

    @PutMapping("/update")
wzp's avatar
wzp committed
103
    @RequiresAuthentication  //@RequiresPermissions("/organ/update")
104
    @ApiOperation(value = "update", notes = "修改机构信息")
wzp's avatar
wzp committed
105
    @MethodLog(operModule = OperModule.ORG, operType = OperType.UPDATE)
106
    public Map<String, Object> updateTOrgan(TOrgan tOrgan) {
wzp's avatar
wzp committed
107
        try {
108 109 110 111 112 113
            if (tOrgan.getParentId() != null && tOrgan.getId() != null && tOrgan.getId().equals(tOrgan.getParentId())) {
                HashMap<String, Object> resultMap = new HashMap<>(2);
                resultMap.put("resultCode", "400");
                resultMap.put("message", "父级机构不能是自己!");
                return resultMap;
            }
wzp's avatar
wzp committed
114 115 116 117 118 119 120
            if (StringUtils.isBlank(tOrgan.getName().trim())) {
                HashMap<String, Object> resultMap = new HashMap<>();
                resultMap.put("resultCode", "400");
                resultMap.put("message", "机构名称不能为空!");
                return resultMap;

            }
wzp's avatar
wzp committed
121
            TOrgan byId = tOrganService.selectById(tOrgan.getId());
wzp's avatar
wzp committed
122 123 124 125
            if (StringUtils.isNotBlank(tOrgan.getParentId())) {
                if (!byId.getParentId().equals(tOrgan.getParentId())) {
                    tOrgan.setCode(getOrganCode(tOrgan.getParentId()));
                }
wzp's avatar
wzp committed
126 127 128
            }else {
                tOrgan.setParentId("0");
                tOrgan.setCode(getOrganCode(tOrgan.getParentId()));
wzp's avatar
wzp committed
129 130 131
            }
            if (StringUtils.isNotBlank(tOrgan.getCode())) {
                if (byId.getChildren().size() > 0) {
wzp's avatar
wzp committed
132
                    HashMap<String, Object> map = new HashMap<>();
wzp's avatar
wzp committed
133
                    map.put("resultCode", "400");
wzp's avatar
wzp committed
134 135 136 137 138 139 140
                    map.put("message", "机构存在下级机构,不能修改机构级别!");
                    map.put("data", "");
                    return map;
                }
                tOrgan.setLevel(tOrgan.getCode().length() / 3);
                if (tOrgan.getLevel() > 3) {
                    HashMap<String, Object> resultMap = new HashMap<>();
wzp's avatar
wzp committed
141
                    resultMap.put("resultCode", "400");
wzp's avatar
wzp committed
142 143 144
                    resultMap.put("message", "不能修改机构为4级!");
                    return resultMap;
                }
wzp's avatar
wzp committed
145
            }
wzp's avatar
wzp committed
146
            tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
liqin's avatar
liqin committed
147
            boolean flag;
wzp's avatar
wzp committed
148 149 150 151
            flag = tOrganService.updateById(tOrgan);
            if (!flag) {
                return getFailResult();
            }
152
            return getSuccessResult();
wzp's avatar
wzp committed
153 154
        } catch (Exception e) {
            e.printStackTrace();
155
        }
wzp's avatar
wzp committed
156

157 158 159 160
        return getFailResult();
    }

    @DeleteMapping("/delete")
wzp's avatar
wzp committed
161
    @RequiresAuthentication  //@RequiresPermissions("/organ/delete")
162
    @ApiOperation(value = "根据ID删除机构", notes = "根据ID删除机构")
wzp's avatar
wzp committed
163 164 165
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "标识ID", paramType = "query", dataType = "String")
    })
wzp's avatar
wzp committed
166
    @MethodLog(operModule = OperModule.ORG, operType = OperType.DELETE)
wzp's avatar
wzp committed
167
    public Map<String, Object> deleteTOrgan(String id) {
liqin's avatar
liqin committed
168
        boolean result;
wzp's avatar
wzp committed
169
        try {
wzp's avatar
wzp committed
170 171 172
            TOrgan tOrgan = tOrganService.getById(id);
            tOrgan.setIsDeleted(true);
            tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
wzp's avatar
wzp committed
173
            result = tOrganService.removeById(tOrgan);
wzp's avatar
wzp committed
174 175 176
            if (!result) {
                return getFailResult();
            }
177
            return getSuccessResult();
wzp's avatar
wzp committed
178 179
        } catch (Exception e) {
            e.printStackTrace();
180 181 182 183 184
        }
        return getFailResult();
    }

    @GetMapping("/getList")
wzp's avatar
wzp committed
185
    @RequiresAuthentication  //@RequiresPermissions("/organ/getList")
186
    @ApiOperation(value = "获取机构全部列表(无分页)", notes = "获取机构全部列表(无分页)")
wzp's avatar
wzp committed
187
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
188
    public Map<String, Object> getTOrganList() {
liqin's avatar
liqin committed
189
        TUser user;
190 191
        try {
            user = getcurUser();
wzp's avatar
wzp committed
192
            TOrgan tOrgan = new TOrgan();
liqin's avatar
liqin committed
193
            if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
wzp's avatar
wzp committed
194 195 196
                //设置用户数据权限
                tOrgan.setCode(user.getOrgCode());
            }
wzp's avatar
wzp committed
197 198
            List<TOrgan> list = tOrganService.getAllList(tOrgan);
            return getResult(list);
199 200 201
        } catch (Exception e) {
            e.printStackTrace();
        }
wzp's avatar
wzp committed
202
        return getFailResult();
203 204 205 206 207 208 209 210 211 212
    }

    @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
213
    @RequiresAuthentication  //@RequiresPermissions("/organ/getPageList")
214
    @ApiOperation(value = "获取机构分页列表", notes = "获取机构分页列表")
wzp's avatar
wzp committed
215
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
216
    public Map<String, Object> getTOrganPageList(GenericPageParam genericPageParam) {
217
        TUser user = getcurUser();
liqin's avatar
liqin committed
218
        Page<TOrgan> page;
wzp's avatar
wzp committed
219 220 221 222 223 224 225 226 227 228 229
        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
230
            if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
wzp's avatar
wzp committed
231 232 233
                //设置用户数据权限
                queryWrapper.likeRight(TOrgan::getCode, user.getOrgCode());
            }
wzp's avatar
wzp committed
234
            queryWrapper.eq(TOrgan::getIsDeleted, false);
wzp's avatar
wzp committed
235 236 237 238 239 240
            // 设置排序规则
            queryWrapper.orderByDesc(TOrgan::getCreateTime);
            page = this.tOrganService.page(getPage(), queryWrapper);
            return getResult(page);
        } catch (Exception e) {
            e.printStackTrace();
241
        }
wzp's avatar
wzp committed
242
        return getFailResult();
243 244 245 246
    }

    @ApiOperation(value = "获取机构详情", notes = "获取机构详情")
    @GetMapping("/getById")
wzp's avatar
wzp committed
247
    @RequiresAuthentication  //@RequiresPermissions("/organ/getById")
wzp's avatar
wzp committed
248
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
249
    public Map<String, Object> getById(String id) {
liqin's avatar
liqin committed
250
        TOrgan tOrgan;
wzp's avatar
wzp committed
251 252 253 254 255 256 257
        try {
            tOrgan = tOrganService.selectById(id);
            return getResult(tOrgan);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
258 259 260
    }


wzp's avatar
wzp committed
261 262 263 264 265
    @GetMapping("/getTree")
    @RequiresAuthentication  //@RequiresPermissions("/organ/getTree")
    @ApiOperation(value = "获取机构树", notes = "获取机构树")
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
    public Map<String, Object> getTree(String name) {
266
        TUser user = getcurUser();
liqin's avatar
liqin committed
267
        List<TOrgan> list;
wzp's avatar
wzp committed
268
        UpdateWrapper<TOrgan> wrapper = new UpdateWrapper<>();
wzp's avatar
wzp committed
269
        try {
liqin's avatar
liqin committed
270
            if (user.getRoleList().size() > 0 && !user.getRoleList().contains("1")) {
wzp's avatar
wzp committed
271 272
                //设置用户数据权限
                wrapper.likeRight("code", user.getOrgCode());
liqin's avatar
liqin committed
273
            } else {
wzp's avatar
wzp committed
274 275
                user.setOrgCode("");
            }
wzp's avatar
wzp committed
276
            if (StringUtils.isBlank(name)) {
wzp's avatar
wzp committed
277
                list = tOrganService.getTree(user.getOrgCode());
wzp's avatar
wzp committed
278
            } else {
wzp's avatar
wzp committed
279 280
                wrapper.like("name", name);
                list = tOrganService.list(wrapper);
wzp's avatar
wzp committed
281 282 283 284
            }
            return getResult(list);
        } catch (Exception e) {
            e.printStackTrace();
285
        }
wzp's avatar
wzp committed
286 287
        return getFailResult();
    }
288

wzp's avatar
wzp committed
289
    @GetMapping("/getAreaTree")
wzp's avatar
wzp committed
290
    @RequiresAuthentication  //@RequiresPermissions("/organ/getAreaTree")
wzp's avatar
wzp committed
291
    @ApiOperation(value = "获取区域树", notes = "获取区域树")
wzp's avatar
wzp committed
292
    @MethodLog(operModule = OperModule.ORG, operType = OperType.SELECT)
wzp's avatar
wzp committed
293
    public Map<String, Object> getAreaTree() {
liqin's avatar
liqin committed
294
        List<TArea> list;
wzp's avatar
wzp committed
295 296 297 298 299 300 301
        try {
            list = tAreaService.getAreaTree();
            return getResult(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFailResult();
wzp's avatar
wzp committed
302 303
    }

304 305 306 307

    // 导入EXCEL
    @ApiOperation(value = "导入EXCEL", notes = "导入EXCEL", httpMethod = "POST")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
wzp's avatar
wzp committed
308
    @RequiresAuthentication  //@RequiresPermissions("/organ/import")
wzp's avatar
wzp committed
309
    @MethodLog(operModule = OperModule.ORG, operType = OperType.IMPORT)
wzp's avatar
wzp committed
310
    public Map<String, Object> upload(MultipartFile file) {
liqin's avatar
liqin committed
311
        Map<String, Object> resultMap = new LinkedHashMap<>();
312
        try {
liqin's avatar
liqin committed
313
            boolean flag;
314
            //使用工具类从文件中读取数据
liqin's avatar
liqin committed
315
            final List<Map<String, String>> excelList = ImportExcelUtil.readExcel(file.getOriginalFilename(), file.getInputStream());
316 317
            flag = tOrganService.batchUpload(excelList);
            if (!flag) {
wzp's avatar
wzp committed
318
                resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
319
                resultMap.put("message", "导入失败!");
wzp's avatar
wzp committed
320
                return resultMap;
321
            }
wzp's avatar
wzp committed
322
            resultMap.put("resultCode", "200");
wzp's avatar
wzp committed
323
            resultMap.put("message", "导入成功!");
wzp's avatar
wzp committed
324
            return resultMap;
325
        } catch (Exception e) {
wzp's avatar
wzp committed
326
            resultMap.put("resultCode", "500");
wzp's avatar
wzp committed
327
            resultMap.put("message", e.getMessage());
wzp's avatar
wzp committed
328
            return resultMap;
329 330 331
        }
    }

wzp's avatar
wzp committed
332 333
    private String getOrganCode(String parentId) {
        TOrgan max = tOrganService.selectCodeMax(parentId);
wzp's avatar
wzp committed
334 335 336
        TOrgan byId = tOrganService.getById(parentId);
        if (max == null) {
            if (byId != null) {
liqin's avatar
liqin committed
337 338
                return byId.getCode() + "001";
            } else {
wzp's avatar
wzp committed
339 340
                return "001";
            }
wzp's avatar
wzp committed
341
        }
wzp's avatar
wzp committed
342
        String s = max.getCode().substring(max.getCode().length() - 3);
wzp's avatar
wzp committed
343
        String s1 = max.getCode().substring(0, max.getCode().length() - 3);
liqin's avatar
liqin committed
344
        int integer = Integer.parseInt(s) + 1;
wzp's avatar
wzp committed
345
        if (integer < 10) {
liqin's avatar
liqin committed
346
            s1 = s1 + "00" + integer;
wzp's avatar
wzp committed
347
        } else if (integer < 100) {
liqin's avatar
liqin committed
348
            s1 = s1 + "0" + integer;
wzp's avatar
wzp committed
349
        } else {
liqin's avatar
liqin committed
350
            s1 = s1 + integer;
wzp's avatar
wzp committed
351 352 353 354
        }
        return s1;
    }

355 356
}