//OLY发货

global class DealerConsumableManageBatch2 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly;
    global DealerConsumableManageBatch2(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT id,Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c,asset__r.Product2.ConsumCategory2__c,NetWorthNoProviston__c,DeliveryDate__c,Statu_Achievements__c,Statu_Achievements__r.Opportunity__c,Statu_Achievements__r.Opportunity__r.Agency1__c,Statu_Achievements__r.Opportunity_Category__c'
                  +' FROM Statu_Achievements_DN_details__c ' 
                  +' WHERE  (Statu_Achievements__r.Opportunity_Category__c = \'ET\' OR asset__r.Product2.ConsumCategory2__c=\'ENG耗材\') '
                  +' AND DeliveryDate__c>=:startDate AND DeliveryDate__c<=:endDate';
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        if(!isIncrementOnly){
            Integer year = startDate.year();
            Integer FYear = year;
            if(startDate>=Date.newInstance(year, 4, 1)){
                FYear = FYear+1;
            }
            String  OCSMYear= 'FY'+FYear;
            List<Dealer_ConsumableManage__c> dcmList= [SELECT ET_ENG__c,FY__c,Dealer_Name__c
                                                 ,FY_O_1__c,FY_O_2__c,FY_O_3__c,FY_O_4__c,FY_O_5__c,FY_O_6__c,FY_O_7__c,FY_O_8__c,FY_O_9__c,FY_O_10__c,FY_O_11__c,FY_O_12__c  
                                                 FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear];
            for(Dealer_ConsumableManage__c dcm:dcmList){
                dcm.FY_S_1__c=0;
                dcm.FY_S_2__c=0;
                dcm.FY_S_3__c=0;
                dcm.FY_S_4__c=0;
                dcm.FY_S_5__c=0;
                dcm.FY_S_6__c=0;
                dcm.FY_S_7__c=0;
                dcm.FY_S_8__c=0;
                dcm.FY_S_9__c=0;
                dcm.FY_S_10__c=0;
                dcm.FY_S_11__c=0;
                dcm.FY_S_12__c=0;
            }
            update dcmList;
        }
        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC, list<Statu_Achievements_DN_details__c> scope) {
        Map<String,List<Statu_Achievements_DN_details__c>> saMap = new Map<String,List<Statu_Achievements_DN_details__c>>();
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        Set<String> dcmKeySet=new Set<String>();
        for(Statu_Achievements_DN_details__c sa:scope){
            String hpId='';
            if(sa.Statu_Achievements__c!=null&&sa.Statu_Achievements__r.Opportunity__c!=null&&sa.Statu_Achievements__r.Opportunity__r.Agency1__c!=null){
                hpId=sa.Statu_Achievements__r.Opportunity__r.Agency1__c;
            }

            String key;
            if(sa.Statu_Achievements__r.Opportunity_Category__c == 'ET' && sa.asset__r.Product2.ConsumCategory2__c!='ENG耗材'){
                key=hpId+'-ET'+'-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Statu_Achievements__r.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c);
            }else{
                key=hpId+'-ENG'+'-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Statu_Achievements__r.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c);
            }
            if(saMap.containsKey(key)){
                saMap.get(key).add(sa);
            }else{
                List<Statu_Achievements_DN_details__c> arList = new List<Statu_Achievements_DN_details__c>();
                arList.add(sa);
                saMap.put(key,arList);
            }
        }


        List<Dealer_ConsumableManage__c> dcmList= [SELECT TMS_Key__c,TMS_Province__c,ET_ENG__c,FY__c,Dealer_Name__c
                                                 ,FY_S_1__c,FY_S_2__c,FY_S_3__c,FY_S_4__c,FY_S_5__c,FY_S_6__c,FY_S_7__c,FY_S_8__c,FY_S_9__c,FY_S_10__c,FY_S_11__c,FY_S_12__c  
                                                 FROM Dealer_ConsumableManage__c WHERE TMS_Key__c in :dcmKeySet ];
        for(Dealer_ConsumableManage__c dcm:dcmList){
            String key=dcm.TMS_Key__c;
            if(dcm.FY_S_1__c==null){
                dcm.FY_S_1__c=0;
            }
            if(dcm.FY_S_2__c==null){
                dcm.FY_S_2__c=0;
            }
            if(dcm.FY_S_3__c==null){
                dcm.FY_S_3__c=0;
            }
            if(dcm.FY_S_4__c==null){
                dcm.FY_S_4__c=0;
            }
            if(dcm.FY_S_5__c==null){
                dcm.FY_S_5__c=0;
            }
            if(dcm.FY_S_6__c==null){
                dcm.FY_S_6__c=0;
            }
            if(dcm.FY_S_7__c==null){
                dcm.FY_S_7__c=0;
            }
            if(dcm.FY_S_8__c==null){
                dcm.FY_S_8__c=0;
            }
            if(dcm.FY_S_9__c==null){
                dcm.FY_S_9__c=0;
            }
            if(dcm.FY_S_10__c==null){
                dcm.FY_S_10__c=0;
            }
            if(dcm.FY_S_11__c==null){
                dcm.FY_S_11__c=0;
            }
            if(dcm.FY_S_12__c==null){
                dcm.FY_S_12__c=0;
            }
            if(saMap.containsKey(key)){
                List<Statu_Achievements_DN_details__c> saList=saMap.get(key);

                for(Statu_Achievements_DN_details__c sa:saList){

                    if(sa.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                        dcm.FY_S_1__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                        dcm.FY_S_2__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                        dcm.FY_S_3__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                        dcm.FY_S_4__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                        dcm.FY_S_5__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                        dcm.FY_S_6__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                        dcm.FY_S_7__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                        dcm.FY_S_8__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                        dcm.FY_S_9__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                        dcm.FY_S_10__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                        dcm.FY_S_11__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                        dcm.FY_S_12__c+=sa.NetWorthNoProviston__c/1000;
                    }
                }
            }
            saMap.remove(key);
        }
        List<Dealer_ConsumableManage__c> newDCM=new List<Dealer_ConsumableManage__c>();
        for(String key:saMap.keySet()){
            List<Statu_Achievements_DN_details__c> saList=saMap.get(key);

            Dealer_ConsumableManage__c dcm=new Dealer_ConsumableManage__c();
            dcm.Dealer_Name__c = saList[0].Statu_Achievements__r.Opportunity__r.Agency1__c;

            if(saList[0].Statu_Achievements__r.Opportunity_Category__c == 'ET' && saList[0].asset__r.Product2.ConsumCategory2__c!='ENG耗材'){
                dcm.ET_ENG__c='ET';
            }else{
                dcm.ET_ENG__c='ENG';
            }
            dcm.FY__c = OCSMYear;
            dcm.TMS_Key__c = dcm.Dealer_Name__c+'-'+dcm.ET_ENG__c+'-'+dcm.FY__c+'-'+saList[0].Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;


            if(dcm.FY_S_1__c==null){
                dcm.FY_S_1__c=0;
            }
            if(dcm.FY_S_2__c==null){
                dcm.FY_S_2__c=0;
            }
            if(dcm.FY_S_3__c==null){
                dcm.FY_S_3__c=0;
            }
            if(dcm.FY_S_4__c==null){
                dcm.FY_S_4__c=0;
            }
            if(dcm.FY_S_5__c==null){
                dcm.FY_S_5__c=0;
            }
            if(dcm.FY_S_6__c==null){
                dcm.FY_S_6__c=0;
            }
            if(dcm.FY_S_7__c==null){
                dcm.FY_S_7__c=0;
            }
            if(dcm.FY_S_8__c==null){
                dcm.FY_S_8__c=0;
            }
            if(dcm.FY_S_9__c==null){
                dcm.FY_S_9__c=0;
            }
            if(dcm.FY_S_10__c==null){
                dcm.FY_S_10__c=0;
            }
            if(dcm.FY_S_11__c==null){
                dcm.FY_S_11__c=0;
            }
            if(dcm.FY_S_12__c==null){
                dcm.FY_S_12__c=0;
            }
            for(Statu_Achievements_DN_details__c sa:saList){

                if(sa.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                    dcm.FY_S_1__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                    dcm.FY_S_2__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                    dcm.FY_S_3__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                    dcm.FY_S_4__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                    dcm.FY_S_5__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                    dcm.FY_S_6__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                    dcm.FY_S_7__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                    dcm.FY_S_8__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                    dcm.FY_S_9__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                    dcm.FY_S_10__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                    dcm.FY_S_11__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                    dcm.FY_S_12__c+=sa.NetWorthNoProviston__c/1000;
                }
            }
            newDCM.add(dcm);
        }
        if(dcmList.size()>0){
            update dcmList;
        }
        if(newDCM.size()>0){
            insert newDCM;
        }
    }

    global void finish(Database.BatchableContext BC) {
        Database.executeBatch(new DealerConsumableManageBatch3(startDate,endDate,this.isIncrementOnly),200);
    }
}