From bc4f6588b92d207bef56edf3b5b38d8e4e2b8236 Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期四, 12 六月 2025 16:34:11 +0800
Subject: [PATCH] 合同管理

---
 src/main/java/com/product/contract/controller/ContractInfoController.java |   87 +++++++++++++++++++++
 src/main/java/com/product/contract/service/ContractPaymentService.java    |    5 
 src/main/java/com/product/contract/config/CmnConst.java                   |    3 
 src/main/java/com/product/contract/config/SystemCode.java                 |    4 +
 src/main/java/com/product/contract/service/ContractInfoService.java       |  134 +++++++++++++++++++++++++--------
 5 files changed, 198 insertions(+), 35 deletions(-)

diff --git a/src/main/java/com/product/contract/config/CmnConst.java b/src/main/java/com/product/contract/config/CmnConst.java
index 79e976d..b1288c1 100644
--- a/src/main/java/com/product/contract/config/CmnConst.java
+++ b/src/main/java/com/product/contract/config/CmnConst.java
@@ -26,6 +26,7 @@
     public static final String PRODUCT_PROJECT_BUSINESS = "product_project_business";//椤圭洰琛�
     public static final String PRODUCT_PROJECT_BUSINESS_PLAN = "product_project_business_plan";//椤圭洰璁″垝琛�
     public static final String PRODUCT_PROJECT_MANAGER_DATA_LIST = "product_project_manager_data_list";//璧勬枡琛�
+    public static final String PRODUCT_PROJECT_CONTRACT_INVOICE_INFO = "product_project_contract_invoice_info";// 寮�绁ㄦ槑缁�
 
     //甯搁噺瀹氫箟
     public static final String UUID = "uuid"; // uuid
@@ -39,5 +40,7 @@
     public static final String PROJECT_UUID = "project_uuid";
     public static final String PROJECT_TYPE = "project_type";
 
+    public static final String CONTRACT_STATUS = "contract_status";
+
 
 }
diff --git a/src/main/java/com/product/contract/config/SystemCode.java b/src/main/java/com/product/contract/config/SystemCode.java
index cda113c..6044872 100644
--- a/src/main/java/com/product/contract/config/SystemCode.java
+++ b/src/main/java/com/product/contract/config/SystemCode.java
@@ -91,6 +91,10 @@
     SYSTEM_FIND_CONTRACT_PROJECT_TRANSLATE_FAIL("鑾峰彇椤圭洰杩涘害妯$増璇︽儏澶辫触", ModuleEnum.CONTRACT.getValue() + "049"),
     SYSTEM_FIND_CONTRACT_PROJECT_TYPE_FAIL("椤圭洰绫诲瀷閿欒", ModuleEnum.CONTRACT.getValue() + "050"),
 
+    CHECK_INVOICE_NUMBER_FAIL("楠岃瘉鍙戠エ鍙锋槸鍚﹂噸澶嶅け璐�", ModuleEnum.CONTRACT.getValue() + "051"),
+    CHECK_INVOICE_PAYMENT_FAIL("楠岃瘉鍙戠エ鍥炴鏄惁瓒呴澶辫触", ModuleEnum.CONTRACT.getValue() + "052"),
+    LIST_CONTRACT_INVOICE_AND_COLLECTION_FAIL("鏌ヨ鍚堝悓涓嬬殑寮�绁ㄥ拰鍥炴璁板綍澶辫触", ModuleEnum.CONTRACT.getValue() + "053"),
+
     ;
 
     private String text;
diff --git a/src/main/java/com/product/contract/controller/ContractInfoController.java b/src/main/java/com/product/contract/controller/ContractInfoController.java
index 0f1bd79..cfb5237 100644
--- a/src/main/java/com/product/contract/controller/ContractInfoController.java
+++ b/src/main/java/com/product/contract/controller/ContractInfoController.java
@@ -1,5 +1,6 @@
 package com.product.contract.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.product.common.lang.StringUtils;
 import com.product.contract.config.CmnConst;
 import com.product.contract.config.SystemCode;
@@ -13,6 +14,7 @@
 import com.product.core.exception.BaseException;
 import com.product.core.spring.context.SpringMVCContextHolder;
 import com.product.module.sys.version.ApiVersion;
+import com.product.util.BaseUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -206,7 +208,9 @@
                 return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText());
             }
             FieldSetEntity fs = contractInfoService.findContractInfo(fse);
