package cn.com.goldenwater.dcproj.controller.pblm; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.dao.ObjInspPblmsDao; import cn.com.goldenwater.dcproj.dto.ObjInspPblmsDto; import cn.com.goldenwater.dcproj.model.ObjInspPblms; import cn.com.goldenwater.dcproj.model.ObjTypeCheck; import cn.com.goldenwater.dcproj.model.OlBisInspOrg; import cn.com.goldenwater.dcproj.param.ObjInspPblmsParam; import cn.com.goldenwater.dcproj.param.ObjInspPblmsParams; import cn.com.goldenwater.dcproj.param.WagaPblm; import cn.com.goldenwater.dcproj.service.BisInspPblmPService; import cn.com.goldenwater.dcproj.service.ObjInspPblmsService; import cn.com.goldenwater.dcproj.service.OlBisInspOrgService; import cn.com.goldenwater.dcproj.util.ReadExcelUtil; import cn.com.goldenwater.dcproj.utils.impexcel.ExpAndImpUtil; import cn.com.goldenwater.id.util.UuidUtil; import cn.com.goldenwater.target.CheckException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.*; import static cn.com.goldenwater.dcproj.util.CheckUtil.check; /** * @author lune * @date 2019-2-18 */ @Api(value = "小水库督查问题分类标准库", tags = "小水库督查问题分类标准库") @RestController @RequestMapping("/dc/insp/pblms") public class ObjInspPblmsController extends BaseController { private Logger logger = LoggerFactory.getLogger(getClass()); @Value("${export.templatePath}") private String templatePath; @Autowired private ObjInspPblmsService objInspPblmsService; @Autowired private OlBisInspOrgService olBisInspOrgService; @Autowired private BisInspPblmPService bisInspPblmPService; @ApiOperation(value = "添加问题分类标准库") @RequestMapping(value = "", method = RequestMethod.POST) public BaseResponse insert(@ApiParam(name = "objInspPblms", value = "ObjInspPblms", required = true) @RequestBody ObjInspPblms objInspPblms) { if (StringUtils.isBlank(objInspPblms.getType()) || StringUtils.isBlank(objInspPblms.getInspPblmsName())) { return buildFailResponse("请选择问题类别或填写问题名称!!"); } String uuid = UuidUtil.uuid(); objInspPblms.setGuid(uuid); if (StringUtils.isBlank(objInspPblms.getPguid())) { objInspPblms.setPguid("00000000000000000000000000000000"); } objInspPblms.setAttach("1"); objInspPblms.setSnClass("1"); int ret = objInspPblmsService.insert(objInspPblms); return buildSuccessResponse(uuid); } @ApiOperation(value = "获取问题类别") @RequestMapping(value = "/getPblmType", method = {RequestMethod.GET, RequestMethod.POST}) public BaseResponse> getPblmType(ObjInspPblmsParam objInspPblmsParam) { if (StringUtils.isBlank(objInspPblmsParam.getType()) && StringUtils.isBlank(objInspPblmsParam.getPguid())) { return buildSuccessResponse(objInspPblmsService.findList(null)); } String orgId = getCurrentOrgId(); OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(orgId); Optional.ofNullable(olBisInspOrg).orElseThrow(() -> new CheckException("当前机构不存在!!")); if ("1".equals(olBisInspOrg.getPblmLogo()) && "1".equals(objInspPblmsParam.getType())) { objInspPblmsParam.setOrgId(olBisInspOrg.getQuoteOrgId()); } else if ("22".equals(objInspPblmsParam.getType())) { objInspPblmsParam.setPguid(null); objInspPblmsParam.setOrgId(null); // if ("063".equals(orgId)) { // objInspPblmsParam.setOrgId(orgId); // } } else { objInspPblmsParam.setOrgId(null); } List objInspPblms; objInspPblms = objInspPblmsService.findList(objInspPblmsParam); //如果没找到问题清单,则设置通用问题清单 if (objInspPblms.size() == 0) { ObjInspPblmsParam objInspPblmsParam1 = new ObjInspPblmsParam(); objInspPblmsParam1.setType("111"); objInspPblms = objInspPblmsService.findList(objInspPblmsParam1); for (ObjInspPblms item : objInspPblms) { item.setType(objInspPblmsParam.getType()); } } return buildSuccessResponse(objInspPblms); } @ApiOperation(value = "获取问题类别") @GetMapping("/listByRgstr/{rgstrId}") public BaseResponse> listByRgstr(@PathVariable String rgstrId) { String type = bisInspPblmPService.getIdByRgstrId(rgstrId); Optional.ofNullable(type).orElseThrow(() -> new CheckException("当前注册单不存在!!")); ObjInspPblmsParam objInspPblmsParam = new ObjInspPblmsParam(); objInspPblmsParam.setType(type); return getPblmType(objInspPblmsParam); } @ApiOperation(value = "获取问题类别") @GetMapping("/listByObjId/{objId}") public BaseResponse> listByObjId(@PathVariable String objId) { String type = bisInspPblmPService.getIdByObjId(objId); Optional.ofNullable(type).orElseThrow(() -> new CheckException("当前注册单不存在!!")); ObjInspPblmsParam objInspPblmsParam = new ObjInspPblmsParam(); objInspPblmsParam.setType(type); return getPblmType(objInspPblmsParam); } @ApiOperation(value = "获取根据附件表问题类别") @RequestMapping(value = "/getPblmTypeInAttach", method = {RequestMethod.POST}) public BaseResponse> getPblmTypeInAttach(@RequestBody ObjInspPblmsParams objInspPblms) { List obj = new ArrayList<>(); try { obj = objInspPblmsService.getPblmTypeInAttach(objInspPblms); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return buildFailResponse(e.getMessage()); } return buildSuccessResponse(obj); } @ApiOperation(value = "根据ID删除问题分类标准库") @RequestMapping(value = "/{id}", method = RequestMethod.POST) public BaseResponse delete(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) { int ret = objInspPblmsService.delete(id); return buildSuccessResponse(); } @ApiOperation(value = "根据ID删除问题分类标准库") @RequestMapping(value = "/delete", method = RequestMethod.POST) public BaseResponse deletes(@ApiParam(name = "id", value = "id", required = true) @RequestParam String id) { if (StringUtils.isBlank(id)) { return buildFailResponse("请选择要删除的问题!"); } String[] ids = id.split(","); for (String item : ids) { objInspPblmsService.delete(item); } return buildSuccessResponse(); } @ApiOperation(value = "更新问题分类标准库信息") @RequestMapping(value = "/update", method = RequestMethod.POST) public BaseResponse update(@ApiParam(name = "objInspPblms", value = "ObjInspPblms", required = true) @RequestBody ObjInspPblms objInspPblms) { Assert.notNull(objInspPblms.getGuid(), "主键id为必填参数"); int ret = objInspPblmsService.update(objInspPblms); return buildSuccessResponse(); } @ApiOperation(value = "根据ID获取问题分类标准库(单表)") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public BaseResponse get(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) { ObjInspPblms objInspPblms = objInspPblmsService.get(id); return buildSuccessResponse(objInspPblms); } @ApiOperation(value = "获取整个标准库数据") @RequestMapping(value = "/findAll", method = RequestMethod.GET) public BaseResponse> findAll() { List pblmsList = objInspPblmsService.findList(null); return buildSuccessResponse(pblmsList); } @ApiOperation(value = "获取所有数据类型") @RequestMapping(value = "/getPblmsType", method = {RequestMethod.GET}) public BaseResponse> getPblmsType() { List pblmsType = objInspPblmsService.findPblmsName(getCurrentOrgId()); return buildSuccessResponse(pblmsType); } @ApiOperation(value = "获取所有的问题名称,去重") @RequestMapping(value = "/getPblmNames", method = RequestMethod.GET) public BaseResponse> getPblmNames(ObjInspPblmsParam objInspPblmsParam) { OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(getCurrentOrgId()); if (olBisInspOrg == null) { throw new CheckException("当前机构不存在!!"); } if ("1".equals(olBisInspOrg.getPblmLogo()) && "1".equals(objInspPblmsParam.getType())) { objInspPblmsParam.setOrgId(olBisInspOrg.getQuoteOrgId()); } else { objInspPblmsParam.setOrgId(""); } List pblmNames = objInspPblmsService.getPblmNames(objInspPblmsParam); return buildSuccessResponse(pblmNames); } @ApiOperation(value = "获取所有数据检查项") @RequestMapping(value = "/getCheckPoints", method = {RequestMethod.POST}) public BaseResponse> getPblmsType(@RequestBody ObjInspPblmsParam objInspPblmsParam) { if (StringUtils.isBlank(objInspPblmsParam.getType())) { objInspPblmsParam.setType("1"); } List pblmsType = objInspPblmsService.findPblmsCheckPoints(objInspPblmsParam, getCurrentOrgId()); return buildSuccessResponse(pblmsType); } @ApiOperation(value = "获取所有问题描述信息") @RequestMapping(value = "/getPblmsDesc", method = {RequestMethod.POST}) public BaseResponse> getPblmsDesc(@RequestBody ObjInspPblmsParam objInspPblmsParam) { if (StringUtils.isBlank(objInspPblmsParam.getType())) { objInspPblmsParam.setType("1"); } OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(getCurrentOrgId()); if (olBisInspOrg == null) { throw new CheckException("当前机构不存在!!"); } Map params = new HashMap<>(); params.put("type", objInspPblmsParam.getType()); params.put("inspPblmsName", objInspPblmsParam.getInspPblmsName()); params.put("checkPoint", objInspPblmsParam.getCheckPoint()); if ("1".equals(olBisInspOrg.getPblmLogo()) && "1".equals(objInspPblmsParam.getType())) { params.put("orgId", olBisInspOrg.getQuoteOrgId()); } List pblmsType = objInspPblmsService.findPblmsDesc(params); return buildSuccessResponse(pblmsType); } @ApiOperation(value = "获取标准表单个信息") @RequestMapping(value = "/getByPblms", method = {RequestMethod.POST}) public BaseResponse getByPblms(@RequestBody ObjInspPblmsParam objInspPblmsParam) { check(StringUtils.isNotBlank(objInspPblmsParam.getType()), "type.no"); Map params = new HashMap<>(6); params.put("inspPblmsName", objInspPblmsParam.getInspPblmsName()); params.put("checkPoint", objInspPblmsParam.getCheckPoint()); params.put("pblmDesc", objInspPblmsParam.getPblmDesc()); params.put("type", objInspPblmsParam.getType()); params.put("guid", objInspPblmsParam.getGuid()); String orgId = getCurrentOrgId(); OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(orgId); if ("1".equals(olBisInspOrg.getPblmLogo()) && "1".equals(objInspPblmsParam.getType())) { params.put("orgId", olBisInspOrg.getQuoteOrgId()); } else { params.put("orgId", ""); } ObjInspPblms pblmsType = objInspPblmsService.getByPblms(params); return buildSuccessResponse(pblmsType); } @ApiOperation(value = "获取标准表单个信息") @RequestMapping(value = "/getPblmsByType", method = {RequestMethod.POST}) public BaseResponse getPblmsByType(@RequestBody ObjInspPblmsParam objInspPblmsParam) { String orgId = getCurrentOrgId(); OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(orgId); if (olBisInspOrg == null) { throw new CheckException("当前机构不存在!!"); } if ("1".equals(olBisInspOrg.getPblmLogo()) && "1".equals(objInspPblmsParam.getType())) { objInspPblmsParam.setOrgId(olBisInspOrg.getQuoteOrgId()); } else { objInspPblmsParam.setOrgId(null); } ObjInspPblms pblmsType = objInspPblmsService.getPblmsByType(objInspPblmsParam); return buildSuccessResponse(pblmsType); } @Autowired private ObjInspPblmsDao objInspPblmsDao; @ApiOperation(value = "获取标准表单个信息(未启用)") @RequestMapping(value = "/getByWagaPblm", method = {RequestMethod.POST}) public BaseResponse> getByPblms(@RequestBody WagaPblm wagaPblm) { String sn = wagaPblm.getSn(); if (StringUtils.isNotBlank(sn)) { sn = sn.replace("至", "-"); sn = sn.replace("到", "-"); StringBuffer buffer = new StringBuffer(); if (sn.contains(",")) { String[] arrays = sn.split(","); for (String array : arrays) { if (array.contains("-")) { String[] startAndEnd = array.split("-"); if (startAndEnd.length == 2) { int start = Integer.parseInt(startAndEnd[0]); int end = Integer.parseInt(startAndEnd[1]); for (int i = start; i <= end; i++) { buffer.append(i + ","); } } else { buffer.append(startAndEnd[0] + ","); } } else { buffer.append(array + ","); } } } else if (sn.contains("-")) { String[] startAndEnd = sn.split("-"); if (startAndEnd.length == 2) { int start = Integer.parseInt(startAndEnd[0]); int end = Integer.parseInt(startAndEnd[1]); for (int i = start; i <= end; i++) { buffer.append(i + ","); } } else { buffer.append(startAndEnd[0] + ","); } } else { buffer.append(sn); } String snStr = buffer.toString(); if (snStr.endsWith(",")) { snStr = snStr.substring(0, snStr.length() - 1); } wagaPblm.setSn(snStr); List inspPblms = objInspPblmsDao.findWagaPblm(wagaPblm.getSn(), wagaPblm.getAttach(), getCurrentOrgId()); Map retMap = new HashMap<>(); List pblmNames = new ArrayList<>(); for (ObjInspPblms pblms : inspPblms) { if (!pblmNames.contains(pblms.getInspPblmsName())) { pblmNames.add(pblms.getInspPblmsName()); } } retMap.put("pblms", pblmNames); retMap.put("datas", inspPblms); return buildSuccessResponse(retMap); } return buildSuccessResponse(null); } @ApiOperation(value = "根据规则id和类型获取对应的描述信息") @RequestMapping(value = "/getPblmsByQuesNum/{id}/{type}", method = {RequestMethod.GET}) public BaseResponse> getPblmsByQuesNum(@ApiParam(name = "id", value = "规则id", required = true) @PathVariable String id, @ApiParam(name = "type", value = "类型", required = true) @PathVariable String type) { // 1. 跟id取出对应的 规则(问题标准 0:全国标准,1:自定义标准) OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(getCurrentOrgId()); if (olBisInspOrg == null) { throw new CheckException("当前机构不存在!!"); } List bisInspItemQuesNumDtoList = objInspPblmsService.getPblmsByQuesNum(id, type, olBisInspOrg); return buildSuccessResponse(bisInspItemQuesNumDtoList); } @ApiOperation(value = "根据督查类型获取所有的问题标准的分类、检查项和描述信息") @RequestMapping(value = "/getAllPblmsByType/{type}", method = {RequestMethod.GET}) public BaseResponse getAllPblmsByType(@ApiParam(name = "type", value = "类型", required = true) @PathVariable String type) { return buildSuccessResponse(objInspPblmsService.getAllPblmsByType(type, getCurrentOrgId())); } @ApiOperation(value = "获取小水库(水闸)标准数据tree类型:type=1水库,type=6水闸") @RequestMapping(value = "/getPblmsRverTree/{type}", method = {RequestMethod.GET}) public BaseResponse> getPblmsRverTree(@PathVariable(required = true) String type, HttpServletRequest request) { //第一节点 OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(getCurrentOrgId()); String orgId = ""; if ("1".equals(olBisInspOrg.getPblmLogo()) && "1".equals(type)) { orgId = olBisInspOrg.getQuoteOrgId(); } String pblmsName = request.getParameter("pblmsName"); String sort1 = request.getParameter("sort1"); List pblmsType = new ArrayList<>(); List pblmsList = new ArrayList<>(); pblmsType = objInspPblmsService.findPblmsDtoName(type, pblmsName, orgId, sort1); pblmsList = objInspPblmsService.findPblmsDTO(type, pblmsName, orgId, sort1); Set secondList = new HashSet<>(); if (pblmsList == null) { throw new CheckException("未查到任何数据!!"); } for (ObjInspPblmsDto pblm : pblmsList) { ObjInspPblmsDto secChild = new ObjInspPblmsDto(); secChild.setCheckPoint(pblm.getCheckPoint()); secChild.setInspPblmsName(pblm.getInspPblmsName()); secondList.add(secChild); } for (ObjInspPblmsDto pblm : pblmsType) { List chlist = new ArrayList<>(); for (ObjInspPblmsDto sec : secondList) { if (pblm.getInspPblmsName().equals(sec.getInspPblmsName())) { chlist.add(sec); } } pblm.setChildren(chlist); } for (ObjInspPblmsDto sec : secondList) { List chlist = new ArrayList<>(); for (ObjInspPblmsDto pblm : pblmsList) { if (sec.getInspPblmsName().equals(pblm.getInspPblmsName()) && sec.getCheckPoint().equals(pblm.getCheckPoint())) { chlist.add(pblm); } } sec.setChildren(chlist); } return buildSuccessResponse(pblmsType); } @ApiOperation("获取导入模板") @GetMapping(value = "/getExl") public void getExl(HttpServletResponse response) { String path = templatePath + File.separator + "objInspPblms.xlsx"; try { ExpAndImpUtil.downloadFile(response, path, "问题清单表"); } catch (Exception e) { logger.error(e.getMessage(), e); } } private List sort1List = new ArrayList<>(); private int sort1Index = 1; private List sort2List = new ArrayList<>(); private int sort2Index = 1; @ApiOperation("上传 xls 导入数据") @RequestMapping(value = "/impExl/{type}", method = RequestMethod.POST) public BaseResponse> impExl(@PathVariable String type, @RequestParam("file") MultipartFile multfile) throws IOException, InvalidFormatException { // 获取文件名 String fileName = multfile.getOriginalFilename(); // 获取文件后缀 String prefix = fileName.substring(fileName.lastIndexOf(".")); // 用uuid作为文件名,防止生成的临时文件重复 String uuid = UuidUtil.uuid(); File file = File.createTempFile(uuid, prefix); // MultipartFile to File multfile.transferTo(Paths.get(file.getPath())); Map map = new HashMap<>(2); int shu = 0; int success = 0; String[][] data = ReadExcelUtil.getData(file, 1); try { if (data != null && data.length > 0) { for (int i = 0; i < data.length; i++) { ObjInspPblms objInspPblms = new ObjInspPblms(); objInspPblms.setType(type); objInspPblms.setGuid(UuidUtil.uuid()); objInspPblms.setPguid("00000000000000000000000000000000"); objInspPblms.setAttach("1"); objInspPblms.setSnClass("1"); objInspPblms.setSn(data[i][0]); objInspPblms.setInspPblmsName(data[i][1]); objInspPblms.setCheckPoint(data[i][2]); objInspPblms.setPblmDesc(data[i][3]); objInspPblms.setSort1(generateSort1(data[i][1])); // Sort1是一级排序 objInspPblms.setSort2(generateSort2(data[i][2])); // Sort2是二级排序 objInspPblms.setInspPblmCate("0"); insert(objInspPblms); success++; } } } catch (Exception e) { shu++; logger.error(e.getMessage(), e); } map.put("false", shu); map.put("success", success); return buildSuccessResponse(map); } /** * 根据一级分类名称和顺序生成一级排序值 * * @param inspPblmsName 一级分类名称 * @return 排序值,长度不超过2位 */ private String generateSort1(String inspPblmsName) { if (sort1List.contains(inspPblmsName)) { for (int i = 0; i < sort1List.size(); i++) { if (inspPblmsName.equals(sort1List.get(i))) { return String.valueOf(i + 1); } } } else { sort1List.add(inspPblmsName); return String.valueOf(sort1Index++); } return null; } /** * 根据二级分类名称和顺序生成二级排序值 * * @param checkPoint 二级分类名称 * @return 排序值,长度不超过2位 */ private String generateSort2(String checkPoint) { if (sort2List.contains(checkPoint)) { for (int i = 0; i < sort2List.size(); i++) { if (checkPoint.equals(sort2List.get(i))) { return String.valueOf(i + 1); } } } else { sort2List.add(checkPoint); return String.valueOf(sort2Index++); } return null; } }