//OLY订货
global class DealerConsumableManageBatch1 implements Database.Batchable<sObject> {
 public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly;
    global DealerConsumableManageBatch1(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        //1.OCM_Sales_Forecast_F__c
        //2.

        queryStr = 'SELECT id,OCM_Sales_Forecast_F__c,Opportunity.OCM_man_province_no_dealer__c,Opportunity.Contract_recognized_day_1__c,Opportunity.Agency1__c';
        queryStr +=',Opportunity.Estimation_Id__c,Product2.OT_CODE_no_link__c,Opportunity.ID';
        queryStr +=',Opportunity.SPOApprovedNo__c,Product2.ConsumCategory2__c,Opportunity.Opportunity_Category__c';
        queryStr +=' FROM OpportunityLineItem ' ;
        queryStr +=' WHERE ( (Opportunity.StageName in (\'出荷\',\'完了\',\'注残\') ';
        queryStr +=' AND Product2.ENG_New__c=\'ENG2\' ';

        queryStr +=' AND ( (Opportunity.Estimation_Id__c like \'a15%\' AND Product2.OT_CODE_no_link__c!=\'\' ) OR ';
        queryStr +='  ( Opportunity.SPOApprovedNo__c!=\'\' ) ) ) ';

        queryStr +=' OR ( Opportunity.Opportunity_Category__c=\'ET\' AND  Opportunity.RecordType.Name in (\'引合\',\'SI询价\') ) )';

        queryStr +=' AND Opportunity.Contract_recognized_day_1__c>=:startDate AND Opportunity.Contract_recognized_day_1__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_O_1__c=0;
                dcm.FY_O_2__c=0;
                dcm.FY_O_3__c=0;
                dcm.FY_O_4__c=0;
                dcm.FY_O_5__c=0;
                dcm.FY_O_6__c=0;
                dcm.FY_O_7__c=0;
                dcm.FY_O_8__c=0;
                dcm.FY_O_9__c=0;
                dcm.FY_O_10__c=0;
                dcm.FY_O_11__c=0;
                dcm.FY_O_12__c=0;
            }
            update dcmList;
        }
        
        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC, list<OpportunityLineItem> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        Map<String,List<OpportunityLineItem>> saMap = new Map<String,List<OpportunityLineItem>>();
        List<String> accIdList=new List<String>();
        List<String> oppIdList=new List<String>();
        for(OpportunityLineItem sa:scope){
            oppIdList.add(sa.Opportunity.Id);
        }

        List<Quote> quoteList=[SELECT Quote_Decision__c,OpportunityId from Quote where OpportunityId in :oppIdList and Quote_Decision__c='√'];
        Map<String,Quote> quoteMap=new Map<String,Quote>();
        for(Quote q:quoteList){
            if(!quoteMap.containsKey(q.OpportunityId)){
                quoteMap.put(q.OpportunityId,q);
            }
        }

        Set<String> dcmKeySet=new Set<String>();
        for(OpportunityLineItem sa:scope){
            String hpId=sa.Opportunity.Agency1__c;
            accIdList.add(sa.Opportunity.Agency1__c);
            String key=hpId;
            if(sa.Opportunity.Opportunity_Category__c=='ET'){
                key+='-ET';
                dcmKeySet.add(sa.Opportunity.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Opportunity.OCM_man_province_no_dealer__c);
            }else{
                String eId=sa.Opportunity.Estimation_Id__c;
                if(eId!=null){
                    eId=eId.substring(0,3);
                }
                if( ( eId!='a15' || sa.Product2.OT_CODE_no_link__c=='') && (sa.Opportunity.SPOApprovedNo__c!='' )  ){
                    if(!quoteMap.containsKey(sa.Opportunity.Id)){
                        continue;
                    }
                }
                key+='-ENG';
                dcmKeySet.add(sa.Opportunity.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Opportunity.OCM_man_province_no_dealer__c);
            }
            key+='-'+OCSMYear+'-'+sa.Opportunity.OCM_man_province_no_dealer__c;
            if(saMap.containsKey(key)){
                saMap.get(key).add(sa);
            }else{
                List<OpportunityLineItem> arList = new List<OpportunityLineItem>();
                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_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  TMS_Key__c in : dcmKeySet];
        for(Dealer_ConsumableManage__c dcm:dcmList){
            String key=dcm.TMS_Key__c;
            if(dcm.FY_O_1__c==null){
                dcm.FY_O_1__c=0;
            }
            if(dcm.FY_O_2__c==null){
                dcm.FY_O_2__c=0;
            }
            if(dcm.FY_O_3__c==null){
                dcm.FY_O_3__c=0;
            }
            if(dcm.FY_O_4__c==null){
                dcm.FY_O_4__c=0;
            }
            if(dcm.FY_O_5__c==null){
                dcm.FY_O_5__c=0;
            }
            if(dcm.FY_O_6__c==null){
                dcm.FY_O_6__c=0;
            }
            if(dcm.FY_O_7__c==null){
                dcm.FY_O_7__c=0;
            }
            if(dcm.FY_O_8__c==null){
                dcm.FY_O_8__c=0;
            }
            if(dcm.FY_O_9__c==null){
                dcm.FY_O_9__c=0;
            }
            if(dcm.FY_O_10__c==null){
                dcm.FY_O_10__c=0;
            }
            if(dcm.FY_O_11__c==null){
                dcm.FY_O_11__c=0;
            }
            if(dcm.FY_O_12__c==null){
                dcm.FY_O_12__c=0;
            }
            if(saMap.containsKey(key)){
                List<OpportunityLineItem> saList=saMap.get(key);
                for(OpportunityLineItem sa:saList){

                    if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 1, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 2, 1)){
                        dcm.FY_O_1__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 2, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 3, 1)){
                        dcm.FY_O_2__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 3, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 4, 1)){
                        dcm.FY_O_3__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 4, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 5, 1)){
                        dcm.FY_O_4__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 5, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 6, 1)){
                        dcm.FY_O_5__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 6, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 7, 1)){
                        dcm.FY_O_6__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 7, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 8, 1)){
                        dcm.FY_O_7__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 8, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 9, 1)){
                        dcm.FY_O_8__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 9, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 10, 1)){
                        dcm.FY_O_9__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 10, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 11, 1)){
                        dcm.FY_O_10__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 11, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 12, 1)){
                        dcm.FY_O_11__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 12, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 1, 1)){
                        dcm.FY_O_12__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }
                }
            }
            saMap.remove(key);
        }


        List<Dealer_ConsumableManage__c> newDCM=new List<Dealer_ConsumableManage__c>();
        for(String key:saMap.keySet()){
            List<OpportunityLineItem> saList=saMap.get(key);

            Dealer_ConsumableManage__c dcm=new Dealer_ConsumableManage__c();
            dcm.Dealer_Name__c = saList[0].Opportunity.Agency1__c;
            if(saList[0].Opportunity.Opportunity_Category__c=='ET'){
                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].Opportunity.OCM_man_province_no_dealer__c;
            if(dcm.FY_O_1__c==null){
                dcm.FY_O_1__c=0;
            }
            if(dcm.FY_O_2__c==null){
                dcm.FY_O_2__c=0;
            }
            if(dcm.FY_O_3__c==null){
                dcm.FY_O_3__c=0;
            }
            if(dcm.FY_O_4__c==null){
                dcm.FY_O_4__c=0;
            }
            if(dcm.FY_O_5__c==null){
                dcm.FY_O_5__c=0;
            }
            if(dcm.FY_O_6__c==null){
                dcm.FY_O_6__c=0;
            }
            if(dcm.FY_O_7__c==null){
                dcm.FY_O_7__c=0;
            }
            if(dcm.FY_O_8__c==null){
                dcm.FY_O_8__c=0;
            }
            if(dcm.FY_O_9__c==null){
                dcm.FY_O_9__c=0;
            }
            if(dcm.FY_O_10__c==null){
                dcm.FY_O_10__c=0;
            }
            if(dcm.FY_O_11__c==null){
                dcm.FY_O_11__c=0;
            }
            if(dcm.FY_O_12__c==null){
                dcm.FY_O_12__c=0;
            }
            for(OpportunityLineItem sa:saList){

                if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 1, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 2, 1)){
                    dcm.FY_O_1__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 2, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 3, 1)){
                    dcm.FY_O_2__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 3, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 4, 1)){
                    dcm.FY_O_3__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 4, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 5, 1)){
                    dcm.FY_O_4__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 5, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 6, 1)){
                    dcm.FY_O_5__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 6, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 7, 1)){
                    dcm.FY_O_6__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 7, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 8, 1)){
                    dcm.FY_O_7__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 8, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 9, 1)){
                    dcm.FY_O_8__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 9, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 10, 1)){
                    dcm.FY_O_9__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 10, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 11, 1)){
                    dcm.FY_O_10__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 11, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 12, 1)){
                    dcm.FY_O_11__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 12, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 1, 1)){
                    dcm.FY_O_12__c+=sa.OCM_Sales_Forecast_F__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 DealerConsumableManageBatch2(startDate,endDate,this.isIncrementOnly),200);

    }
}