李金换
2022-03-28 9b197b7fac92278fb591ea8f4942c7d5687cb5ce
force-app/main/default/classes/NewQuoteEntrytheBatch.cls
@@ -8,7 +8,7 @@
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String quoteLN = '%01';
        query='select Id,Opportunity.Department_Class__c,CreatedDate,Category4__c from Quote where Bollow_Date__c like :quoteLN and CreatedDate=TODAY';
        query='select Id,Opportunity.Department_Class__c,CreatedDate,OpportunityId from Quote where Quote_No__c like :quoteLN and CreatedDate=TODAY';
        return Database.getQueryLocator(query);
    }
@@ -18,6 +18,7 @@
        List<Opportunity> opplist = New List<Opportunity>();
        Opportunity o=new Opportunity();
        List<Opportunity> os=new List<Opportunity>();
        System.debug('this is the point ------------------------------------------!');
        //遍历每一个报价单
        for(Quote oneQuote:qu){
           o=oneQuote.opportunity;
@@ -30,30 +31,47 @@
           }
           // 一年前的日期
           Date createdDateYear = createdDateStr.addYears(-1);
           System.debug('----------创建的时间-----------'+createdDateYear);
           //获取报价行信息
           list<QuoteLineItem> qli=[select id,Product2Id from QuoteLineItem where QuoteId=:oneQuote.id];
           list<QuoteLineItem> qli=[select id,Product2Id,Product2.Category4__c from QuoteLineItem where QuoteId=:oneQuote.id];
           List<String> c4=new List<String>();
           for(QuoteLineItem quit:qli){
             c4.add(quit.Product2.Category4__c);
           }
            System.debug('this is the point2 ------------------------------------------!');
            System.debug('报价行第四分类'+c4);
            System.debug('战略科室'+o.Department_Class__c);
           //获取明细
           List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select id, product__c, Bollow_Date__c, Rental_Apply__r.Hospital__c  from Rental_Apply_Equipment_Set_Detail__c
                where Rental_Apply__r.Strategic_dept__c = : o.Department_Class__c and Key_product__c != null and Bollow_Date__c >= :createdDateYear
                where Rental_Apply__r.Strategic_dept__c = : o.Department_Class__c and Bollow_Date__c >= :createdDateYear
                 and Category4__c In :c4 and Rental_Apply__r.demo_purpose2__c='试用(无询价)'
                order by Bollow_Date__c];
           System.debug('raesdList'+raesdList[0]);
           Map<String, Date> ProOfDate = new Map<String, Date>();
             for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
             ProOfDate.put(raesd.product__c, raesd.Bollow_Date__c);
           }
           System.debug('-----------proofdate---------:'+ProOfDate);
           o.WhetherTrySpareParts_3m__c = false;
           o.WhetherTrySpareParts_6m__c = false;
           o.WhetherTrySpareParts_1y__c = false;
           //获取一次询价中的每一个物品的询价
           list<OpportunityLineItem> ols=[select Id from OpportunityLineItem where OpportunityId =:o.id];
           for (OpportunityLineItem oppitem : ols) {
             if (ProOfDate.containsKey(((String)oppitem.Id__c).substring(0, 15))) {
               Date bollDate = ProOfDate.get(((String)oppitem.Id__c).substring(0, 15));
           //list<OpportunityLineItem> ols=[select Id__c from OpportunityLineItem where OpportunityId =:o.id];
           //System.debug('-----ols-------'+ols);
           //for (OpportunityLineItem oppitem : ols) {
             //if (ProOfDate.containsKey(oppitem.Id__c)) {
               for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
               System.debug('---------进入了目标循环---------------------------------');
               //Date bollDate = ProOfDate.get(oppitem.Id__c);
               Date bollDate = raesd.Bollow_Date__c;
               System.debug('---------time-------------:'+bollDate);
               System.debug('---------time2-------------:'+createdDateStr);
               if (bollDate != null && bollDate.addMonths(3) > createdDateStr) {
                 o.WhetherTrySpareParts_3m__c = true;
               }
@@ -63,15 +81,26 @@
               if (bollDate != null && bollDate.addYears(1) > createdDateStr && bollDate.addMonths(6) < createdDateStr) {
                 o.WhetherTrySpareParts_1y__c = true;
               }
               System.debug('o1:'+o.WhetherTrySpareParts_3m__c);
               System.debug('o2:'+o.WhetherTrySpareParts_6m__c);
               System.debug('o3:'+o.WhetherTrySpareParts_1y__c);
           }
             }
             //}
             os.add(o);
           }
           if(os.size()>0){
            update os;
           }
           //}
        }
        if(os.size()>0){
          try{
            update os;
              System.debug('update successed');
             }
          catch(DmlException e){
                System.debug('系统错误');
             }
        }else{
            System.debug('没有数据');
        }
    }
    global void finish(Database.BatchableContext BC) {