global class NewQuoteEntrytheBatch implements Database.Batchable { public String query; global NewQuoteEntrytheBatch() { this.query = query; } 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'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list qu) { Date createdDateStr; //获取已经生成的询价单 List opplist = New List(); Opportunity o=new Opportunity(); List os=new List(); //遍历每一个报价单 for(Quote oneQuote:qu){ o=oneQuote.opportunity; //获取报价时间 if (oneQuote.id != null && oneQuote.CreatedDate != null ) { // createdDateStr = [select id,CreatedDate from Quote where id = :quoId][0].CreatedDate.date(); createdDateStr = oneQuote.CreatedDate.date(); } else { createdDateStr = Date.today(); } // 一年前的日期 Date createdDateYear = createdDateStr.addYears(-1); //获取报价行信息 list qli=[select id,Product2Id from QuoteLineItem where QuoteId=:oneQuote.id]; List c4=new List(); for(QuoteLineItem quit:qli){ c4.add(quit.Product2.Category4__c); } //获取明细 List 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 and Category4__c In :c4 and Rental_Apply__r.demo_purpose2__c='试用(无询价)' order by Bollow_Date__c]; Map ProOfDate = new Map(); for (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) { ProOfDate.put(raesd.product__c, raesd.Bollow_Date__c); } o.WhetherTrySpareParts_3m__c = false; o.WhetherTrySpareParts_6m__c = false; o.WhetherTrySpareParts_1y__c = false; //获取一次询价中的每一个物品的询价 list 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)); 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){ update os; } } } global void finish(Database.BatchableContext BC) { } }