From 928399eceec50e3d37ea08669a12789a9410a9d2 Mon Sep 17 00:00:00 2001
From: 沙世明 <shashiming@prec-tech.com>
Date: 星期二, 22 十一月 2022 16:51:16 +0800
Subject: [PATCH] 111

---
 force-app/main/default/classes/SumAnnualRepairAmountBatch.cls |   92 ++++++++++++++++++++++++++++++----------------
 1 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/force-app/main/default/classes/SumAnnualRepairAmountBatch.cls b/force-app/main/default/classes/SumAnnualRepairAmountBatch.cls
index 55a224a..885a432 100644
--- a/force-app/main/default/classes/SumAnnualRepairAmountBatch.cls
+++ b/force-app/main/default/classes/SumAnnualRepairAmountBatch.cls
@@ -35,13 +35,13 @@
         //鍒ゆ柇杩囧幓涓�骞存椂闂�
         if(nowDt.month() >= 4){
             sTime = Date.newInstance(nowDt.year()-1,4,1);
-            eTime = Date.newInstance(nowDt.year(),3,31);
+            eTime = Date.newInstance(nowDt.year(),4,1);
         }else{
             sTime = Date.newInstance(nowDt.year()-2,4,1);
-            eTime = Date.newInstance(nowDt.year()-1,3,31);
+            eTime = Date.newInstance(nowDt.year()-1,4,1);
         }
         //淇悊琛ㄦ煡璇� 淇悊鍗曠殑閲戦
-        query = 'select Id,Hospital__c from Repair__c where Agreed_Date__c >=:sTime and Agreed_Date__c <=:eTime and Discount_Price_formula__c !=null and Discount_Price_formula__c!=0 ';
+        query = 'select Id,Hospital__c,PurchaseOrInstallationDate__c,Repair_List_Price_formula__c from Repair__c where Agreed_Date__c >=:sTime and Agreed_Date__c <:eTime and Repair_List_Price_formula__c !=null and Repair_List_Price_formula__c!=0 and Status1__c!=\'0.鍒犻櫎\' and Status1__c!=\'0.鍙栨秷\' and Status2__c!=\'00.鍒犻櫎\' and Status2__c!=\'00.鍙栨秷\'  ';
 
         if (accountIdList != null && accountIdList.size() > 0) {
             query += ' AND Hospital__c IN :accountIdList ';
@@ -58,33 +58,55 @@
         }
         System.debug(LoggingLevel.INFO, '*** the OCSM_Period_half__c: ' + OCSM_Period_half);
 
-        List<Id>scopeId = new List<Id>();
+        // List<Id>scopeId = new List<Id>();
         List<Id>hosId = new List<Id>();
+        //姹囨�� 淇悊鍘熶环 瀹夎鏃ユ湡灏忎簬12涓湀鐨� 绠楀钩鍧囨湀 鍐�*12
+        Map<Id,Decimal> HosMoneys = new Map<Id,Decimal>();
         for (Repair__c mcc : scope) {
-            scopeId.add(mcc.Id);
+            // scopeId.add(mcc.Id);
             hosId.add(mcc.Hospital__c);
+            Date purDate = mcc.PurchaseOrInstallationDate__c;
+            if(purDate!=null){
+                Decimal bMonth = purDate.monthsBetween(eTime);
+                Decimal hmoney = 0;
+                if(bMonth < 0){
+                    bMonth *=-1;
+                }
+                //璁惧 瀹夎鏃ユ湡鑷� Etime 涓嶈冻涓夊勾鐨� 鎸夊钩鍧囨湀璁$畻鍐嶄箻12
+                if((bMonth < 12 * 3 ) && bMonth > 0){
+                hmoney = (mcc.Repair_List_Price_formula__c / bMonth) *12;
+                }else{
+                    hmoney = mcc.Repair_List_Price_formula__c;
+                }
+                if(HosMoneys.containsKey(mcc.Hospital__c)){
+                    HosMoneys.put(mcc.Hospital__c, HosMoneys.get(mcc.Hospital__c)+hmoney);
+                }else{
+                HosMoneys.put(mcc.Hospital__c, hmoney);
+                }
+            }
+           
         }
 
 
         //姹囨�讳慨鐞嗚〃涓尰闄� 淇悊閲戦
