/**************************************************************************************************
@Author: Denny陈帮才
@Name: SummaryConsumptionRateBatch
@CreateDate: 2023/05/08
@Description: 多年保修合同  合同消费率分子分母赋值
@Version 1.0
*****************************************************************************************************/
global class SummaryConsumptionRateBatch implements Database.Batchable<sObject>,Database.Stateful {

    global SummaryConsumptionRateBatch() {
    }

    global List<Maintenance_Contract__c> start(Database.BatchableContext bc) {
        List<Maintenance_Contract__c> mcUpdateList =  [SELECT Id,VM_Consumption_Rate_Molecular__c,VM_Consumption_Rate_Denominator__c FROM 
                                    Maintenance_Contract__c WHERE RecordType_Name__c = '多年保修合同' AND RecordType_DeveloperName__c = 'VM_Contract' AND (Status__c = '契約' OR Status__c = '契約満了')];
        return mcUpdateList;
    }

    global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> mcUpdateList) {

        List<String> mcIds = new List<String>();
        for (Maintenance_Contract__c mcObj : mcUpdateList) {
            mcIds.add(mcObj.Id);
        }
        List<Maintenance_Contract_Asset__c> scope = [SELECT Id,Summary_FZ__c,Summary_FM_Year__c,Maintenance_Contract__c FROM Maintenance_Contract_Asset__c WHERE Maintenance_Contract__c IN :mcIds 
                                                AND Maintenance_Contract__r.RecordType_DeveloperName__c ='VM_Contract' AND Asset__r.RecordType.Name!='备品' AND 
                                                (Maintenance_Contract__r.Status__c='契約' OR Maintenance_Contract__r.Status__c='契約満了')];
        //2023/05/08  多年保修合同  合同消费率分子分母赋值 start
        List<String>  mcIdList= new List<String>();
        Map<Id,Decimal>  vmMolecularMap = new Map<Id,Decimal>();
        Map<Id,Decimal>  vmDenominatorMap = new Map<Id,Decimal>();
        for ( Maintenance_Contract_Asset__c mc : scope) {
            Decimal vmMolecular = 0;
            Decimal vmDenominator = 0;
            if (vmMolecularMap.containsKey(mc.Maintenance_Contract__c)) {
                vmMolecular = vmMolecularMap.get(mc.Maintenance_Contract__c);
                vmDenominator = vmDenominatorMap.get(mc.Maintenance_Contract__c);
            }
            //Summary_FM_Year__c  分母   Summary_FZ__c  分子
            vmMolecular += (mc.Summary_FZ__c == null ? 0 : mc.Summary_FZ__c);
            vmDenominator += (mc.Summary_FM_Year__c == null ? 0 : mc.Summary_FM_Year__c);

            mcIdList.add(mc.Maintenance_Contract__c);

            vmMolecularMap.put(mc.Maintenance_Contract__c,vmMolecular);
            vmDenominatorMap.put(mc.Maintenance_Contract__c,vmDenominator);
        }

        for (Maintenance_Contract__c mcObj : mcUpdateList) {
            if (vmMolecularMap.containsKey(mcObj.Id)) {
                mcObj.VM_Consumption_Rate_Molecular__c = vmMolecularMap.get(mcObj.Id);
                mcObj.VM_Consumption_Rate_Denominator__c = vmDenominatorMap.get(mcObj.Id)*3;
            }
        }
        update mcUpdateList;
        //2023/05/08  多年保修合同  合同消费率分子分母赋值 end

    }

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