global without sharing class SyncProvinceWIndowToSignForm implements Database.Batchable, Database.Stateful { private Set changedWindowProvince { get; set; } private final string JCKStr ='集采课'; private static Map provinceManagementMap{set;get;} public static Map getManagementProvince(){ if(provinceManagementMap !=null && provinceManagementMap.keySet().size()>0 ){ return provinceManagementMap; } provinceManagementMap = new Map(); List ompList = [select id, Name, SalesManage__c,GI_assistant__c,SP_assistant__c from OCM_Management_Province__c]; for (OCM_Management_Province__c omp: ompList) { provinceManagementMap.put(omp.Name,omp); } return provinceManagementMap; } global SyncProvinceWIndowToSignForm(Set changedWindowProvince) { this.changedWindowProvince = changedWindowProvince; } global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'select Sales_assistant_name_text__c,isProcessed__c,OCM_man_province_cus__c,Statu_Achievements__r.Opportunity__r.Opportunity_Category__c,Group_purchase_PCL__c from eSignForm__c '; if (changedWindowProvince != null && changedWindowProvince.size() > 0) { if (!Test.isRunningTest()) { query += ' where isProcessed__c = false and (OCM_man_province_cus__c = :changedWindowProvince or Group_purchase_PCL__c = true)'; } system.debug('SOQL:' + query); return Database.getQueryLocator(query); } return null; } global void execute(Database.BatchableContext BC, List signFormList) { system.debug('sign form list size:' + signFormList.size()); // 取得OCM管理省信息 Map provinceMap = getManagementProvince(); // 判断需要更新的签收单 List updList = new List(); if (signFormList != null && signFormList.size() > 0) { for (eSignForm__c temp : signFormList) { String oppType = String.isBlank(temp.Statu_Achievements__r.Opportunity__r.Opportunity_Category__c)?'':temp.Statu_Achievements__r.Opportunity__r.Opportunity_Category__c; system.debug('Opportunity Type:'+ oppType); String provinceName = Test.isRunningTest() ? '北京市' : (temp.Group_purchase_PCL__c?JCKStr:temp.OCM_man_province_cus__c); Boolean userGIId =(oppType == 'GI' || oppType == 'BF' || oppType == 'ET')?true:false; if(provinceName != ''&& provinceMap.containsKey(provinceName)){ String GIId = provinceMap.get(provinceName).GI_assistant__c!=null?provinceMap.get(provinceName).GI_assistant__c:''; STring SPId = provinceMap.get(provinceName).SP_assistant__c!=null?provinceMap.get(provinceName).SP_assistant__c:''; Boolean updateGIId = (userGIId && GIId!='' && temp.Sales_assistant_name_text__c != GIId) ? true:false; Boolean updateSPId = ((!userGIId) && SPId!='' && temp.Sales_assistant_name_text__c!=SPId)? true:false; if(updateGIId) { temp.Sales_assistant_name_text__c = provinceMap.get(provinceName).GI_assistant__c; } if(updateSPId){ temp.Sales_assistant_name_text__c = provinceMap.get(provinceName).SP_assistant__c; } if(updateGIId || updateSPId){ updList.add(temp); } } } // 签收单更新 if (updList.size() > 0) { Database.SaveResult[] lsr = Database.update(updList, false); system.debug('Upsert Result' + lsr); } } } global void finish(Database.BatchableContext BC) { } public static void syncProvinceWIndow(Set changedWindowProvince) { if (changedWindowProvince != null && changedWindowProvince.size() > 0) { Database.executeBatch(new SyncProvinceWIndowToSignForm(changedWindowProvince), 200); } } }