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/EquipmentRepairBatch.cls |  187 +++++++++++++++++++++++++++++++---------------
 1 files changed, 124 insertions(+), 63 deletions(-)

diff --git a/force-app/main/default/classes/EquipmentRepairBatch.cls b/force-app/main/default/classes/EquipmentRepairBatch.cls
index 26e6492..38892d3 100644
--- a/force-app/main/default/classes/EquipmentRepairBatch.cls
+++ b/force-app/main/default/classes/EquipmentRepairBatch.cls
@@ -9,11 +9,11 @@
     public String query;
     public List < String > accountIdList;
     private BatchIF_Log__c iflog;
-    public Date td = Date.today();
+    public Date td = Date.today();  //濡�2022.10.15
     public String OCSM_Period_half;
     public String OCSM_Period;
-    public Date sTime;
-    public Date eTime;
+    public Date eTime=td.toStartOfMonth(); //濡� 2022.10.1
+    public Date sTime=eTime.addYears(-3);  // 2019.10.1
 
     global EquipmentRepairBatch() {
         this.query = query;
@@ -39,14 +39,14 @@
             OCSM_Period_half = '2H';
         }
 
-        if(td.month() >= 4){
-            sTime = Date.newInstance(td.year()-3,4,1);
-            eTime = Date.newInstance(td.year(),3,31);
+        // if(td.month() >= 4){
+        //     sTime = Date.newInstance(td.year()-3,4,1);
+        //     eTime = Date.newInstance(td.year(),3,31);
             
-        }else{
-            sTime = Date.newInstance(td.year()-4,4,1);
-            eTime = Date.newInstance(td.year()-1,3,31);
-        }
+        // }else{
+        //     sTime = Date.newInstance(td.year()-4,4,1);
+        //     eTime = Date.newInstance(td.year()-1,3,31);
+        // }
         // query ='select Id,Hospital__c,Product2.ServiceCategory__c from Asset where Id not in (Select Asset__c from Maintenance_Contract_Asset__c) and Id in (select Delivered_Product__c from Repair__c)';//娌℃湁缁翠慨鍚堝悓鐨勬暟鎹�
         // query += 'and IF_Coverage_Target_Asset_F__c=1';
         //涓嶅啀鏌ヨ鏄惁鏈夌淮淇悎鍚�
@@ -112,47 +112,90 @@
 
         //鏈�鍚巙psert瀹㈡埛鏈嶅姟鐩爣瀵硅薄 list鐩爣
         List<Account_Service_Of_Target__c> asslist = new List<Account_Service_Of_Target__c>();
-        //amaplist
+        //淇濇湁璁惧涓哄崟浣� 杩囧幓涓夊勾缁翠慨瀹炵哗
+        Map<Id,Decimal> ThreeYearPriceSumMap = new Map<Id,Decimal>();
+
+        //杩囧幓涓夊勾缁翠慨瀹為檯 2022/10/13 淇敼 涓哄綋鍓嶆椂闂寸殑涓婁竴涓湀鏈�鍚庝竴澶� 寰�鍓嶆帹涓夊勾 
+        for(Repair__c rp :[SELECT Hospital__c,PurchaseOrInstallationDate__c,Repair_List_Price_formula__c,Delivered_Product__c 
+                           FROM Repair__c WHERE Delivered_Product__c in:mids
+                           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.鍙栨秷'
+                           AND Agreed_Date__c >=:sTime 
+                           AND Agreed_Date__c <:eTime 
+                           ]){
+            Date purDate = rp.PurchaseOrInstallationDate__c;
+            if(purDate!=null){
+                Decimal bDay = purDate.daysBetween(eTime);
+                Decimal hmoney = 0;
+                if(bDay < 0){
+                    bDay *=-1;
+                }
+                //璁惧骞撮緞涓嶈冻涓夊勾鐨� 鎸夊钩鍧囨瘡澶╄绠� 鍐嶄箻 365 鍐嶄箻 3
+                if((bDay < 365*3) && bDay>0){
+                    hmoney = (rp.Repair_List_Price_formula__c / bDay) *365*3;
+                }else{
+                    hmoney = rp.Repair_List_Price_formula__c / 3;
+                }
+                if(ThreeYearPriceSumMap.containsKey(rp.Delivered_Product__c)){
+                    ThreeYearPriceSumMap.put(rp.Delivered_Product__c, ThreeYearPriceSumMap.get(rp.Delivered_Product__c)+hmoney);
+                }else{
+                    ThreeYearPriceSumMap.put(rp.Delivered_Product__c, hmoney);
+                }
+
+            }
+            
+        }
         
 
         //淇敼鏃堕棿涓鸿繃鍘讳笁涓畬鎴愯储骞�
         
         // Date LastThirdYearDate = td.addYears(-3);
         // System.debug(LoggingLevel.INFO, '*** LastThirdYearDate: ' + LastThirdYearDate);