-            return OK_List(fs);
+            JSONObject resultObj = BaseUtil.fieldSetEntityToJson(fs);
+            resultObj.put("~showType~", fse.getString("showType"));
+            return OK(resultObj);
         } catch (BaseException e) {
             e.printStackTrace();
             SpringMVCContextHolder.getSystemLogger().error(e);
@@ -662,4 +666,85 @@
                     SystemCode.SYSTEM_SAVE_CONTRACT_FILE_FAIL.getText());
         }
     }
+
+    /**
+     * 楠岃瘉鍙戠エ鍙凤紝涓嶅悓娆℃暟鐨勫紑绁ㄤ腑涓嶈兘鏈夌浉鍚岀殑鍙戠エ鍙凤紙鍚屾鐨勫彲浠ワ級
+     * @param request
+     * @return
+     */
+    @RequestMapping(value="/check-invoice-number/{version}", method = RequestMethod.POST)
+    @ApiVersion(1)
+    public String checkInvoiceNumber(HttpServletRequest request) {
+        try {
+            FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.PRODUCT_PROJECT_CONTRACT_INVOICE_INFO);
+            // 鍒ゆ柇鍙傛暟鏄惁涓虹┖
+            if (fse == null) {
+                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
+                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
+            }
+            return OK(contractInfoService.checkInvoiceNumber(fse));
+        } catch (BaseException e) {
+            e.printStackTrace();
+            SpringMVCContextHolder.getSystemLogger().error(e);
+            return this.error(e);
+        } catch (Exception e) {
+            e.printStackTrace();
+            SpringMVCContextHolder.getSystemLogger().error(e);
+            return this.error(SystemCode.CHECK_INVOICE_NUMBER_FAIL.getValue(), SystemCode.CHECK_INVOICE_NUMBER_FAIL.getText());
+        }
+    }
+
+    /**
+     * 楠岃瘉鍙戠エ鍙凤紝鍗曚釜鍙戠エ鐨勫洖娆炬�婚涓嶈兘瓒呰繃寮�绁ㄩ噾棰�
+     * @param request
+     * @return
+     */
+    @RequestMapping(value="/check-invoice-payment/{version}", method = RequestMethod.POST)
+    @ApiVersion(1)
+    public String checkInvoicePayment(HttpServletRequest request) {
+        try {
+            FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.PRODUCT_PROJECT_CONTRACT_INVOICE_INFO);
+            // 鍒ゆ柇鍙傛暟鏄惁涓虹┖
+            if (fse == null) {
+                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
+                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
+            }
+            return OK(contractInfoService.checkInvoicePayment(fse));
+        } catch (BaseException e) {
+            e.printStackTrace();
+            SpringMVCContextHolder.getSystemLogger().error(e);
+            return this.error(e);
+        } catch (Exception e) {
+            e.printStackTrace();
+            SpringMVCContextHolder.getSystemLogger().error(e);
+            return this.error(SystemCode.CHECK_INVOICE_NUMBER_FAIL.getValue(), SystemCode.CHECK_INVOICE_NUMBER_FAIL.getText());
+        }
+    }
+
+    /**
+     * 鏌ヨ鍚堝悓涓嬬殑寮�绁ㄥ拰鍥炴璁板綍
+     * @param request
+     * @return
+     */
+    @RequestMapping(value="/list-contract-invoice-and-collection/{version}", method = RequestMethod.POST)
+    @ApiVersion(1)
+    public String listContractInvoiceAndCollection(HttpServletRequest request) {
+        try {
+            FieldSetEntity fse = BaseUtil.getFieldSetEntity(request, CmnConst.LX_PROJECT_CONTRACT_INFO);
+            // 鍒ゆ柇鍙傛暟鏄惁涓虹┖
+            if (fse == null) {
+                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
+                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
+            }
+            return OK(contractInfoService.listContractInvoiceAndCollection(fse));
+        } catch (BaseException e) {
+            e.printStackTrace();
+            SpringMVCContextHolder.getSystemLogger().error(e);
+            return this.error(e);
+        } catch (Exception e) {
+            e.printStackTrace();
+            SpringMVCContextHolder.getSystemLogger().error(e);
+            return this.error(SystemCode.LIST_CONTRACT_INVOICE_AND_COLLECTION_FAIL.getValue(), SystemCode.LIST_CONTRACT_INVOICE_AND_COLLECTION_FAIL.getText());
+        }
+    }
 }
