global class DealerConsumableManageBatch4 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    global DealerConsumableManageBatch4(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT DeliveryDate__c,ContractAmount_F__c,Paid_Amount_Rate_F__c,Status_1__c,Opportunity__r.Agency1__c,Opportunity_Category__c,Opportunity__r.OCM_man_province_no_dealer__c,Opportunity__r.Opp_order_Type__c  '+
        ' FROM Statu_Achievements__c '+
        ' WHERE Status_1__c not in (\'无效\',\'冻结\') and Paid_Amount_Rate_F__c!=100 AND Status_2_Formula__c not in (\'退货\',\'14 已发货\') and ((NOT ContractNO__c like \'%-A\') OR  (NOT ContractNO__c like\'%-B\'))  AND PaymentStatus__c!=\'不需付款\' and Opportunity__r.Opp_order_Type__c in (\'ET\',\'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  id FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear ];
            if(dcmList.size()>0){
                for(Dealer_ConsumableManage__c dcm:dcmList){
                    dcm.TMS_Unpaid_amount_1__c=0;
                    dcm.TMS_Unpaid_amount_2__c=0;
                    dcm.TMS_Unpaid_amount_3__c=0;
                    dcm.TMS_Unpaid_amount_4__c=0;
                    dcm.TMS_Unpaid_amount_5__c=0;
                    dcm.TMS_Unpaid_amount_6__c=0;
                    dcm.TMS_Unpaid_amount_7__c=0;
                    dcm.TMS_Unpaid_amount_8__c=0;
                    dcm.TMS_Unpaid_amount_9__c=0;
                    dcm.TMS_Unpaid_amount_10__c=0;
                    dcm.TMS_Unpaid_amount_11__c=0;
                    dcm.TMS_Unpaid_amount_12__c=0;
                }
                update dcmList;
            }
            
        }

        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC,  list<Statu_Achievements__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> dcmKeySet=new Set<String>();
        Map<String,List<Statu_Achievements__c>> codMap = new Map<String,List<Statu_Achievements__c>>();
        for(Statu_Achievements__c sa:scope){
            String key;
            if(sa.Opportunity__r.Opp_order_Type__c == 'ET'){
                key=sa.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c);
            }else{
                key=sa.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c);
            }
            
            if(codMap.containsKey(key)){
                codMap.get(key).add(sa);
            }else{
                List<Statu_Achievements__c> codList = new List<Statu_Achievements__c>();
                codList.add(sa);
                codMap.put(key,codList);
            }
        }


        List<Dealer_ConsumableManage__c> ctmList= [SELECT  TMS_Province__c,TMS_Unpaid_amount_1__c,TMS_Unpaid_amount_2__c,TMS_Unpaid_amount_3__c,TMS_Unpaid_amount_4__c
                                                    ,TMS_Unpaid_amount_5__c,TMS_Unpaid_amount_6__c,TMS_Unpaid_amount_7__c,TMS_Unpaid_amount_8__c
                                                    ,TMS_Unpaid_amount_9__c,TMS_Unpaid_amount_10__c,TMS_Unpaid_amount_11__c,TMS_Unpaid_amount_12__c
                                                    ,TMS_Key__c
                                                     FROM Dealer_ConsumableManage__c WHERE  TMS_Key__c in :dcmKeySet];
        System.debug('获取到现存IMS:');
        System.debug(ctmList);

        for(Dealer_ConsumableManage__c ctm:ctmList){
            String key = ctm.TMS_Key__c;
            if(ctm.TMS_Unpaid_amount_1__c==null){
                ctm.TMS_Unpaid_amount_1__c=0;
            }
            if(ctm.TMS_Unpaid_amount_2__c==null){
                ctm.TMS_Unpaid_amount_2__c=0;
            }
            if(ctm.TMS_Unpaid_amount_3__c==null){
                ctm.TMS_Unpaid_amount_3__c=0;
            }
            if(ctm.TMS_Unpaid_amount_4__c==null){
                ctm.TMS_Unpaid_amount_4__c=0;
            }
            if(ctm.TMS_Unpaid_amount_5__c==null){
                ctm.TMS_Unpaid_amount_5__c=0;
            }
            if(ctm.TMS_Unpaid_amount_6__c==null){
                ctm.TMS_Unpaid_amount_6__c=0;
            }
            if(ctm.TMS_Unpaid_amount_7__c==null){
                ctm.TMS_Unpaid_amount_7__c=0;
            }
            if(ctm.TMS_Unpaid_amount_8__c==null){
                ctm.TMS_Unpaid_amount_8__c=0;
            }
            if(ctm.TMS_Unpaid_amount_9__c==null){
                ctm.TMS_Unpaid_amount_9__c=0;
            }
            if(ctm.TMS_Unpaid_amount_10__c==null){
                ctm.TMS_Unpaid_amount_10__c=0;
            }
            if(ctm.TMS_Unpaid_amount_11__c==null){
                ctm.TMS_Unpaid_amount_11__c=0;
            }
            if(ctm.TMS_Unpaid_amount_12__c==null){
                ctm.TMS_Unpaid_amount_12__c=0;
            }
            if(codMap.containsKey(key)){
                List<Statu_Achievements__c> saList=codMap.get(key);
                for(Statu_Achievements__c cod:saList){
                   	Decimal PARF=cod.Paid_Amount_Rate_F__c;
                    if(PARF==null){
                        PARF=0;
                    }
                    if(cod.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.TMS_Unpaid_amount_1__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.TMS_Unpaid_amount_2__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.TMS_Unpaid_amount_3__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.TMS_Unpaid_amount_4__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.TMS_Unpaid_amount_5__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.TMS_Unpaid_amount_6__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.TMS_Unpaid_amount_7__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.TMS_Unpaid_amount_8__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.TMS_Unpaid_amount_9__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.TMS_Unpaid_amount_10__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.TMS_Unpaid_amount_11__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.TMS_Unpaid_amount_12__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }
                }
                codMap.remove(key);
            }
        }

        List<Dealer_ConsumableManage__c> newCtm=new List<Dealer_ConsumableManage__c>();
        for(String key:codMap.keySet()){
            List<Statu_Achievements__c> codList=codMap.get(key);
            Dealer_ConsumableManage__c ctm=new Dealer_ConsumableManage__c();
            ctm.Dealer_Name__c = codList[0].Opportunity__r.Agency1__c;
            if(codList[0].Opportunity__r.Opp_order_Type__c == 'ET'){
                ctm.ET_ENG__c='ET';
            }else{
                ctm.ET_ENG__c='ENG';
            }
            ctm.FY__c = OCSMYear;
            ctm.TMS_Key__c = ctm.Dealer_Name__c+'-'+ctm.ET_ENG__c+'-'+ctm.FY__c+'-'+codList[0].Opportunity__r.OCM_man_province_no_dealer__c;
                    ctm.TMS_Unpaid_amount_1__c=0;
                    ctm.TMS_Unpaid_amount_2__c=0;
                    ctm.TMS_Unpaid_amount_3__c=0;
                    ctm.TMS_Unpaid_amount_4__c=0;
                    ctm.TMS_Unpaid_amount_5__c=0;
                    ctm.TMS_Unpaid_amount_6__c=0;
                    ctm.TMS_Unpaid_amount_7__c=0;
                    ctm.TMS_Unpaid_amount_8__c=0;
                    ctm.TMS_Unpaid_amount_9__c=0;
                    ctm.TMS_Unpaid_amount_10__c=0;
                    ctm.TMS_Unpaid_amount_11__c=0;
                    ctm.TMS_Unpaid_amount_12__c=0;

                for(Statu_Achievements__c cod:codList){
                   	Decimal PARF=cod.Paid_Amount_Rate_F__c;
                    if(PARF==null){
                        PARF=0;
                    }
                    if(cod.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.TMS_Unpaid_amount_1__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.TMS_Unpaid_amount_2__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.TMS_Unpaid_amount_3__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.TMS_Unpaid_amount_4__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.TMS_Unpaid_amount_5__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.TMS_Unpaid_amount_6__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.TMS_Unpaid_amount_7__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.TMS_Unpaid_amount_8__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.TMS_Unpaid_amount_9__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.TMS_Unpaid_amount_10__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.TMS_Unpaid_amount_11__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.TMS_Unpaid_amount_12__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }
                }
            newCtm.add(ctm);
        }
        if(ctmList.size()>0){
            update ctmList;
        }
        if(newCtm.size()>0){
            insert newCtm;
        }

    }

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