-        List<AggregateResult> ThreeyearList = [
-            select
-            sum(Discount_Price_formula__c) SumPrice,
-            sum(Repair_Quotation_Id__r.sales_discount__c) sales_discount,
-            sum(Repair_Quotation_Id__r.Contract_target__c) contract_target,
-            sum(Repair_Quotation_Id__r.Loaner_repair__c) loaner_repair,
-            sum(Repair_Quotation_Id__r.long_term_insurance__c) long_term_insurance,
-            sum(Repair_Quotation_Id__r.Set_discount__c) set_discount,
-            sum(Repair_Quotation_Id__r.Servince_contract_discount_amount__c) sercince,
-            sum(Repair_Quotation_Id__r.long_term_insurance_MD__c) long_term_insuranceMD,
-            sum(Repair_Quotation_Id__r.Delivery_compensation__c) delivery,
-            sum(Repair_Quotation_Id__r.Other_discount__c) other,
-            AVG(Delivered_Product__r.Last_Years_Repair_Month__c) threeYearM,
-            Delivered_Product__c
-            from
-            Repair__c
-            where
-            Delivered_Product__c in:mids
-            and Agreed_Date__c != null
-            and Agreed_Date__c <= :eTime
-            and Agreed_Date__c >= :sTime
-            group by Delivered_Product__c
-        ];       
-        Map<Id, Decimal> ThreeYearPriceSumMap = new Map<id, Decimal>();
-        Map<Id, Decimal> ThiYearMonthMap = new Map<id, Decimal>();
+        // List<AggregateResult> ThreeyearList = [
+        //     select
+        //     sum(Repair_List_Price_formula__c) SumPrice,
+        //     sum(Repair_Quotation_Id__r.sales_discount__c) sales_discount,
+        //     sum(Repair_Quotation_Id__r.Contract_target__c) contract_target,
+        //     sum(Repair_Quotation_Id__r.Loaner_repair__c) loaner_repair,
+        //     sum(Repair_Quotation_Id__r.long_term_insurance__c) long_term_insurance,
+        //     sum(Repair_Quotation_Id__r.Set_discount__c) set_discount,
+        //     sum(Repair_Quotation_Id__r.Servince_contract_discount_amount__c) sercince,
+        //     sum(Repair_Quotation_Id__r.long_term_insurance_MD__c) long_term_insuranceMD,
+        //     sum(Repair_Quotation_Id__r.Delivery_compensation__c) delivery,
+        //     sum(Repair_Quotation_Id__r.Other_discount__c) other,
+        //     AVG(Delivered_Product__r.Last_Years_Repair_Month__c) threeYearM,
+        //     Delivered_Product__c
+        //     from
+        //     Repair__c
+        //     where
+        //     Delivered_Product__c in:mids
+        //     and Agreed_Date__c != null
+        //     and Agreed_Date__c <= :eTime
+        //     and Agreed_Date__c >= :sTime
+        //     and Repair_List_Price_formula__c!=0
+        //     and Repair_List_Price_formula__c!=null
+        //     and Status1__c!='0.鍒犻櫎' 
+        //     and Status1__c!='0.鍙栨秷' 
+        //     and Status2__c!='00.鍒犻櫎' 
+        //     and Status2__c!='00.鍙栨秷'
+        //     group by Delivered_Product__c
+        // ];       
+        // Map<Id, Decimal> ThreeYearPriceSumMap = new Map<id, Decimal>();
+        // Map<Id, Decimal> ThiYearMonthMap = new Map<id, Decimal>();
 
