shiyunteng
2 天以前 96119ca3973d5ea643db6a87b6a23fe404ddb8cc
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -7,6 +7,8 @@
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@@ -21,7 +23,6 @@
import com.by4cloud.platformx.business.entity.*;
import com.by4cloud.platformx.business.mapper.*;
import com.by4cloud.platformx.business.service.ContractService;
import com.by4cloud.platformx.business.utils.ContractNumberGenerator;
import com.by4cloud.platformx.business.utils.ItemGeneratorUtil;
import com.by4cloud.platformx.business.vo.ContractDetailVo;
import com.by4cloud.platformx.common.core.util.R;
@@ -37,6 +38,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.io.IOException;
@@ -71,9 +73,13 @@
   private final RemoteDeptService remoteDeptService;
   private final ContractDelayOutMapper contractDelayOutMapper;
   private final ErpRequestRecordMapper erpRequestRecordMapper;
   private final StringRedisTemplate redisTemplate;
   @Value("${erp.ymj}")
   private String ymjErp;
   @Value("${erp.ymjToken}")
   private String ymjToken;
   @Value("${erp.smj}")
   private String smjErp;
@@ -107,7 +113,7 @@
      Contract contract = BeanUtil.copyProperties(addDTO, Contract.class);
      contract.setPartyBId(SecurityUtils.getUser().getCompId());
      contract.setPartyB(SecurityUtils.getUser().getCompName());
      contract.setContractNo(ContractNumberGenerator.generateContractNumber());
      contract.setPaidAmount(new BigDecimal("0"));
      contract.setBillingStatus("0");
      contract.setErpPushFlag("0");
