global class DealerConsumableManageBatch3 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    global DealerConsumableManageBatch3(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT OCM_man_province__c,Product_Type__c,Dealer_Info_ID__c,Detail_Count__c,Intra_Trade_KRMB__c,Product_OutDate__c '+
        ' FROM Consumable_order_details2__c '+
        ' WHERE Lose_Flag__c = FALSE AND Dealer_Returned__c = FALSE AND Dealer_Saled__c=true AND Product_OutDate__c >= :startDate AND Product_OutDate__c  <= :endDate' +
        ' AND (Inventory_Status__c = \'直销到医院\' OR Inventory_Status__c = \'给二级到医院\')  AND ShipmentAccount__c != \'\' AND Consumable_product__r.Product2__c!=null AND Consumable_Category1__c!=null AND Intra_Trade_KRMB__c!=null AND Hospital_ID__c!=null AND Dealer_Info_ID__c!=null and Product_Type__c!=\'\' ';
        // ' and Consumable_product__r.Product2__r.ConsumCategory3__c=\'高频治疗钳\' and Consumable_product__r.Product2__r.ConsumCategory2__c=\'ESD\' and Hospital_ID__c=\'0011000000V9RHzAAN\''+
        // ' limit 3000';
    }

    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> ctmList= [SELECT  id FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear ];
            if(ctmList.size()>0){
                for(Dealer_ConsumableManage__c ctm:ctmList){
                    ctm.IMS_Shipments_1__c=0;
                    ctm.IMS_Shipments_2__c=0;
                    ctm.IMS_Shipments_3__c=0;
                    ctm.IMS_Shipments_4__c=0;
                    ctm.IMS_Shipments_5__c=0;
                    ctm.IMS_Shipments_6__c=0;
                    ctm.IMS_Shipments_7__c=0;
                    ctm.IMS_Shipments_8__c=0;
                    ctm.IMS_Shipments_9__c=0;
                    ctm.IMS_Shipments_10__c=0;
                    ctm.IMS_Shipments_11__c=0;
                    ctm.IMS_Shipments_12__c=0;
                }
                update ctmList;
            }
            
        }

        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC,  list<Consumable_order_details2__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<Consumable_order_details2__c>> codMap = new Map<String,List<Consumable_order_details2__c>>();
        for(Consumable_order_details2__c cod:scope){
            String key = cod.Dealer_Info_ID__c+'-'+cod.Product_Type__c+'-'+OCSMYear+'-'+cod.OCM_man_province__c;
            ctmKeySet.add(cod.Dealer_Info_ID__c+'-'+cod.Product_Type__c+'-'+OCSMYear+'-'+cod.OCM_man_province__c);
            if(codMap.containsKey(key)){
                codMap.get(key).add(cod);
            }else{
                List<Consumable_order_details2__c> codList = new List<Consumable_order_details2__c>();
                codList.add(cod);
                codMap.put(key,codList);
            }
        }


        List<Dealer_ConsumableManage__c> ctmList= [SELECT  TMS_Province__c,IMS_Shipments_1__c,IMS_Shipments_2__c,IMS_Shipments_3__c,IMS_Shipments_4__c
                                                    ,IMS_Shipments_5__c,IMS_Shipments_6__c,IMS_Shipments_7__c,IMS_Shipments_8__c
                                                    ,IMS_Shipments_9__c,IMS_Shipments_10__c,IMS_Shipments_11__c,IMS_Shipments_12__c
                                                    ,TMS_Key__c
                                                     FROM Dealer_ConsumableManage__c WHERE  TMS_Key__c in :ctmKeySet];
        System.debug('获取到现存IMS:');
        System.debug(ctmList);

        for(Dealer_ConsumableManage__c ctm:ctmList){
            String key = ctm.TMS_Key__c;
            if(ctm.IMS_Shipments_1__c==null){
                ctm.IMS_Shipments_1__c=0;
            }
            if(ctm.IMS_Shipments_2__c==null){
                ctm.IMS_Shipments_2__c=0;
            }
            if(ctm.IMS_Shipments_3__c==null){
                ctm.IMS_Shipments_3__c=0;
            }
            if(ctm.IMS_Shipments_4__c==null){
                ctm.IMS_Shipments_4__c=0;
            }
            if(ctm.IMS_Shipments_5__c==null){
                ctm.IMS_Shipments_5__c=0;
            }
            if(ctm.IMS_Shipments_6__c==null){
                ctm.IMS_Shipments_6__c=0;
            }
            if(ctm.IMS_Shipments_7__c==null){
                ctm.IMS_Shipments_7__c=0;
            }
            if(ctm.IMS_Shipments_8__c==null){
                ctm.IMS_Shipments_8__c=0;
            }
            if(ctm.IMS_Shipments_9__c==null){
                ctm.IMS_Shipments_9__c=0;
            }
            if(ctm.IMS_Shipments_10__c==null){
                ctm.IMS_Shipments_10__c=0;
            }
            if(ctm.IMS_Shipments_11__c==null){
                ctm.IMS_Shipments_11__c=0;
            }
            if(ctm.IMS_Shipments_12__c==null){
                ctm.IMS_Shipments_12__c=0;
            }
            if(codMap.containsKey(key)){
                List<Consumable_order_details2__c> codList=codMap.get(key);
                for(Consumable_order_details2__c cod:codList){
                    if(cod.Product_OutDate__c >= Date.newInstance(FYear, 1, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.IMS_Shipments_1__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 2, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.IMS_Shipments_2__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 3, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.IMS_Shipments_3__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.IMS_Shipments_4__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.IMS_Shipments_5__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.IMS_Shipments_6__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.IMS_Shipments_7__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.IMS_Shipments_8__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.IMS_Shipments_9__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.IMS_Shipments_10__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.IMS_Shipments_11__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.IMS_Shipments_12__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                    }
                }
                codMap.remove(key);
            }
        }

        List<Dealer_ConsumableManage__c> newCtm=new List<Dealer_ConsumableManage__c>();
        for(String key:codMap.keySet()){
            List<Consumable_order_details2__c> codList=codMap.get(key);
            Dealer_ConsumableManage__c ctm=new Dealer_ConsumableManage__c();
            ctm.Dealer_Name__c = codList[0].Dealer_Info_ID__c;
            if(codList[0].Product_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].OCM_man_province__c;
            ctm.IMS_Shipments_1__c=0;
            ctm.IMS_Shipments_2__c=0;
            ctm.IMS_Shipments_3__c=0;
            ctm.IMS_Shipments_4__c=0;
            ctm.IMS_Shipments_5__c=0;
            ctm.IMS_Shipments_6__c=0;
            ctm.IMS_Shipments_7__c=0;
            ctm.IMS_Shipments_8__c=0;
            ctm.IMS_Shipments_9__c=0;
            ctm.IMS_Shipments_10__c=0;
            ctm.IMS_Shipments_11__c=0;
            ctm.IMS_Shipments_12__c=0;

            for(Consumable_order_details2__c cod:codList){
                if(cod.Product_OutDate__c >= Date.newInstance(FYear, 1, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 2, 1)){
                    ctm.IMS_Shipments_1__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 2, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 3, 1)){
                    ctm.IMS_Shipments_2__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear, 3, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 4, 1)){
                    ctm.IMS_Shipments_3__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 5, 1)){
                    ctm.IMS_Shipments_4__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 6, 1)){
                    ctm.IMS_Shipments_5__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 7, 1)){
                    ctm.IMS_Shipments_6__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 8, 1)){
                    ctm.IMS_Shipments_7__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 9, 1)){
                    ctm.IMS_Shipments_8__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 10, 1)){
                    ctm.IMS_Shipments_9__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 11, 1)){
                    ctm.IMS_Shipments_10__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.Product_OutDate__c < Date.newInstance(FYear-1, 12, 1)){
                    ctm.IMS_Shipments_11__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }else if(cod.Product_OutDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.Product_OutDate__c < Date.newInstance(FYear, 1, 1)){
                    ctm.IMS_Shipments_12__c+=cod.Detail_Count__c*cod.Intra_Trade_KRMB__c;
                }
            }
            newCtm.add(ctm);
        }
        if(ctmList.size()>0){
            update ctmList;
        }
        if(newCtm.size()>0){
            insert newCtm;
        }

    }

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