global class NewQuoteEntrytheBatch implements Database.Batchable<sObject> {
|
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,OpportunityId from Quote where Quote_No__c like :quoteLN and CreatedDate=TODAY';
|
return Database.getQueryLocator(query);
|
}
|
|
global void execute(Database.BatchableContext BC, list<Quote> qu) {
|
Date createdDateStr;
|
//获取已经生成的询价单
|
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;
|
//获取报价时间
|
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);
|
System.debug('----------创建的时间-----------'+createdDateYear);
|
//获取报价行信息
|
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 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__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;
|
}
|
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;
|
}
|
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){
|
try{
|
update os;
|
System.debug('update successed');
|
}
|
catch(DmlException e){
|
System.debug('系统错误');
|
}
|
}else{
|
System.debug('没有数据');
|
}
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
|
}
|
}
|