package cn.com.goldenwater.dcproj.service.impl.plansd; import cn.com.goldenwater.dcproj.constValue.AdCodeLabel; import cn.com.goldenwater.dcproj.constValue.BisInspEnum; import cn.com.goldenwater.dcproj.dao.*; import cn.com.goldenwater.dcproj.dto.*; import cn.com.goldenwater.dcproj.model.*; import cn.com.goldenwater.dcproj.param.*; import cn.com.goldenwater.dcproj.service.*; import cn.com.goldenwater.dcproj.service.impl.system.BisInspObjFactory; import cn.com.goldenwater.dcproj.utils.Constant; import cn.com.goldenwater.dcproj.utils.DateUtils; import cn.com.goldenwater.dcproj.utils.StringUtils; import cn.com.goldenwater.id.util.UuidUtil; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) public class BisInspGroupArrangementServiceImpl implements BisInspGroupArrangementService { @Autowired private BisInspGroupArrangementDao bisInspGroupArrangementDao; @Autowired private AttInspTypeDao attInspTypeDao; @Autowired private SupervisionPlanDao supervisionPlanDao; @Autowired private BisInspAllDao bisInspAllDao; @Autowired private BisInspPlanYearDao bisInspPlanYearDao; @Autowired private BisInspPlanDtlDao bisInspPlanDtlDao; @Autowired private OlBisInspOrgService olBisInspOrgService; @Autowired private BisInspPlanService bisInspPlanService; @Autowired private BisInspAllObjDao bisInspAllObjDao; @Autowired private OlBisInspOrgService inspOrgService; @Autowired private BisInspObjFactory factory; @Autowired private BisInspPlanDpDao bisInspPlanDpDao; @Autowired private BisInspPlanDtlAddvcdDao bisInspPlanDtlAddvcdDao; @Autowired private SupervisionPlanService supervisionPlanService; @Autowired private BisInspAllRlationDao bisInspAllRlationDao; @Autowired private BisInspSelAreaDao bisInspSelAreaDao; @Autowired private BisInspPlanLogService bisInspPlanLogService; @Autowired private BisInspPlanDtlPtypDao bisInspPlanDtlPtypDao; @Override public List getAllInfoByPnmAndTypeAndOrgId(BisInspGroupArrangementParam param) { List list = bisInspGroupArrangementDao.getAllInfoByPnmAndTypeAndOrgId(param); if (CollectionUtils.isNotEmpty(list)) { for (PlanAllInfoDto p : list) { Optional.ofNullable(bisInspPlanDtlDao.get(p.getDtlId())).ifPresent(dtl -> { BisInspPlanYear year = bisInspPlanYearDao.get(dtl.getPlanId()); p.setChkName(year.getChkName()); p.setPlanYearId(year.getId()); p.setState(year.getState()); p.setDtlName(dtl.getNm()); p.setChkYear(year.getChkYear()); }); String planDpId = p.getPlanDpId(); if (StringUtils.isNotBlank(planDpId)) { Optional.ofNullable(bisInspPlanDpDao.get(planDpId)).ifPresent(dp -> p.setPlanDpNm(dp.getDpName())); } List idsList = Arrays.stream(StringUtils.split(p.getIds(), ",")).collect(Collectors.toList()); String idsStr = idsList.stream().collect(Collectors.joining("','", "'", "'")); //获取人员 p.setPersonnelList(this.supervisionPlanDao.getPersAllDtoByIdsList(idsStr)); //获取分工区域 p.setAreaList(this.supervisionPlanDao.getPersAllAreaDtoByIdsList(idsStr)); //组 List groupList = new ArrayList<>(); for (String id : idsList) { PlanAllGroupDto dto = new PlanAllGroupDto(); //督查类别信息 AttInspType attInspType = attInspTypeDao.get(StringUtils.substring(id, Constant.INT_ZERO, Constant.INT_THREE)); dto.setNm(attInspType.getName()); dto.setpType(attInspType.getPtype()); BisInspAll bisInspAll = bisInspAllDao.get(id); dto.setId(bisInspAll.getId()); dto.setQuantity(bisInspAll.getQuantity()); dto.setObjectList(this.supervisionPlanDao.getPersAllObjDtoByIdList(bisInspAll.getId())); groupList.add(dto); } int numberOfUnits = bisInspPlanDtlPtypDao.selectCount(new BisInspPlanDtlPtypParam(p.getDtlId())); //判断是否可添加 if (groupList.size() >= numberOfUnits) { p.setItPossibleToAdd(Boolean.FALSE); } p.setGroupList(groupList); } } return list; } @Override public void add(PlanAllAddDto addDto) { BisInspPlanDtl dtl = bisInspPlanDtlDao.get(addDto.getDtlId()); if (Objects.isNull(dtl)) { return; } BisInspPlanYear planYear = bisInspPlanYearDao.get(dtl.getPlanId()); if (Objects.isNull(planYear)) { return; } OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(addDto.getOrgId()); String province = inspOrgService.getProvince(addDto.getOrgId()); List typeList = addDto.getTypeList(); for (String type : typeList) { String guId = lengthType(type) + addDto.getOrgId(); String batchName = Calendar.getInstance().get(Calendar.YEAR) + "年" + (StringUtils.isBlank(planYear.getChkName()) ? "" : planYear.getChkName()) + "督查"; //查询类别中是否有创建的批次 BisInspAllParam bisInspAllParam = new BisInspAllParam(); bisInspAllParam.setId(guId); bisInspAllParam.setPnm(batchName); bisInspAllParam.setIsPlan(Constant.STRING_ONE); List eList = bisInspAllDao.findList(bisInspAllParam); BisInspPlan bisInspPlan = new BisInspPlan(); bisInspPlan.setChkType(null); bisInspPlan.setChkForm(null); bisInspPlan.setIsPlan(Constant.STRING_ONE); bisInspPlan.setLeadDep(null); bisInspPlan.setPalnSttm(DateUtils.Str2Date(addDto.getStTm())); bisInspPlan.setPalnEntm(DateUtils.Str2Date(addDto.getEnTm())); bisInspPlan.setYearTaskId(dtl.getId()); bisInspPlan.setOrgId(dtl.getOrgId()); bisInspPlan.setPlanDpId(addDto.getPlanDpId()); bisInspPlan.setProvince(province); bisInspPlan.setNote(addDto.getNote()); Optional.ofNullable(olBisInspOrg).ifPresent(o -> bisInspPlan.setOrgName(o.getOrgNm())); String batchId = null; if (CollectionUtils.isEmpty(eList)) { //创建批次 bisInspPlan.setGuid(guId); bisInspPlan.setPrsnTitle(batchName); bisInspPlan.setQuantity(addDto.getQuantity()); OrganizationAndBatchAndGroup r = bisInspPlanService.addBisInspPlan(bisInspPlan); if (Objects.nonNull(r)) { batchId = r.getCode(); } } else { batchId = eList.get(Constant.INT_ZERO).getId(); } //创建组 bisInspPlan.setGuid(batchId); bisInspPlan.setPrsnTitle(addDto.getPnm()); bisInspPlan.setQuantity(addDto.getQuantity()); OrganizationAndBatchAndGroup r = bisInspPlanService.addBisInspPlan(bisInspPlan); String code = r.getCode(); List addvcdList = bisInspPlanDtlAddvcdDao.findList(new BisInspPlanDtlAddvcdParam(dtl.getId())); if (CollectionUtils.isNotEmpty(addvcdList)) { //督查区域 List areaList = new ArrayList<>(); addvcdList.forEach(addvcd -> areaList.add(new PersAllAreaDto(addvcd.getAdCode(), addvcd.getAdName()))); if (CollectionUtils.isNotEmpty(areaList)) { for (PersAllAreaDto pad : areaList) { String uuid = UuidUtil.uuid(); this.supervisionPlanDao.insertSupArea(uuid, code, pad.getAdCode(), pad.getAdName(), addDto.getOrgId(), province); } } } } } private String lengthType(String type) { if (type.length() == Constant.INT_ONE) { return "00" + type; } else if (type.length() == Constant.INT_TWO) { return "0" + type; } else { return type; } } @Override public void update(PlanAllUpDto dto) { String code = dto.getId(); String province = inspOrgService.getProvince(dto.getOrgId()); //添加督查人员 List personnelList = dto.getPersonnelList(); if (CollectionUtils.isNotEmpty(personnelList)) { for (PersAllDto pers : personnelList) { BisInspAllRlationParam rlationParam = new BisInspAllRlationParam(); rlationParam.setProvince(province); rlationParam.setId(code); rlationParam.setPersid(pers.getGuid()); List list = bisInspAllRlationDao.findList(rlationParam); if (CollectionUtils.isEmpty(list)){ this.supervisionPlanDao.insertPers(code, pers.getGuid(), pers.getPersType(), dto.getOrgId(), province, String.valueOf(Integer.parseInt(code.substring(0, 3)))); } } } List objectList = dto.getObjectList(); //添加督查对象 if (CollectionUtils.isNotEmpty(objectList)) { for (PersAllObjDto objDto : objectList) { String type = lengthType(objDto.getPtype()); // 去重判断 跟据 基础表ID、组ID、PTYPE 判NullPointerException断是否重复 BisInspAllObjParam inspAllObjParam = new BisInspAllObjParam(); inspAllObjParam.setId(code); inspAllObjParam.setCode(objDto.getCode()); inspAllObjParam.setPtype(objDto.getPtype()); List inspAllObjDaoList = bisInspAllObjDao.findList(inspAllObjParam); if (CollectionUtils.isNotEmpty(inspAllObjDaoList)) { continue; } // 添加督查对象 BisInspAllObj bisInspAllObj = this.addObj(code, dto.getPersId(), dto.getOrgId(), objDto, type); // 添加登记表 BisInspObjStrategy strategy = factory.createStrategy(objDto.getPtype()); strategy.insertObj(bisInspAllObj, dto.getPersId()); } } } @Override public void cleanGroupInfo(String groupId, String orgId ,String persId) { BisInspAllObjParam bisInspObjParam = new BisInspAllObjParam(); bisInspObjParam.setId(groupId); //根据组id获取objid bisInspObjParam.setOrgId(orgId); List bisInspAllObjList = this.bisInspAllObjDao.findList(bisInspObjParam); if (bisInspAllObjList.size() > 0) { for (BisInspAllObj bisInspAllObj : bisInspAllObjList) { //根据objid删除登记表等数据 supervisionPlanService.cleanObjDataByObjId(String.valueOf(Integer.parseInt(groupId.substring(0, 3))), bisInspAllObj.getObjId()); } } BisInspSelAreaParam bisInspSelAreaParam = new BisInspSelAreaParam(); bisInspSelAreaParam.setId(groupId); //删除督查区域 this.bisInspSelAreaDao.deleteBy(bisInspSelAreaParam); BisInspAllRlationParam bisInspAllRlationParam = new BisInspAllRlationParam(); bisInspAllRlationParam.setId(groupId); //删除督查人员 this.bisInspAllRlationDao.deleteBy(bisInspAllRlationParam); //删除组 this.bisInspAllDao.delete(groupId); //删除组记录 this.bisInspPlanLogService.insert(new BisInspPlanLog(groupId,persId)); } /** * @param ids 组id ,分割 * @param orgId 机构id */ @Override public void delAll(String ids, String orgId, String persId) { List idList = Arrays.stream(StringUtils.split(ids, ",")).collect(Collectors.toList()); for (String id : idList) { this.cleanGroupInfo(id, orgId, persId); } } @Override public void updateInfo(PlanAllUpDto dto) { List idList = Arrays.stream(StringUtils.split(dto.getIds(), ",")).collect(Collectors.toList()); //修改督查组名称 if (StringUtils.isNotBlank(dto.getPnm()) || StringUtils.isNotBlank(dto.getStTm()) || StringUtils.isNotBlank(dto.getEnTm()) || Objects.nonNull(dto.getQuantity())) { BisInspAllDto bisInspAllDto = new BisInspAllDto(); bisInspAllDto.setPnm(dto.getPnm()); bisInspAllDto.setSttm(dto.getStTm()); bisInspAllDto.setEntm(dto.getEnTm()); bisInspAllDto.setQuantity(dto.getQuantity()); for (String id : idList) { bisInspAllDto.setId(id); this.supervisionPlanDao.updateNode(bisInspAllDto); } } } @Override public void delSinglePersonnel(PlanAllDelDto dto) { BisInspAllRlationParam param = new BisInspAllRlationParam(); param.setId(dto.getGroupId()); param.setPersid(dto.getDelId()); //删除督查人员 this.bisInspAllRlationDao.deleteBy(param); } @Override public void delSingleObject(PlanAllDelDto dto) { String groupId = dto.getGroupId(); if (groupId.length() > Constant.INT_THREE) { BisInspAllObjParam objParam = new BisInspAllObjParam(); objParam.setId(groupId); objParam.setCode(dto.getDelId()); Optional.ofNullable(bisInspAllObjDao.findList(objParam)).ifPresent(objList -> { for (BisInspAllObj bisInspAllObj : objList) { //根据objid删除登记表等数据 this.supervisionPlanService.cleanObjDataByObjId(String.valueOf(Integer.parseInt(StringUtils.substring(groupId, 0, 3))), bisInspAllObj.getObjId()); } }); } } /** * 添加对象表 BIS_INSP_ALL_OBJ * * @param code 组id * @param persId 创建人 * @param orgId 机构id * @param persObj obj * @param type 督查类别 * @return BisInspAllObj */ private BisInspAllObj addObj(String code, String persId, String orgId, PersAllObjDto persObj, String type) { OlBisInspOrg olBisInspOrg = olBisInspOrgService.get(orgId); //行政区划 String adCode = null; if (Objects.nonNull(olBisInspOrg)) { adCode = olBisInspOrg.getRlcode(); } //省级行政区划 String province = inspOrgService.getProvince(orgId); BisInspAllObj bisInspAllObj = new BisInspAllObj(); bisInspAllObj.setObjId(UuidUtil.uuid()); bisInspAllObj.setPtype(persObj.getPtype()); bisInspAllObj.setCode(persObj.getCode()); bisInspAllObj.setId(code); bisInspAllObj.setNm(persObj.getName()); bisInspAllObj.setProvince(province); bisInspAllObj.setPersId(persId); // 查询经纬度 // 30 超标洪水防御 没有省级划分,所以无法获取经纬度 if (!BisInspEnum.EFP.getRlation().equals(type)) { LgtdAndLttd lgtdAndLttd = this.supervisionPlanDao.getLgtdAndLttd(persObj.getCode(), type); Optional.ofNullable(lgtdAndLttd).ifPresent(ld -> { bisInspAllObj.setLgtd(ld.getLgtd()); bisInspAllObj.setLttd(ld.getLttd()); bisInspAllObj.setLgtdpc(ld.getLgtdPc()); bisInspAllObj.setLttdpc(ld.getLttdPc()); }); } // 判断是否是全国数据 if (!adCode.startsWith(AdCodeLabel.START_PARENT_ADCODE)) { bisInspAllObj.setAdCode(adCode); bisInspAllObj.setOrgId(orgId); } // 添加督查对象 this.bisInspAllObjDao.insert(bisInspAllObj); return bisInspAllObj; } }