global class AgencyQuolifiedExpireBatch implements Database.Batchable<sObject> {
    String query;
    global AgencyQuolifiedExpireBatch() {
        
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'select id, UnshippedAmount_F__c,Opportunity__c from Statu_Achievements__c where UnshippedAmount_F__c >=1 and Status_2_Formula__c NOT IN (\'无效\', \'冻结\', \'16 已结束\',\'14 已发货\')'; 
        return Database.getQueryLocator(query);
        
    }

    global void execute(Database.BatchableContext BC, List<SObject> scope) {
        List<id> OppIdlist = new List<id>();
        List<Statu_Achievements__c> StatuAchievementList = new List<Statu_Achievements__c>();
        StatuAchievementList = scope;
        List<Opportunity> OpportunityList = new List<Opportunity>();

        Map<id, Statu_Achievements__c> saMap = new Map<id, Statu_Achievements__c>();
        for (Statu_Achievements__c sa : StatuAchievementList) {
            if(!saMap.containskey(sa.Opportunity__c)){
                OppIdlist.add(sa.Opportunity__c);
            }
            saMap.put(sa.Opportunity__c, sa);
        }

        // CHAN-BEZB4N 20190819 Start
        System.debug('OppIdlist:::::' + OppIdlist);
        // String[] stageNames = new String[] {'询价', '取消', '失单'};
        String[] stageNames = new String[] {'询价', '取消', '失单', '完毕'};
        // CHAN-BEZB4N 20190819 End


        OpportunityList = [select id, Agency1__r.id, StageName__c,Agency1__r.Name, Sales_assistantWIN_ID__c
                          FROM  Opportunity
                          where Id in:OppIdlist and StageName__c NOT IN :stageNames];

        System.debug('OpportunityList:::::' + OpportunityList);

        
        Map<id, Opportunity> accoppMap = new Map<id, Opportunity>();
        Map<id, Opportunity> useroppMap = new Map<id, Opportunity>();
        List<id> accIdList = new List<id>();
        List<id> userIdList = new List<id>();
        for(Opportunity Opp : OpportunityList){
            if(!accoppMap.containsKey(Opp.Agency1__r.id)){
                accIdList.add(Opp.Agency1__r.id);
            }
            accoppMap.put(Opp.Agency1__r.id, Opp);
            
            if(!useroppMap.containsKey(Opp.Sales_assistantWIN_ID__c)){
                userIdList.add(Opp.Sales_assistantWIN_ID__c);
            }
            useroppMap.put(Opp.Sales_assistantWIN_ID__c, Opp);
            
        }

        Map<id, Account> accMap = new Map<id, Account>();
        List<Account> accList = [select id,Business_Paper_Expiration_Date__c,Tax_Practice_Expiration_Date__c,Medical_Equipment_Expiration_Date__c from Account where id in:accIdList];
        for(Account acc : accList){
            accMap.put(acc.id, acc);
        }

        Map<id, User> userMap = new Map<id, User>();
        List<User> uList = [select id,IsActive,Email from User where id in:userIdList];
        for(User u : uList){
            userMap.put(u.id, u);
        }

        List<Opportunity> OpportunitynewList = new List<Opportunity>();
        for(Opportunity Opp : OpportunityList){
            if(Opp.Agency1__r.id != null){
                Account acc = new Account();
                acc = accMap.get(Opp.Agency1__r.id);

                if((acc.Business_Paper_Expiration_Date__c != null && acc.Business_Paper_Expiration_Date__c <= Date.today().addDays(30))
                         || (acc.Tax_Practice_Expiration_Date__c != null && acc.Tax_Practice_Expiration_Date__c <= Date.today().addDays(30))
                         || (acc.Medical_Equipment_Expiration_Date__c != null && acc.Medical_Equipment_Expiration_Date__c <= Date.today().addDays(30))){
                    //过期
                    if(Opp.Sales_assistantWIN_ID__c != null){
                        User u = new User();
                        u = userMap.get(Opp.Sales_assistantWIN_ID__c);

                        if(u!= null && u.IsActive == true){
                            //无异常
                            Opp.AgencyQuolified_Flag__c = 10;
                        }else{
                            //异常
                            Opp.AgencyQuolified_Flag__c = 20;
                        }
                    }else{
                        //异常
                        Opp.AgencyQuolified_Flag__c = 20;
                    } 

                }
            }
            OpportunitynewList.add(Opp);
        }
        update OpportunitynewList;

    }
    
    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
    }
}