//获取追溯日报的活动区分，经销商状态
global class ConsumableTargetManageBatch4 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    global ConsumableTargetManageBatch4(Date startDate,Date endDate) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        queryStr = 'SELECT id,WorkRecord__c,WarlockClassification__c,ProductClassification__c,Agency_Hospital__c,Agency_Hospital__r.Hospital__c,Report_Date__c'
                  +' FROM Agency_Report__c ' 
                  +' WHERE WarlockClassification__c!=null AND WorkRecord__c != null AND Agency_Hospital__c!=null AND Agency_Hospital__r.Hospital__c!=null AND ProductClassification__c!=null'
                  +' AND Report_Date__c>=:startDate AND Report_Date__c<=:endDate';
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC, list<Agency_Report__c> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        Set<String> ctmKeySet=new Set<String>();
        Map<String,List<Agency_Report__c>> arMap = new Map<String,List<Agency_Report__c>>();
        for(Agency_Report__c ar:scope){
            String hpId='';
            if(ar.Agency_Hospital__c!=null&&ar.Agency_Hospital__r.Hospital__c!=null){
                hpId=ar.Agency_Hospital__r.Hospital__c;
            }
            String key=ar.ProductClassification__c+'-'+ar.WarlockClassification__c+'-'+hpId;
            ctmKeySet.add(hpId+'-'+ar.ProductClassification__c+'-'+ar.WarlockClassification__c+'-'+OCSMYear);
            if(arMap.containsKey(key)){
                arMap.get(key).add(ar);
            }else{
                List<Agency_Report__c> arList = new List<Agency_Report__c>();
                arList.add(ar);
                arMap.put(key,arList);
            }
        }

        List<ConsumableTargetManage__c> ctmList= [SELECT  DealerActivities_time__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c 
                                                FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear  AND ConsumableTargetManageKey__c in :ctmKeySet];
        
        Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
        Schema.SObjectType objType = globalDescribe.get('ConsumableTargetManage__c');
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
        List<Schema.PicklistEntry> SalesdepartmentMap=fieldMap.get('DealerActivities__c').getDescribe().getPicklistValues();
        List<String> DealerActivitiesList=new List<String>();
        for(Schema.PicklistEntry st:SalesdepartmentMap){
            if(st.isActive()){
                DealerActivitiesList.add(st.getValue());
            }
        }

        for(ConsumableTargetManage__c ctm:ctmList){
            String hpId='';
            if(ctm.Hospital__c!=null){
                hpId=ctm.Hospital__c;
            }
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ hpId;
            if(arMap.containsKey(key)){
                List<Agency_Report__c> arList=arMap.get(key);
                String statu='';
                Date lastDate=Date.newInstance(FYear-1, 4, 1);
                if(ctm.DealerActivities_time__c!=null){
                    lastDate=ctm.DealerActivities_time__c;
                }
                for(Agency_Report__c ar:arList){
                    if(ar.Report_Date__c>=lastDate && DealerActivitiesList.contains(ar.WorkRecord__c)){
                        lastDate=ar.Report_Date__c;
                        statu=ar.WorkRecord__c;
                        ctm.DealerActivities_time__c=lastDate;
                        ctm.DealerActivities__c=ar.WorkRecord__c;
                    }
                }
            }
        }

        if(ctmList.size()>0){
            update ctmList;
        }
    }

    global void finish(Database.BatchableContext BC) {
        Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch5(startDate,endDate), 200);

    }
}