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
    }
}