1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
global without sharing class SyncProvinceWIndowToSignForm implements Database.Batchable<sObject>, Database.Stateful {
    private Set<String> changedWindowProvince { get; set; }
    private final string JCKStr ='集采课'; 
    private static Map<String,OCM_Management_Province__c> provinceManagementMap{set;get;}
    public static Map<String,OCM_Management_Province__c> getManagementProvince(){ 
        if(provinceManagementMap !=null && provinceManagementMap.keySet().size()>0 ){
            return provinceManagementMap;
        }
        provinceManagementMap = new Map<String,OCM_Management_Province__c>();
        List <OCM_Management_Province__c > 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<String> 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<eSignForm__c> signFormList) {
        system.debug('sign form list size:' + signFormList.size());
        // 取得OCM管理省信息
        Map<String, OCM_Management_Province__c> provinceMap = getManagementProvince();
        // 判断需要更新的签收单
        List<eSignForm__c> updList = new List<eSignForm__c>();
        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<String> changedWindowProvince) {
        if (changedWindowProvince != null && changedWindowProvince.size() > 0) {
            Database.executeBatch(new SyncProvinceWIndowToSignForm(changedWindowProvince), 200);
        }
    }
}