public with sharing class CurrentFYMoneyTmsBatch implements Database.Batchable<sObject> {
    String query;
    String accid;
    public CurrentFYMoneyTmsBatch() {}
    public CurrentFYMoneyTmsBatch(String accid) {
        this.accid = accid;
    }
    public Database.QueryLocator start(Database.BatchableContext BC) {

        query = 'SELECT id,Intra_Trade_List_RMB__c,Consumable_Category3__c,Consumable_Category2__c,Product_Type__c,Deliver_date__c,Used_account__r.Agent_Ref__c,Used_account__c,Used_Account_Type__c '
            + 'FROM Consumable_order_details2__c where id !=null and Inventory_Status__c = \'经销商库存\' and Deliver_date__c >=2024-01-01  ' ;
        if (accid !=null) {
            query += ' and Used_account__c = :accid';
        }
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<Consumable_order_details2__c> DcList) { 
        Map<String,double> map1= new Map<String,double>();
        List<Id> accList = new List<Id>();
        for (Consumable_order_details2__c dc : DcList) {
            if (dc.Intra_Trade_List_RMB__c !=0 && dc.Intra_Trade_List_RMB__c !=null) {
                String  key = '';
                Integer year = dc.Deliver_date__c.year();
                Integer month = dc.Deliver_date__c.month();
                if (dc.Used_Account_Type__c == '契約') {
                    key = dc.Used_account__r.Agent_Ref__c+'-'+year+'-'+month;
                    if (!accList.contains(dc.Used_account__r.Agent_Ref__c)) {
                        accList.add(dc.Used_account__r.Agent_Ref__c);
                    }
                    
                }else if(dc.Used_Account_Type__c == '販売店'){
                    key = dc.Used_account__c+'-'+year+'-'+month;
                    if (!accList.contains(dc.Used_account__c)) {
                        accList.add(dc.Used_account__c);
                    }
                }
                
                if (!map1.containsKey(key)) {
                    map1.put(key, dc.Intra_Trade_List_RMB__c);
                }else {
                    Double money = map1.get(key);
                    money += dc.Intra_Trade_List_RMB__c;
                    map1.put(key, money);
                }
            }
        }
        system.debug('map1='+map1);
        system.debug('accList='+accList);
        List<Dealer_ConsumableManage__c> dcmList = [select id,FY__c,Dealer_Name__c,FY_I_1__c,FY_I_10__c,FY_I_11__c,FY_I_12__c,FY_I_2__c,FY_I_3__c,FY_I_4__c,FY_I_5__c,FY_I_6__c,FY_I_7__c,FY_I_8__c,FY_I_9__c
                                                        from Dealer_ConsumableManage__c where Dealer_Name__c in :accList];
        List<Dealer_ConsumableManage__c> updateList = new List<Dealer_ConsumableManage__c>();
        for (Dealer_ConsumableManage__c dc : dcmList) {
            integer startIndex = dc.FY__c.indexOf('FY');
            String year = dc.FY__c.substring(startIndex+2, startIndex+6);
            Dealer_ConsumableManage__c dc1 = new Dealer_ConsumableManage__c();
            dc1.id = dc.id;
            for (Integer i=1;i<=12;i++) {
                String key = dc.Dealer_Name__c+'-'+year+'-'+i;
                if (map1.containsKey(key)) {
                    Double money = map1.get(key);
                    if (i==1) {
                        dc1.FY_I_1__c = money;
                    }
                    if (i==11) {
                        dc1.FY_I_11__c = money;
                    }
                    if (i==12) {
                        dc1.FY_I_12__c = money;
                    }
                    if (i==10) {
                        dc1.FY_I_10__c = money;
                    }
                    if (i==2) {
                        dc1.FY_I_2__c = money;
                    }
                    if (i==3) {
                        dc1.FY_I_3__c = money;
                    }
                    if (i==4) {
                        dc1.FY_I_4__c = money;
                    }
                    if (i==5) {
                        dc1.FY_I_5__c = money;
                    }
                    if (i==6) {
                        dc1.FY_I_6__c = money;
                    }
                    if (i==7) {
                        dc1.FY_I_7__c = money;
                    }
                    if (i==8) {
                        dc1.FY_I_8__c = money;
                    }
                    if (i==9) {
                        dc1.FY_I_9__c = money;
                    }
                }
            }
            updateList.add(dc1);
        }
        if (updateList.size()>0) {
            update updateList; 
        }

    }

    public void finish(Database.BatchableContext BC) {}
}