From 2b8e522ec7c38570463a08e04bc1cc264a038e5b Mon Sep 17 00:00:00 2001
From: zm <2369059705qq.com>
Date: 星期一, 17 三月 2025 11:26:39 +0800
Subject: [PATCH] 整体BUG修改

---
 src/main/java/com/product/contract/service/ContractPaymentService.java |   68 +++++++++++++++++++++++++---------
 1 files changed, 50 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/product/contract/service/ContractPaymentService.java b/src/main/java/com/product/contract/service/ContractPaymentService.java
index ed2a64b..159b3b6 100644
--- a/src/main/java/com/product/contract/service/ContractPaymentService.java
+++ b/src/main/java/com/product/contract/service/ContractPaymentService.java
@@ -46,26 +46,49 @@
     @Override
     @Transactional
     public String saveContractPayment(FieldSetEntity fs) throws BaseException {
-        FieldSetEntity fspay = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB,"contract_info_uuid=?",new Object[]{fs.getString("contract_name")},false);
-        Double amount_collected = fs.getDouble("amount_collected");//宸插洖娆鹃噾棰�
-        //鍥炲啓鍥炴閲戦鍒板悎鍚屽瓙琛�
-        FieldSetEntity fcosub = new FieldSetEntity();
-        if(fspay!=null){
-            fcosub.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
-            fcosub.setValue("uuid",fspay.getUUID());
-            fcosub.setValue("payment_received",amount_collected);//宸插洖娆鹃噾棰�
-            baseDao.update(fcosub);
-        }
+
         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());
             fs.setValue("created_utc_datetime",new Date());
-            return baseDao.add(fs,true);
+            String add = baseDao.add(fs, true);
+            writeBackPayment(fs);
+            return add;
         } else {
             fs.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
             fs.setValue("updated_utc_datetime",new Date());
             baseDao.update(fs);
+            writeBackPayment(fs);
             return fs.getString(CmnConst.UUID);
+        }
+    }
+    private void writeBackPayment(FieldSetEntity fs){
+        //鍥炲啓鍙戠エ鍙峰洖娆�
+        DataTableEntity dt = fs.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_PAYMENT_COLLECTION);
+        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")});
+        }
+        //鍥炲啓鏉℃鍥炴
+        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);
+        if (contractTerms!=null&&!StringUtils.isEmpty(contractTerms.getString("collection_amount"))) {
+            FieldSetEntity contractInfoSub = new FieldSetEntity();
+            contractInfoSub.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
+            contractInfoSub.setValue("uuid",fs.getString("contract_terms"));
+            contractInfoSub.setValue("payment_amount",contractTerms.getString("collection_amount"));
+            baseDao.update(contractInfoSub);
+        }
+        //鍥炲啓鍚堝悓鎬诲洖娆�
+        FieldSetEntity contractName = 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_name=?)", new String[]{fs.getString("contract_name")}, false);
+        if (contractName!=null&&!StringUtils.isEmpty(contractName.getString("collection_amount"))){
+            FieldSetEntity contractInfo = new FieldSetEntity();
+            contractInfo.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO);
+            contractInfo.setValue("uuid",fs.getString("contract_name"));
+            contractInfo.setValue("collection_amount",contractName.getString("collection_amount"));
+            baseDao.update(contractInfo);
         }
     }
 
@@ -117,13 +140,22 @@
      * @throws BaseException
      */
     public  DataTableEntity paymentCollectionData()throws BaseException{
-       return baseDao.listTable("SELECT b.project_name,b.customer_name,a.contract_name,c.clause_content,d.invoice_num,e.apply_time,d.invoice_amount,f.collection_amount,f.collection_time FROM \n" +
-                "product_project_contract_info a \n" +
-                "LEFT JOIN product_project_business b on a.project_uuid=b.uuid\n" +
-                "LEFT JOIN product_project_contract_info_sub c on a.uuid =c.contract_info_uuid\n" +
-                "LEFT JOIN product_project_contract_invoice_info d  on c.uuid=d.contract_terms\n" +
-                "LEFT JOIN product_project_contract_invoice e on e.uuid=d.contract_invoice_uuid\n" +
-                "LEFT JOIN product_project_contract_payment_collection f on d.invoice_num=f.invoice_num");
+    	StringBuilder sbSql = new StringBuilder();
+    	sbSql.append("SELECT A.*,(A.contract_amount - A.amount_collected) AS unpaid_amount \n");
+    	sbSql.append("FROM ( \n");
+    	sbSql.append("	SELECT \n");
+    	sbSql.append("		b.project_name,g.customer_name,a.contract_name,a.contract_amount, \n");
+    	sbSql.append("		(SELECT IFNULL(SUM(amount_collected), 0) FROM product_project_contract_payment WHERE contract_name = A.uuid)AS amount_collected, \n");
+    	sbSql.append("		c.clause_content,d.invoice_num,e.apply_time,d.invoice_amount,f.collection_amount,f.collection_time \n");
+    	sbSql.append("	FROM product_project_contract_info a  \n");
+    	sbSql.append("	LEFT JOIN product_project_business b on a.project_uuid=b.uuid \n");
+    	sbSql.append("	LEFT JOIN product_project_contract_info_sub c on a.uuid =c.contract_info_uuid \n");
+    	sbSql.append("	LEFT JOIN product_project_contract_invoice_info d  on c.uuid=d.contract_terms \n");
+    	sbSql.append("	LEFT JOIN product_project_contract_invoice e on e.uuid=d.contract_invoice_uuid \n");
+    	sbSql.append("	LEFT JOIN product_project_contract_payment_collection f on d.invoice_num=f.invoice_num \n");
+    	sbSql.append("	LEFT JOIN product_project_customer g on b.customer_name=g.uuid \n");
+    	sbSql.append(")A \n");
+       return baseDao.listTable(sbSql.toString(),new String[]{});
     }
 
 }

--
Gitblit v1.9.2