From 2f3267bcc0e42e09b6f191cad6a0374f4a59b317 Mon Sep 17 00:00:00 2001
From: Denny chen <chenbangcai@prec-tech.com>
Date: 星期二, 10 五月 2022 17:45:34 +0800
Subject: [PATCH] InquiryVerifyBatch update

---
 force-app/main/default/classes/InquiryVerifyBatch.cls |  109 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 62 insertions(+), 47 deletions(-)

diff --git a/force-app/main/default/classes/InquiryVerifyBatch.cls b/force-app/main/default/classes/InquiryVerifyBatch.cls
index 335c567..c309fad 100644
--- a/force-app/main/default/classes/InquiryVerifyBatch.cls
+++ b/force-app/main/default/classes/InquiryVerifyBatch.cls
@@ -1,41 +1,67 @@
 global class InquiryVerifyBatch  implements Schedulable,Database.Batchable<SObject>,Database.Stateful {
     public String query;
-
     public Date start_date;
-
+    public Integer year;
+    public Integer month;
     global InquiryVerifyBatch() {
-        // if(Test.isRunningTest()){
-            this.query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No_last2__c = \'01\' and CreatedDate = today';
-        // }else{
-            // this.query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No__c like \'%01\' and CreatedDate = today';
-        // }
-        
+        this.query = query;        
     }
 
     global InquiryVerifyBatch(Integer year, Integer month) {
         if (year == null) {
-            year = 2021;
+            this.year = 2021;
         }
         if (month == null) {
-            month = 4;
+            this.month = 4;
         }
         start_date = Date.newInstance(year, month, 1);
-
-        this.query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No_last2__c = \'01\' and CreatedDate >= :start_date';
+        this.query = query;
     }
-
     public void execute(SchedulableContext sc) {
         Database.executeBatch(this);
     }
-    // global void execute(SchedulableContext sc){Database.executebatch(this);}
     global Database.QueryLocator start(Database.BatchableContext bc) {
         // 鑾峰彇鎶ヤ环鍗曟暟鎹�
+        
+        
+        if (start_date!=null) {
+           query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No_last2__c = \'01\' and CreatedDate >= :start_date';
+        }else{
+           query = 'select id,quoteSavedDate__c,Opportunity.Id,Opportunity.Department_Class__c from quote where Quote_No_last2__c = \'01\' and CreatedDate = today';
+        }
+        
+        System.debug(LoggingLevel.INFO, '*** query: ' + query);
         return Database.getQueryLocator(query);
     }
  
     global void execute(Database.BatchableContext BC, list<quote> quotes) {
             
        List<Opportunity> os = new List<Opportunity>();
+       List<String> qids = new List<String>();
+       List<String> oppDepartmentList = new List<String>();
+       List<String> ca4 = new List<String>();
+       for (quote quo : quotes) {
+            qids.add(quo.id);
+            oppDepartmentList.add(quo.Opportunity.Department_Class__c);
+        }
+        Map<String,List<String>> mapc4 = new Map<String,List<String>>();
+       for(List<QuoteLineItem> qlt:[Select Id,Product2.Category4__c,QuoteId 
+                            from QuoteLineItem 
+                            where QuoteId  In :qids and Product2.Important_product__c = true]){
+       
+        for ( QuoteLineItem qlt1:qlt ) {
+            ca4.add(qlt1.Product2.Category4__c);
+        }
+        if (!mapc4.containskey(qlt[0].QuoteId)) {
+            mapc4.put(qlt[0].QuoteId,ca4);
+        } 
+       }
+       List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select id, product__c, Bollow_Date__c,Category4__c, Rental_Apply__r.Hospital__c,Rental_Apply__r.Strategic_dept__c 
+                                                                from Rental_Apply_Equipment_Set_Detail__c 
+                                                                where Rental_Apply__r.Demo_purpose2__c = '璇曠敤锛堟棤璇环锛�'
+                                                                AND Rental_Apply__r.Strategic_dept__c IN : oppDepartmentList 
+                                                                and Category4__c IN :ca4 
+                                                                order by Bollow_Date__c];
        // 閬嶅巻鎶ヤ环鍗�
         for (quote  quo: quotes) {
             Opportunity o = New Opportunity();
@@ -47,48 +73,37 @@
             }else{
                 createdDateStr = Date.today();
             }
-            // 鑾峰彇鎶ヤ环浜у搧鏁版嵁锛岃幏鍙栫鍥涘垎绫�
-             List<QuoteLineItem> qlts = [Select Id,Product2.Category4__c from QuoteLineItem where QuoteId  = :quo.Id and Product2.Important_product__c = true];
-             List<String> c4  = new List<String>();
-             List<String> qdept  = new List<String>();
-             // 绗洓鍒嗙被鏀惧叆list
-            for (QuoteLineItem qlt : qlts) {
-                c4.add(qlt.Product2.Category4__c);
-            }
+           
             
               // 涓�骞村墠鐨勬棩鏈�
             Date createdDateYear = createdDateStr.addYears(-1);
 
-            // 鑾峰彇澶囧搧閰嶅涓�瑙堟槑缁�
-            List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select id, product__c, Bollow_Date__c,Category4__c, Rental_Apply__r.Hospital__c 
-                                                                from Rental_Apply_Equipment_Set_Detail__c 
-                                                                where Rental_Apply__r.Strategic_dept__c = : quo.Opportunity.Department_Class__c 
-                                                                and Category4__c IN :c4 
-                                                                and Rental_Apply__r.Demo_purpose2__c = '璇曠敤锛堟棤璇环锛�'
-                                                                and Bollow_Date__c >= :createdDateYear order by Bollow_Date__c limit 200 ];
-
-
-              for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
-                o.WhetherTrySpareParts_3m__c = false;
-                o.WhetherTrySpareParts_6m__c = false;
-                o.WhetherTrySpareParts_1y__c = false;
-                Date bollDate = raesd.Bollow_Date__c;
-                if (bollDate != null && bollDate.addMonths(3) > createdDateStr) {
-                    o.WhetherTrySpareParts_3m__c = true;
-                }
-                if (bollDate != null && bollDate.addMonths(6) > createdDateStr && bollDate.addMonths(3) < createdDateStr) {
-                    o.WhetherTrySpareParts_6m__c = true;
-                }
-                if (bollDate != null && bollDate.addYears(1) > createdDateStr && bollDate.addMonths(6) < createdDateStr) {
-                    o.WhetherTrySpareParts_1y__c = true;
-                }
-              }
+           for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {   
+                if (raesd.Rental_Apply__r.Strategic_dept__c == quo.Opportunity.Department_Class__c && mapc4.containskey(quo.id) && mapc4.get(quo.id).contains(raesd.Category4__c) && raesd.Bollow_Date__c >= createdDateYear) {
+                    o.WhetherTrySpareParts_3m__c = false;
+                    o.WhetherTrySpareParts_6m__c = false;
+                    o.WhetherTrySpareParts_1y__c = false;
+                    Date bollDate = raesd.Bollow_Date__c;
+                    if (bollDate != null && bollDate.addMonths(3) > createdDateStr) {
+                        o.WhetherTrySpareParts_3m__c = true;
+                    }
+                    if (bollDate != null && bollDate.addMonths(6) > createdDateStr && bollDate.addMonths(3) < createdDateStr) {
+                        o.WhetherTrySpareParts_6m__c = true;
+                    }
+                    if (bollDate != null && bollDate.addYears(1) > createdDateStr && bollDate.addMonths(6) < createdDateStr) {
+                        o.WhetherTrySpareParts_1y__c = true;
+                    }              
+                }                            
+            }
+              
             os.add(o);
         }
         if (os.size()>0) {
+            Oly_TriggerHandler.bypass('AWSServiceTool2');
+            Oly_TriggerHandler.bypass('PIHelper');
             update os;
             system.debug('鎵ц鎴愬姛');
-        }else{
+        }else{  
              system.debug('鎵ц澶辫触');
         }
     }

--
Gitblit v1.9.1