zhongrj
2023-09-05 2220383df9ee6bfbced20b83b83c90b6a68d995f
skjcmanager/skjcmanager-service/skjcmanager-sm/src/main/java/cn/gistack/sm/sjztmd/service/impl/TbAttResRsbNorspiServiceImpl.java
@@ -1,17 +1,32 @@
package cn.gistack.sm.sjztmd.service.impl;
import cn.gistack.sm.sjztmd.entity.TbAttResRsbNorspi;
import cn.gistack.sm.sjztmd.entity.TbAttResRsbNorspi;
import cn.gistack.sm.sjztmd.entity.TbAttResRsbSpillway;
import cn.gistack.sm.sjztmd.entity.TbAttResRsbVerspi;
import cn.gistack.sm.sjztmd.mapper.TbAttResRsbNorspiMapper;
import cn.gistack.sm.sjztmd.mapper.TbAttResRsbVerspiMapper;
import cn.gistack.sm.sjztmd.service.ITbAttResRsbNorspiService;
import cn.gistack.sm.sjztmd.service.ITbAttResRsbSpillwayService;
import cn.gistack.sm.sjztmd.service.ITbAttResRsbVerspiService;
import cn.gistack.system.user.feign.IUserClient;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @ClassName TbAttResRsbNorspiServiceImpl
@@ -27,168 +42,322 @@
   @Autowired
   private IUserClient userClient;
   @Autowired
   private ITbAttResRsbVerspiService tbAttResRsbVerspiService;
   @Autowired
   private ITbAttResRsbSpillwayService tbAttResRsbSpillwayService;
   /**
    * 查询水库填报详情信息
    * @param guid
    * @return
    */
   @Override
   public TbAttResRsbNorspi getDetailByGuid(String guid) {
   public List<TbAttResRsbNorspi> getDetailByGuid(String guid) {
      return baseMapper.getDetailByResGuid(guid);
   }
   /**
    * 编辑
    * @param tbAttResRsbNorspi
    * @param newList
    * @return
    */
   @Override
   public boolean updateByGuid(TbAttResRsbNorspi tbAttResRsbNorspi) {
   public boolean updateByGuid(List<TbAttResRsbNorspi> newList) {
      String resGuid = newList.get(0).getResGuid();
      // 查询是否存在
      TbAttResRsbNorspi detail = baseMapper.getDetailByResGuid(tbAttResRsbNorspi.getResGuid());
      List<TbAttResRsbNorspi> oldList = baseMapper.getDetailByResGuid(resGuid);
      // 更新日志
      setUpdateLog(detail,tbAttResRsbNorspi);
      // 修改
      int i = addOrUpdateByGuid(detail,tbAttResRsbNorspi);
      if (i>0){
      setUpdateLog(oldList,newList);
      // 创建或更新数据
      boolean flag = addOrUpdate(oldList,newList);
      // 返回
      return flag;
   }
   /**
    * 新增或更新数据
    * @param oldList
    * @param newList
    * @return
    */
   @Transactional(rollbackFor = Exception.class)
   public boolean addOrUpdate(List<TbAttResRsbNorspi> oldList,List<TbAttResRsbNorspi> newList) {
      // 取旧数据和新提交数据差集--删除
      // 去除前一天已填报的人员手机号集合
      List<TbAttResRsbNorspi> removeList = oldList.stream().filter(vo -> !newList.stream().map(e ->
         e.getGuid()).collect(Collectors.toList()).contains(vo.getGuid())).collect(Collectors.toList());
      // 取旧数据和新提交数据交集--更新
      List<TbAttResRsbNorspi> updateList = newList.stream().filter(vo -> oldList.stream().map(e ->
         e.getGuid()).collect(Collectors.toList()).contains(vo.getGuid())).collect(Collectors.toList());
      // 取新数提交据和旧数据交集--新增
      List<TbAttResRsbNorspi> addList = newList.stream().filter(vo -> !oldList.stream().map(e ->
         e.getGuid()).collect(Collectors.toList()).contains(vo.getGuid())).collect(Collectors.toList());
      boolean addFlag = true;
      boolean updateFlag = true;
      boolean removeFlag = true;
      if (addList.size()>0){
         // 批量新增
         addFlag = saveBatch(addList);
      }
      if (updateList.size()>0){
         // 批量更新
         addFlag = updateBatchById(updateList);
      }
      if (removeList.size()>0){
         // 批量删除
         removeFlag = removeBatchByIds(removeList);
      }
      if (addFlag && updateFlag && removeFlag){
         return true;
      }
      // 返回
      return false;
   }
   /**
    * 审核
    * @param tbAttResRsbNorspi
    * @param newList
    * @return
    */
   @Override
   public boolean auditByGuid(TbAttResRsbNorspi tbAttResRsbNorspi) {
   public boolean auditByGuid(List<TbAttResRsbNorspi> newList) {
      // 查询是否存在
      TbAttResRsbNorspi detail = baseMapper.getDetailByResGuid(tbAttResRsbNorspi.getResGuid());
      List<TbAttResRsbNorspi> oldList = baseMapper.getDetailByResGuid(newList.get(0).getResGuid());
      // 更新操作日志
      setUpdateLog(detail,tbAttResRsbNorspi);
      setUpdateLog(oldList,newList);
      // 更新审核日志
      setUpdateCheckLog(detail,tbAttResRsbNorspi);
      if (null!=detail){
         tbAttResRsbNorspi.setGuid(detail.getGuid());
      }
      // 驳回情况下将状态修改回0即已填写未提交状态
      if (tbAttResRsbNorspi.getCheckState()==3){
         tbAttResRsbNorspi.setCheckState(0);
      }
      setUpdateCheckLog(oldList,newList);
      // 更新数据
      int i = baseMapper.updateById(tbAttResRsbNorspi);
      if (i>0){
      boolean batch = updateBatchById(newList);
      // 返回
      return batch;
   }
   /**
    * @param oldList
    * @param newList
    * 更新审核记录
    */
   private void setUpdateCheckLog(List<TbAttResRsbNorspi> oldList,List<TbAttResRsbNorspi> newList) {
      // 遍历
      for (TbAttResRsbNorspi newTbAttResRsbNorspi : newList) {
         String fillName = "";
         if (newTbAttResRsbNorspi.getCheckState() == 2) {
            fillName = "审核";
         }
         if (newTbAttResRsbNorspi.getCheckState() == 3) {
            fillName = "退回" + (null == newTbAttResRsbNorspi.getCheckLog() ? "" : ":" + newTbAttResRsbNorspi.getCheckLog());
            for (TbAttResRsbNorspi tbAttResRsbNorspi : oldList) {
               if (tbAttResRsbNorspi.getGuid().equals(newTbAttResRsbNorspi.getGuid())){
                  // 退回时进行设置状态为填写中
                  newTbAttResRsbNorspi.setCheckState(0);
               }
            }
         }
         // 拼接
         String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
         String userName = AuthUtil.getUser().getUserName();
         String phone = userClient.userInfoById(AuthUtil.getUserId()).getData().getPhone();
         StringBuilder builder = new StringBuilder();
         builder.append(time)
            .append(",")
            .append(fillName)
            .append(",")
            .append(userName)
            .append(",")
            .append(phone)
            .append(";");
         // 判断是否存在
         if (null==newTbAttResRsbNorspi.getGuid() || newTbAttResRsbNorspi.getGuid().equals("")){
            // 填报
            newTbAttResRsbNorspi.setCheckLog(builder.toString());
         } else {
            for (TbAttResRsbNorspi tbAttResRsbNorspi : oldList) {
               if (tbAttResRsbNorspi.getGuid().equals(newTbAttResRsbNorspi.getGuid())){
                  StringBuilder stringBuilder = new StringBuilder(null == tbAttResRsbNorspi.getCheckLog() ? "" : tbAttResRsbNorspi.getCheckLog());
                  stringBuilder.append(builder);
                  // 填报
                  newTbAttResRsbNorspi.setCheckLog(stringBuilder.toString());
               }
            }
         }
      }
   }
   /**
    * @param oldList
    * @param newList
    * 更新操作记录
    */
   private void setUpdateLog(List<TbAttResRsbNorspi> oldList,List<TbAttResRsbNorspi> newList) {
      // 遍历
      for (TbAttResRsbNorspi newTbAttResRsbNorspi : newList){
         String fillName = "";
         if (newTbAttResRsbNorspi.getCheckState()==0){
            fillName = "编辑";
         }
         if (newTbAttResRsbNorspi.getCheckState()==1){
            fillName = "提交";
         }
         if (newTbAttResRsbNorspi.getCheckState()==2){
            fillName = "审核";
         }
         if (newTbAttResRsbNorspi.getCheckState()==3){
            fillName = "退回" + (null==newTbAttResRsbNorspi.getCheckLog()?"":":" + newTbAttResRsbNorspi.getCheckLog());
         }
         // 管理员编辑
         if (newTbAttResRsbNorspi.getCheckState()==4){
            fillName = "编辑";
            // 找到相关的 guid 数据
            for (TbAttResRsbNorspi tbAttResRsbNorspi : oldList) {
               if (tbAttResRsbNorspi.getGuid().equals(newTbAttResRsbNorspi.getGuid())){
                  newTbAttResRsbNorspi.setCheckState(tbAttResRsbNorspi.getCheckState());
               }
            }
         }
         // 拼接
         String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
         String userName = AuthUtil.getUser().getUserName();
         String phone = userClient.userInfoById(AuthUtil.getUserId()).getData().getPhone();
         StringBuilder builder = new StringBuilder();
         builder.append(time)
            .append(",")
            .append(fillName)
            .append(",")
            .append(userName)
            .append(",")
            .append(phone)
            .append(";");
         // 判断是否存在
         if (null==newTbAttResRsbNorspi.getGuid() || newTbAttResRsbNorspi.getGuid().equals("")){
            // 填报
            newTbAttResRsbNorspi.setLog(builder.toString());
         }else {
            for (TbAttResRsbNorspi tbAttResRsbNorspi : oldList) {
               if (tbAttResRsbNorspi.getGuid().equals(newTbAttResRsbNorspi.getGuid())){
                  StringBuilder stringBuilder = new StringBuilder(null==tbAttResRsbNorspi.getLog()?"":tbAttResRsbNorspi.getLog());
                  stringBuilder.append(builder);
                  // 填报
                  newTbAttResRsbNorspi.setLog(stringBuilder.toString());
               }
            }
         }
      }
   }
   /**
    * 查询水库填报详情信息(正常,非常溢洪道,泄洪洞)
    * @param resGuid
    * @return
    */
   @Override
   public Map<String,Object> getResRsbNorsipiVerspiMionitorDetailByGuid(String resGuid) {
      Map<String, Object> map = new HashMap<>();
      // 查询正常溢洪道信息
      List<TbAttResRsbNorspi> tbAttResRsbNorspiList = baseMapper.getDetailByResGuid(resGuid);
      // 查询非常溢洪道信息
      List<TbAttResRsbVerspi> tbAttResRsbVerspiList = tbAttResRsbVerspiService.getDetailByGuid(resGuid);
      // 查询泄洪洞信息
      List<TbAttResRsbSpillway> tbAttResRsbSpillwayList = tbAttResRsbSpillwayService.getDetailByGuid(resGuid);
      // 封装数据
      map.put("tbAttResRsbNorspiList",tbAttResRsbNorspiList);
      map.put("tbAttResRsbVerspiList",tbAttResRsbVerspiList);
      map.put("tbAttResRsbSpillwayList",tbAttResRsbSpillwayList);
      // 返回
      return map;
   }
   /**
    * 编辑(正常,非常溢洪道,泄洪洞)
    * @param map
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean editResRsbNorsipiVerspiMionitor(String map) {
      ObjectMapper objectMapper = new ObjectMapper();
      JsonNode jsonNode = null;
      try {
         jsonNode = objectMapper.readTree(map);
      } catch (JsonProcessingException e) {
         e.printStackTrace();
      }
      JsonNode tbAttResRsbNorspiListJsonNode = jsonNode.get("tbAttResRsbNorspiList");
      JsonNode tbAttResRsbVerspiListJsonNode = jsonNode.get("tbAttResRsbVerspiList");
      JsonNode tbAttResRsbSpillwayListJsonNode = jsonNode.get("tbAttResRsbSpillwayList");
      JavaType tbAttResRsbNorspiType = objectMapper.getTypeFactory().constructParametricType(List.class, TbAttResRsbNorspi.class);
      JavaType tbAttResRsbVerspiType = objectMapper.getTypeFactory().constructParametricType(List.class, TbAttResRsbVerspi.class);
      JavaType tbAttResRsbSpillwayType = objectMapper.getTypeFactory().constructParametricType(List.class, TbAttResRsbSpillway.class);
      List<TbAttResRsbNorspi> tbAttResRsbNorspiList = null;
      List<TbAttResRsbVerspi> tbAttResRsbVerspiList = null;
      List<TbAttResRsbSpillway> tbAttResRsbSpillwayList = null;
      try {
         tbAttResRsbNorspiList = objectMapper.readValue(tbAttResRsbNorspiListJsonNode.toString(), tbAttResRsbNorspiType);
         tbAttResRsbVerspiList = objectMapper.readValue(tbAttResRsbVerspiListJsonNode.toString(), tbAttResRsbVerspiType);
         tbAttResRsbSpillwayList = objectMapper.readValue(tbAttResRsbSpillwayListJsonNode.toString(), tbAttResRsbSpillwayType);
      } catch (JsonProcessingException e) {
         e.printStackTrace();
      }
      // 取出集合数据,这样转换格式异常
//      List<TbAttResRsbNorspi> tbAttResRsbNorspiList = (List<TbAttResRsbNorspi>)map.get("tbAttResRsbNorspiList");
//      List<TbAttResRsbVerspi> tbAttResRsbVerspiList = (List<TbAttResRsbVerspi>)map.get("tbAttResRsbVerspiList");
//      List<TbAttResRsbSpillway> tbAttResRsbSpillwayList = (List<TbAttResRsbSpillway>)map.get("tbAttResRsbSpillwayList");
      // 分别更新
      boolean flag1 = updateByGuid(tbAttResRsbNorspiList);
      boolean flag2 = tbAttResRsbVerspiService.updateByGuid(tbAttResRsbVerspiList);
      boolean flag3 = tbAttResRsbSpillwayService.updateByGuid(tbAttResRsbSpillwayList);
      if (flag1 && flag2 && flag3){
         return true;
      }
      return false;
   }
   /**
    * 更新
    * @param tbAttResRsbNorspi
    * 审核(正常,非常溢洪道,泄洪洞)
    * @param map
    * @return
    */
   private int addOrUpdateByGuid(TbAttResRsbNorspi oldAbAttResBase,TbAttResRsbNorspi tbAttResRsbNorspi){
      int i = 0;
      if (null!=oldAbAttResBase){
         tbAttResRsbNorspi.setGuid(oldAbAttResBase.getGuid());
         // 更新
         i = baseMapper.updateById(tbAttResRsbNorspi);
      }else {
         tbAttResRsbNorspi.setCreateTime(new Date());
         i = baseMapper.insert(tbAttResRsbNorspi);
   @Override
   @Transactional(rollbackFor = Exception.class)
   public boolean auditResRsbNorsipiVerspiMionitor(String map) {
      ObjectMapper objectMapper = new ObjectMapper();
      JsonNode jsonNode = null;
      try {
         jsonNode = objectMapper.readTree(map);
      } catch (JsonProcessingException e) {
         e.printStackTrace();
      }
      // 返回
      return i;
   }
      JsonNode tbAttResRsbNorspiListJsonNode = jsonNode.get("tbAttResRsbNorspiList");
      JsonNode tbAttResRsbVerspiListJsonNode = jsonNode.get("tbAttResRsbVerspiList");
      JsonNode tbAttResRsbSpillwayListJsonNode = jsonNode.get("tbAttResRsbSpillwayList");
   /**
    * @param oldAbAttResBase
    * @param newTbAttResRsbNorspi
    * 更新审核记录
    */
   private void setUpdateCheckLog(TbAttResRsbNorspi oldAbAttResBase,TbAttResRsbNorspi newTbAttResRsbNorspi) {
      String fillName = "";
      if (newTbAttResRsbNorspi.getCheckState()==2){
         fillName = "审核";
      }
      if (newTbAttResRsbNorspi.getCheckState()==3){
         fillName = "退回" + (null==newTbAttResRsbNorspi.getCheckLog()?"":":" + newTbAttResRsbNorspi.getCheckLog());
      }
      JavaType tbAttResRsbNorspiType = objectMapper.getTypeFactory().constructParametricType(List.class, TbAttResRsbNorspi.class);
      JavaType tbAttResRsbVerspiType = objectMapper.getTypeFactory().constructParametricType(List.class, TbAttResRsbVerspi.class);
      JavaType tbAttResRsbSpillwayType = objectMapper.getTypeFactory().constructParametricType(List.class, TbAttResRsbSpillway.class);
      // 拼接
      String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
      String userName = AuthUtil.getUser().getUserName();
      String phone = userClient.userInfoById(AuthUtil.getUserId()).getData().getPhone();
      StringBuilder builder = new StringBuilder();
      builder.append(time)
         .append(",")
         .append(fillName)
         .append(",")
         .append(userName)
         .append(",")
         .append(phone)
         .append(";");
      // 判断是否存在
      if (null==oldAbAttResBase){
         // 填报
         newTbAttResRsbNorspi.setCheckLog(builder.toString());
      }else {
         StringBuilder stringBuilder = new StringBuilder(null==oldAbAttResBase.getCheckLog()?"":oldAbAttResBase.getCheckLog());
         stringBuilder.append(builder);
         // 填报
         newTbAttResRsbNorspi.setCheckLog(stringBuilder.toString());
      List<TbAttResRsbNorspi> tbAttResRsbNorspiList = null;
      List<TbAttResRsbVerspi> tbAttResRsbVerspiList = null;
      List<TbAttResRsbSpillway> tbAttResRsbSpillwayList = null;
      try {
         tbAttResRsbNorspiList = objectMapper.readValue(tbAttResRsbNorspiListJsonNode.toString(), tbAttResRsbNorspiType);
         tbAttResRsbVerspiList = objectMapper.readValue(tbAttResRsbVerspiListJsonNode.toString(), tbAttResRsbVerspiType);
         tbAttResRsbSpillwayList = objectMapper.readValue(tbAttResRsbSpillwayListJsonNode.toString(), tbAttResRsbSpillwayType);
      } catch (JsonProcessingException e) {
         e.printStackTrace();
      }
   }
   /**
    * @param oldAbAttResBase
    * @param newTbAttResRsbNorspi
    * 更新操作记录
    */
   private void setUpdateLog(TbAttResRsbNorspi oldAbAttResBase,TbAttResRsbNorspi newTbAttResRsbNorspi) {
      String fillName = "";
      if (newTbAttResRsbNorspi.getCheckState()==0){
         fillName = "编辑";
      // 分别更新
      boolean flag1 = auditByGuid(tbAttResRsbNorspiList);
      boolean flag2 = tbAttResRsbVerspiService.auditByGuid(tbAttResRsbVerspiList);
      boolean flag3 = tbAttResRsbSpillwayService.auditByGuid(tbAttResRsbSpillwayList);
      if (flag1 && flag2 && flag3){
         return true;
      }
      if (newTbAttResRsbNorspi.getCheckState()==1){
         fillName = "提交";
      }
      if (newTbAttResRsbNorspi.getCheckState()==2){
         fillName = "审核";
      }
      if (newTbAttResRsbNorspi.getCheckState()==3){
         fillName = "退回" + (null==newTbAttResRsbNorspi.getCheckLog()?"":":" + newTbAttResRsbNorspi.getCheckLog());
      }
      // 管理员编辑
      if (newTbAttResRsbNorspi.getCheckState()==4){
         fillName = "编辑";
         newTbAttResRsbNorspi.setCheckState(oldAbAttResBase.getCheckState());
      }
      // 拼接
      String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
      String userName = AuthUtil.getUser().getUserName();
      String phone = userClient.userInfoById(AuthUtil.getUserId()).getData().getPhone();
      StringBuilder builder = new StringBuilder();
      builder.append(time)
         .append(",")
         .append(fillName)
         .append(",")
         .append(userName)
         .append(",")
         .append(phone)
         .append(";");
      // 判断是否存在
      if (null==oldAbAttResBase){
         // 填报
         newTbAttResRsbNorspi.setLog(builder.toString());
      }else {
         StringBuilder stringBuilder = new StringBuilder(null==oldAbAttResBase.getLog()?"":oldAbAttResBase.getLog());
         stringBuilder.append(builder);
         // 填报
         newTbAttResRsbNorspi.setLog(stringBuilder.toString());
      }
      return false;
   }
}