global class HistoryTotalAmountBatch implements Database.Batchable<sObject> {
    public String query;
    public List<String> idl;
    global HistoryTotalAmountBatch() {
        this.query = query;
    }

    global HistoryTotalAmountBatch(List<String> idl) {
        this.idl = idl;
        this.query = query;
    } 
    global Database.QueryLocator start(Database.BatchableContext bc) {
          query = 'SELECT Id,Total_Contract_Price__c From Maintenance_Contract_Estimate__c Where  Maintenance_Contract_Status__c = \'契約\' and Dealer__r.Is_Active_Formula__c !=\'无效\' and Process_Status__c!=\'不批准\' and Process_Status__c!=\'中止\' ';
          if (idl!=null) {
              query+=' and id In :idl';
          }
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Estimate__c> mceList) {
        List<String> mceIds = new List<String>();
        Map<String,Maintenance_Contract_Estimate__c> map1 = new Map<String,Maintenance_Contract_Estimate__c>();
        Map<String,Double> map2 = new Map<String,Double>();
        for (Maintenance_Contract_Estimate__c mce :mceList) {
            mceIds.add(mce.id);
        }
        List<Maintenance_Contract_Asset_Estimate__c> mcaeList =  [SELECT id,Product_Manual__c,Product_Manual__r.Maintenance_Price_Year__c,Asset__r.Product2.Maintenance_Price_Year__c,Asset__c,Maintenance_Contract_Estimate__r.id,Maintenance_Contract_Estimate__r.Contract_Range__c FROM Maintenance_Contract_Asset_Estimate__c WHERE Maintenance_Contract_Estimate__c IN :mceIds];
        for (String mceid:mceIds) {
            Maintenance_Contract_Estimate__c mce = new Maintenance_Contract_Estimate__c();
            for (Maintenance_Contract_Asset_Estimate__c mcae: mcaeList) {
                Decimal price = 0;
                if (mcae.Asset__c != null) {
                    system.debug('mcae.Asset__c=='+mcae.Asset__r.Product2.Maintenance_Price_Year__c);
                    price = mcae.Asset__r.Product2.Maintenance_Price_Year__c;
                }
                if (mcae.Product_Manual__c != null) {
                    system.debug('mcae.Product_Manual__c =='+mcae.Product_Manual__r.Maintenance_Price_Year__c);
                    price =  mcae.Product_Manual__r.Maintenance_Price_Year__c;
                }
                if (price != 0 && price!=null) {
                    if (mceid == mcae.Maintenance_Contract_Estimate__r.id ) {
                       
                            if (map1.containsKey(mceid)) {
                            Maintenance_Contract_Estimate__c mce1 = map1.get(mceid);
                            
                            if (mce1 !=null) {  
                                mce1.Total_Contract_Price__c += price;
                                map1.remove(mceid);
                                map1.put(mceid,mce1);
                            }
                            }else{
                                mce.id = mceid;
                                mce.Total_Contract_Price__c = price;
                                map1.put(mceid,mce);
                            }
                        if (mcae.Maintenance_Contract_Estimate__r.Contract_Range__c!=null) {
                            if (!map2.containsKey(mceid)) {
                                 map2.put(mceid,mcae.Maintenance_Contract_Estimate__r.Contract_Range__c);
                            }
                        }
                    }
                }
            }
        }
        for( String mceid :map1.keySet()){
           Maintenance_Contract_Estimate__c mce =  map1.get(mceid);
           if (map2.get(mceid)!=null) {
               Double month = map2.get(mceid);
               Decimal totalPrice = mce.Total_Contract_Price__c;
               totalPrice = totalPrice/12*month;
               mce.Total_Contract_Price__c = totalPrice;
               map1.remove(mceid);
               map1.put(mceid,mce);
           }
        }
        if (map1.keySet()!=null) {
            update map1.values();
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}