@@ -127,11 +133,22 @@
         }
      }
      List<Contract> contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, contract.getContractNo()));
      while (ArrayUtil.isNotEmpty(contracts.toArray())) {
         contract.setContractNo(ContractNumberGenerator.generateContractNumber());
         contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, contract.getContractNo()));
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)){
         contract.setContractNo(generateSMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",sgb)){
         contract.setContractNo(generateGYBContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",ymj)){
         contract.setContractNo(generateYMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",tfgs)){
         contract.setContractNo(generateTFContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",jxc)){
         contract.setContractNo(generateJXCContractNo());
      }
      baseMapper.insert(contract);
      if (ArrayUtil.isNotEmpty(addDTO.getContractSubjectMatter())) {
@@ -213,15 +230,23 @@
         return R.failed("当前状态无法修改合同");
      }
      Contract contract = BeanUtil.copyProperties(updateDTO, Contract.class);
      contract.setContractNo(ContractNumberGenerator.generateContractNumber());
      contract.setBillingStatus("0");
      contract.setErpPushFlag("0");
      contract.setPaidAmount(new BigDecimal("0"));
      List<Contract> contracts;
      contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, contract.getContractNo()));
      while (ArrayUtil.isNotEmpty(contracts.toArray())) {
         contract.setContractNo(ContractNumberGenerator.generateContractNumber());
         contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, contract.getContractNo()));
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)){
         contract.setContractNo(generateSMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",sgb)){
         contract.setContractNo(generateGYBContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",ymj)){
         contract.setContractNo(generateYMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",tfgs)){
         contract.setContractNo(generateTFContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",jxc)){
         contract.setContractNo(generateJXCContractNo());
      }
      if (ObjUtil.isNotNull(updateDTO.getDeliveryCycle())){
         contract.setExpirationDate(DateUtil.offsetDay(updateDTO.getSignDate(),updateDTO.getDeliveryCycle()));
@@ -572,10 +597,14 @@
            request.put("details",subjectMatter);
         }
         log.info("一煤机合同推送erp入参:{}",request.toJSONString());
         String result = HttpUtil.post(ymjErp,request.toJSONString());
         log.info("一煤机合同推送erp回参:{}",result);
         HttpResponse response = HttpRequest.post(ymjErp)
               .header("token", ymjToken)       // 添加认证头
               .header("Content-Type", "application/json")         // 设置内容类型
               .body(request.toJSONString())                        // 设置请求体
               .execute();                                         // 执行请求.post(ymjErp,request.toJSONString());
         log.info("一煤机合同推送erp回参:{}",response.body());
         //保存请求记录
         saveErpRequestRecord("ymj",request.toJSONString(),result);
         saveErpRequestRecord("ymj",request.toJSONString(),response.body());
         //更新合同erp推送标识
         contract.setErpPushFlag("1");
         baseMapper.updateById(contract);
@@ -770,16 +799,24 @@
   public R copyNewContract(Long id) {
      Contract oldContract = baseMapper.selectById(id);
      Contract newContract = BeanUtil.copyProperties(oldContract, Contract.class, "id", "createTime");
      newContract.setContractNo(ContractNumberGenerator.generateContractNumber());
      newContract.setPaidAmount(new BigDecimal("0"));
      newContract.setBillingStatus("0");
      newContract.setErpPushFlag("0");
      newContract.setContractStatus(0);
      List<Contract> contracts;
      contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, newContract.getContractNo()));
      while (ArrayUtil.isNotEmpty(contracts.toArray())) {
         newContract.setContractNo(ContractNumberGenerator.generateContractNumber());
         contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, newContract.getContractNo()));
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)){
         newContract.setContractNo(generateSMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",sgb)){
         newContract.setContractNo(generateGYBContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",ymj)){
         newContract.setContractNo(generateYMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",tfgs)){
         newContract.setContractNo(generateTFContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",jxc)){
         newContract.setContractNo(generateJXCContractNo());
      }
      baseMapper.insert(newContract);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery().eq(ContractSubjectMatter::getContractId, id));
@@ -1412,6 +1449,7 @@
         return R.failed("付款阶段比例异常");
      }
      Contract contract = BeanUtil.copyProperties(addDTO, Contract.class);
      contract.setErpContractNo(contract.getContractNo());
      contract.setPartyBId(SecurityUtils.getUser().getCompId());
      contract.setPartyB(SecurityUtils.getUser().getCompName());
      contract.setContractStatus(2);
@@ -1421,6 +1459,9 @@
      if (ObjUtil.isNotNull(addDTO.getDeliveryCycle())){
         contract.setExpirationDate(DateUtil.offsetDay(addDTO.getSignDate(),addDTO.getDeliveryCycle()));
      }
      contract.setContractNo(generateGYBContractNo());
      if (!StrUtil.equals(addDTO.getContractCategory(), "water_house")){
         // 假设 list 是你的 List<ContractSubjectMatterAddDTO>
         Optional<ContractSubjectMatterAddDTO> maxDateObj = addDTO.getContractSubjectMatter().stream()
@@ -1636,4 +1677,97 @@
      return VALID_STAGES.equals(actualStageSet);
   }
   /**
    * 生成格式为: yyyyMMdd + 4位序列号 (例如: 20260629000001)
    */
   public  String generateSMJContractNo() {
      // 1. 获取当前日期字符串
      String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
      // 2. 构建 Redis Key,按天隔离,例如: order:no:20260629
      String key = "ZB-SMJ:" + dateStr;
      // 3. Redis 原子递增,设置过期时间为2天(防止Key堆积,同时覆盖跨天边界情况)
      Long sequence = redisTemplate.opsForValue().increment(key, 1);
      redisTemplate.expire(key, 1, java.util.concurrent.TimeUnit.DAYS);
      // 4. 格式化序列号,不足6位前补0
      String seqStr = String.format("%04d", sequence);
      // 5. 拼接返回
      return "ZB-SMJ-"+dateStr + seqStr;
   }
   public  String generateYMJContractNo() {
      // 1. 获取当前日期字符串
      String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
      // 2. 构建 Redis Key,按天隔离,例如: order:no:20260629
      String key = "ZB-YMJ:" + dateStr;
      // 3. Redis 原子递增,设置过期时间为2天(防止Key堆积,同时覆盖跨天边界情况)
      Long sequence = redisTemplate.opsForValue().increment(key, 1);
      redisTemplate.expire(key, 1, java.util.concurrent.TimeUnit.DAYS);
      // 4. 格式化序列号,不足6位前补0
      String seqStr = String.format("%04d", sequence);
      // 5. 拼接返回
      return "ZB-YMJ-"+dateStr + seqStr;
   }
   public  String generateJXCContractNo() {
      // 1. 获取当前日期字符串
      String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
      // 2. 构建 Redis Key,按天隔离,例如: order:no:20260629
      String key = "ZB-JXC:" + dateStr;
      // 3. Redis 原子递增,设置过期时间为2天(防止Key堆积,同时覆盖跨天边界情况)
      Long sequence = redisTemplate.opsForValue().increment(key, 1);
      redisTemplate.expire(key, 1, java.util.concurrent.TimeUnit.DAYS);
      // 4. 格式化序列号,不足6位前补0
      String seqStr = String.format("%04d", sequence);
      // 5. 拼接返回
      return "ZB-JXC-"+dateStr + seqStr;
   }
   public  String generateTFContractNo() {
      // 1. 获取当前日期字符串
      String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
      // 2. 构建 Redis Key,按天隔离,例如: order:no:20260629
      String key = "ZB-TF:" + dateStr;
      // 3. Redis 原子递增,设置过期时间为2天(防止Key堆积,同时覆盖跨天边界情况)
      Long sequence = redisTemplate.opsForValue().increment(key, 1);
      redisTemplate.expire(key, 1, java.util.concurrent.TimeUnit.DAYS);
      // 4. 格式化序列号,不足6位前补0
      String seqStr = String.format("%04d", sequence);
      // 5. 拼接返回
      return "ZB-TF-"+dateStr + seqStr;
   }
   public  String generateGYBContractNo() {
      // 1. 获取当前日期字符串
      String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
      // 2. 构建 Redis Key,按天隔离,例如: order:no:20260629
      String key = "ZB-GYB:" + dateStr;
      // 3. Redis 原子递增,设置过期时间为2天(防止Key堆积,同时覆盖跨天边界情况)
      Long sequence = redisTemplate.opsForValue().increment(key, 1);
      redisTemplate.expire(key, 1, java.util.concurrent.TimeUnit.DAYS);
      // 4. 格式化序列号,不足6位前补0
      String seqStr = String.format("%04d", sequence);
      // 5. 拼接返回
      return "ZB-GYB-"+dateStr + seqStr;
   }
}