public with sharing class VirtualDealerToTmsBatch implements Database.Batchable<sObject>{
    String query;
    public VirtualDealerToTmsBatch() {

    }

    public Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'select id,name,Target_Source__c,CloseDate, Amount, Opportunity_Category__c ,RecordType.DeveloperName,OCM_Target_period_New__c  from Opportunity where (Opportunity_Category__c = \'ET\' or Opportunity_Category__c = \'ENG2\') and Target_Source__c =  \'SetProvinceTarget\'';
        
        return Database.getQueryLocator(query);
    }


    public void execute(Database.BatchableContext BC, List<Opportunity> OppoList) { 
        List<String> dealerNameList =  new List<String>();
        Map<String,String> dealerlocationList =  new Map<String,String>();
        Map<String,id> dealerNameList2 =  new Map<String,id>();
        Set<String> dealerNamemap =  new Set<String>();
        List<Dealer_ConsumableManage__c> dcmList = new  List<Dealer_ConsumableManage__c>();
        List<Dealer_ConsumableManage__c> dcmList2 = new  List<Dealer_ConsumableManage__c>();
        for (Opportunity opp : OppoList) {
            String year = opp.OCM_Target_period_New__c.split('FY')[1];
            
            if (Double.valueOf(year)>=2024) {
                system.debug('year:'+year);
                integer startIndex = opp.name.indexOf(':');
                String dealerName = opp.name.substring(startIndex+2, startIndex+5);
                if (!dealerNameList.contains(dealerName)) {
                    dealerNameList.add(dealerName);
                }
                String key = dealerName+':'+opp.Opportunity_Category__c+':'+opp.OCM_Target_period_New__c+':'+opp.Amount;
                dealerNamemap.add(key);
            }
            
        }
        // 虚拟经销商
        List<account> accList =[select id,name,OCM_man_province_HP__c from Account where ISVirtualDealer__c = true];
        for (account acc : accList) {
            for(String deName:dealerNameList)
                if (acc.name.indexOf(deName) != -1 && !dealerNameList2.containsKey(deName)) {
                    dealerNameList2.put(deName,acc.id);
                    dealerlocationList.put(acc.id,acc.OCM_man_province_HP__c);
                }
        }
        System.debug('dealerNameList='+dealerNameList);
        System.debug('dealerNameList2='+dealerNameList2);
        List<Dealer_ConsumableManage__c> dcList = [select id ,ET_ENG__c,FY__c,Dealer_Name__c,Dealer_Name__r.name from Dealer_ConsumableManage__c where Dealer_Name__c in :dealerNameList2.values() ];
        for (Dealer_ConsumableManage__c dc : dcList) {
            for (String key : dealerNamemap) {
                String name = key.split(':')[0];
                String oc = key.split(':')[1];
                String otp = key.split(':')[2];
                String Amount = key.split(':')[3];
                if (dc.Dealer_Name__r.name.indexOf(name)!=-1) {
                    if (dc.ET_ENG__c == 'ET' && oc =='ET' && dc.FY__c == otp) {
                        Dealer_ConsumableManage__c dcm =  new Dealer_ConsumableManage__c();
                        dcm.id = dc.id;
                        dcm.FY_P_1__c = Double.valueOf(Amount);
                        dcm.FY_P_10__c = Double.valueOf(Amount);
                        dcm.FY_P_11__c = Double.valueOf(Amount);
                        dcm.FY_P_12__c = Double.valueOf(Amount);
                        dcm.FY_P_2__c = Double.valueOf(Amount);
                        dcm.FY_P_3__c = Double.valueOf(Amount);
                        dcm.FY_P_4__c = Double.valueOf(Amount);
                        dcm.FY_P_5__c = Double.valueOf(Amount);
                        dcm.FY_P_6__c = Double.valueOf(Amount);
                        dcm.FY_P_7__c = Double.valueOf(Amount);
                        dcm.FY_P_8__c = Double.valueOf(Amount);
                        dcm.FY_P_9__c = Double.valueOf(Amount);
                        dcmList.add(dcm);
                        dealerNamemap.remove(key);
                    }
                    if (dc.ET_ENG__c == 'ENG2' && oc =='ENG' && dc.FY__c == otp) {
                        Dealer_ConsumableManage__c dcm =  new Dealer_ConsumableManage__c();
                        dcm.id = dc.id;
                        dcm.FY_P_1__c = Double.valueOf(Amount);
                        dcm.FY_P_10__c = Double.valueOf(Amount);
                        dcm.FY_P_11__c = Double.valueOf(Amount);
                        dcm.FY_P_12__c = Double.valueOf(Amount);
                        dcm.FY_P_2__c = Double.valueOf(Amount);
                        dcm.FY_P_3__c = Double.valueOf(Amount);
                        dcm.FY_P_4__c = Double.valueOf(Amount);
                        dcm.FY_P_5__c = Double.valueOf(Amount);
                        dcm.FY_P_6__c = Double.valueOf(Amount);
                        dcm.FY_P_7__c = Double.valueOf(Amount);
                        dcm.FY_P_8__c = Double.valueOf(Amount);
                        dcm.FY_P_9__c = Double.valueOf(Amount);
                        dcmList.add(dcm);
                        dealerNamemap.remove(key);
                    }
                }
            }
        }
        for (String  key : dealerNamemap) {
            String name = key.split(':')[0];
            String oc = key.split(':')[1];
            String otp = key.split(':')[2];
            String Amount = key.split(':')[3];
            if (dealerNameList2.get(name) !=null) {
                Dealer_ConsumableManage__c dcm =  new Dealer_ConsumableManage__c();
                if (oc=='ET') {
                    dcm.ET_ENG__c = oc; 
                }
                if (oc=='ENG2') {
                    dcm.ET_ENG__c = 'ENG'; 
                }
                dcm.FY_P_1__c = Double.valueOf(Amount);
                dcm.FY_P_10__c = Double.valueOf(Amount);
                dcm.FY_P_11__c = Double.valueOf(Amount);
                dcm.FY_P_12__c = Double.valueOf(Amount);
                dcm.FY_P_2__c = Double.valueOf(Amount);
                dcm.FY_P_3__c = Double.valueOf(Amount);
                dcm.FY_P_4__c = Double.valueOf(Amount);
                dcm.FY_P_5__c = Double.valueOf(Amount);
                dcm.FY_P_6__c = Double.valueOf(Amount);
                dcm.FY_P_7__c = Double.valueOf(Amount);
                dcm.FY_P_8__c = Double.valueOf(Amount);
                dcm.FY_P_9__c = Double.valueOf(Amount);
                dcm.FY__c = otp;
                String deid = dealerNameList2.get(name);
                dcm.TMS_Key__c = dealerNameList2.get(name)+'-'+dcm.ET_ENG__c+'-'+dcm.FY__c+'-'+dealerlocationList.get(deid);
                dcm.Dealer_Name__c = dealerNameList2.get(name);
                // dcm.Dealer_Name_Account__c = dealerNameList2.get(name);
                if (!dcmList2.contains(dcm)) {
                    dcmList2.add(dcm);
                }
            }
            

        }
        System.debug('dcmList='+dcmList);
        System.debug('dcmList2='+dcmList2);
        if (dcmList.size()>0) {
            update dcmList;
        }
        if (dcmList2.size()>0) {
            insert dcmList2;
        }
    }

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