//从日报上获取入院进展
global class ConsumableTargetManageBatch5 implements Database.Batchable<sObject> {   
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    global ConsumableTargetManageBatch5(Date startDate,Date endDate) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        queryStr = 'SELECT id,Purpose_Type__c,Purpose_Type2__c,Purpose_Type3__c,Hospital_ID__c,etapp_third_category__c,etapp_third_category2__c,etapp_third_category3__c,etapp_forth_category__c,etapp_forth_category2__c,etapp_forth_category3__c,ActivityDate__c'
                  +' FROM Event__c ' 
                  +' WHERE (Status__c=\'申請中\' OR Status__c=\'承認\') AND Hospital_ID__c!=null '
                  +' AND ((etapp_third_category__c !=null AND etapp_forth_category__c!=null) '
                  +' OR (etapp_third_category2__c !=null AND etapp_forth_category2__c!=null)  '
                  +' OR (etapp_third_category3__c !=null AND etapp_forth_category3__c!=null) ) '
                  +' AND ActivityDate__c>=:startDate AND ActivityDate__c<=:endDate';
    }

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

    global void execute(Database.BatchableContext BC, list<Event__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<EventClass>> eventMap = new Map<String,List<EventClass>>();
        Map<String,String> hpIdMap=new Map<String,String>();
        List<String> hpIdList=new List<String>();
        for(Event__c event:scope){
            hpIdList.add(event.Hospital_ID__c);
        }
        List<Account> accList=[SELECT id from Account where id IN :hpIdList];
        for(Account acc:accList){
            String id=acc.Id;
            hpIdMap.put(id.substring(0,15),id);
        }
        for(Event__c event:scope){
            String hpId='';
            if(event.Hospital_ID__c!=null){
                hpId=event.Hospital_ID__c;
                hpId=hpId.substring(0,15);
            }
            String key1=event.etapp_third_category__c+'-'+event.etapp_forth_category__c+'-'+event.Hospital_ID__c;
            String key2=event.etapp_third_category2__c+'-'+event.etapp_forth_category2__c+'-'+event.Hospital_ID__c;
            String key3=event.etapp_third_category3__c+'-'+event.etapp_forth_category3__c+'-'+event.Hospital_ID__c;
            String eventHpId=hpIdMap.get(event.Hospital_ID__c);
            ctmKeySet.add(eventHpId+'-'+event.etapp_third_category__c+'-'+event.etapp_forth_category__c+'-'+OCSMYear);
            ctmKeySet.add(eventHpId+'-'+event.etapp_third_category2__c+'-'+event.etapp_forth_category2__c+'-'+OCSMYear);
            ctmKeySet.add(eventHpId+'-'+event.etapp_third_category3__c+'-'+event.etapp_forth_category3__c+'-'+OCSMYear);
            
            if(eventMap.containsKey(key1)){
                EventClass ec=new EventClass(event.ActivityDate__c,event.Purpose_Type__c);
                eventMap.get(key1).add(ec);
            }else{
                EventClass ec=new EventClass(event.ActivityDate__c,event.Purpose_Type__c);
                List<EventClass> eventList = new List<EventClass>();
                eventList.add(ec);
                eventMap.put(key1,eventList);
            }

            if(eventMap.containsKey(key2)){
                EventClass ec=new EventClass(event.ActivityDate__c,event.Purpose_Type2__c);
                eventMap.get(key2).add(ec);
            }else{
                EventClass ec=new EventClass(event.ActivityDate__c,event.Purpose_Type2__c);
                List<EventClass> eventList = new List<EventClass>();
                eventList.add(ec);
                eventMap.put(key2,eventList);
            }

            if(eventMap.containsKey(key3)){
                EventClass ec=new EventClass(event.ActivityDate__c,event.Purpose_Type3__c);
                eventMap.get(key3).add(ec);
            }else{
                EventClass ec=new EventClass(event.ActivityDate__c,event.Purpose_Type3__c);
                List<EventClass> eventList = new List<EventClass>();
                eventList.add(ec);
                eventMap.put(key3,eventList);
            }
        }


        List<ConsumableTargetManage__c> ctmList= [SELECT  Progress_AdmissionHospitale_Time__c,Before_AdmissionHospitale_Time__c,After_AdmissionHospitale_Time__c,
                Before_AdmissionHospitale_Volve__c,Progress_AdmissionHospitale_Volve__c,After_AdmissionHospitale_Volve__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c 
                FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear  AND ConsumableTargetManageKey__c in :ctmKeySet];
        




        List<String> be=new List<String>();
        List<String> pr=new List<String>();
        List<String> af=new List<String>();


        Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
        Schema.SObjectType objType = globalDescribe.get('ConsumableTargetManage__c');
        if (objType != null) {
            Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
            Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
            List<Schema.PicklistEntry> BeList=fieldMap.get('Before_AdmissionHospitale_Volve__c').getDescribe().getPicklistValues();
            List<Schema.PicklistEntry> PrList=fieldMap.get('Progress_AdmissionHospitale_Volve__c').getDescribe().getPicklistValues();
            List<Schema.PicklistEntry> AfList=fieldMap.get('After_AdmissionHospitale_Volve__c').getDescribe().getPicklistValues();
            System.debug('BeList!:');
            System.debug(BeList);
            for(Schema.PicklistEntry et:BeList){
                if(et.isActive()){
                    be.add(et.getValue());
                }
            }
            for(Schema.PicklistEntry et:PrList){
                if(et.isActive()){
                    pr.add(et.getValue());
                }
                
            }
            for(Schema.PicklistEntry et:AfList){
                if(et.isActive()){
                    af.add(et.getValue());
                }
                
            }
        }
        for(ConsumableTargetManage__c ctm:ctmList){
            String hpId='';
            if(ctm.Hospital__c!=null){
                hpId=ctm.Hospital__c;
                hpId=hpId.substring(0,15);
            }
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ hpId;
            if(eventMap.containsKey(key)){
                List<EventClass> eventList=eventMap.get(key);

                Date lastDateBe=Date.newInstance(FYear-1, 4, 1);
                Date lastDatePr=Date.newInstance(FYear-1, 4, 1);
                Date lastDateAf=Date.newInstance(FYear-1, 4, 1);
                if(ctm.Before_AdmissionHospitale_Time__c!=null){
                    lastDateBe=ctm.Before_AdmissionHospitale_Time__c;
                }
                if(ctm.Progress_AdmissionHospitale_Time__c!=null){
                    lastDatePr=ctm.Progress_AdmissionHospitale_Time__c;
                    
                }
                if(ctm.After_AdmissionHospitale_Time__c!=null){
                    lastDateAf=ctm.After_AdmissionHospitale_Time__c;
                }
                for(EventClass ec:eventList){

                    if(ec.ActivityDate>=lastDateBe){
                        if(be.contains(ec.PurposeType)){
                            ctm.Before_AdmissionHospitale_Volve__c = ec.PurposeType;
                            lastDateBe=ec.ActivityDate;
                            ctm.Before_AdmissionHospitale_Time__c=lastDateBe;
                        }
                    }

                    if(ec.ActivityDate>=lastDatePr){
                        if(pr.contains(ec.PurposeType)){
                            ctm.Progress_AdmissionHospitale_Volve__c = ec.PurposeType;
                            lastDatePr=ec.ActivityDate;
                            ctm.Progress_AdmissionHospitale_Time__c=lastDatePr;
                        }
                    }

                    if(ec.ActivityDate>=lastDateAf){
                        if(af.contains(ec.PurposeType)){
                            ctm.After_AdmissionHospitale_Volve__c = ec.PurposeType;
                            lastDateAf=ec.ActivityDate;
                            ctm.After_AdmissionHospitale_Time__c=lastDateAf;
                        }
                    }
                }
            }
        }

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

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

    }
    class EventClass{
        public Date ActivityDate;
        public String PurposeType;
        EventClass(Date ActivityDate,String PurposeType){
            this.ActivityDate=ActivityDate;
            this.PurposeType=PurposeType;
        }
    }
}