shiyunteng
2026-06-18 5badb0e02901c3ea5570fdb9cf497aec7e48fd65
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java
@@ -13,6 +13,7 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.admin.api.entity.SysDept;
import com.by4cloud.platformx.admin.api.feign.RemoteDeptService;
import com.by4cloud.platformx.business.dto.AgingQueryDTO;
import com.by4cloud.platformx.business.dto.PaymentConfirmAddDTO;
import com.by4cloud.platformx.business.entity.*;
import com.by4cloud.platformx.business.mapper.*;
@@ -20,7 +21,11 @@
import com.by4cloud.platformx.business.service.ContractService;
import com.by4cloud.platformx.business.service.PaymentConfirmService;
import com.by4cloud.platformx.business.utils.SignHelper;
import com.by4cloud.platformx.business.vo.AgingAnalysisVo;
import com.by4cloud.platformx.business.vo.AgingCompanyVo;
import com.by4cloud.platformx.business.vo.AgingVo;
import com.by4cloud.platformx.common.core.util.R;
import com.by4cloud.platformx.common.data.datascope.DataScope;
import com.by4cloud.platformx.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -30,10 +35,13 @@
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
 * 收款确认
@@ -53,6 +61,7 @@
   private final HistoryOverdueMapper historyOverdueMapper;
   private final PaymentSlipMapper paymentSlipMapper;
   private final BipRequestRecordMapper bipRequestRecordMapper;
   private final ContractExecDateMapper contractExecDateMapper;
   private final RemoteDeptService remoteDeptService;
   private final RedisTemplate redisTemplate;
@@ -72,8 +81,11 @@
   public R add(PaymentConfirmAddDTO addDTO) {
      if (StrUtil.isNotBlank(addDTO.getContractNo())) {
         Contract contract = contractService.getOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, addDTO.getContractNo()));
         if (ObjUtil.isNull(contract)) {
            return R.failed("合同查询失败,请检查合同编号");
         }
         BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
               .eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getCompanyName, addDTO.getBusGuestName())
               .eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getRegisterName, addDTO.getBusGuestName())
               .eq(StrUtil.isNotBlank(addDTO.getBusGuestId()), BusinessCustomer::getId, addDTO.getBusGuestId()));
         if (ObjUtil.isNull(customer)) {
            return R.failed("客商查询失败,请检查客商");
@@ -98,7 +110,7 @@
               entity.setScheduleName(lastConfirm.getScheduleName());
            }
            entity.setBusGuestId(customer.getId());
            entity.setBusGuestName(customer.getCompanyName());
            entity.setBusGuestName(customer.getRegisterName());
            entity.setBusinessType("客户付款");
            if (newtotal.compareTo(new BigDecimal("0")) > 0) {
               entity.setTransationAmount(addDTO.getTransationAmount());
@@ -192,8 +204,8 @@
            entity.setContractNo(addDTO.getContractNo());
            entity.setBusGuestId(customer.getId());
            entity.setBusGuestName(customer.getCompanyName());
            entity.setBusinessType("");
            entity.setBusGuestName(customer.getRegisterName());
            entity.setBusinessType("退款");
            entity.setTransationAmount(addDTO.getTransationAmount());
            BigDecimal newtotal = addDTO.getTransationAmount().multiply(new BigDecimal("-1")).add(lastConfirm.getTotalAmount());
            if (newtotal.compareTo(new BigDecimal("0")) > 0) {
@@ -224,7 +236,7 @@
            return R.failed("为查询到未付款合同");
         }
         BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
               .eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getCompanyName, addDTO.getBusGuestName())
               .eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getRegisterName, addDTO.getBusGuestName())
               .eq(ObjUtil.isNotNull(addDTO.getBusGuestId()), BusinessCustomer::getId, addDTO.getBusGuestId())
               .last("limit 1"));
         if (ObjUtil.isNull(customer)) {
@@ -233,7 +245,7 @@
         //新增付款单
         PaymentSlip slip = new PaymentSlip();
         slip.setBusGuestId(customer.getId());
         slip.setBusGuestName(customer.getCompanyName());
         slip.setBusGuestName(customer.getRegisterName());
         slip.setPayeeCompId(SecurityUtils.getUser().getCompId());
         slip.setPaymentTime(addDTO.getConfirmTime());
         slip.setPaymentAmount(addDTO.getTransationAmount());
@@ -284,7 +296,7 @@
               entity.setScheduleName(lastConfirm.getScheduleName());
            }
            entity.setBusGuestId(customer.getId());
            entity.setBusGuestName(customer.getCompanyName());
            entity.setBusGuestName(customer.getRegisterName());
            entity.setBusinessType("客户付款");
            if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
               entity.setConfirmTime(payDate);
