From 7c30cc34b1fae37b6cd94fcf0a5d421a8e1bdb24 Mon Sep 17 00:00:00 2001
From: baolhyhzhg <baolh@yhzhg.cn>
Date: Mon, 02 Dec 2024 07:35:25 +0000
Subject: [PATCH] 合同类型变更为两级,两级间可联动

---
 eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java |  215 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 171 insertions(+), 44 deletions(-)

diff --git a/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java b/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java
index 4704a9e..e5e9935 100644
--- a/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java
+++ b/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/contract/service/impl/ContractServiceImpl.java
@@ -16,32 +16,24 @@
 package me.zhengjie.modules.contract.service.impl;
 
 import me.zhengjie.modules.constant.Constant;
-import me.zhengjie.modules.contract.domain.Contract;
-import me.zhengjie.modules.contract.domain.ContractInvoice;
-import me.zhengjie.modules.contract.domain.ContractInvoicePayment;
-import me.zhengjie.modules.contract.domain.ContractPayment;
-import me.zhengjie.modules.contract.repository.ContractInvoicePaymentRepository;
-import me.zhengjie.modules.contract.repository.ContractInvoiceRepository;
-import me.zhengjie.modules.contract.repository.ContractPaymentRepository;
-import me.zhengjie.modules.contract.service.dto.ContractFirstDto;
-import me.zhengjie.modules.contract.service.dto.ContractSecondDto;
+import me.zhengjie.modules.contract.domain.*;
+import me.zhengjie.modules.contract.repository.*;
+import me.zhengjie.modules.contract.service.dto.*;
+import me.zhengjie.modules.contract.service.mapstruct.ContractProjectMapper;
 import me.zhengjie.modules.project.service.dto.ProjectDto;
 import me.zhengjie.modules.project.service.impl.ProjectServiceImpl;
+import me.zhengjie.modules.system.domain.Dict;
+import me.zhengjie.modules.system.domain.DictDetail;
 import me.zhengjie.modules.system.repository.DictDetailRepository;
-import me.zhengjie.utils.ValidationUtil;
-import me.zhengjie.utils.FileUtil;
+import me.zhengjie.modules.system.repository.DictRepository;
+import me.zhengjie.utils.*;
 import lombok.RequiredArgsConstructor;
-import me.zhengjie.modules.contract.repository.ContractRepository;
 import me.zhengjie.modules.contract.service.ContractService;
-import me.zhengjie.modules.contract.service.dto.ContractDto;
-import me.zhengjie.modules.contract.service.dto.ContractQueryCriteria;
 import me.zhengjie.modules.contract.service.mapstruct.ContractMapper;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
-import me.zhengjie.utils.PageUtil;
-import me.zhengjie.utils.QueryHelp;
 
 import java.math.BigDecimal;
 import java.text.ParseException;
@@ -69,10 +61,11 @@
     private final ContractPaymentRepository contractPaymentRepository;
     private final ContractInvoicePaymentRepository contractInvoicePaymentRepository;
     private final ContractInvoiceRepository contractInvoiceRepository;
+    private final DictRepository dictRepository;
     private final DictDetailRepository dictDetailRepository;
     private final ContractFirstServiceImpl contractFirstServiceImpl;
     private final ContractSecondServiceImpl contractSecondServiceImpl;
-    private final ProjectServiceImpl projectServiceImpl;
+    private final ContractProjectRepository contractProjectRepository;
 
     @Override
     public Map<String,Object> queryAll(ContractQueryCriteria criteria, Pageable pageable){
@@ -83,7 +76,6 @@
             public ContractDto apply(Contract t) {
                 // 将实体类转换成DTO
                 ContractDto contractDto = contractMapper.toDto(t);
-
                 // 合同款延期情况
                 contractDto.setContractPostponement(contractPostponement(t));
                 // 服务款延期情况
@@ -104,8 +96,14 @@
                 contractDto.setFirstName(contractFirstName(t));
                 // 乙方姓名
                 contractDto.setSecondName(contractSecondName(t));
-                // 项目名称
-                contractDto.setProjectName(projectName(t));
+                // 合同费数量
+                contractDto.setFundContractNum(contractProjectNum(t, Constant.FUND_CONTRACT));
+                // 合同费金额
+                contractDto.setFundContractAmount(contractProjectAmount(t, Constant.FUND_CONTRACT));
+                // 服务费数量
+                contractDto.setFundServiceNum(contractProjectNum(t, Constant.FUND_SERVICE));
+                // 服务费金额
+                contractDto.setFundServiceAmount(contractProjectAmount(t, Constant.FUND_SERVICE));
                 // 返回处理好的DTO
                 return contractDto;
             }
@@ -139,8 +137,14 @@
             contractDto.setFirstName(contractFirstName(contractList.get(i)));
             // 乙方姓名
             contractDto.setSecondName(contractSecondName(contractList.get(i)));
-            // 项目名称
-            contractDto.setProjectName(projectName(contractList.get(i)));
+            // 合同费数量
+            contractDto.setFundContractNum(contractProjectNum(contractList.get(i), Constant.FUND_CONTRACT));
+            // 合同费金额
+            contractDto.setFundContractAmount(contractProjectAmount(contractList.get(i), Constant.FUND_CONTRACT));
+            // 服务费数量
+            contractDto.setFundServiceNum(contractProjectNum(contractList.get(i), Constant.FUND_SERVICE));
+            // 服务费金额
+            contractDto.setFundServiceAmount(contractProjectAmount(contractList.get(i), Constant.FUND_SERVICE));
             // DTO新增
             contractDtoList.add(contractDto);
         }
