/**
 * 测试类：PaymentMaintenanceContractTriggerHTest
 */
public with sharing class PaymentMaintenanceContractTriggerHandler  extends Oly_TriggerHandler {
    
    private Map<Id, Payment_Maintenance_Contract__c> newMap;
    private Map<Id, Payment_Maintenance_Contract__c> oldMap;
    private List<Payment_Maintenance_Contract__c> newList;
    private List<Payment_Maintenance_Contract__c> oldList;


    public PaymentMaintenanceContractTriggerHandler() {
        this.newMap = (Map<Id, Payment_Maintenance_Contract__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Payment_Maintenance_Contract__c>) Trigger.oldMap;
        this.newList = (List<Payment_Maintenance_Contract__c>) Trigger.new;
        this.oldList = (List<Payment_Maintenance_Contract__c>) Trigger.old;
        
    }

    protected override void afterUpdate() {
        //add  wangweipeng       2021/11/12                start
        //统计维修合同的所有认款金额
        countAllAmount();
        //add  wangweipeng       2021/11/12                end
    }

    protected override void afterInsert() {
        //add  wangweipeng       2021/11/12                start
        //统计维修合同的所有认款金额
        countAllAmount();
        //add  wangweipeng       2021/11/12                end
    }


    //add  wangweipeng       2021/11/12                start
    //统计维修合同的所有认款金额
    private void countAllAmount() {
        if(newList != null && newList.size() > 0){
            List<String> maintenanceContractId = new List<String>();
            for(Payment_Maintenance_Contract__c pmcc : newList){
                if(Trigger.isUpdate){
                    //如果认款金额或认款删除标识有修改，那么重新汇总认款
                    if(pmcc.Amount__c != oldMap.get(pmcc.id).Amount__c || pmcc.DeleteFlag__c != oldMap.get(pmcc.id).DeleteFlag__c){
                        maintenanceContractId.add(pmcc.Find_Contact__c);
                    }
                }
                if(Trigger.isInsert){
                    //判断 认款金额是否为空或不等于0
                    if(pmcc.Amount__c != null && pmcc.Amount__c != 0){
                        maintenanceContractId.add(pmcc.Find_Contact__c);
                    }
                }
            }

            if(maintenanceContractId.size() > 0){
                //查找对应的维修合同
                List<Maintenance_Contract__c> mccData = [select id,Contract_Amount__c,Is_Recognition_Model_True__c from Maintenance_Contract__c where id in :maintenanceContractId];
                if(mccData != null && mccData.size() > 0){
                    List<String> mccList = new List<String>();
                    for(Maintenance_Contract__c mcc : mccData){
                        mccList.add(mcc.id);
                    }
                    //查找维修合同的所有认款信息
                    List<Payment_Maintenance_Contract__c> allPMCc = [select id,name,Amount__c,DeleteFlag__c,Find_Contact__c from Payment_Maintenance_Contract__c where Amount__c != 0.0 and Amount__c != null and Find_Contact__c in :mccList];
                    for(Maintenance_Contract__c mcc : mccData){
                        Decimal countAmount = 0.0;
                        for(Payment_Maintenance_Contract__c pmcc : allPMCc){
                            // gzw bug fix 统计回款价格是区分合同 20220721
                            // if(pmcc.Amount__c != 0 || pmcc.Amount__c != null){//判断认款金额不为0
                            //     //如果删除标识为X，那么把认款金额转为负数,然后相加
                            //     if(pmcc.DeleteFlag__c == 'X' ){
                            //         countAmount += (pmcc.Amount__c - pmcc.Amount__c * 2);
                            //     }else{
                            //         countAmount += pmcc.Amount__c;
                            //     }
                            // }
                            if (pmcc.Find_Contact__c == mcc.Id) {
                                if(pmcc.Amount__c != 0 || pmcc.Amount__c != null){//判断认款金额不为0
                                    //如果删除标识为X，那么把认款金额转为负数,然后相加
                                    if(pmcc.DeleteFlag__c == 'X' ){
                                        countAmount += (pmcc.Amount__c - pmcc.Amount__c * 2);
                                    }else{
                                        countAmount += pmcc.Amount__c;
                                    }
                                } 
                            }
                        }
                        mcc.All_Amount__c = countAmount;
                    }
                    update mccData;
                }
            }
        }
    }
    //add  wangweipeng       2021/11/12                end
}