diff --git a/src/main/java/com/product/contract/service/ContractInfoService.java b/src/main/java/com/product/contract/service/ContractInfoService.java
index 4fccc04..5893e8e 100644
--- a/src/main/java/com/product/contract/service/ContractInfoService.java
+++ b/src/main/java/com/product/contract/service/ContractInfoService.java
@@ -1,5 +1,7 @@
 package com.product.contract.service;
 
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
 import com.product.common.lang.StringUtils;
 import com.product.contract.config.CmnConst;
 import com.product.contract.service.ide.IContractInfoService;
@@ -14,13 +16,14 @@
 import com.product.core.transfer.Transactional;
 import com.product.module.sys.entity.SystemUser;
 import com.product.util.BaseUtil;
-import org.apache.bcel.generic.IF_ACMPEQ;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
 import java.util.Date;
+import java.util.List;
 
 /**
  * Copyright LX
@@ -49,6 +52,9 @@
     @Override
     @Transactional
     public String saveContractInfo(FieldSetEntity fs) throws BaseException {
+        if (StringUtils.isEmpty(fs.getString(CmnConst.CONTRACT_STATUS))) {
+            fs.setValue(CmnConst.CONTRACT_STATUS, 0);
+        }
         if(StringUtils.isEmpty(fs.getString(CmnConst.UUID))){
             fs.setValue("org_level_uuid", SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid());
             fs.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
@@ -129,32 +135,6 @@
      */
     public FieldSetEntity findContractInfo(FieldSetEntity fs) throws BaseException {
     	FieldSetEntity cfs =baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
-    	DataTableEntity sub=cfs.getSubDataTable("product_project_contract_info_sub");
-        StringBuilder  filter=new StringBuilder(128);
-        filter.append(" uuid='"+cfs.getValues().get("project_uuid")+"'");
-        DataTableEntity product_project_business = baseDao.listTable("product_project_business",filter.toString(),new Object[]{});
-//        String guarantee_period = product_project_business.getData().get(0).getValues().get("guarantee_period").toString();
-        String guarantee_period = product_project_business.getString(0, "guarantee_period");
-        StringBuilder  dictFilter=new StringBuilder(128);
-        dictFilter.append("dict_name='椤圭洰绠$悊椤圭洰绫诲瀷' and dict_value="+product_project_business.getData().get(0).getValues().get("project_type").toString());
-        DataTableEntity product_sys_dict = baseDao.listTable("product_sys_dict", dictFilter.toString(), new Object[]{});
-       //鍚堝悓鏈夋晥鏈燂細濡傛灉鏄紑鍙戦」鐩�佸疄鏂介」鐩紝浠庨」鐩俊鎭殑璐ㄤ繚鏈熷甫杩囨潵
-        String dict_label = product_sys_dict.getData().get(0).getValues().get("dict_label").toString();
-        if(dict_label.equals("寮�鍙戦」鐩�") ||dict_label.equals("瀹炴柦椤圭洰")){
-            cfs.setValue("guarantee_period",guarantee_period);
-            cfs.setValue("project_type_now",dict_label);
-        }
-       //缁撴潫鏃ユ湡锛� 濡傛灉鏄紑鍙戦」鐩�佸疄鏂介」鐩紝楠屾敹鏃ユ湡+鍚堝悓鏈夋晥鏈燂紝缁存姢鍚堝悓鍒欐槸绛惧畾鏃ユ湡+鍚堝悓鏈夋晥鏈�
-        if(dict_label.equals("寮�鍙戦」鐩�") || dict_label.equals("瀹炴柦椤圭洰")){
-//            String carefully_check = product_project_business.getData().get(0).getValues().get("carefully_check").toString();
-            String carefully_check = product_project_business.getString(0, "carefully_check");
-            if (!StringUtils.isEmpty(carefully_check)){
-                String[] dateTime = carefully_check.split(" ");
-                String time=dateTime[0];
-                String date = getDate(time,guarantee_period);
-                cfs.setValue("end_time",date);
-            }
-        }
         return cfs;
     }
 
@@ -178,18 +158,22 @@
      */
     public FieldSetEntity getContractInfoByName(FieldSetEntity fs) throws BaseException {
         FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.LX_PROJECT_CONTRACT_INFO,fs.getUUID(),true);
