高章伟
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
global class Sfdc2SapDealersContractBatch implements Database.Batchable<sObject>, Database.AllowsCallouts {
 
    Boolean IsNeedExecute = false; // 2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
 
    //xinhonglu
    global Sfdc2SapDealersContractBatch() {
 
    }
 
    // 2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
    global Sfdc2SapDealersContractBatch(Boolean NeedExecute) {
        this.IsNeedExecute = NeedExecute;
    }
    // 2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end
 
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        System.debug('================================进入batch');
        return Database.getQueryLocator(
                   [SELECT Id, RecordType_DeveloperName__c , RecordTypeId ,
                    Sap2sfdcDealers_ModifycationLogo__c, Is_Active_Formula__c,
                    ContractSfdc2SapLog__c
                    FROM Account
                    where Is_Active_Formula__c = '有效'
                            and ((Parent.Sap2sfdcDealers_ModifycationLogo__c = '1' and Parent.RecordType_DeveloperName__c = 'Agency')
                                 or Sap2sfdcDealers_ModifycationLogo__c = '1' )]);
        //[SELECT Id,RecordType_DeveloperName__c , RecordTypeId , Sap2sfdcDealers_ModifycationLogo__c, Is_Active_Formula__c,ContractSfdc2SapLog__c  FROM Account where Is_Active_Formula__c = '有效' and ((Parent.Sap2sfdcDealers_ModifycationLogo__c = '1' and Parent.RecordType_DeveloperName__c = 'Agency') or (Sap2sfdcDealers_ModifycationLogo__c = '1' and RecordType_DeveloperName__c = 'Agency'))]
        //);
    }
 
    global void execute(Database.BatchableContext BC, List<Account> accList) {
        
        String[] needSendTypes = new String[] {'契約', '販売店'};
        List<Account> updateAccountList = new List<Account>();
        Map<String, String> purposeOfAdviceMap = new Map<String, String>();
        List<RecordType> rects = [select Id, Name
                                  from RecordType
                                  where IsActive = true
                                          and SobjectType = 'Account'
                                                  and Name IN :needSendTypes];
        Map<String, String> needSendRectMap = new Map<String, String>();
        for (RecordType rect : rects) {
            needSendRectMap.put(rect.Id, rect.Name);
        }
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        //iflog.Type__c = 'NFM001';
        //iflog.ErrorLog__c = '';
        //iflog.Log__c  = 'callout start\n';
        //iflog.Id = '20181211';
        //insert iflog;
 
        //iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
        System.debug(Logginglevel.DEBUG, 'NFM001_' + iflog.Name + ' start');
 
        //Savepoint sp = Database.setSavepoint();
        try {
            List<String> accIds = new List<String>();
            List<String> accIdList2Applet = new List<String>(); 
            for (Account acc : accList) {
                acc.Sap2sfdcDealers_ModifycationLogo__c = '0';
                if (needSendRectMap.get(acc.RecordTypeId) == '契約') {
                    accIds.add(acc.Id);
 
                }
                purposeOfAdviceMap.put(acc.Id, acc.ContractSfdc2SapLog__c);
                if (needSendRectMap.get(acc.RecordTypeId) == '販売店'){
                    accIdList2Applet.add(acc.Id);
                }
                //if (needSendRectMap.get(acc.RecordTypeId) == '販売店'){
                //  //acc.Sap2sfdcDealers_ModifycationLogo__c = '0';
                //  updateAccountList.add(acc);
                //}
                updateAccountList.add(acc);
            }
            //if(updateAccountList.size()>0){
            //  upsert updateAccountList;
            //}
            NFM001Controller.calloutNotFuture(null, accIds, purposeOfAdviceMap, needSendRectMap);
            system.debug('accIdList2Applet--->'+accIdList2Applet);
            if (accIdList2Applet.size() > 0) {
                NFM601Controller.executefuture(null, accIdList2Applet);
            }
            if (updateAccountList.size() > 0) {
                upsert updateAccountList;
            }
            // system.debug('accIdList2Applet--->'+accIdList2Applet);
            // if (accIdList2Applet.size() > 0) {
            //     NFM601Controller.executefuture(null, accIdList2Applet);
            // }
 
            iflog.Log__c  = 'end\n';
        } catch (Exception ex) {
            // エラーが発生した場合
            //Database.rollback(sp);
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            iflog.Log__c  = 'end\n';
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
 
}