@@ -180,11 +184,13 @@
         }
         // 计算下一编号
         String nextNumber = maxNumber + 1 >= 100 ? "" + (maxNumber + 1) : maxNumber + 1 >= 10 ? "0" + (maxNumber + 1) : "00" + (maxNumber + 1);
-        // 合同编号
-        resources.setContractNumber(year + "-" + month + date + "-" + nextNumber);
         // 更新金额相关数据
         Contract contract = updateAmountData(resources);
-        return contractMapper.toDto(contractRepository.save(contract));
+
+        // 保存后再更新合同编号(获取健全信息,防止顺序编号冲突)
+        contract = updateContractNumber(contractRepository.save(contract));
+
+        return contractMapper.toDto(contract);
     }
 
     @Override
@@ -197,7 +203,8 @@
         Contract contractOne = updatePaymentStatus(contract);
         // 更新金额相关数据
         Contract contractTwo = updateAmountData(contractOne);
-        contractRepository.save(contractTwo);
+
+        updateContractNumber(contractRepository.save(contractTwo));
     }
 
     @Override
@@ -207,6 +214,7 @@
             contractRepository.deleteById(contractId);
             contractInvoiceRepository.deleteByContractId(contractId);
             contractPaymentRepository.deleteByContractId(contractId);
+            contractProjectRepository.deleteByContractId(contractId);
         }
     }
 
@@ -216,8 +224,7 @@
         for (ContractDto contract : all) {
             String firstName = contractFirstName(contractMapper.toEntity(contract));
             String secondName = contractSecondName(contractMapper.toEntity(contract));
-            String projectName = projectName(contractMapper.toEntity(contract));
-            String contractType = contract.getContractType() != null ? dictDetailRepository.findByDictNameAndValue(Constant.CONTRACT_TYPE, contract.getContractType()).getLabel() : "";
+            String contractType = contract.getContractType() != null ? dictDetailRepository.findByDictNameAndValue(Constant.CONTRACT_DICT_TYPE, contract.getContractType()).getLabel() : "";
             String contractPaymentState = contract.getContractPaymentState() != null ? dictDetailRepository.findByDictNameAndValue(Constant.PAYMENT_STATE, contract.getContractPaymentState()).getLabel() : "";
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             String servicePaymentDate = null;
@@ -236,7 +243,8 @@
             map.put("起始日期", contract.getStartDate());
             map.put("截止日期", contract.getEndDate());
             map.put("合同所属地区", contract.getContractArea());
-            map.put("合同类型", contractType);
+            map.put("合同类型(一级)", contractType);
+            map.put("合同类型(二级)", contract.getContractSecondType());
             map.put("合同内容", contract.getContractContent());
             map.put("开票总金额", contract.getBillingTotalAmount());
             map.put("付款总金额", contract.getPaymentTotalAmount());
@@ -254,7 +262,6 @@
             map.put("合同初版文件", contract.getRelatedDocumentsFirst());
             map.put("合同终版文件", contract.getRelatedDocumentsFinal());
             map.put("合同状态", contractStatus);
-            map.put("项目名称", projectName);
             map.put("备注信息", contract.getRemarks());
             map.put("创建者", contract.getCreateBy());
             map.put("创建日期", contract.getCreateTime());
@@ -286,19 +293,23 @@
     }
 
     /**
-     * 项目名称
+     * 合同项目数量
      * @param resources
      * @return
      */