-        FieldSetEntity fsProject = baseDao.getFieldSetEntity(CmnConst.PRODUCT_PROJECT_BUSINESS,fss.getString("project_uuid"),false);
-        FieldSetEntity fsCustomer = baseDao.getFieldSet("product_project_customer", fsProject.getString("customer_name"), false);
+        if (!StringUtils.isEmpty(fss.getString("project_uuid"))) {
+            FieldSetEntity fsProject = baseDao.getFieldSetEntity(CmnConst.PRODUCT_PROJECT_BUSINESS,fss.getString("project_uuid"),false);
+            FieldSetEntity fsCustomer = baseDao.getFieldSet("product_project_customer", fsProject.getString("customer_name"), false);
+            fss.setValue("customer_name", fsCustomer.getString("customer_name"));
+            fss.setValue("project_num",fsProject.getString("project_num"));
+            fss.setValue("project_name",fsProject.getString("project_name"));
+        } else {
+            fss.setValue("customer_name", fss.getString("owner"));
+        }
         FieldSetEntity contractName = baseDao.getFieldSetEntityBySQL("SELECT sum(invoice_amount) invoice_amount FROM product_project_contract_invoice_info WHERE contract_invoice_uuid in(SELECT uuid FROM product_project_contract_invoice where contract_name=?)", new String[]{fs.getString("uuid")},false);
         if (contractName==null||StringUtils.isEmpty(contractName.getString("invoice_amount"))){
             fss.setValue("invoice_amount",0);
         }else {
             fss.setValue("invoice_amount",contractName.getString("invoice_amount"));
         }
-        fss.setValue("customer_name", fsCustomer.getString("customer_name"));
         fss.setValue("collection_amount",0);
-        fss.setValue("project_num",fsProject.getString("project_num"));
-        fss.setValue("project_name",fsProject.getString("project_name"));
         return fss;
     }
 
@@ -481,4 +465,90 @@
             return baseDao.update(fieldSetEntity);
         }
     }
+
+    /**
+     * 楠岃瘉鍙戠エ鍙凤紝涓嶅悓娆℃暟鐨勫紑绁ㄤ腑涓嶈兘鏈夌浉鍚岀殑鍙戠エ鍙凤紙鍚屾鐨勫彲浠ワ級
+     * @param fse
+     * @return
+     */
+    public Object[] checkInvoiceNumber(FieldSetEntity fse) {
+        String uuid = fse.getUUID();
+        String[] invoiceNumberArr = fse.getString("invoice_num").split(",");
+        StringBuilder filter = new StringBuilder(128);
+        List<String> paramList  = Lists.newArrayList();
+        if (!StringUtils.isEmpty(uuid)) {
+            filter.append("contract_invoice_uuid<>? AND ");
+            paramList.add(uuid);
+        }
+        filter.append(BaseUtil.buildQuestionMarkFilter("invoice_num", invoiceNumberArr.length, true));
+        paramList.addAll(Arrays.asList(invoiceNumberArr));
+        DataTableEntity dte = baseDao.listTable(CmnConst.PRODUCT_PROJECT_CONTRACT_INVOICE_INFO, filter.toString(), paramList.toArray());
+        if (!DataTableEntity.isEmpty(dte)) {
+            return dte.getFieldAllValues("invoice_num");
+        }
+        return new Object[]{};
+    }
+
+    /**
+     * 楠岃瘉鍙戠エ鍙凤紝鍗曚釜鍙戠エ鐨勫洖娆炬�婚涓嶈兘瓒呰繃寮�绁ㄩ噾棰�
+     * @param fse
+     * @return
+     */
+    public Object checkInvoicePayment(FieldSetEntity fse) {
+        String uuid = fse.getUUID();
+        DataTableEntity paramDte = fse.getSubDataTable("product_project_contract_payment_collection");
+        StringBuilder partSql = new StringBuilder(256);
+        for (int i = 0; i < paramDte.getRows(); i++) {
+            if (partSql.length() > 0) {
+                partSql.append("\n        UNION ALL");
+            }
+            FieldSetEntity paramFse = paramDte.getFieldSetEntity(i);
+            partSql.append("\n        SELECT '").append(paramFse.getString("invoice_num")).append("' invoice_num,'").append(paramFse.getString("collection_amount")).append("' cur_payment_amount");
+        }
+        StringBuilder sql = new StringBuilder(256);
+        sql.append("\nSELECT DISTINCT i.invoice_num,i.invoice_amount,i.payment_amount,p.cur_payment_amount,IFNULL(i.payment_amount,0) + p.cur_payment_amount - i.invoice_amount diff_amount");
+        sql.append("\nFROM product_project_contract_invoice_info i");
+        sql.append("\nLEFT JOIN product_project_contract_payment_collection ps ON i.invoice_num=ps.invoice_num");
+        List<String> paramList = Lists.newArrayList();
+        if (!StringUtils.isEmpty(uuid)) {
+            sql.append(" AND ps.contract_payment_uuid<>?");
+            paramList.add(uuid);
+        }
+        sql.append("\nINNER JOIN (");
+        sql.append("\n    SELECT invoice_num,SUM(cur_payment_amount) cur_payment_amount");
+        sql.append("\n    FROM (");
+        sql.append(partSql);
+        sql.append("\n    ) t");
+        sql.append("\n    GROUP BY 1");
+        sql.append("\n) p ON i.invoice_num=p.invoice_num");
+        sql.append("\nWHERE i.invoice_amount - IFNULL(i.payment_amount,0) < p.cur_payment_amount");
+        DataTableEntity resultDte = baseDao.listTable(sql.toString(), paramList.toArray());
+        return BaseUtil.dataTableEntityToJson(resultDte);
+    }
+
+    /**
+     * 鏌ヨ鍚堝悓涓嬬殑寮�绁ㄥ拰鍥炴璁板綍
+     * @param fse
+     * @return
+     */
+    public JSONObject listContractInvoiceAndCollection(FieldSetEntity fse) {
+        String contractUUID = fse.getUUID();
+        JSONObject resultObj = new JSONObject();
+        StringBuilder sql = new StringBuilder(128);
+        sql.append("\nSELECT id.*");
+        sql.append("\nFROM product_project_contract_invoice_info id");
+        sql.append("\nINNER JOIN product_project_contract_invoice i ON id.contract_invoice_uuid=i.uuid");
+        sql.append("\nWHERE i.contract_name=?");
+        DataTableEntity invoiceDte = baseDao.listTable(sql.toString(), new Object[]{contractUUID});
+        resultObj.put("invoice", BaseUtil.dataTableEntityToJson(invoiceDte));
+
+        sql.setLength(0);
+        sql.append("\nSELECT c.*");
+        sql.append("\nFROM product_project_contract_payment_collection c");
+        sql.append("\nINNER JOIN product_project_contract_payment p ON c.contract_payment_uuid=p.uuid");
+        sql.append("\nWHERE p.contract_name=?");
+        DataTableEntity collectionDte = baseDao.listTable(sql.toString(), new Object[]{contractUUID});
+        resultObj.put("collection", BaseUtil.dataTableEntityToJson(collectionDte));
+        return resultObj;
+    }
 }
