global class MCUpdateEstimateCostBatch implements Database.Batchable<sObject> {
    // 设定运行的维修合同的ID
    public string TestID;

    // 设定运行的维修合同的list<ID>
    public list<string> TestIDList;

    // 报错的维修合同ID
    public String logstr = '';

    // 报错信息
    public String errorstr = '';

    global Integer totalCount = 0; // 总件数
    global Integer failedCount = 0; // 失败件数

    global List<String> emailMessages = new List<String>();

    global MCUpdateEstimateCostBatch() {

    }

    global MCUpdateEstimateCostBatch(string TestID ) {
        this.TestID = TestID;
    }

    global MCUpdateEstimateCostBatch(list<string> TestIDList ) {
        this.TestIDList = TestIDList;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        string query =
            ' select id ,  Contract_Amount__c , Estimation_Id__c, RecordType_DeveloperName__c, ' +
            ' Estimation_Id__r.Asset_Repair_Sum_Price__c, '+
            'Estimation_Id__r.NewEstimation_Amount__c ,Estimate_Trial_Money__c, '+ 
            'Estimation_Id__r.Asset_Sum_Price__c '+
            '  from Maintenance_Contract__c '
            + ' where RecordType_DeveloperName__c = \'NewMaintenance_Contract\' ';
        if (string.isNotBlank(TestID) ) {
            query += ' and id =: TestID';
        } else if (TestIDList != null && TestIDList.size() > 0) {
            query += ' and id in: TestIDList';
        }
        system.debug('query:'+query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> MCList) {
        set<id> MCIDSet = new set<id>();
        Map<id,Maintenance_Contract__c> MCMap = new Map<id,Maintenance_Contract__c>();
        for (Maintenance_Contract__c tempMC : MCList) {
            mcIDSet.add(tempMC.id);
            MCMap.put(tempMC.id, tempMC);
        }
        list<Maintenance_Contract_Asset__c> UpdateMCAList = new list<Maintenance_Contract_Asset__c>();
        map<id,Maintenance_Contract_Asset_Estimate__c> UpdateMCAEMap = new map<id,Maintenance_Contract_Asset_Estimate__c>();
        list<Maintenance_Contract_Asset__c> MCAList = 
                    [select id, Estimate_List_Price_All__c, Estimate_Cost__c,
                        Maintenance_Contract_Asset_Estimate__c, 
                        Maintenance_Contract_Asset_Estimate__r.Estimate_Cost__c,
                        Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Maintenance_Price__c,
                        Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Asset_Repair_Sum_Price__c,
                        Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c,
                        Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.GuidePrice_Up__c,
                        Maintenance_Contract__c
                        from Maintenance_Contract_Asset__c 
                        where Maintenance_Contract__c in: mcIDSet];
        for (Maintenance_Contract_Asset__c tempMCA : MCAList) {
            decimal tempEC = 0;
            decimal realprice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Maintenance_Price__c - tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Asset_Repair_Sum_Price__c;
            decimal assetListPrice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c == null? 0 : tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c;
            decimal guidePriceUp = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.GuidePrice_Up__c;
            tempEC = ((realprice)*( assetListPrice / guidePriceUp)).setScale(2);
            if(tempMCA.Maintenance_Contract_Asset_Estimate__c != null
            &&!UpdateMCAEMap.containskey(tempMCA.Maintenance_Contract_Asset_Estimate__c)){
                // decimal realprice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Maintenance_Price__c - tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Asset_Repair_Sum_Price__c;
                // decimal assetListPrice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c == null? 0 : tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c;
                // decimal guidePriceUp = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.GuidePrice_Up__c;
                // tempEC = ((realprice)*( assetListPrice / guidePriceUp)).setScale(2);
                Maintenance_Contract_Asset_Estimate__c tempMCAE = new Maintenance_Contract_Asset_Estimate__c();
                tempMCAE.id = tempMCA.Maintenance_Contract_Asset_Estimate__c;
                tempMCAE.Estimate_Cost__c = tempEC;
                UpdateMCAEMap.put(tempMCAE.id,tempMCAE);
            }
            System.debug(' ++++ ' + realprice + ' ++++ ' + assetListPrice + ' ++++ ' + guidePriceUp);
            if(tempMCA.Estimate_Cost__c != tempEC){
                Maintenance_Contract_Asset__c newMCA = new Maintenance_Contract_Asset__c();
                newMCA.id = tempMCA.id;
                newMCA.Estimate_Cost__c = tempEC;
                UpdateMCAList.add(newMCA);
            }
        }
        if( UpdateMCAList.size() > 0 ){
            Database.SaveResult[] saveResults = Database.update(UpdateMCAList, false);
            totalCount = + saveResults.size();
            for (Integer i = 0; i < saveResults.size(); i++) {
                if (!saveResults.get(i).isSuccess() || Test.isRunningTest()) {
                    logstr += '维修合同保有设备:' + UpdateMCAList.get(i).Id + ' ,';
                    errorstr += saveResults.get(i).getErrors() + '\n';
                    failedCount ++;
                }
            }
        }
        if( UpdateMCAEMap.size() > 0 ){
            Database.SaveResult[] saveResults = Database.update(UpdateMCAEMap.values(), false);
            totalCount = + saveResults.size();
            for (Integer i = 0; i < saveResults.size(); i++) {
                if (!saveResults.get(i).isSuccess()) {
                    logstr += '维修合同报价保有设备:' + UpdateMCAEMap.values().get(i).Id + ' ,';
                    errorstr += saveResults.get(i).getErrors() + '\n';
                    failedCount ++;
                }
            }
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}