-    public String projectName(Contract resources) {
-        // 判断项目ID是否为空
-        if (resources.getProjectId() != null) {
-            // 查询项目数据
-            ProjectDto projectDto = projectServiceImpl.findById(resources.getProjectId());
-            return projectDto.getName();
-        } else {
-            return null;
-        }
+    public Integer contractProjectNum(Contract resources, String fundType) {
+        // 查询合同项目数据
+        return contractProjectRepository.countByContractIdAndFundType(resources.getContractId(), fundType);
+    }
+
+    /**
+     * 合同项目金额
+     * @param resources
+     * @return
+     */
+    public BigDecimal contractProjectAmount(Contract resources, String fundType) {
+        // 查询合同项目数据
+        return contractProjectRepository.sumAllocatedAmountByContractIdAndFundType(resources.getContractId(), fundType);
     }
 
     /**
@@ -555,10 +566,28 @@
      * @return
      */
     public Contract updateAmountData(Contract resources) {
+        if (StringUtils.isNotBlank(resources.getContractType()) && StringUtils.isNotBlank(resources.getContractSecondType())) {
+            // 判断是否是已存在的合同类型(二级)
+            if (ElAdminConstant.Enum.ContractTypeDict.findByDictValue(resources.getContractType()) != null) {
+                String secondDictName = ElAdminConstant.Enum.ContractTypeDict.findByDictValue(resources.getContractType()).getSecondDictName();
+                DictDetail dictDetail = dictDetailRepository.findByDictNameAndValue(secondDictName, resources.getContractSecondType());
+                if (dictDetail == null) {
+                    // 新建合同类型(二级)
+                    dictDetail = new DictDetail();
+                    Dict dict = dictRepository.findByName(secondDictName);
+                    dictDetail.setDict(dict);
+                    dictDetail.setLabel(resources.getContractSecondType());
+                    dictDetail.setValue(resources.getContractSecondType());
+                    dictDetail.setDictSort(10);
+                    dictDetailRepository.save(dictDetail);
+                }
+            }
+        }
+
         // 判断合同类型
-        if (Constant.CONTRACT_DEVELOP_SERVICE.equals(resources.getContractType())) {
+        if (Constant.CONTRACT_DEVELOP_SERVICE.equals(resources.getContractSecondType())) {
             // 不处理
-        } else if (Constant.CONTRACT_SERVICE.equals(resources.getContractType())) {
+        } else if (Constant.CONTRACT_SERVICE.equals(resources.getContractSecondType())) {
             // 合同费金额
             resources.setContractAmount(null);
             // 预计付款日期
@@ -626,7 +655,7 @@
             BigDecimal contractAmount = contractPaymentRepository.findByContractIdAndFundType(contract.getContractId(), Constant.FUND_CONTRACT);
             contractAmount = contractAmount == null ? new BigDecimal(0) : contractAmount;
             // 判断合同款项付款金额
-            if(Constant.CONTRACT_SERVICE.equals(contract.getContractType())) {
+            if(Constant.CONTRACT_SERVICE.equals(contract.getContractSecondType())) {
                 // 付款状态
                 contract.setContractPaymentState(null);
             } else if(contractAmount.compareTo(new BigDecimal(0)) == 0) {
@@ -661,7 +690,7 @@
                 BigDecimal serviceAmount = contractPaymentRepository.findByContractIdAndFundType(contract.getContractId(), Constant.FUND_SERVICE);
                 serviceAmount = serviceAmount == null ? new BigDecimal(0) : serviceAmount;
                 // 判断服务款项付款金额
-                if(Constant.CONTRACT_DEVELOP.equals(contract.getContractType())) {
+                if(Constant.CONTRACT_DEVELOP.equals(contract.getContractSecondType())) {
                     // 付款状态
                     contract.setServicePaymentState(null);
                 } else if(serviceAmount.compareTo(new BigDecimal(0)) == 0) {
@@ -682,6 +711,104 @@
     }
 
     /**
+     * 更新合同编号(因更新序号需求加锁执行)(本更新为先存其他后改)
+     * @param resources
+     * @return
+     */
+    public synchronized Contract updateContractNumber(Contract resources) {
+        StringBuilder contractNumber = new StringBuilder();
+        Calendar calendar = Calendar.getInstance();
+
+        if (StringUtils.isNotBlank(resources.getContractNumber())) {
+            // 修改编号
+            calendar.setTime(resources.getSignDate());
+            contractNumber.append(resources.getContractNumber().substring(0, 9));
+
+            // 业绩年度
+            String oldSignYear = resources.getContractNumber().substring(9, 11);
+            String newSignYear = String.valueOf(calendar.get(Calendar.YEAR)).substring(2);
+            // 是否有真实合同
+            String oldTrueContract = resources.getContractNumber().substring(14, 15);
+            String newTrueContract = resources.getContractStatus().equals("contract_not_sign") ? "N" : "Y";
+
+            if (oldSignYear.equals(newSignYear)) {
+                contractNumber.append(resources.getContractNumber().substring(9, 14));
+            } else {
+                String companyCode = resources.getContractNumber().substring(0, 4);
+                // 业绩年度(以签订日期为准)
+                contractNumber.append(newSignYear);
+                // 本公司当年编号(以签订日期为准)(因先存后改故排除本身数据)
+                Long companySort = contractRepository.countContractSort(String.valueOf(calendar.get(Calendar.YEAR)), companyCode, resources.getContractId());
+                contractNumber.append(String.format("%03d", (companySort == null ? 0L : companySort) + 1));
+            }
+            if (oldTrueContract.equals(newTrueContract)) {
+                contractNumber.append(resources.getContractNumber().substring(14, 15));
+            } else {
+                // 是否有真实合同
+                contractNumber.append(newTrueContract);
+            }
+            if (oldSignYear.equals(newSignYear)) {
+                contractNumber.append(resources.getContractNumber().substring(15));
+            } else {
+                // 全部公司当年编号(以签订日期为准)(因先存后改故排除本身数据)
+                Long allCompanySort = contractRepository.countAllContractSort(String.valueOf(calendar.get(Calendar.YEAR)), resources.getContractId());
+                contractNumber.append(String.format("%04d", (allCompanySort == null ? 0L : allCompanySort) + 1));
+            }
+
+            if (resources.getContractNumber().equals(contractNumber.toString())) {
+                return resources;
+            } else {
+                resources.setContractNumber(contractNumber.toString());
+            }
+        } else {
+            // 新建编号
+            String companyCode;
+            String sign;
+
+            // 合同前缀
+            String firstName = contractFirstName(resources);
+            String SecondName = contractSecondName(resources);
+            if (ElAdminConstant.Enum.CompanySuffix.findByCompanyName(firstName) != null) {
+                companyCode = ElAdminConstant.Enum.CompanySuffix.findByCompanyName(firstName).getCode();
+                sign = "J";
+                contractNumber.append(companyCode);
+            } else if (ElAdminConstant.Enum.CompanySuffix.findByCompanyName(SecondName) != null) {
+                companyCode = ElAdminConstant.Enum.CompanySuffix.findByCompanyName(SecondName).getCode();
+                sign = "Y";
+                contractNumber.append(companyCode);
+            } else {
+                return resources;
+            }
+            // 签订年度(以创建日期为准)
+            calendar.setTime(resources.getCreateTime());
+            contractNumber.append(String.valueOf(calendar.get(Calendar.YEAR)).substring(2));
+            // 签订月份(以创建日期为准)
+            contractNumber.append(calendar.get(Calendar.MONTH) + 1 >= 10 ? "" + (calendar.get(Calendar.MONTH) + 1) : "0" + (calendar.get(Calendar.MONTH) + 1));
+            // 甲乙标识
+            contractNumber.append(sign);
+            // 业绩年度(以签订日期为准)
+            calendar.setTime(resources.getSignDate());
+            contractNumber.append(String.valueOf(calendar.get(Calendar.YEAR)).substring(2));
+            // 本公司当年编号(以签订日期为准)(因先存后改故排除本身数据)
+            Long companySort = contractRepository.countContractSort(String.valueOf(calendar.get(Calendar.YEAR)), companyCode, resources.getContractId());
+            contractNumber.append(String.format("%03d", (companySort == null ? 0L : companySort) + 1));
+            // 是否有真实合同
+            if (resources.getContractStatus().equals("contract_not_sign")) {
+                contractNumber.append("N");
+            } else {
+                contractNumber.append("Y");
+            }
+            // 全部公司当年编号(以签订日期为准)(因先存后改故排除本身数据)
+            Long allCompanySort = contractRepository.countAllContractSort(String.valueOf(calendar.get(Calendar.YEAR)), resources.getContractId());
+            contractNumber.append(String.format("%04d", (allCompanySort == null ? 0L : allCompanySort) + 1));
+
+            resources.setContractNumber(contractNumber.toString());
+        }
+
+        return contractRepository.save(resources);
+    }
+
+    /**
      * 查询合同款总金额
      * @param criteria
      * @return

--
Gitblit v1.9.3