/** * wangweipeng 2021/07/12 11:25 SWAG-C4P8CM * 每天更新 opd计划表的 if_Newest_HaveOpportunity__c 字段值 */ global class OPDIfNewestHaveOpportunityBatch implements Database.Batchable { public String query; private String errStr; private BatchIF_Log__c iflog; private final List TEST_ID = null; global OPDIfNewestHaveOpportunityBatch() { query = ''; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'OPDIfNewestHaveOpportunityBatch start1\n'; iflog.ErrorLog__c = ''; insert iflog; } global OPDIfNewestHaveOpportunityBatch(List testId) { query = ''; iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Is_Error__c = 0; iflog.Log__c = 'OPDIfNewestHaveOpportunityBatch start1\n'; iflog.ErrorLog__c = ''; insert iflog; TEST_ID = testId; } global Database.QueryLocator start(Database.BatchableContext bc) { Datetime startDate = Date.today(); Datetime endDate = Date.today().addDays(1); Date dateToday = Date.today(); Date yesterday = dateToday.addDays(-1); Integer year = yesterday.year(); Integer month = yesterday.month(); Integer buffer = Integer.valueOf(System.Label.update_If_Newest_HaveOpportunity); if (month < 4) { year -= 1; } String ocmYear = year + '年度'; query = 'SELECT id,Name,Status__c,OPD_Customers_Target__c,OPD_Customers_Target_Last__c,NoNSupplementaryApplication__c,ModelLending__c,PlanProdDetail__c,if_Newest_HaveOpportunity__c' + ' from OPDPlan__c' + ' WHERE PlanProdDetail__c != null' //计划出借备品信息 + ' AND OPD_Customers_Target__c != null '; //当期的客户-目标客户 if(buffer == 1){//更新一下2021/7/28 到 2020年11月1号的数据 query += ' AND Status__c != null ' + ' AND OPD_Customers_Target__r.OCM_Year__c in (\'2020年度\',\'2021年度\') ' + ' AND OPD_Customers_Target__r.CreatedDate >= 2020-11-01T00:01:01.000+0000 and OPD_Customers_Target__r.CreatedDate <= 2021-07-28T23:59:59.000+0000 '; }else{ query+= ' AND Status__c in (\'计划中\',\'取消\',\'完毕未报告\',\'完毕\')' + ' AND OPD_Customers_Target__r.OCM_Year__c = :ocmYear ' + ' AND OPD_Customers_Target__r.LastModifiedDate >= :startDate and OPD_Customers_Target__r.LastModifiedDate <= :endDate '; } if (TEST_ID <> null) { query += ' and Id IN: TEST_ID'; } iflog.Log__c += query+'\n'; update iflog; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { if(scope != null && scope.size() > 0){ List dataOpdList = scope; Integer FIELDMAX = 144; //去mapping表中找到名字为targetKeyProduct的一条数据 SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('targetKeyProduct'); //对应目标客户中的一系列 询价数 用来判断“是否有询价” List columnOppCnt = new List(); Map tempImportantOppCnt = new Map(); //获取到 客户-目标客户 表的询价的字段名 for (Integer i = 129; i <= FIELDMAX; i++) { String lpadI = ('00' + i).right(3); String fromColumn = 'From_Column_' + lpadI + '__c';//假设执行此时 值是 From_Column_115__c String apiStr = String.valueOf(mpdMapping.get(fromColumn)); //取字段fromColumn上存储的数据为Rental_Cnt_USG400__c if (String.isBlank(apiStr) == false) { String ssColumn = 'SS_Column_' + lpadI + '__c';//SS_Column_115__c String ssApiStr = String.valueOf(mpdMapping.get(ssColumn));//取得是 字段SS_Column_115__c 上的存储的数据 USG-400 if (i >= 129 && i < 144) { //询价数 1,0 columnOppCnt.add(apiStr); tempImportantOppCnt.put(apiStr, ssApiStr); } } } Map OpdTargetAcc = new Map(); for(Integer i = 0;i < dataOpdList.size() ;i++){ if(dataOpdList[i].PlanProdDetail__c != null){ OpdTargetAcc.put(dataOpdList[i].id,dataOpdList[i].OPD_Customers_Target__c); } } Map acctarList = new Map([select id, OPD_CV290__c, OPD_EUME2__c, OPD_290Miro__c, OPD_GFUCT260__c, OPD_OTVS300S400__c, OPD_CV170__c, OPD_190Main__c, OPD_ENG__c, OPD_TB__c, Sales_target_CV290__c, Sales_target_290Miro__c, Sales_target_EUME2__c, Sales_target_GFUCT260__c, Sales_target_OTVS300S400__c, Sales_target_CV170__c, Sales_target_190Main__c, Sales_target_ENG__c, Sales_target_TB__c, Opp_cnt_290MiroBF__c, Opp_cnt_290MiroGI__c, Opp_cnt_290MiroGI_CF__c, Opp_cnt_290MiroGI_Plus__c, Opp_cnt_CV170__c, Opp_cnt_CV190__c, Opp_cnt_CV290__c, Opp_cnt_ESG400__c, Opp_cnt_EUME2__c, Opp_cnt_GFUCT260__c, Opp_cnt_OTVS190__c, Opp_cnt_OTVS300__c, Opp_cnt_OTVS400__c, Opp_cnt_TB__c, Opp_cnt_USG400__c, Opp_cnt_190Main__c, Asset_Cnt_290MiroBF__c, Asset_Cnt_290MiroGI__c, Asset_Cnt_290MiroGI_CF__c, Asset_Cnt_290MiroGI_Plus__c, Asset_Cnt_CV170__c, Asset_Cnt_CV190__c, Asset_Cnt_CV290__c, Asset_Cnt_ESG400__c, Asset_Cnt_EUME2__c, Asset_Cnt_GFUCT260__c, Asset_Cnt_OTVS190__c, Asset_Cnt_OTVS300__c, Asset_Cnt_OTVS400__c, Asset_Cnt_TB__c, Asset_Cnt_USG400__c, Asset_Cnt_190Main__c, OPD_amount_290MiroBF__c, OPD_amount_290MiroGI__c, OPD_amount_290MiroGI_CF__c, OPD_amount_290MiroGI_Plus__c, OPD_amount_CV170__c, OPD_amount_CV190__c, OPD_amount_CV290__c, OPD_amount_ESG400__c, OPD_amount_EUME2__c, OPD_amount_GFUCT260__c, OPD_amount_OTVS190__c, OPD_amount_OTVS300__c, OPD_amount_OTVS400__c, OPD_amount_TB__c, OPD_amount_USG400__c, OPD_amount_190Main__c, Rental_Cnt_290MiroBF__c, Rental_Cnt_290MiroGI__c, Rental_Cnt_290MiroGI_CF__c, Rental_Cnt_290MiroGI_Plus__c, Rental_Cnt_CV170__c, Rental_Cnt_CV190__c, Rental_Cnt_CV290__c, Rental_Cnt_ESG400__c, Rental_Cnt_EUME2__c, Rental_Cnt_GFUCT260__c, Rental_Cnt_OTVS190__c, Rental_Cnt_OTVS300__c, Rental_Cnt_OTVS400__c, Rental_Cnt_TB__c, Rental_Cnt_USG400__c, Rental_Cnt_190Main__c from Account_Number_of_target__c where id in :OpdTargetAcc.values()]);//查询目标客户 本财年的 //修改对象 List ListUpdOpdplan = new List(); for(Integer i = 0;i < dataOpdList.size() ;i++){ OPDPlan__c opdplan = new OPDPlan__c(); opdplan.id = dataOpdList[i].id; Integer ifNewestHaveOpportunity; //计划出借备品信息 不为空 if(dataOpdList[i].PlanProdDetail__c != null){ if (acctarList.containskey(dataOpdList[i].OPD_Customers_Target__c)) { //不能去掉所有空格,因为有一个特殊得(OTV-S400 4K主机).replaceAll(' ', ''); //GI-290镜子*3; GI-290镜子CF*1; OTV-S400 4K主机*3; OTV-S190*2; TB*8 String ppd = dataOpdList[i].PlanProdDetail__c; //(GI-290镜子*3, GI-290镜子CF*1, OTV-S400 4K主机*3, OTV-S190*2, TB*8) String[] strs = ppd.split(';'); //控制是否跳出循环 boolean flag = false; for (Integer j = 0; j < strs.size(); j++) { String strss = strs[j].split('\\*')[0].trim(); if (columnOppCnt != null) { for (Integer o = 0; o < columnOppCnt.size(); o++) { //是否有询价数 String titleAccout2 = tempImportantOppCnt.get(columnOppCnt[o]); if (strss.indexOf(titleAccout2) != -1) { if (acctarList.get(dataOpdList[i].OPD_Customers_Target__c).get(columnOppCnt[o]) != null && acctarList.get(dataOpdList[i].OPD_Customers_Target__c).get(columnOppCnt[o]) != 0) { ifNewestHaveOpportunity = 1; flag = true; break; } } } } if(flag){ break; } } } } opdplan.if_Newest_HaveOpportunity__c = ifNewestHaveOpportunity; ListUpdOpdplan.add(opdplan); } Savepoint sp = Database.setSavepoint(); try{ update ListUpdOpdplan; }catch(exception e){ errStr = e.getMessage()+'\n'; errStr += e.getLineNumber()+'\n'; Database.rollback(sp); } } } global void finish(Database.BatchableContext BC) { String tmp = ''; if (String.isNotBlank(errStr)) { tmp = errStr; } iflog.Log__c += 'OPDIfNewestHaveOpportunityBatch finish()\n'; iflog.Log__c += '\nOPDIfNewestHaveOpportunityBatch end'; tmp += iflog.ErrorLog__c; if (tmp.length() > 60000) { tmp = tmp.substring(0, 60000); tmp += ' ...have more lines...'; iflog.ErrorLog__c = tmp; } String tmp2 = iflog.Log__c; if (tmp2.length() > 60000) { tmp2 = tmp2.substring(0, 60000); tmp2 += ' ...have more lines...'; iflog.Log__c = tmp2; } if (System.Label.Log_IO_Flag == 'Keep') { update iflog; } else if (System.Label.Log_IO_Flag == 'Auto') { if (iflog.Is_Error__c > 0) { update iflog; } } } }