package cn.com.goldenwater.dcproj.service.impl.importex; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.dao.*; import cn.com.goldenwater.dcproj.dto.BisInspRgstrDto; import cn.com.goldenwater.dcproj.dto.BisInspSwhsRgstrPcDtoVo; import cn.com.goldenwater.dcproj.model.*; import cn.com.goldenwater.dcproj.param.*; import cn.com.goldenwater.dcproj.service.BisInspSwhsRgstrService; import cn.com.goldenwater.dcproj.service.ImpDcInfoService; import cn.com.goldenwater.dcproj.utils.BeanUtil; import cn.com.goldenwater.dcproj.utils.export.CreateExcelHelper; import cn.com.goldenwater.dcproj.utils.impexcel.ExpAndImpUtil; import cn.com.goldenwater.dcproj.utils.impexcel.ImportExcel; import cn.com.goldenwater.dcproj.utils.impexcel.format.DcSydFieldFormatExp; import cn.com.goldenwater.dcproj.utils.impexcel.format.DcSydFieldFormatImp; import cn.com.goldenwater.id.util.UuidUtil; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; import static cn.com.goldenwater.dcproj.utils.export.FieldFormat.formatCheckNull; import static cn.com.goldenwater.dcproj.utils.export.FieldFormat.formatState; import static cn.com.goldenwater.dcproj.utils.impexcel.field.ExcelFieldLists.*; /** *

* 批量填报 *

* * @author liyz * @date 2019/8/14 13:34 **/ @Service @Transactional(rollbackFor = Exception.class) public class ImpDcInfoServiceImpl extends AbstractCrudService implements ImpDcInfoService { @Value("${export.templatePath}") private String templatePath; @Autowired private ImpDcInfoDao impDcInfoDao; @Autowired private BisInspAllDao bisInspAllDao; @Autowired private CreateExcelHelper createExcelHelper; @Autowired private BisInspSwhsRgstrDao bisInspSwhsRgstrDao; @Autowired private BisInspSwhsRgstrService bisInspSwhsRgstrService; @Autowired private AttSwhsBaseCrrctDao attSwhsBaseCrrctDao; @Autowired private BisInspSwhsRgstrSafetyDao bisInspSwhsRgstrSafetyDao; @Autowired private BisInspSwhsRgstrMeasuresDao bisInspSwhsRgstrMeasuresDao; @Autowired private AttSwhsBaseDao attSwhsBaseDao; @Autowired private RedisTemplate redisTemplate; public ImpDcInfoServiceImpl(ImpDcInfoDao impDcInfoDao) { super(impDcInfoDao); this.impDcInfoDao = impDcInfoDao; } @Override public void downloadTemplate(HttpServletResponse response, String userId, String orgType, String orgIds) throws Exception { // 模板路径 String filePath = templatePath + File.separator + "tbSyd.xls"; // SHEET名称 String sheetName = "水源地填报表"; // 文件名称带后缀 String fileName = sheetName + ".xls"; // startRow int startRow = 4; List rgstrList = this.getRgstrList(userId,orgIds); List dtoVoList = this.getBisDtoVo(rgstrList,userId); List> dataList = this.getExpDataList(dtoVoList); ExpAndImpUtil.download(response,dataList,filePath,startRow,sheetName); } public List getBisDtoVo(List dataList, String userId) { List dataListVo = new ArrayList<>(); dataList.forEach(rgstrDto -> { BisInspSwhsRgstrPcDtoVo bisVo = new BisInspSwhsRgstrPcDtoVo(); // 放入登记表 bisVo.setBisInspSwhsRgstr(this.getBisInspSwhsRgstr(rgstrDto)); // 放入通用登记表 bisVo.setBisInspRgstrDto(rgstrDto); // 1.基本信息纠错表 // 2.安全保障达标落实情况 bisVo.setBisInspSwhsRgstrSafety(this.getBisInspSwhsRgstrSafety(rgstrDto)); // 3.保护措施监管情况 bisVo.setBisInspSwhsRgstrMeasures(this.getBisInspSwhsRgstrMeasures(rgstrDto)); dataListVo.add(bisVo); }); return dataListVo; } private BisInspSwhsRgstr getBisInspSwhsRgstr(BisInspRgstrDto rgstrDto){ return bisInspSwhsRgstrDao.get(rgstrDto.getRgstrId()); } private AttSwhsBaseCrrct getAttSwhsBaseCrrct(BisInspRgstrDto rgstrDto,String userId){ CrrctParam crrctParam = new CrrctParam(); crrctParam.setPersId(userId); crrctParam.setCode(rgstrDto.getCode()); // 根据人员id 和 水源地编码 AttSwhsBaseCrrctParam attSwhsBaseCrrctParam = new AttSwhsBaseCrrctParam(); attSwhsBaseCrrctParam.setSwhsCode(rgstrDto.getCode()); attSwhsBaseCrrctParam.setPersId(userId); if(StringUtils.isBlank(attSwhsBaseCrrctParam.getSwhsCode())){ return new AttSwhsBaseCrrct(); }else { AttSwhsBaseCrrct crrct = attSwhsBaseCrrctDao.getBy(attSwhsBaseCrrctParam); if (crrct == null) { AttSwhsBase base = attSwhsBaseDao.get(crrctParam.getCode()); crrct = new AttSwhsBaseCrrct(); BeanUtil.copyObject1(base,crrct); crrct.setId(""); crrct.setSwhsCode(base.getId()); } return crrct; } } private BisInspSwhsRgstrSafety getBisInspSwhsRgstrSafety(BisInspRgstrDto rgstrDto){ BisInspSwhsRgstrSafetyParam bisInspSwhsRgstrSafetyParam = new BisInspSwhsRgstrSafetyParam(); bisInspSwhsRgstrSafetyParam.setRgstrId(rgstrDto.getId()); if(StringUtils.isBlank(bisInspSwhsRgstrSafetyParam.getRgstrId())){ return new BisInspSwhsRgstrSafety(); }else { return bisInspSwhsRgstrSafetyDao.getBy(bisInspSwhsRgstrSafetyParam); } } private BisInspSwhsRgstrMeasures getBisInspSwhsRgstrMeasures(BisInspRgstrDto rgstrDto){ BisInspSwhsRgstrMeasuresParam bisInspSwhsRgstrMeasuresParam = new BisInspSwhsRgstrMeasuresParam(); bisInspSwhsRgstrMeasuresParam.setRgstrId(rgstrDto.getId()); if(StringUtils.isBlank(bisInspSwhsRgstrMeasuresParam.getRgstrId())){ return new BisInspSwhsRgstrMeasures(); }else { return bisInspSwhsRgstrMeasuresDao.getBy(bisInspSwhsRgstrMeasuresParam); } } private List> getExpDataList(List list){ // 把实体类的数据放入到List里去 List> dataList = new LinkedList<>(); for (BisInspSwhsRgstrPcDtoVo bis : list) { HashMap map = new HashMap<>(); //通用登记表 BisInspRgstrDto bisInspRgstrDto = bis.getBisInspRgstrDto(); // 放入登记表主键 map.put("rgstrId", bisInspRgstrDto.getId()); // 督查组 map.put("groupName", formatCheckNull(bisInspRgstrDto.getUpGroupName())); // 督查状态 map.put("state", formatState(bisInspRgstrDto.getState())); // 改成放入登记表 BisInspSwhsRgstr bisInspSwhsRgstr = bis.getBisInspSwhsRgstr(); ExpAndImpUtil.convertBeanToMap(bisInspSwhsRgstr, attSwhsBaseCrrctArrays, map, DcSydFieldFormatExp.class); // 2.安全保障达标落实情况 BisInspSwhsRgstrSafety bisInspSwhsRgstrSafety = bis.getBisInspSwhsRgstrSafety(); ExpAndImpUtil.convertBeanToMap(bisInspSwhsRgstrSafety, bisInspSwhsRgstrSafetyArrays, map, DcSydFieldFormatExp.class); // 3.保护措施监管情况 BisInspSwhsRgstrMeasures bisInspSwhsRgstrMeasures = bis.getBisInspSwhsRgstrMeasures(); ExpAndImpUtil.convertBeanToMap(bisInspSwhsRgstrMeasures, bisInspSwhsRgstrMeasuresArrays, map, DcSydFieldFormatExp.class); dataList.add(map); } return dataList; } @Override public Map parseTemplate(String filePath, String userId, String orgType) { Map map = new HashMap<>(); List> listParse = null; int startRow = 4; int endRow = 0; List> listShow = new ArrayList<>(); try { // 获取到从Excel里提取到的数据,并已格式化 listParse = ImportExcel.getParseData(filePath,startRow,endRow, DcSydFieldFormatImp.class); if(listParse.size() > 0){ String cacheId = UuidUtil.uuid(); redisTemplate.opsForList().rightPushAll(cacheId,listParse); map.put("cacheId",cacheId); }else{ map.put("cacheId",""); } listShow = ImportExcel.getShowData(listParse, DcSydFieldFormatExp.class); // 过滤掉不是此用户的节点 listShow = this.filterNotTreeIdData(listShow, userId, null); map.put("listParse",listShow); } catch (IOException e) { e.printStackTrace(); } return map; } @Override public String insertTemplateInfo(String cacheId, String userId, String orgType) { // 从redis里获取到数据 List> listParse = this.getParseDataByRedis(cacheId); // 过滤掉不是此用户的节点 listParse = this.filterNotTreeIdData(listParse,userId,null); switch (orgType){ case "013": return this.insertTemplateInfoSyd(listParse,userId); default: return null; } } /** * 添加入库从模板里获取到的水源地督查信息 */ private String insertTemplateInfoSyd(List> listParse, String userId) { int successCount = 0; for (LinkedHashMap lhp : listParse) { /* 只对未督查过和督查中的水库进行批量填报 */ try { String rgstrId = MapUtils.getString(lhp,"rgstrId"); if (StringUtils.isBlank(rgstrId) || rgstrId.length() != 32) { continue; } // 1.修改基础信息表和基础信息纠错表 // 改成修改登记表 this.insertOrUpdateSwhsRgstr(lhp,userId); // 2.安全保障达标落实情况 this.insertOrUpdateSwhsRgstrSafety(lhp, userId); // 3.保护措施监管情况 this.insertOrUpdateSwhsRgstrMeasures(lhp, userId); successCount ++; } catch (Exception e) { e.printStackTrace(); } } int sumCount = listParse.size(); return " 此次填报共:" + sumCount + "条," + " 成功" + successCount + "条,失败"+ (sumCount - successCount) + "条"; } /** * 只入库一次,只取一次,取完及时删除 */ private List> getParseDataByRedis(String cacheId){ // 取出 List> listParse = redisTemplate.opsForList().range(cacheId, 0, -1); // 删除 redisTemplate.delete(cacheId); return listParse; } /** * @see cn.com.goldenwater.dcproj.controller.BisInspSwhsRgstrController#insert(BisInspSwhsRgstr) */ private void insertOrUpdateSwhsRgstr(LinkedHashMap lhp,String userId) throws InstantiationException, IllegalAccessException { BisInspSwhsRgstr bisInspSwhsRgstr = (BisInspSwhsRgstr) ExpAndImpUtil.convertMapToBean(BisInspSwhsRgstr.class, lhp); // 先用登记表id查一下 BisInspSwhsRgstr rgstr = bisInspSwhsRgstrDao.get(MapUtils.getString(lhp,RGSTRID)); bisInspSwhsRgstr.setId(rgstr.getId()); bisInspSwhsRgstr.setObjId(rgstr.getObjId()); bisInspSwhsRgstr.setSwhsId(rgstr.getSwhsId()); bisInspSwhsRgstr.setUpTm(new Date()); bisInspSwhsRgstr.setPersId(userId); bisInspSwhsRgstr.setGdX(rgstr.getGdX()); bisInspSwhsRgstr.setGdY(rgstr.getGdY()); bisInspSwhsRgstr.setGroupId(rgstr.getGroupId()); if(StringUtils.isNotBlank(bisInspSwhsRgstr.getId())){ bisInspSwhsRgstrService.updateRgstr(bisInspSwhsRgstr); } } /** * @see cn.com.goldenwater.dcproj.controller.AttSwhsBaseCrrctController#insert(AttSwhsBaseCrrct) */ private void insertOrUpdateSwhsBaseCrrct(LinkedHashMap lhp,String userId){ try{ // 从导入的Excel里的字段赋值给实体类 AttSwhsBaseCrrct attSwhsBaseCrrct = (AttSwhsBaseCrrct) ExpAndImpUtil.convertMapToBean(AttSwhsBaseCrrct.class, lhp); // 先用登记表id查一下 BisInspSwhsRgstr rgstr = bisInspSwhsRgstrDao.get(MapUtils.getString(lhp,"rgstrId")); // 有没有纠错信息 AttSwhsBaseCrrctParam param = new AttSwhsBaseCrrctParam(); param.setSwhsCode(rgstr.getSwhsId()); param.setPersId(userId); List list = attSwhsBaseCrrctDao.findList(param); // 总是添加一条纠错到纠错表 attSwhsBaseCrrct.setId(UuidUtil.uuid()); attSwhsBaseCrrct.setPersId(userId); attSwhsBaseCrrct.setInTm(new Date()); attSwhsBaseCrrct.setUpTm(new Date()); attSwhsBaseCrrctDao.insert(attSwhsBaseCrrct); // 更新登记表 BeanUtil.copyObject1(attSwhsBaseCrrct, rgstr); rgstr.setId(rgstr.getId()); if(list.size() > 0){ rgstr.setGdX(list.get(0).getSwhsLongGd()); rgstr.setGdY(list.get(0).getSwhsLatGd()); } rgstr.setCenterX(attSwhsBaseCrrct.getSwhsLong()); rgstr.setCenterY(attSwhsBaseCrrct.getSwhsLat()); rgstr.setPersId(userId); bisInspSwhsRgstrDao.update(rgstr); }catch (Exception e){ e.printStackTrace(); } } /** * @see cn.com.goldenwater.dcproj.controller.BisInspSwhsRgstrSafetyController#insert(BisInspSwhsRgstrSafety) */ private void insertOrUpdateSwhsRgstrSafety(LinkedHashMap lhp,String userId) throws InstantiationException, IllegalAccessException { BisInspSwhsRgstrSafety bisInspSwhsRgstrSafety = (BisInspSwhsRgstrSafety) ExpAndImpUtil.convertMapToBean(BisInspSwhsRgstrSafety.class, lhp); // 新增或更新本表 BisInspSwhsRgstrSafetyParam param = new BisInspSwhsRgstrSafetyParam(); param.setRgstrId(bisInspSwhsRgstrSafety.getRgstrId()); List list = bisInspSwhsRgstrSafetyDao.findList(param); if (list.size() > 0 ){ bisInspSwhsRgstrSafety.setId(list.get(0).getId()); bisInspSwhsRgstrSafety.setUpTm(new Date()); bisInspSwhsRgstrSafety.setRecPersId(userId); bisInspSwhsRgstrSafetyDao.update(bisInspSwhsRgstrSafety); }else { bisInspSwhsRgstrSafety.setInTm(new Date()); bisInspSwhsRgstrSafety.setUpTm(new Date()); bisInspSwhsRgstrSafety.setId(UuidUtil.uuid()); bisInspSwhsRgstrSafety.setRecPersId(userId); bisInspSwhsRgstrSafetyDao.insert(bisInspSwhsRgstrSafety); } // 更新登记表 if (org.apache.commons.lang3.StringUtils.isNotBlank(bisInspSwhsRgstrSafety.getRgstrId())) { BisInspSwhsRgstr rgstr = bisInspSwhsRgstrDao.get(bisInspSwhsRgstrSafety.getRgstrId()); if (rgstr != null) { // 判断督查状态 if(StringUtils.isNotBlank(bisInspSwhsRgstrSafety.getIsCoaeattsr().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrSafety.getIsFrmsaisasdsam().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrSafety.getIsWiqistcs().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrSafety.getIsMcpacdar().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrSafety.getIsWtpfilyeafrai().replace("0","")) ){ // 已完成 rgstr.setSafetyStat("2"); }else{ // 未完成 rgstr.setSafetyStat("1"); } rgstr.setState("2".equals(rgstr.getState()) ? "2" : "1"); bisInspSwhsRgstrDao.update(rgstr); } } } /** * @see cn.com.goldenwater.dcproj.controller.BisInspSwhsRgstrMeasuresController#insert(BisInspSwhsRgstrMeasures) */ private void insertOrUpdateSwhsRgstrMeasures(LinkedHashMap lhp,String userId) throws InstantiationException, IllegalAccessException { BisInspSwhsRgstrMeasures bisInspSwhsRgstrMeasures = (BisInspSwhsRgstrMeasures) ExpAndImpUtil.convertMapToBean(BisInspSwhsRgstrMeasures.class, lhp); // 更新本表 BisInspSwhsRgstrMeasuresParam param = new BisInspSwhsRgstrMeasuresParam(); param.setRgstrId(bisInspSwhsRgstrMeasures.getRgstrId()); List list = bisInspSwhsRgstrMeasuresDao.findList(param); if (list.size() > 0 ){ bisInspSwhsRgstrMeasures.setUpTm(new Date()); bisInspSwhsRgstrMeasures.setId(list.get(0).getId()); bisInspSwhsRgstrMeasures.setRecPersId(userId); bisInspSwhsRgstrMeasuresDao.update(bisInspSwhsRgstrMeasures); }else { bisInspSwhsRgstrMeasures.setInTm(new Date()); bisInspSwhsRgstrMeasures.setUpTm(new Date()); String uuid = UuidUtil.uuid(); bisInspSwhsRgstrMeasures.setId(uuid); bisInspSwhsRgstrMeasures.setRecPersId(userId); bisInspSwhsRgstrMeasuresDao.insert(bisInspSwhsRgstrMeasures); } // 更新 登记表 if (org.apache.commons.lang3.StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getRgstrId())) { BisInspSwhsRgstr rgstr = bisInspSwhsRgstrDao.get(bisInspSwhsRgstrMeasures.getRgstrId()); if (rgstr != null) { // 判断督查状态 if(StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsTcowqmWtmiafmtr().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsEowqoms().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsSwqiowstswrmisar().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsAmwqiadtfad().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsPzfdwsad().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsEcgbacws().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsHasProject().replace("0","")) && StringUtils.isNotBlank(bisInspSwhsRgstrMeasures.getIsHasIwba().replace("0","")) ){ // 已完成 rgstr.setMeasuresStat("2"); }else{ // 未完成 rgstr.setMeasuresStat("1"); } rgstr.setState("2".equals(rgstr.getState()) ? "2" : "1"); bisInspSwhsRgstrDao.update(rgstr); } } } /** * 过滤掉findList不是此用户下的此节点的数据 */ private List> filterNotTreeIdData(List> listParse, String userId, String orgIds) { List rgstrList = this.getRgstrList(userId,orgIds); Object[] rgstrIds = rgstrList.stream().map(BisInspRgstrDto::getRgstrId).toArray(); if (listParse == null) { return new ArrayList<>(); } else { listParse = listParse.stream().filter(x -> ArrayUtils.contains(rgstrIds,MapUtils.getString(x,"rgstrId"))).collect(Collectors.toList()); } return listParse; } private List getRgstrList(String userId,String orgIds){ // 导出参数 TypeParam typeParam = new TypeParam(); typeParam.setpType("13"); typeParam.setPresId(userId); // 0未督查 1督查中 2已督查 typeParam.setState("1,0"); typeParam.setGroupId(orgIds); return bisInspSwhsRgstrDao.findSwhsPage(typeParam); } }