@@ -393,7 +405,21 @@
               if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
                  payNoCompleteContractList.add(contract);
               }
               contractService.updateById(contract);
               if (StrUtil.isNotEmpty(contract.getContractCategory())&&StrUtil.equals(contract.getContractCategory(),"water_house")){
                  List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery()
                        .eq(ContractExecDate::getContractId,contract.getId()).eq(ContractExecDate::getGenFlag,"0"));
                  if (ArrayUtil.isEmpty(execDateList.toArray())){
                     List<ContractPaymentSchedule> contractPaymentScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                           .eq(ContractPaymentSchedule::getContractId, contract.getId()));
                     if (ArrayUtil.isNotEmpty(contractPaymentScheduleList.toArray())&&contractPaymentScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
                        contract.setContractStatus(3);
                        contractService.updateById(contract);
                     }
                  }
               }else {
                  contractService.updateById(contract);
               }
            }
         });
@@ -450,7 +476,7 @@
            String result = HttpUtil.post(url + "/yonbip/EFI/collection/list?access_token=" + finalAccessToken, params.toJSONString());
            log.info("SK_CX Response:{}", result);
            //保存请求记录
            saveBipRequestRecord(params, result);
            saveBipRequestRecord(params,"<XSJZ_SK_CX_001>-收款单查询接口", result);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) {
               handleAndSave(bipCode, resultJson);
@@ -478,7 +504,8 @@
      return params;
   }
   private void getAccessToken(String accessToken) {
   @Override
   public String getAccessToken(String accessToken) {
      long timestamp = System.currentTimeMillis();
      Map<String, Object> paramMap = new HashMap<>();
      paramMap.put("appKey", appKey);
@@ -499,20 +526,22 @@
      JSONObject tokenJson = JSONObject.parseObject(getTokenResult);
      if (!tokenJson.containsKey("code") || !tokenJson.getString("code").equals("200")) {
         log.error("Get access_token ERROR");
         return;
         return "";
      }
      if (ObjUtil.isNull(tokenJson.get("data")) || ObjUtil.isNull(JSONObject.parseObject(tokenJson.getString("data")))) {
         log.error("access_token value exception");
         return;
         return "";
      }
      accessToken = JSONObject.parseObject(tokenJson.getString("data")).getString("access_token");
      log.info("access_token value:", accessToken);
      redisTemplate.opsForValue().set("BIP_TOKEN", accessToken, tokenJson.getIntValue("expire") - 10, TimeUnit.SECONDS);
      return accessToken;
   }
   private void saveBipRequestRecord(JSONObject params, String result) {
   @Override
   public void saveBipRequestRecord(JSONObject params, String methodName, String result) {
      BipRequestRecord record = new BipRequestRecord();
      record.setInterfaceName("<XSJZ_SK_CX_001>-收款单查询接口");
      record.setInterfaceName(methodName);
      record.setRequestParams(params.toJSONString());
      record.setResponseParams(result);
      record.setResponseCode(JSONObject.parseObject(result).getString("code"));
@@ -553,7 +582,7 @@
         return;
      }
      BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
            .eq(StrUtil.isNotBlank(record.getString("bodyItemCustomerName")), BusinessCustomer::getCompanyName, record.getString("bodyItemCustomerName"))
            .eq(StrUtil.isNotBlank(record.getString("bodyItemCustomerName")), BusinessCustomer::getRegisterName, record.getString("bodyItemCustomerName"))
      );
      if (ObjUtil.isNull(customer)) {
         log.error("付款单id:{},客商查询失败,请检查客商", record.getString("id"));
@@ -609,7 +638,7 @@
            entity.setScheduleName(lastConfirm.getScheduleName());
         }
         entity.setBusGuestId(customer.getId());
         entity.setBusGuestName(customer.getCompanyName());
         entity.setBusGuestName(customer.getRegisterName());
         entity.setBusinessType("客户付款");
         entity.setIsBip(1);
         entity.setPaymentCode(record.getString("code"));
@@ -721,7 +750,20 @@
            if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
               payNoCompleteContractList.add(contract);
            }
            contractService.updateById(contract);
            if (StrUtil.isNotEmpty(contract.getContractCategory())&&StrUtil.equals(contract.getContractCategory(),"water_house")){
               List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery()
                     .eq(ContractExecDate::getContractId,contract.getId()).eq(ContractExecDate::getGenFlag,"0"));
               if (ArrayUtil.isEmpty(execDateList)){
                  List<ContractPaymentSchedule> contractPaymentScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                        .eq(ContractPaymentSchedule::getContractId, contract.getId()));
                  if (ArrayUtil.isNotEmpty(contractPaymentScheduleList.toArray())&&contractPaymentScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
                     contract.setContractStatus(3);
                     contractService.updateById(contract);
                  }
               }
            }else {
               contractService.updateById(contract);
            }
         }
      });