-        List<AggregateResult> LastyearList = [
-            select
-            sum(Discount_Price_formula__c) SumPrice,
-            Hospital__c
-            from
-            Repair__c
-            where
-            Id in:scopeId
-            group by Hospital__c
-        ];  
-        //瀛樺尰闄� 浠ュ強鍖婚櫌骞翠慨鐞嗛噾棰�
-        Map<Id,Decimal> LastYearPriceSumMap = new Map<Id,Decimal>();   
-        for (AggregateResult Rpc : LastyearList) {
-            Id idf        = String.valueOf(Rpc.get('Hospital__c'));
-            Decimal Defir = (Decimal)Rpc.get('SumPrice');
+        // List<AggregateResult> LastyearList = [
+        //     select
+        //     sum(Repair_List_Price_formula__c) SumPrice,
+        //     Hospital__c
+        //     from
+        //     Repair__c
+        //     where
+        //     Id in:scopeId
+        //     group by Hospital__c
+        // ];  
+        // //瀛樺尰闄� 浠ュ強鍖婚櫌骞翠慨鐞嗛噾棰�
+        // Map<Id,Decimal> LastYearPriceSumMap = new Map<Id,Decimal>();   
+        // for (AggregateResult Rpc : LastyearList) {
+        //     Id idf        = String.valueOf(Rpc.get('Hospital__c'));
+        //     Decimal Defir = (Decimal)Rpc.get('SumPrice');
 
-            LastYearPriceSumMap.put(idf, Defir);
-        } 
+        //     LastYearPriceSumMap.put(idf, Defir);
+        // } 
         
         //鏈嶅姟瀹㈡埛鐩爣瀵硅薄閲� 鍖婚櫌鍦╯cope閲屼互鍙婂勾浠界瓑浜庢煡璇㈠勾浠界殑
         List<Account_Service_Of_Target__c> asotList = [select Id,Account_HP__c 
@@ -106,17 +128,23 @@
             // }else{
             //     hospitalName.put(mc.Hospital__c,mc.Request_quotation_AmountF__c);
             // }
-            Account_Service_Of_Target__c ast = new Account_Service_Of_Target__c();
-            ast.Account_HP__c = mc.Hospital__c;
-            ast.Annual_repair_amount__c = LastYearPriceSumMap.get(mc.Hospital__c);
-            ast.OCSM_Period_half__c = OCSM_Period_half;
-            ast.OCSM_Period__c = OCSM_Period;
-            if(oldMap.containsKey(mc.Hospital__c)){
-                ast.Id = oldMap.get(mc.Hospital__c).Id;
+            try{
+                Account_Service_Of_Target__c ast = new Account_Service_Of_Target__c();
+                ast.Account_HP__c = mc.Hospital__c;
+                ast.Annual_repair_amount__c = HosMoneys.get(mc.Hospital__c);
+                ast.OCSM_Period_half__c = OCSM_Period_half;
+                ast.OCSM_Period__c = OCSM_Period;
+                if(oldMap.containsKey(mc.Hospital__c)){
+                    ast.Id = oldMap.get(mc.Hospital__c).Id;
+                }
+                if(!hospital.contains(ast)){
+                    hospital.add(ast); 
+                }      
+            }catch(Exception e){
+                System.debug(LoggingLevel.INFO, '*** mc.Id: ' + mc.Id);
+                System.debug(LoggingLevel.INFO, '*** mc: ' + mc);
             }
-            if(!hospital.contains(ast)){
-                hospital.add(ast); 
-            }                
+                      
         }
 
         upsert hospital;

--
Gitblit v1.9.1