diff --git a/src/main/java/com/product/contract/service/ContractPaymentService.java b/src/main/java/com/product/contract/service/ContractPaymentService.java
index 159b3b6..9d08061 100644
--- a/src/main/java/com/product/contract/service/ContractPaymentService.java
+++ b/src/main/java/com/product/contract/service/ContractPaymentService.java
@@ -65,14 +65,15 @@
     private void writeBackPayment(FieldSetEntity fs){
         //鍥炲啓鍙戠エ鍙峰洖娆�
         DataTableEntity dt = fs.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_PAYMENT_COLLECTION);
+        String contractTermsUUID = fs.getString("contract_terms");
         for (int i = 0; i <dt.getRows() ; i++) {
             FieldSetEntity subFs = dt.getFieldSetEntity(i);
             FieldSetEntity collection_amount_fs = baseDao.getFieldSetBySQL("SELECT sum(collection_amount) collection_amount FROM `product_project_contract_payment_collection` where invoice_num=?", new String[]{subFs.getString("invoice_num")}, false);
-            baseDao.executeUpdate("UPDATE product_project_contract_invoice_info set payment_amount=? where invoice_num=?",new String[]{collection_amount_fs.getString("collection_amount"),subFs.getString("invoice_num")});
+            baseDao.executeUpdate("UPDATE product_project_contract_invoice_info set payment_amount=? where invoice_num=? AND contract_terms=?",new String[]{collection_amount_fs.getString("collection_amount"), subFs.getString("invoice_num"), contractTermsUUID});
         }
         //鍥炲啓鏉℃鍥炴
         FieldSetEntity contractTerms = baseDao.getFieldSetBySQL("SELECT sum(collection_amount) collection_amount FROM `product_project_contract_payment_collection` \n" +
-                "where contract_payment_uuid in(SELECT uuid FROM product_project_contract_payment where contract_terms=?)", new String[]{fs.getString("contract_terms")}, false);
+                "where contract_payment_uuid in(SELECT uuid FROM product_project_contract_payment where contract_terms=?)", new String[]{contractTermsUUID}, false);
         if (contractTerms!=null&&!StringUtils.isEmpty(contractTerms.getString("collection_amount"))) {
             FieldSetEntity contractInfoSub = new FieldSetEntity();
             contractInfoSub.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);

--
Gitblit v1.9.2