@@ -753,7 +795,7 @@
   private Boolean savePaymentSlip(BusinessCustomer customer, JSONObject record,SysDept dept) {
      PaymentSlip slip = new PaymentSlip();
      slip.setBusGuestId(customer.getId());
      slip.setBusGuestName(customer.getCompanyName());
      slip.setBusGuestName(customer.getRegisterName());
      slip.setPayee(dept.getName());
      slip.setPayeeCompId(dept.getDeptId());
      slip.setPaymentTime(ObjUtil.isNotNull(record.get("billDate"))?
@@ -790,7 +832,7 @@
      }
      entity.setBusGuestId(customer.getId());
      entity.setBusGuestName(customer.getCompanyName());
      entity.setBusGuestName(customer.getRegisterName());
      entity.setBusinessType("客户付款");
      if (ObjUtil.isNotNull(record)){
         entity.setIsBip(1);
@@ -808,4 +850,102 @@
      }
      baseMapper.insert(entity);
   }
   @Override
   public R queryAllBusinessType() {
      return R.ok(baseMapper.selectList(Wrappers.<PaymentConfirm>lambdaQuery().select(PaymentConfirm::getBusinessType).isNotNull(PaymentConfirm::getBusinessType)
                  .groupBy(PaymentConfirm::getBusinessType)).stream().map(PaymentConfirm::getBusinessType).collect(Collectors.toList()));
   }
   @Override
   public R queryAging() {
      List<AgingCompanyVo> companys = contractPaymentScheduleMapper.selectAgingCompany(null,DataScope.of("comp_id"));
      if (ArrayUtil.isEmpty(companys.toArray())){
         return R.failed("暂无账龄");
      }
      List<AgingAnalysisVo> result = new ArrayList<>();
      AgingQueryDTO queryDTO = new AgingQueryDTO();
      List<AgingVo> agingVoList = contractPaymentScheduleMapper.selectAgingTotal(null);
      companys.stream().forEach(company->{
         BusinessCustomer businessCustomer = businessCustomerService.getById(company.getCompanyId());
         AgingAnalysisVo agingAnalysisVo = new AgingAnalysisVo();
         agingAnalysisVo.setCompanyName(businessCustomer.getCompanyName());
         queryDTO.setCompanyId(company.getCompanyId());
         List<AgingVo> companyAgingVo = contractPaymentScheduleMapper.selectAgingTotal(queryDTO);
         //补全数据
         if (ArrayUtil.isEmpty(companyAgingVo.toArray())){
            companyAgingVo = new ArrayList<>();
            AgingVo agingVo1 = new AgingVo("0-30天",new BigDecimal("0"),null);
            AgingVo agingVo2 = new AgingVo("31-60天",new BigDecimal("0"),null);
            AgingVo agingVo3 = new AgingVo("61-90天",new BigDecimal("0"),null);
            AgingVo agingVo4 = new AgingVo("91-180天",new BigDecimal("0"),null);
            AgingVo agingVo5 = new AgingVo("181-365天",new BigDecimal("0"),null);
            AgingVo agingVo6 = new AgingVo("1年以上",new BigDecimal("0"),null);
            companyAgingVo.add(agingVo1);
            companyAgingVo.add(agingVo2);
            companyAgingVo.add(agingVo3);
            companyAgingVo.add(agingVo4);
            companyAgingVo.add(agingVo5);
            companyAgingVo.add(agingVo6);
         }else {
            List<String> overdueRanges = companyAgingVo.stream().map(AgingVo::getOverdueRange).collect(Collectors.toList());
            if (!ArrayUtil.contains(overdueRanges.toArray(),"0-30天")){
               AgingVo agingVo1 = new AgingVo("0-30天",new BigDecimal("0"),null);
               companyAgingVo.add(agingVo1);
            }
            if (!ArrayUtil.contains(overdueRanges.toArray(),"31-60天")){
               AgingVo agingVo2 = new AgingVo("31-60天",new BigDecimal("0"),null);
               companyAgingVo.add(agingVo2);
            }
            if (!ArrayUtil.contains(overdueRanges.toArray(),"61-90天")){
               AgingVo agingVo3 = new AgingVo("61-90天",new BigDecimal("0"),null);
               companyAgingVo.add(agingVo3);
            }
            if (!ArrayUtil.contains(overdueRanges.toArray(),"91-180天")){
               AgingVo agingVo4 = new AgingVo("91-180天",new BigDecimal("0"),null);
               companyAgingVo.add(agingVo4);
            }
            if (!ArrayUtil.contains(overdueRanges.toArray(),"181-365天")){
               AgingVo agingVo5 = new AgingVo("181-365天",new BigDecimal("0"),null);
               companyAgingVo.add(agingVo5);
            }
            if (!ArrayUtil.contains(overdueRanges.toArray(),"1年以上")){
               AgingVo agingVo6 = new AgingVo("1年以上",new BigDecimal("0"),null);
               companyAgingVo.add(agingVo6);
            }
         }
         agingAnalysisVo.setAmount(companyAgingVo.stream().map(AgingVo::getTotalAmount).collect(Collectors.toList()));
         List<AgingVo> finalCompanyAgingVo = companyAgingVo;
         agingAnalysisVo.setPercent(IntStream.range(0, agingVoList.size())
               .mapToObj(i -> {
                  BigDecimal part = finalCompanyAgingVo.get(i).getTotalAmount();
                  BigDecimal total = agingVoList.get(i).getTotalAmount();
                  if (total == null || total.compareTo(BigDecimal.ZERO) == 0) {
                     return null;
                  }
                  return part.divide(total, 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal("100"))
                        .setScale(2, RoundingMode.HALF_UP);
               })
               .collect(Collectors.toList()));
         result.add(agingAnalysisVo);
      });
      //合计
      AgingAnalysisVo agingAnalysisVo = new AgingAnalysisVo();
      agingAnalysisVo.setCompanyName("合计");
      agingAnalysisVo.setAmount(agingVoList.stream().map(AgingVo::getTotalAmount).collect(Collectors.toList()));
      List<BigDecimal> percent = IntStream.range(0, agingAnalysisVo.getAmount().size())
            .mapToObj(i -> {
               if (agingAnalysisVo.getAmount().get(i) == null || agingAnalysisVo.getAmount().get(i).compareTo(BigDecimal.ZERO) == 0) {
                  return null;
               }else {
                  return new BigDecimal("100");
               }
            })
            .collect(Collectors.toList());
      agingAnalysisVo.setPercent(percent);
      result.add(agingAnalysisVo);
      return R.ok(result);
   }
}