liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * 测试类: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;
                    // 统计服务合同上的认款金额
                    MaintanceContractPack_countAllAmount(maintenanceContractId);
                }
            }
        }
    }
    //add  wangweipeng       2021/11/12                end
 
 
    // add wuyulun          2023/08/25              start
    // 统计服务合同的所有认款金额
    private void MaintanceContractPack_countAllAmount(List<String> maintenanceContractId){
        List<Maintenance_Contract__c > mccList = [select id,name,MaintanceContractPack__c from Maintenance_Contract__c where MaintanceContractPack__c != null and id in :maintenanceContractId];
        Map<String, List<Maintenance_Contract__c>> mccMap = new Map<String, List<Maintenance_Contract__c>>();
        for (Maintenance_Contract__c mcc : mccList) {
            List<Maintenance_Contract__c>  mValue =  mccMap.get(mcc.MaintanceContractPack__c);
           if (mValue == null) {
             // 通过服务合同id 获取所有维修合同
                List<Maintenance_Contract__c> mc = [select id,Contract_Amount__c,Is_Recognition_Model_True__c,All_Amount__c from Maintenance_Contract__c where MaintanceContractPack__c =:mcc.MaintanceContractPack__c];
                mccMap.put(mcc.MaintanceContractPack__c, mc);
           }else {    
           }
        }
        
        System.debug('打印'+mccMap);
        List<MaintanceContractPack__c> mpcList = new List<MaintanceContractPack__c>();
        for (String key : mccMap.keySet()) {
            Decimal countAmount = 0.0;                   
            for (Maintenance_Contract__c m : mccMap.get(key)) {
                countAmount += m.All_Amount__c == null ? 0 : m.All_Amount__c; // 2024-2-27 WYl 解决计算报错 update
            }
            MaintanceContractPack__c mpc = new MaintanceContractPack__c();
            mpc.id = key;
            mpc.All_Amount__c = countAmount;
            mpcList.add(mpc);
        }
        update mpcList;
    }
    // add wuyulun          2023/08/25              end
}