高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
global class AccountUpEffectiveContractBatch implements Database.Batchable<sObject> {
    String query;
 
    Boolean IsNeedExecute = false; // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
 
    global AccountUpEffectiveContractBatch() {
    }
    global AccountUpEffectiveContractBatch(Boolean needExecute) {
      IsNeedExecute = needExecute;  // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  
    }
    global Database.QueryLocator start(Database.BatchableContext BC) {
        //经销商类型
        List<RecordType> rects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        List<String> deptRectIds = new List<String>();
        for (RecordType rect : rects) {
            deptRectIds.add(rect.Id);
        }
        //取得所有经销商信息
        //return Database.getQueryLocator([select id,Agency_With_EffectiveContract__c,Agency_With_EffectiveContractENG__c,Agency_With_EffectiveContractSP__c from Account where Name like '%TEST 大连科尔特%' and RecordTypeId = '01210000000Qem1']);
        return Database.getQueryLocator([select id,Agency_With_EffectiveContract__c,Agency_With_EffectiveContractENG__c,Agency_With_EffectiveContractSP__c from Account where RecordTypeId in :deptRectIds]);
    }
 
    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        List<Account> accList = new List<Account>();
        //经销商
        accList = scope;
        Set<String> agencySet = new Set<String>();
        List<Account> accUpList = new List<Account>();
        Map<id, Boolean> accOriETMap = new Map<id, Boolean>();
        Map<id, Boolean> accOriSPMap = new Map<id, Boolean>();
        Map<id, Boolean> accOriENGMap = new Map<id, Boolean>();
        for(Account acc :accList){
            agencySet.add(acc.id);
        }
        //汇总举要有效ET契约的经销商数据
        AggregateResult[] resultsET = [SELECT ParentId,count(Id) cnt
                                     FROM Account
                                     WHERE ParentId in :agencySet
                                     AND ET_SP_Dealer__c = true
                                     AND Is_Active_Formula__c = '有效'
                                     group by ParentId];
        //数据Map作成
        for(AggregateResult ar: resultsET){
            accOriETMap.put(String.valueOf(ar.get('ParentId')), true);
        }
        //汇总举要有效ENG契约的经销商数据
        AggregateResult[] resultsENG = [SELECT ParentId,count(Id) cnt
                                     FROM Account
                                     WHERE ParentId in :agencySet
                                     AND ENG_Dealer__c = true
                                     AND Is_Active_Formula__c = '有效'
                                     group by ParentId];
        //数据Map作成
        for(AggregateResult ar: resultsENG){
            accOriENGMap.put(String.valueOf(ar.get('ParentId')), true);
        }
        //汇总举要有效SP契约的经销商数据
        AggregateResult[] resultsSP = [SELECT ParentId,count(Id) cnt
                                     FROM Account
                                     WHERE ParentId in :agencySet
                                     AND SP_DealerContact__c = true
                                     AND Is_Active_Formula__c = '有效'
                                     group by ParentId];
        //数据Map作成
        for(AggregateResult ar: resultsSP){
            accOriSPMap.put(String.valueOf(ar.get('ParentId')), true);
        }
        //取出需要更新的经销商数据
        for(Account accR : accList){
            //Boolean existFlg = false;
            //if(!accOriETMap.containsKey(accR.Id) && accR.Agency_With_EffectiveContract__c == true){
            //    existFlg = false;
            //}else if(accOriETMap.containsKey(accR.Id) && accR.Agency_With_EffectiveContract__c == false){
            //    existFlg = true;
            //}else{
            //    continue;
            //}
            Account accinfo = new Account();
            accinfo.id = accR.Id;
            if(accOriETMap.containsKey(accR.Id) && accOriETMap.get(accR.Id)!=accR.Agency_With_EffectiveContract__c){
                accinfo.Agency_With_EffectiveContract__c = true;
            }else if(accOriENGMap.containsKey(accR.Id) && accOriENGMap.get(accR.Id)!=accR.Agency_With_EffectiveContractENG__c){
                accinfo.Agency_With_EffectiveContractENG__c = true;
            }
            else if(accOriSPMap.containsKey(accR.Id) && accOriSPMap.get(accR.Id)!=accR.Agency_With_EffectiveContractSP__c){
                accinfo.Agency_With_EffectiveContractSP__c = true;
            }else{
                continue;
            }
            //accinfo.Agency_With_EffectiveContract__c = existFlg;
            accUpList.add(accinfo);
        }
        if(accUpList.size() > 0){
            update accUpList;
        }
    }
    global void finish(Database.BatchableContext BC) {
 
          //2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
        if(!Test.isRunningTest() &&IsNeedExecute==true){
         //batch里调用下一个batch时,希望跟原有的Schedule里面传的条数保持一致
           Id execBTId = Database.executebatch(new RepairProductGuaranteUpdateBatch (true),200);
        }
      //2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end
    }
}