From e5c385b510c54107f9a4ab26d24603706c95cf9f Mon Sep 17 00:00:00 2001
From: zm <2369059705qq.com>
Date: 星期五, 26 七月 2024 18:22:33 +0800
Subject: [PATCH] 整体优化

---
 src/main/java/com/product/contract/service/ContractInvoiceService.java |   51 +++++++++++++----
 src/main/java/com/product/contract/service/ContractPaymentService.java |   45 +++++++++++---
 src/main/java/com/product/contract/service/ContractInfoService.java    |   27 ++------
 3 files changed, 80 insertions(+), 43 deletions(-)

diff --git a/src/main/java/com/product/contract/service/ContractInfoService.java b/src/main/java/com/product/contract/service/ContractInfoService.java
index d913f63..d2b6d6e 100644
--- a/src/main/java/com/product/contract/service/ContractInfoService.java
+++ b/src/main/java/com/product/contract/service/ContractInfoService.java
@@ -140,27 +140,14 @@
     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);
-        DataTableEntity dt = fss.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
-        FieldSetEntity fsub = null;
-        int invoiced = 0;//宸插紑绁ㄩ噾棰�
-        int payment_received = 0;//宸插洖娆鹃噾棰�
-        if(dt!=null && dt.getRows()>0){
-            for (int i = 0; i < dt.getRows(); i++) {
-                fsub = dt.getFieldSetEntity(i);
-                String invo = fsub.getString("invoiced");
-                String paym = fsub.getString("payment_received");
-                if(BaseUtil.strIsNull(invo)){
-                    invo = "0";
-                }
-                if(BaseUtil.strIsNull(paym)){
-                    paym = "0";
-                }
-                invoiced += Double.parseDouble(invo);
-                payment_received += Double.parseDouble(paym);
-            }
+        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("invoiced",invoiced);
-        fss.setValue("payment_received",payment_received);
+
+        fss.setValue("collection_amount",0);
         fss.setValue("project_num",fsProject.getString("project_num"));
         fss.setValue("project_name",fsProject.getString("project_name"));
         return fss;
diff --git a/src/main/java/com/product/contract/service/ContractInvoiceService.java b/src/main/java/com/product/contract/service/ContractInvoiceService.java
index 83b1112..9092684 100644
--- a/src/main/java/com/product/contract/service/ContractInvoiceService.java
+++ b/src/main/java/com/product/contract/service/ContractInvoiceService.java
@@ -44,30 +44,57 @@
     @Override
     @Transactional
     public String saveContractInvoice(FieldSetEntity fs) {
-        FieldSetEntity fsinvo = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB,"contract_info_uuid=?",new Object[]{fs.getString("contract_name")},false);
-        Double invoiced_amount = fs.getDouble("invoiced_amount");//宸插紑绁ㄩ噾棰�
-        //鍥炲啓宸插紑绁ㄩ噾棰濆埌鍚堝悓瀛愯〃
-        FieldSetEntity fcosub = new FieldSetEntity();
-        if(fsinvo!=null){
-            fcosub.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
-            fcosub.setValue("uuid",fsinvo.getUUID());
-            fcosub.setValue("invoice_amount",invoiced_amount);//宸插紑绁ㄩ噾棰�
-            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("org_level_uuid", SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid());
             fs.setValue("created_utc_datetime",new Date());
-            return baseDao.add(fs,true);
+            String add = baseDao.add(fs, true);
+            writeBackInvoice(fs);
+            return add;
         } else {
             fs.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
             fs.setValue("updated_utc_datetime",new Date());
             baseDao.update(fs);
+            writeBackInvoice(fs);
             return fs.getString(CmnConst.UUID);
         }
     }
 
     /**
+     * 鍥炲啓鍥炴閲戦鍒板悎鍚�
+     * @param fs
+     */
+   private void  writeBackInvoice(FieldSetEntity fs){
+       //鎸夌収鏉℃鍥炲啓鍒板悎鍚屽瓙琛�
+       DataTableEntity subDataTable = fs.getSubDataTable(CmnConst.LX_PROJECT_CONTRACT_INVOICE_INFO);
+       for (int i = 0; i <subDataTable.getRows() ; i++) {
+           FieldSetEntity fsData = subDataTable.getFieldSetEntity(i);
+           FieldSetEntity fsinvo = baseDao.getFieldSetEntityByFilter(CmnConst.LX_PROJECT_CONTRACT_INVOICE_INFO,"contract_terms=?",new Object[]{fsData.getString("contract_terms")},false);
+
+           FieldSetEntity contractTerms = baseDao.getFieldSetEntityBySQL("SELECT sum(invoice_amount) invoice_amount FROM product_project_contract_invoice_info where contract_terms=?   GROUP BY contract_terms", new Object[]{fsData.getString("contract_terms")}, false);
+
+           Double invoiced_amount = contractTerms.getDouble("invoice_amount");//宸插紑绁ㄩ噾棰�
+           //鍥炲啓宸插紑绁ㄩ噾棰濆埌鍚堝悓瀛愯〃
+           FieldSetEntity fcosub = new FieldSetEntity();
+           if(fsinvo!=null){
+               fcosub.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO_SUB);
+               fcosub.setValue("uuid",fsData.getString("contract_terms"));
+               fcosub.setValue("invoice_amount",invoiced_amount);//宸插紑绁ㄩ噾棰�
+               baseDao.update(fcosub);
+           }
+       }
+       //鎸夌収鍚堝悓鍥炲啓鍒板悎鍚屼富琛�
+       FieldSetEntity invoice_amount_fs = baseDao.getFieldSetEntityBySQL("SELECT sum(invoice_amount) invoice_amount FROM product_project_contract_invoice_info \n" +
+               "where contract_invoice_uuid in (SELECT uuid FROM product_project_contract_invoice where contract_name=?)", new Object[]{fs.getString("contract_name")}, false);
+       if(invoice_amount_fs!=null) {
+           FieldSetEntity invoiceFs = new FieldSetEntity();
+           invoiceFs.setTableName(CmnConst.LX_PROJECT_CONTRACT_INFO);
+           invoiceFs.setValue("uuid",fs.getString("contract_name"));
+           invoiceFs.setValue("invoice_amount",invoice_amount_fs.getString("invoice_amount"));//宸插紑绁ㄩ噾棰�
+           baseDao.update(invoiceFs);
+       }
+   }
+    /**
      * 鍒犻櫎鍚堝悓寮�绁�
      * @param fs
      * @return
diff --git a/src/main/java/com/product/contract/service/ContractPaymentService.java b/src/main/java/com/product/contract/service/ContractPaymentService.java
index 0576fe4..8129f18 100644
--- a/src/main/java/com/product/contract/service/ContractPaymentService.java
+++ b/src/main/java/com/product/contract/service/ContractPaymentService.java
@@ -46,28 +46,51 @@
     @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);
+        }
+    }
 
     /**
      * 鍒犻櫎鍚堝悓鍥炴

--
Gitblit v1.9.2