global class RentalIsOPDBatch implements Database.Batchable { public String query; public String Id; //public Date thisYd = Date.valueOf('2020-4-1'); //public Date nextYd = Date.valueOf('2021-4-1'); public static Integer FIELDMAX = 200; global RentalIsOPDBatch() { this.query = query; } global RentalIsOPDBatch(String Id) { this.query = query; this.Id = Id; } global Database.QueryLocator start(Database.BatchableContext bc) { Date dateNow = Date.today(); Integer year = dateNow.year(); Integer month = dateNow.month(); if (month < 4) { year -= 1; } Integer nextYear = year+1; Date thisYd = Date.valueOf(year+'-4-1'); Date nextYd = Date.valueOf(nextYear+'-4-1'); query = 'SELECT Id, Name,Fixture_Set__c,Rental_Apply__c,Rental_Apply__r.Strategic_dept__c,Product2__c,IsOPD_Account__c'; query += ' FROM Rental_Apply_Equipment_Set__c '; query += 'WHERE Rental_Apply__r.Strategic_dept__c !=null and ( (Bollow_Date__c >= :thisYd' +' AND Bollow_Date__c < :nextYd'+')'; query += ' OR ( CreatedDate__c >= :thisYd' +' AND CreatedDate__c < :nextYd'+'))'; if(String.isNotBlank(this.Id)){ query += ' AND Id = \''+this.Id+'\''; } //System.debug('====000111:'+query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { List RAES_objs = new List(); RAES_objs = scope; //System.debug('====000:'+RAES_objs.size()+'===111:'+RAES_objs); List StrategicDeptId = new List();//备品借出申请的战略科室 List FixtureSetId = new List();//备品配套 List RAESId = new List();//借出备品配套一览 Map RAES_SMap = new Map();//借出备品配套一览+备品借出申请的战略科室 Map RAES_FMap = new Map();//借出备品配套一览+备品配套 Map> sdept_targetMap = new Map>();//备品借出申请的战略科室+目标 Map> RAES_targetMap = new Map>();//借出备品配套一览+目标 if(RAES_objs.size()>0){ for (Rental_Apply_Equipment_Set__c rAES_obj: RAES_objs) { RAESId.add(rAES_obj.Id); StrategicDeptId.add(rAES_obj.Rental_Apply__r.Strategic_dept__c); FixtureSetId.add(rAES_obj.Fixture_Set__c); RAES_SMap.put(rAES_obj.Id,rAES_obj.Rental_Apply__r.Strategic_dept__c); RAES_FMap.put(rAES_obj.Id,rAES_obj.Fixture_Set__c); } //客户这条线 // 目标客户重点产品和目标客户产品借出次数 List column = new List(); //List columnNum = new List(); SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('targetKeyProduct'); Map> tempImportantProduct = new Map>(); for (Integer i = 1; i <= FIELDMAX; i++) { String lpadI = ('00' + i).right(3); String fromColumn = 'From_Column_' + lpadI + '__c'; String apiStr = String.valueOf(mpdMapping.get(fromColumn)); if (String.isBlank(apiStr) == false) { String ssColumn = 'SS_Column_' + lpadI + '__c'; String ssApiStr = String.valueOf(mpdMapping.get(ssColumn)); if(i<101){ column.add(apiStr); List ssApiStrList = new List(ssApiStr.Split(',')); tempImportantProduct.put(apiStr,ssApiStrList); } } } //所在期数 Date dateNow = Date.today(); Integer year = dateNow.year(); Integer month = dateNow.month(); if (month < 4) { year -= 1; } String currentPeriod = String.valueOf(year - 1867 + 'P'); //战略科室 String StrategicDeptId_s = '(\''; for(Integer i = 0 ; i< StrategicDeptId.size();i++){ if(i antargetList = Database.query(soql); for(Account_Number_of_target__c ant:antargetList){ List titleAccout = new List(); for(Integer i=0;i keySet0 = RAES_SMap.keySet(); for(String ks0:keySet0){ RAES_targetMap.put(ks0,sdept_targetMap.get(RAES_SMap.get(ks0))); } //备品主体产品这条线 List fsDetail = [select Id,Fixture_Set__c, Product2__c from Fixture_Set_Detail__c where Is_Body__c=true and Fixture_Set__c in:FixtureSetId]; List Product2Id = new List(); Map map1 = new Map(); Map map2 = new Map(); Map map1_2 = new Map(); Map RAES_ImpMap = new Map(); //RAES_FMap<借出备品配套一览,备品配套> map1<备品配套,重点产品ID> for(Fixture_Set_Detail__c fs1:fsDetail){ map1.put(fs1.Fixture_Set__c,fs1.Product2__c); Product2Id.add(fs1.Product2__c); } //map2<重点产品ID,重点产品> List product2 = [select id,Key_product_147P__c from Product2 where Id in:Product2Id]; for(Product2 p:product2){ map2.put(p.Id,p.Key_product_147P__c); } //两个Map1与Map2合并 在与RAES_FMap合并得到 Map<借出备品配套一览,重点产品> Set keySet = map1.keySet(); for(String ks:keySet){ map1_2.put(ks,Map2.get(map1.get(ks))); } Set keySet1 = RAES_FMap.keySet(); for(String ks1:keySet1){ RAES_ImpMap.put(ks1,map1_2.get(RAES_FMap.get(ks1))); } //对比是否是重点考察产品 Map mapIdIs = new Map(); Set keySet2 = RAES_ImpMap.keySet(); for(String ks2:keySet2){ Integer tempIs = 0; if(RAES_targetMap.get(ks2) != null &&RAES_targetMap.get(ks2).size()>0){ if(String.isNotBlank(RAES_ImpMap.get(ks2))&&RAES_targetMap.get(ks2).contains(RAES_ImpMap.get(ks2).substring(3))){ tempIs = 1; } } mapIdIs.put(ks2,tempIs); } Set keySet3 = RAES_ImpMap.keySet(); //System.debug('111===333:'+keySet3.size()); for (Rental_Apply_Equipment_Set__c rAES_obj1: RAES_objs) { if(keySet3.contains(rAES_obj1.Id)){ rAES_obj1.IsOPD_Account__c = mapIdIs.get(rAES_obj1.Id); rAES_obj1.Product2__c = rAES_ImpMap.get(rAES_obj1.Id); } } update RAES_objs; } } global void finish(Database.BatchableContext BC) { } }