-        for (AggregateResult Rpc : ThreeyearList) {
-            id idf        = String.valueOf(Rpc.get('Delivered_Product__c'));
-            //Decimal Defir = decimal.valueOf(Rpc.get('SumPrice')+'');
-            Decimal threeYearM = decimal.valueOf(Rpc.get('threeYearM') + '');
-            Decimal Defir = sumPrice1(Rpc);
-            ThreeYearPriceSumMap.put(idf, Defir);
-            ThiYearMonthMap.put(idf, threeYearM);
-        }
+        // System.debug(LoggingLevel.INFO, '*** ThreeyearList: ' + ThreeyearList);
+        // for (AggregateResult Rpc : ThreeyearList) {
+        //     id idf        = String.valueOf(Rpc.get('Delivered_Product__c'));
+        //     //Decimal Defir = decimal.valueOf(Rpc.get('SumPrice')+'');
+        //     Decimal threeYearM = decimal.valueOf(Rpc.get('threeYearM') + '');
+        //     Decimal Defir = sumPrice1(Rpc);
+        //     ThreeYearPriceSumMap.put(idf, Defir);
+        //     ThiYearMonthMap.put(idf, threeYearM);
+        // }
 
         //浠ュ尰闄负瀵硅薄瀛樺偍鍖婚櫌鎵�鏈変繚鏈夎澶嘗ist
         Map<Id,List<Asset>> aMapLists =  new Map<Id,List<Asset>>();  
@@ -182,7 +225,7 @@
                 }         
             }
         }
-        // update resultList;
+        update resultList;
         System.debug(LoggingLevel.INFO, '*** updated resultList: ' + resultList);
         for (Id mapId : aMapLists.keySet()) {
 
@@ -209,6 +252,11 @@
 
             for (Asset asset : aMapLists.get(mapId) ) {
                 sumThreePrice += asset.Three_Years_Repair_Cost_Text__c;
+                System.debug(LoggingLevel.INFO, '*** sumThreePrice: ' + sumThreePrice);
+
+                System.debug(LoggingLevel.INFO, '*** asset.Product2.ServiceCategory__c: ' + asset.Product2.ServiceCategory__c);
+                System.debug(LoggingLevel.INFO, '*** asset.Is_Has_Contract_History__c: ' + asset.Is_Has_Contract_History__c);
+
                 //鍒嗗埆缁熻杞�佺‖銆佸懆杈� 1.瀹氫环鎬昏  2.锛堟湭鍙備繚锛夎繃鍘讳笁骞村钩鍧囩淮淇疄缁� 3.鏈弬淇濇暟閲�
                 if(asset.Product2.ServiceCategory__c!=null){
                     if(asset.Product2.ServiceCategory__c == '杞�ч暅'){
@@ -235,6 +283,10 @@
                     }
                 }      
             }
+            System.debug(LoggingLevel.INFO, '*** 鍛ㄨ竟鏈弬淇濋噾棰�: ' + periE);
+            System.debug(LoggingLevel.INFO, '*** 纭� 鏈弬淇濋噾棰�: ' + hardE);
+            System.debug(LoggingLevel.INFO, '*** 杞� 鏈弬淇濋噾棰�: ' + softE);
+
             Account_Service_Of_Target__c astItem = new Account_Service_Of_Target__c();
             if(targetMap.containsKey(mapId)){
                 astItem = targetMap.get(mapId);  
@@ -255,10 +307,19 @@
             astItem.Uninsured_Quantity_Lightsource__c = periCount;
             asslist.add(astItem);
             System.debug(LoggingLevel.INFO, '*** 杞�ч暅鎬讳釜鏁�: ' + softCountall);
+            System.debug(LoggingLevel.INFO, '*** 杞�ч暅鏈弬淇濅釜鏁�: ' + astItem.Uninsured_Quantity_Soft_Mirror__c);
+            System.debug(LoggingLevel.INFO, '*** :astItem.Three_Years_Repair_Cost_Soft_Mirror__c ' + astItem.Three_Years_Repair_Cost_Soft_Mirror__c);
+
 
         }
-        update resultList;
-        upsert asslist;       
+        // System.debug(LoggingLevel.INFO, '*** asslist: ' + asslist);
+        // update resultList;
+        upsert asslist;
+        // update asslist;   
+
+        System.debug(LoggingLevel.INFO, '***鏇存柊鍚� asslist: ' + asslist);
+
+
     }
 
     global void finish(Database.BatchableContext BC) {
@@ -273,22 +334,22 @@
 
     }
 
