global class RentalIsOPDBatch implements Database.Batchable<sObject> {
|
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<Sobject> scope) {
|
List<Rental_Apply_Equipment_Set__c> RAES_objs = new List<Rental_Apply_Equipment_Set__c>();
|
RAES_objs = scope;
|
//System.debug('====000:'+RAES_objs.size()+'===111:'+RAES_objs);
|
List<String> StrategicDeptId = new List<String>();//备品借出申请的战略科室
|
List<String> FixtureSetId = new List<String>();//备品配套
|
List<String> RAESId = new List<String>();//借出备品配套一览
|
Map<String, String> RAES_SMap = new Map<String, String>();//借出备品配套一览+备品借出申请的战略科室
|
Map<String, String> RAES_FMap = new Map<String, String>();//借出备品配套一览+备品配套
|
Map<String, List<String>> sdept_targetMap = new Map<String, List<String>>();//备品借出申请的战略科室+目标
|
Map<String, List<String>> RAES_targetMap = new Map<String, List<String>>();//借出备品配套一览+目标
|
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<String> column = new List<String>();
|
//List<String> columnNum = new List<String>();
|
SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('targetKeyProduct');
|
Map<String,List<String>> tempImportantProduct = new Map<String,List<String>>();
|
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<String> ssApiStrList = new List<String>(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<StrategicDeptId.size()-1){
|
StrategicDeptId_s += StrategicDeptId[i]+'\',\'';
|
}else{
|
StrategicDeptId_s += StrategicDeptId[i]+'\')';
|
}
|
}
|
String soql = 'select Id ,Name,Account__c ';
|
for (Integer i=0;i<column.size();i++) {
|
soql += ',' + column[i];
|
}
|
/*for (Integer num=0;num<columnNum.size();num++) {
|
soql += ',' + columnNum[num];
|
}*/
|
soql += ' FROM Account_Number_of_target__c WHERE OCM_Period__c = \''+currentPeriod+'\' and Account__c in '+StrategicDeptId_s;
|
List<Account_Number_of_target__c> antargetList = Database.query(soql);
|
for(Account_Number_of_target__c ant:antargetList){
|
List<String> titleAccout = new List<String>();
|
for(Integer i=0;i<column.size();i++){
|
if(1==ant.get(column[i])){
|
titleAccout.addAll(tempImportantProduct.get(column[i]));
|
}
|
}
|
sdept_targetMap.put(ant.Account__c,titleAccout);
|
}
|
//两个Map合并 借出备品配套一览+备品借出申请的战略科室 与 备品借出申请的战略科室+目标=借出备品配套一览+目标
|
Set<String> keySet0 = RAES_SMap.keySet();
|
for(String ks0:keySet0){
|
RAES_targetMap.put(ks0,sdept_targetMap.get(RAES_SMap.get(ks0)));
|
}
|
//备品主体产品这条线
|
List<Fixture_Set_Detail__c> 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<String> Product2Id = new List<String>();
|
Map<String, String> map1 = new Map<String, String>();
|
Map<String, String> map2 = new Map<String, String>();
|
Map<String, String> map1_2 = new Map<String, String>();
|
Map<String, String> RAES_ImpMap = new Map<String, String>();
|
//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> 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<String> keySet = map1.keySet();
|
for(String ks:keySet){
|
map1_2.put(ks,Map2.get(map1.get(ks)));
|
}
|
Set<String> keySet1 = RAES_FMap.keySet();
|
for(String ks1:keySet1){
|
RAES_ImpMap.put(ks1,map1_2.get(RAES_FMap.get(ks1)));
|
}
|
//对比是否是重点考察产品
|
Map<String, Integer> mapIdIs = new Map<String, Integer>();
|
Set<String> 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<String> 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) {
|
|
}
|
}
|