global class DBUpdateContractBatch implements Database.Batchable<sObject>{
    public String query;
    public String conId;
    
    global DBUpdateContractBatch() {
        this.query = query;
    }
    global DBUpdateContractBatch(String conId) {
        this.query = query;
        this.conId = conId;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {

        // 查询打包合同
        query = 'select id,name,RecordType.DeveloperName,Contract_Consumption_rate__c,Sum_repair_price_new__c from MaintanceContractPack__c';
        if (conId != null && conId != '') {
            query += ' where Id = :conId';
        }   
        // System.debug('执行的SQL为：'+query);
        System.debug('执行结果：'+Database.getQueryLocator(query));
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<MaintanceContractPack__c> scope) {
        // System.debug('数据'+scope);
        List<MaintanceContractPack__c> MpcList = new List<MaintanceContractPack__c>();
        for (MaintanceContractPack__c mpc : scope) {
            // 获取 大合同下所有的小合同
            List<Maintenance_Contract__c> MCList = [select
												id,
												Name,
												currentTotalRepairAmount__c,
                                                previousTotalRepairAmount__c,
                                                GuaranteePrice_Amount__c,
                                                contractAmountForRate__c,
                                                RecordType.DeveloperName,
                                                // 新增 2023-09-04 WYL 合同期间修理金额 start
                                                Sum_repair_price_new__c,    
                                                // 新增 2023-09-04 WYL 合同期间修理金额 end
                                                // 新增 2023-09-27 WYL 维修成本总金额 start
                                                Repair_Cost_Sum__c,
                                                // 新增 2023-09-27 WYL 维修成本总金额 end
                                                Status__c
												from
												Maintenance_Contract__c
												where 
												MaintanceContractPack__c = :mpc.Id and Contract_Consumption_rate__c != 0 and Status__c in( '契約','契約満了')];
            if (MCList.size()>0) {
                Double currentTotalRepairAmoun=0.00; // 本期修理金额总额(分子用1)
                Double previousTotalRepairAmount=0.00; // 上期修理金额总额(分子用2)
                Double GuaranteePrice_Amount=0.00; // 计提总额
                Double contractAmountForRate=0.00; // 合同金额(消费率计算用分母)
                Double Sum_repair_price_new = 0.00; // 累计合同期间修理金额
                Double Repair_Cost_Sum = 0.00; // 维修成本总金额
                Double sum = 0.00; //计算结果
                for (Maintenance_Contract__c mc : MCList) {
                    if (mc.currentTotalRepairAmount__c != null) {
                        currentTotalRepairAmoun += mc.currentTotalRepairAmount__c;
                    }
                    if ( mc.previousTotalRepairAmount__c != null) {
                        previousTotalRepairAmount += mc.previousTotalRepairAmount__c;
                    }
                    if ( mc.GuaranteePrice_Amount__c != null) {
                        GuaranteePrice_Amount += mc.GuaranteePrice_Amount__c;
                    }
                    if ( mc.contractAmountForRate__c != null) {
                        contractAmountForRate += mc.contractAmountForRate__c;
                    }
                    if (mc.Sum_repair_price_new__c != null) {
                        Sum_repair_price_new += mc.Sum_repair_price_new__c;
                    }
                    if (mc.Repair_Cost_Sum__c != null) {
                        Repair_Cost_Sum += mc.Repair_Cost_Sum__c;
                    }
                    System.debug('合同：'+mc.name+'是'+mc.RecordType.DeveloperName+'合同');
                }
                    // 判断当前合同是否为多年保修合同
                    if (MCList[0].RecordType.DeveloperName.equals('VM_Contract')) {
                        //(本期修理金额总额(分子用1)+上期修理金额总额(分子用2))/计提总额*1.13
                        sum = (currentTotalRepairAmoun+previousTotalRepairAmount)/GuaranteePrice_Amount*1.13;
                        // System.debug('多年保修合同：'+sum);
                    }else {
                        //(本期修理金额总额(分子用1)+上期修理金额总额(分子用2))/合同金额(消费率计算用分母)
                        sum = (currentTotalRepairAmoun+previousTotalRepairAmount)/contractAmountForRate;
                        // System.debug('非多年保修合同：'+sum);
                    }
                mpc.Contract_Consumption_rate__c = sum*100;
                mpc.Sum_repair_price_new__c = Sum_repair_price_new;
                mpc.Repair_Cost_Sum__c = Repair_Cost_Sum;
                MpcList.add(mpc);
            }
        }
        if (MpcList.size()>0) {
            update MpcList;
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}