-    private static Decimal sumPrice1(AggregateResult rpc) {
-        Decimal SumPrice = Decimal.valueOf(rpc.get('SumPrice') + '');
-        Decimal sales_discount = Decimal.valueOf((rpc.get('sales_discount') == null ? 0 : rpc.get('sales_discount')) + '') * -1;
-        Decimal contract_target = Decimal.valueOf((rpc.get('contract_target') == null ? 0 : rpc.get('contract_target')) + '') * -1;
-        Decimal loaner_repair = Decimal.valueOf((rpc.get('loaner_repair') == null ? 0 : rpc.get('loaner_repair')) + '') * -1;
-        Decimal long_term_insurance = Decimal.valueOf((rpc.get('long_term_insurance') == null ? 0 : rpc.get('long_term_insurance')) + '') * -1;
-        Decimal set_discount = Decimal.valueOf((rpc.get('set_discount') == null ? 0 : rpc.get('set_discount')) + '') * -1;
-        Decimal sercince = Decimal.valueOf((rpc.get('sercince') == null ? 0 : rpc.get('sercince')) + '') * -1;
-        Decimal long_term_insuranceMD = Decimal.valueOf((rpc.get('long_term_insuranceMD') == null ? 0 : rpc.get('long_term_insuranceMD')) + '') * -1;
-        Decimal delivery = Decimal.valueOf((rpc.get('delivery') == null ? 0 : rpc.get('delivery')) + '') * -1;
-        Decimal other = Decimal.valueOf((rpc.get('other') == null ? 0 : rpc.get('other')) + '') * -1;
-        system.debug(rpc.get('SumPrice') + '--' + rpc.get('sales_discount') + '--' + rpc.get('contract_target') + '--' +
-                     rpc.get('loaner_repair') + '--' + rpc.get('long_term_insurance') + '--' + rpc.get('set_discount') + '--' + rpc.get('long_term_insuranceMD')
-                     + '--' + rpc.get('delivery') + '--' + rpc.get('other') + '--');
-        return SumPrice + sales_discount + contract_target + loaner_repair + long_term_insurance + sercince + set_discount + long_term_insuranceMD + delivery + other;
-    }
+    // private static Decimal sumPrice1(AggregateResult rpc) {
+    //     Decimal SumPrice = Decimal.valueOf(rpc.get('SumPrice') + '');
+    //     Decimal sales_discount = Decimal.valueOf((rpc.get('sales_discount') == null ? 0 : rpc.get('sales_discount')) + '') * -1;
+    //     Decimal contract_target = Decimal.valueOf((rpc.get('contract_target') == null ? 0 : rpc.get('contract_target')) + '') * -1;
+    //     Decimal loaner_repair = Decimal.valueOf((rpc.get('loaner_repair') == null ? 0 : rpc.get('loaner_repair')) + '') * -1;
+    //     Decimal long_term_insurance = Decimal.valueOf((rpc.get('long_term_insurance') == null ? 0 : rpc.get('long_term_insurance')) + '') * -1;
+    //     Decimal set_discount = Decimal.valueOf((rpc.get('set_discount') == null ? 0 : rpc.get('set_discount')) + '') * -1;
+    //     Decimal sercince = Decimal.valueOf((rpc.get('sercince') == null ? 0 : rpc.get('sercince')) + '') * -1;
+    //     Decimal long_term_insuranceMD = Decimal.valueOf((rpc.get('long_term_insuranceMD') == null ? 0 : rpc.get('long_term_insuranceMD')) + '') * -1;
+    //     Decimal delivery = Decimal.valueOf((rpc.get('delivery') == null ? 0 : rpc.get('delivery')) + '') * -1;
+    //     Decimal other = Decimal.valueOf((rpc.get('other') == null ? 0 : rpc.get('other')) + '') * -1;
+    //     system.debug(rpc.get('SumPrice') + '--' + rpc.get('sales_discount') + '--' + rpc.get('contract_target') + '--' +
+    //                  rpc.get('loaner_repair') + '--' + rpc.get('long_term_insurance') + '--' + rpc.get('set_discount') + '--' + rpc.get('long_term_insuranceMD')
+    //                  + '--' + rpc.get('delivery') + '--' + rpc.get('other') + '--');
+    //     return SumPrice + sales_discount + contract_target + loaner_repair + long_term_insurance + sercince + set_discount + long_term_insuranceMD + delivery + other;
+    // }
 
 
 }
\ No newline at end of file

--
Gitblit v1.9.1