package cn.com.goldenwater.dcproj.service.impl.tac; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.dao.*; import cn.com.goldenwater.dcproj.dto.TacCountDto; import cn.com.goldenwater.dcproj.model.*; import cn.com.goldenwater.dcproj.param.*; import cn.com.goldenwater.dcproj.service.TacInspPersAreaPrisService; import cn.com.goldenwater.dcproj.utils.InspUtils; import cn.com.goldenwater.id.util.UuidUtil; import org.apache.commons.collections.map.HashedMap; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; /** * @author lune * @date 2019-9-6 */ @Service @Transactional public class TacInspPersAreaPrisServiceImpl extends AbstractCrudService implements TacInspPersAreaPrisService { @Autowired private TacInspPersAreaPrisDao tacInspPersAreaPrisDao; @Autowired private TacWorkerBDao tacWorkerBDao; @Autowired private TacTacInspPersPrisLtsDao ltsDao; @Autowired private TacInspYearBatchDao yearBatchDao; @Autowired private TacInspYearBatchAreaDao areaDao; @Autowired private TacInspYearBatchGroupDao tacInspYearBatchGroupDao; @Autowired private TacInspYearBatchGroupPersDao groupPersDao; @Autowired private AttAdXBaseDao attAdXBaseDao; public TacInspPersAreaPrisServiceImpl(TacInspPersAreaPrisDao tacInspPersAreaPrisDao) { super(tacInspPersAreaPrisDao); this.tacInspPersAreaPrisDao = tacInspPersAreaPrisDao; } @Override public TacInspPersAreaPris getAreaPris(String id, String yearBatchId, String province) { TacWorkerBParam bParam = new TacWorkerBParam(); bParam.setYearBatchId(yearBatchId); bParam.setGroupType(InspUtils.setOrgIds("1")); //获取库表 bParam.setProvince(province); List comList = tacWorkerBDao.countByRole(bParam); bParam.setGroupType(InspUtils.setOrgIds("7")); List proList = tacWorkerBDao.countByRole(bParam); bParam.setGroupType(InspUtils.setOrgIds("4")); List goldList = tacWorkerBDao.countByRole(bParam); bParam.setGroupType(InspUtils.setOrgIds("5")); List redList = tacWorkerBDao.countByRole(bParam); if (StringUtils.isBlank(id) || "null".equals(id)) { TacInspPersAreaPris pris = new TacInspPersAreaPris(); pris.setCommList(comList); pris.setProList(proList); pris.setGoldList(goldList); pris.setRedList(redList); return pris; } //获取规则id TacInspPersAreaPris pris = tacInspPersAreaPrisDao.get(id); TacTacInspPersPrisLtsParam ltsParam = new TacTacInspPersPrisLtsParam(); if ("1".equals(String.valueOf(pris.getIsNew()))) { bParam.setNewSize(pris.getNewSize()); List newList = tacWorkerBDao.countByRoleNew(bParam); getSelectCount(newList, ltsParam, "8"); pris.setNewList(newList); } ltsParam.setPersPrisId(id); getSelectCount(comList, ltsParam, "1"); getSelectCount(proList, ltsParam, "7"); getSelectCount(goldList, ltsParam, "4"); getSelectCount(redList, ltsParam, "5"); pris.setCommList(comList); pris.setProList(proList); pris.setGoldList(goldList); pris.setRedList(redList); return pris; } private void getSelectCount(List list, TacTacInspPersPrisLtsParam ltsParam, String type) { ltsParam.setTenetType(type); TacTacInspPersPrisLts lts = ltsDao.getBy(ltsParam); if (lts == null) { return; } for (TacCountDto tacCountDto : list) { if ("20".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getSpecialSize() == null ? 0 : lts.getSpecialSize())); } if ("19".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getAssistantSize() == null ? 0 : lts.getAssistantSize())); } if ("11".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getEarlyTisSize() == null ? 0 : lts.getEarlyTisSize())); } if ("12".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getDevemanageTisSize() == null ? 0 : lts.getDevemanageTisSize())); } if ("13".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getPlanTisSize() == null ? 0 : lts.getPlanTisSize())); } if ("14".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getFinanceTisSize() == null ? 0 : lts.getFinanceTisSize())); } if ("15".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getQualityTisSize() == null ? 0 : lts.getQualityTisSize())); } if ("16".equals(tacCountDto.getCode())) { tacCountDto.setSelectCount(String.valueOf(lts.getSafetyTisSize() == null ? 0 : lts.getSafetyTisSize())); } } } private TacTacInspPersPrisLts setLtsCount(List list, TacTacInspPersPrisLts lts, TacInspPersAreaPris tacInspPersAreaPris, String type) { if (lts == null) { lts = new TacTacInspPersPrisLts(); lts.setId(UuidUtil.uuid()); lts.setInTm(new Date()); lts.setUpTm(new Date()); lts.setPersPrisId(tacInspPersAreaPris.getId()); lts.setPersId(tacInspPersAreaPris.getPersId()); lts.setTenetType(type); } Long total = new Long(0); for (TacCountDto tacCountDto : list) { if (StringUtils.isNotBlank(tacCountDto.getSelectCount()) && !"null".equals(tacCountDto.getSelectCount())) { total += Long.valueOf(tacCountDto.getSelectCount()); if ("20".equals(tacCountDto.getCode())) { lts.setSpecialSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("19".equals(tacCountDto.getCode())) { lts.setAssistantSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("11".equals(tacCountDto.getCode())) { lts.setEarlyTisSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("12".equals(tacCountDto.getCode())) { lts.setDevemanageTisSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("13".equals(tacCountDto.getCode())) { lts.setPlanTisSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("14".equals(tacCountDto.getCode())) { lts.setFinanceTisSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("15".equals(tacCountDto.getCode())) { lts.setQualityTisSize(Long.valueOf(tacCountDto.getSelectCount())); } if ("16".equals(tacCountDto.getCode())) { lts.setSafetyTisSize(Long.valueOf(tacCountDto.getSelectCount())); } } } lts.setTotalSize(total); return lts; } @Override public int insertPris(TacInspPersAreaPris tacInspPersAreaPris) { TacInspYearBatch batch = yearBatchDao.get(tacInspPersAreaPris.getYearBatchId()); tacInspPersAreaPris.setYear(batch.getYear()); tacInspPersAreaPris.setBatch(batch.getBatch()); this.tacInspPersAreaPrisDao.insert(tacInspPersAreaPris); if (tacInspPersAreaPris.getCommList() != null && tacInspPersAreaPris.getCommList().size() > 0) { TacTacInspPersPrisLts lts = setLtsCount(tacInspPersAreaPris.getCommList(), null, tacInspPersAreaPris, "1"); ltsDao.insert(lts); } if (tacInspPersAreaPris.getProList() != null && tacInspPersAreaPris.getProList().size() > 0) { TacTacInspPersPrisLts lts = setLtsCount(tacInspPersAreaPris.getProList(), null, tacInspPersAreaPris, "7"); ltsDao.insert(lts); } if ("1".equals(tacInspPersAreaPris.getIsPick())) { if (tacInspPersAreaPris.getGoldList() != null && tacInspPersAreaPris.getGoldList().size() > 0) { TacTacInspPersPrisLts lts = setLtsCount(tacInspPersAreaPris.getGoldList(), null, tacInspPersAreaPris, "4"); ltsDao.insert(lts); } if (tacInspPersAreaPris.getRedList() != null && tacInspPersAreaPris.getRedList().size() > 0) { TacTacInspPersPrisLts lts = setLtsCount(tacInspPersAreaPris.getRedList(), null, tacInspPersAreaPris, "5"); ltsDao.insert(lts); } } if ("1".equals(tacInspPersAreaPris.getIsNew()) && tacInspPersAreaPris.getNewList().size() > 0) { TacTacInspPersPrisLts lts = setLtsCount(tacInspPersAreaPris.getNewList(), null, tacInspPersAreaPris, "8"); ltsDao.insert(lts); } return 0; } @Override public int updatePris(TacInspPersAreaPris tacInspPersAreaPris) { TacInspYearBatch batch = yearBatchDao.get(tacInspPersAreaPris.getYearBatchId()); tacInspPersAreaPris.setYear(batch.getYear()); tacInspPersAreaPris.setBatch(batch.getBatch()); this.tacInspPersAreaPrisDao.update(tacInspPersAreaPris); TacTacInspPersPrisLtsParam ltsParam = new TacTacInspPersPrisLtsParam(); ltsParam.setPersPrisId(tacInspPersAreaPris.getId()); if (tacInspPersAreaPris.getCommList() != null && tacInspPersAreaPris.getCommList().size() > 0) { ltsParam.setTenetType("1"); TacTacInspPersPrisLts lts = ltsDao.getBy(ltsParam); if (lts == null) { lts = setLtsCount(tacInspPersAreaPris.getCommList(), null, tacInspPersAreaPris, "1"); ltsDao.insert(lts); } else { setLtsCount(tacInspPersAreaPris.getCommList(), lts, tacInspPersAreaPris, "1"); ltsDao.update(lts); } } if (tacInspPersAreaPris.getProList() != null && tacInspPersAreaPris.getProList().size() > 0) { ltsParam.setTenetType("7"); TacTacInspPersPrisLts lts = ltsDao.getBy(ltsParam); if (lts == null) { lts = setLtsCount(tacInspPersAreaPris.getProList(), null, tacInspPersAreaPris, "7"); ltsDao.insert(lts); } else { setLtsCount(tacInspPersAreaPris.getProList(), lts, tacInspPersAreaPris, "7"); ltsDao.update(lts); } } if ("1".equals(tacInspPersAreaPris.getIsPick())) { if (tacInspPersAreaPris.getGoldList() != null && tacInspPersAreaPris.getGoldList().size() > 0) { ltsParam.setTenetType("4"); TacTacInspPersPrisLts lts = ltsDao.getBy(ltsParam); if (lts == null) { lts = setLtsCount(tacInspPersAreaPris.getGoldList(), null, tacInspPersAreaPris, "4"); ltsDao.insert(lts); } else { setLtsCount(tacInspPersAreaPris.getGoldList(), lts, tacInspPersAreaPris, "4"); ltsDao.update(lts); } } if (tacInspPersAreaPris.getRedList() != null && tacInspPersAreaPris.getRedList().size() > 0) { ltsParam.setTenetType("5"); TacTacInspPersPrisLts lts = ltsDao.getBy(ltsParam); if (lts == null) { lts = setLtsCount(tacInspPersAreaPris.getRedList(), null, tacInspPersAreaPris, "5"); ltsDao.insert(lts); } else { setLtsCount(tacInspPersAreaPris.getRedList(), lts, tacInspPersAreaPris, "5"); ltsDao.update(lts); } } } else { ltsParam.setTenetType("4"); ltsDao.deleteBy(ltsParam); ltsParam.setTenetType("5"); ltsDao.deleteBy(ltsParam); } if ("1".equals(tacInspPersAreaPris.getIsNew()) && tacInspPersAreaPris.getNewList().size() > 0) { ltsParam.setTenetType("8"); TacTacInspPersPrisLts lts = ltsDao.getBy(ltsParam); if (lts == null) { lts = setLtsCount(tacInspPersAreaPris.getNewList(), null, tacInspPersAreaPris, "8"); ltsDao.insert(lts); } else { setLtsCount(tacInspPersAreaPris.getNewList(), lts, tacInspPersAreaPris, "8"); ltsDao.update(lts); } } else { ltsParam.setTenetType("8"); ltsDao.deleteBy(ltsParam); } return 0; } @Override public TacInspPersAreaPris getPersAreaPris(String id, String province) { TacInspPersAreaPris areaPris = this.tacInspPersAreaPrisDao.get(id); if (areaPris == null) { return null; } TacInspYearBatchAreaParam areaParam = new TacInspYearBatchAreaParam(); areaParam.setYearBatchId(areaPris.getYearBatchId()); areaParam.setProvince(province); List list = areaDao.findList(areaParam); Map areaList = new HashedMap(); list.forEach(tacInspYearBatchArea -> { if (!areaList.keySet().contains(tacInspYearBatchArea.getGroupId())) { areaList.put(tacInspYearBatchArea.getGroupId(), tacInspYearBatchArea); } else { TacInspYearBatchArea area = areaList.get(tacInspYearBatchArea.getGroupId()); area.setId(area.getId() + "," + tacInspYearBatchArea.getId()); area.setAdCode(area.getAdCode() + "," + tacInspYearBatchArea.getAdCode()); area.setAdName(area.getAdName() + "," + tacInspYearBatchArea.getAdName()); } }); areaPris.setAreaList(new ArrayList<>(areaList.values())); return areaPris; } @Override public int updatePersAreaPris(TacInspPersAreaPris areaPris) { if (StringUtils.isNotBlank(areaPris.getId())) { tacInspPersAreaPrisDao.update(areaPris); } List list = new ArrayList<>(); if (areaPris.getAreaList() != null) { list = areaPris.getAreaList(); } // Map map = list.stream().collect(Collectors.toMap(TacInspYearBatchArea::getAdName, Function.identity())); Map> map = new HashedMap(); //adName重复判断 list.forEach(area -> { if (map.containsKey(area.getAdName())) { map.get(area.getAdName()).add(area); } else { List areaList = new ArrayList<>(); areaList.add(area); map.put(area.getAdName(), areaList); } }); for (Map stringMap : areaPris.getAdNames()) { String adName = stringMap.get("adName"); if (map.containsKey(adName)) { List areaList = map.get(adName); if (areaList.size() > 0) { areaList.remove(0); continue; } } TacInspYearBatchGroup group = new TacInspYearBatchGroup(); group.setId(UuidUtil.uuid()); group.setYearBatchId(areaPris.getYearBatchId()); group.setTenet(areaPris.getId()); group.setInTm(new Date()); TacInspYearBatchGroupParam groupParam = new TacInspYearBatchGroupParam(); groupParam.setYearBatchId(areaPris.getYearBatchId()); String groupNum = this.tacInspYearBatchGroupDao.getMaxGourpNum(groupParam); if (StringUtils.isBlank(groupNum)) { group.setGroupNm(1L); } else { group.setGroupNm(Long.parseLong(groupNum) + 1); } group.setYear(areaPris.getYear()); group.setBatch(areaPris.getBatch()); group.setProvince(areaPris.getProvince()); group.setConfirmer("0"); tacInspYearBatchGroupDao.insert(group); //新增 if (adName.contains(",")) { // String [] ads = area.getAdCode().split(","); String[] adNames = adName.split(","); for (int i = 0; i < adNames.length; i++) { AttAdXBaseParam adXBaseParam = new AttAdXBaseParam(); adXBaseParam.setAdName(adNames[i]); AttAdXBase attAdXBase = attAdXBaseDao.getByName(adXBaseParam); if (attAdXBase == null) { continue; } TacInspYearBatchArea batchArea = new TacInspYearBatchArea(); batchArea.setId(UuidUtil.uuid()); batchArea.setGroupId(group.getId()); batchArea.setYearBatchId(group.getYearBatchId()); batchArea.setAdCode(attAdXBase.getAdCode()); batchArea.setAdName(adNames[i]); batchArea.setGroupNm(group.getGroupNm()); batchArea.setBatch(areaPris.getBatch()); batchArea.setYear(areaPris.getYear()); batchArea.setInTm(new Date()); batchArea.setUpTm(new Date()); batchArea.setProvince(areaPris.getProvince()); areaDao.insert(batchArea); } } else { AttAdXBaseParam adXBaseParam = new AttAdXBaseParam(); adXBaseParam.setAdName(adName); AttAdXBase attAdXBase = attAdXBaseDao.getByName(adXBaseParam); if (attAdXBase == null) { continue; } TacInspYearBatchArea batchArea = new TacInspYearBatchArea(); batchArea.setId(UuidUtil.uuid()); batchArea.setGroupId(group.getId()); batchArea.setYearBatchId(group.getYearBatchId()); batchArea.setGroupNm(group.getGroupNm()); batchArea.setAdCode(attAdXBase.getAdCode()); batchArea.setAdName(adName); batchArea.setBatch(areaPris.getBatch()); batchArea.setYear(areaPris.getYear()); batchArea.setInTm(new Date()); batchArea.setUpTm(new Date()); batchArea.setProvince(areaPris.getProvince()); areaDao.insert(batchArea); } } //删除组与区域 if (map.size() > 0) { TacInspYearBatchGroupPersParam groupPersParam = new TacInspYearBatchGroupPersParam(); for (String key : map.keySet()) { List areaList = map.get(key); if (areaList == null || areaList.isEmpty()) { continue; } for (TacInspYearBatchArea area : areaList) { tacInspYearBatchGroupDao.delete(area.getGroupId()); groupPersParam.setGroupId(area.getGroupId()); groupPersDao.deleteBy(groupPersParam); if (area.getId().contains(",")) { for (String id : area.getId().split(",")) { areaDao.delete(id); } } else { areaDao.delete(area.getId()); } } } } TacInspYearBatch batch = new TacInspYearBatch(); batch.setId(areaPris.getYearBatchId()); batch.setIsAreaGroup("1"); yearBatchDao.update(batch); return 0; } @Override public TacInspPersAreaPris getByYearBatchId(String yearBatchId) { TacInspPersAreaPrisParam persAreaPrisParam = new TacInspPersAreaPrisParam(); persAreaPrisParam.setYearBatchId(yearBatchId); List list = tacInspPersAreaPrisDao.findList(persAreaPrisParam); if (list.size() > 0) { return list.get(0); } return null; } }