TOrganController.java 10.6 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;
wzp's avatar
wzp committed
8
import cn.wisenergy.chnmuseum.party.model.TArea;
9
import cn.wisenergy.chnmuseum.party.model.TUser;
wzp's avatar
wzp committed
10
import cn.wisenergy.chnmuseum.party.service.TAreaService;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
import cn.wisenergy.chnmuseum.party.model.TOrgan;
import cn.wisenergy.chnmuseum.party.service.TOrganService;
import cn.wisenergy.chnmuseum.party.common.enums.AuditStatusEnum;
import cn.wisenergy.chnmuseum.party.common.validator.groups.Add;
import cn.wisenergy.chnmuseum.party.common.validator.groups.Update;
import cn.wisenergy.chnmuseum.party.common.vo.GenericPageParam;

import com.beust.jcommander.Parameter;
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;
import org.apache.commons.lang3.SystemUtils;
import org.apache.ibatis.annotations.Param;
33
import org.apache.shiro.authz.annotation.RequiresAuthentication;
34
import org.apache.shiro.authz.annotation.RequiresPermissions;
35 36
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
37 38 39
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
40
import org.springframework.web.multipart.MultipartFile;
41 42 43

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
44
import java.time.LocalDateTime;
wzp's avatar
wzp committed
45
import java.util.*;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/**
 * <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
64 65 66
    @Resource
    private TAreaService tAreaService;

67 68 69 70

    @PostMapping("/add")
    @RequiresPermissions("/organ/add")
    @ApiOperation(value = "添加机构", notes = "添加机构")
wzp's avatar
wzp committed
71
//    @MethodLog(operModule = OperModule.ORG,operType = OperType.ADD)
72 73 74
    public Map<String, Object> add(TOrgan organ) {
        organ.setCreateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        organ.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
wzp's avatar
wzp committed
75 76
        String organCode = getOrganCode(organ.getParentId());
        organ.setCode(organCode);
77 78 79 80 81 82 83 84 85 86 87 88 89 90
        // 保存业务节点信息
        boolean result = tOrganService.save(organ);
        // 返回操作结果
        if (result) {
            return getSuccessResult();
        } else {
            // 保存失败
            return getFailResult();
        }
    }

    @PutMapping("/update")
    @RequiresPermissions("/organ/update")
    @ApiOperation(value = "update", notes = "修改机构信息")
wzp's avatar
wzp committed
91
//    @MethodLog(operModule = OperModule.ORG,operType = OperType.UPDATE)
92
    public Map<String, Object> updateTOrgan(TOrgan tOrgan) {
wzp's avatar
wzp committed
93 94 95 96 97 98 99 100 101 102 103
        TOrgan byId = tOrganService.selectById(tOrgan.getId());
        if (byId.getLevel()!=tOrgan.getLevel()&&byId.getChildren().size()>0){
            HashMap<String, Object> map = new HashMap<>();
            map.put("resultCode", 500);
            map.put("message", "机构存在下级机构,不能修改机构级别!");
            map.put("data", "");
            return map;
        }
        if (!byId.getParentId().equals(tOrgan.getParentId())){
            tOrgan.setCode(getOrganCode(tOrgan.getParentId()));
        }
104 105 106 107 108 109 110 111 112 113 114 115
        tOrgan.setUpdateTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        boolean flag = tOrganService.updateById(tOrgan);
        if (flag) {
            return getSuccessResult();
        }
        return getFailResult();
    }


    @DeleteMapping("/delete")
    @RequiresPermissions("/organ/delete")
    @ApiOperation(value = "根据ID删除机构", notes = "根据ID删除机构")
116
    @MethodLog(operModule = OperModule.ORG,operType = OperType.DELETE)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "标识ID", paramType = "path", dataType = "String")
    })
    public Map<String, Object> deleteTOrgan(@PathVariable("id") String id) {
        UpdateWrapper<TOrgan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.eq("is_deleted", 1);
        updateWrapper.eq("update_time", DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
        boolean result = tOrganService.update(updateWrapper);
        if (result) {
            return getSuccessResult();
        }
        return getFailResult();
    }

    @GetMapping("/getList")
    @RequiresPermissions("/organ/getList")
    @ApiOperation(value = "获取机构全部列表(无分页)", notes = "获取机构全部列表(无分页)")
135
    @MethodLog(operModule = OperModule.ORG,operType = OperType.SELECT)
136
    public Map<String, Object> getTOrganList() {
137 138 139 140 141 142 143
        TUser user = null;
        try {
            user = getcurUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<TOrgan> tOrganList = tOrganService.list(Wrappers.<TOrgan>lambdaQuery().eq(TOrgan::getIsDeleted, 0).likeRight(TOrgan::getCode,user.getOrgCode()).orderByDesc(TOrgan::getCreateTime));
144 145 146
        return getResult(tOrganList);
    }

147 148


149 150 151 152 153 154 155 156 157 158
    @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")
    @RequiresPermissions("/organ/getPageList")
    @ApiOperation(value = "获取机构分页列表", notes = "获取机构分页列表")
159
    @MethodLog(operModule = OperModule.ORG,operType = OperType.SELECT)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    public Map<String, Object> getTOrganPageList(GenericPageParam genericPageParam) {
        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));
        }
        // 设置排序规则
        queryWrapper.orderByDesc(TOrgan::getCreateTime);
        Page<TOrgan> page = this.tOrganService.page(getPage(), queryWrapper);
        return getResult(page);
    }

    @ApiOperation(value = "获取机构详情", notes = "获取机构详情")
    @GetMapping("/getById")
    @RequiresPermissions("/organ/getById")
180
    @MethodLog(operModule = OperModule.ORG,operType = OperType.SELECT)
181
    public Map<String, Object> getById(String id) {
wzp's avatar
wzp committed
182
        TOrgan tOrgan = tOrganService.selectById(id);
183 184 185 186 187 188 189
        return getResult(tOrgan);
    }


	@GetMapping("/getTree")
	@RequiresPermissions("/organ/getTree")
	@ApiOperation(value = "获取机构树", notes = "获取机构树")
wzp's avatar
wzp committed
190
//    @MethodLog(operModule = OperModule.ORG,operType = OperType.SELECT)
191 192 193 194 195 196 197 198
	public Map<String, Object> getTree(String name) {
        List<TOrgan> list = new ArrayList<>();
        if (StringUtils.isBlank(name)) {
            list = tOrganService.getTree();
        }else {
            list = tOrganService.list(new UpdateWrapper<TOrgan>().like("name",name));
        }
		return getResult(list);
199 200
	}

wzp's avatar
wzp committed
201 202 203 204 205 206 207 208 209
    @GetMapping("/getAreaTree")
    @RequiresPermissions("/organ/getAreaTree")
    @ApiOperation(value = "获取区域树", notes = "获取机构树")
//    @MethodLog(operModule = OperModule.ORG,operType = OperType.SELECT)
    public Map<String, Object> getAreaTree() {
        List<TArea> list = tAreaService.getAreaTree();
        return getResult(list);
    }

210 211 212 213 214 215 216 217 218 219 220 221 222 223

    // 导入EXCEL
    @ApiOperation(value = "导入EXCEL", notes = "导入EXCEL", httpMethod = "POST")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @RequiresPermissions("/organ/import")
    @MethodLog(operModule = OperModule.ORG,operType = OperType.IMPORT)
    public ResponseEntity<Map> upload(MultipartFile file) {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            boolean flag = false;
            //使用工具类从文件中读取数据
            List excelList = ImportExcelUtil.readExcel(file.getOriginalFilename(), file.getInputStream());
            flag = tOrganService.batchUpload(excelList);
            if (!flag) {
wzp's avatar
wzp committed
224
                resultMap.put("resultCode",500);
225 226 227
                resultMap.put("massage","导入失败!");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
            }
wzp's avatar
wzp committed
228
            resultMap.put("resultCode",200);
229 230 231
            resultMap.put("massage","导入成功!");
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
wzp's avatar
wzp committed
232
            resultMap.put("resultCode",500);
233 234 235 236 237
            resultMap.put("massage","导入失败!");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
        }
    }

wzp's avatar
wzp committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    private String getOrganCode(String parentId) {
        TOrgan max = tOrganService.selectCodeMax(parentId);
        String s = max.getCode().substring(max.getCode().length() - 3);
        String s1 = max.getCode().substring(0,max.getCode().length() - 3);
        Integer integer = Integer.valueOf(s)+1;
        if (integer<10){
            s1=s1+"00"+integer.toString();
        }else if (integer<100){
            s1=s1+"0"+integer.toString();
        }else {
            s1=s1+integer.toString();
        }
        return s1;
    }

253 254
}