高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
131
132
133
134
135
public without sharing class QISMBCTrigger {
    // QISMBCのId
    public static Set<Id> QISMBS_Ids = new Set<Id>();
    
    // after insert, after update
    public static void setQISValues(List<QIS_M_BC__c> newList, List<QIS_M_BC__c> oldList, Map<Id, QIS_M_BC__c> newMap, Map<Id, QIS_M_BC__c> oldMap) {
        List<QIS_Report__c> updQisList = new LIst<QIS_Report__c>();
        for (QIS_M_BC__c qm : newList) {
            if (qm.QIS_request_name__c != null) {
                QIS_Report__c qr = new QIS_Report__c(Id = qm.QIS_request_name__c);
                // ①
                /*
                if (qm.QIS_submit_date_for_MBC__c != null) {
                    qr.Wether_publish_QIS_not__c = '发行';
                } else {
                    qr.Wether_publish_QIS_not__c = '不发行';
                }
                */
                // ②
                qr.QIS_no_for_m_BC__c = qm.Name;
                // ③
                qr.QIS_submit_date_for_MBC__c = qm.QIS_submit_date_for_MBC__c;
                qr.QIS_M_BC_submit_person__c = qm.OwnerAlias__c;
                // ④
                qr.QIS_M_BC_reception__c = qm.QIS_M_BC_reception__c;
                // ⑤
                qr.QIS_M_BC_reply_date__c = qm.QIS_M_BC_answer_day__c;
                // ⑥
                qr.Received_feiqi_date__c = qm.Received_feiqi_date__c;
                // ⑦
                qr.QIS_M_BC_answer__c = qm.QIS_M_BC_answer_detail__c;
                
                updQisList.add(qr);
            }
        }
        if (updQisList.size() > 0) update updQisList;
    }
    
    // before insert, before update
    public static void setMails(List<QIS_M_BC__c> newList, List<QIS_M_BC__c> oldList, Map<Id, QIS_M_BC__c> newMap, Map<Id, QIS_M_BC__c> oldMap) {
        // 使用的QISID
        Map<String, String> use_qisid = new Map<String, String>();
        for (QIS_M_BC__c local : newList) {
            if (use_qisid.containskey(local.QIS_request_name__c) == false) {
                use_qisid.put(local.QIS_request_name__c, local.id);
            }
        }
        // 检索QIS的mail信息
        Map<String, QIS_Report__c> qisMap = new Map<String, QIS_Report__c>();
        //LJPH-C5H8ST QIS判定邮件抄送添加 追加OCM_Repair_Mail1__c字段 liuyan
        List<QIS_Report__c> qisList = [select id, OCM_Manager_Mail__c, OCM_Member_Mail__c,OCM_Repair_Mail__c,OCM_Repair_Mail1__c 
                                         from QIS_Report__c 
                                        where id in :use_qisid.keyset()];
        for (QIS_Report__c qis : qisList) {
            if (qisMap.containskey(qis.Id) == false) {
                qisMap.put(qis.Id, qis);
            }
        }
        // repair,Member,Manager的mail拷贝到QISMBC
        for (QIS_M_BC__c local : newList) {
            QIS_Report__c qis = qisMap.get(local.QIS_request_name__c);
            local.OCM_Manager_Mail__c = qis == null ? null : qis.OCM_Manager_Mail__c;
            local.OCM_Member_Mail__c = qis == null ? null : qis.OCM_Member_Mail__c;
            local.OCM_Repair_Mail__c = qis == null ? null : qis.OCM_Repair_Mail__c;
            //LJPH-C5H8ST QIS判定邮件抄送添加 追加OCM_Member_Mail1__c字段 liuyan START
            local.OCM_Repair_Mail1__c = qis == null ? null : qis.OCM_Repair_Mail1__c;
            //END
        }
    }
    
    // after insert, after update, after delete
    public static Boolean checkAndSetNo(List<QIS_M_BC__c> newList, List<QIS_M_BC__c> oldList, Map<Id, QIS_M_BC__c> newMap, Map<Id, QIS_M_BC__c> oldMap) {
        // 新规或更新后
        if (Trigger.isInsert || Trigger.isUpdate) {
            // 使用的QISID
            Map<String, String> use_qisid = new Map<String, String>();
            for (QIS_M_BC__c local : newList) {
                if (use_qisid.containskey(local.QIS_request_name__c) == false) {
                    use_qisid.put(local.QIS_request_name__c, local.id);
                }
            }
            // 所有使用这些QISID的QISMBC
            Map<String, String> all_qismbc = new Map<String, String>();
            List<QIS_M_BC__c> qismbcList = [select id,name,QIS_request_name__c 
                                              from QIS_M_BC__c 
                                             where QIS_request_name__c in :use_qisid.keyset()];
            for (QIS_M_BC__c qismbc : qismbcList) {
                if (qismbc.QIS_request_name__c != null && qismbc.id != use_qisid.get(qismbc.QIS_request_name__c)) {
                    QIS_M_BC__c q = newMap.get(use_qisid.get(qismbc.QIS_request_name__c));
                    q.addError('一个QIS只能创建一个QISMBC');
                    return false;
                }
            }
        }
        
        // 更新QIS的QIS No(M-BC)
        List<QIS_Report__c> qisList = new List<QIS_Report__c>();
        List<QIS_Report__c> oldqisList = new List<QIS_Report__c>();
        if (Trigger.isInsert) {
            // 新建QIS_M_BC全部更新
            for (QIS_M_BC__c local : newList) {
                QIS_Report__c qis = new QIS_Report__c(id=local.QIS_request_name__c, QIS_no_for_m_BC__c=local.id);
                qisList.add(qis);
            }
        }
        if (Trigger.isUpdate) {
            for (QIS_M_BC__c local : newList) {
                QIS_M_BC__c old = oldMap.get(local.Id);
                // QIS号码变更时,需要更新
                if (old.QIS_request_name__c != local.QIS_request_name__c) {
                    QIS_Report__c qis_old = new QIS_Report__c(id=old.QIS_request_name__c, QIS_no_for_m_BC__c='');
                    oldqisList.add(qis_old);
                    QIS_Report__c qis_new = new QIS_Report__c(id=local.QIS_request_name__c, QIS_no_for_m_BC__c=local.Name);
                    qisList.add(qis_new);
                }
            }
        }
        if (Trigger.isDelete) {
            // 删除QIS_M_BC全部更新
            for (QIS_M_BC__c local : oldList) {
                QIS_Report__c qis = new QIS_Report__c(id=local.QIS_request_name__c, QIS_no_for_m_BC__c='');
                oldqisList.add(qis);
            }
        }
        // 更新QIS
        if (oldqisList.size() > 0) {
            update oldqisList;
        }
        if (qisList.size() > 0) {
            update qisList;
        }
        
        return true;
    }
}