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
131
132
public with sharing class RepairQuoteTrigger {
 
    public static void ChangeRepair(List<Repair_Quotation__c> newList, Map<Id, Repair_Quotation__c> newMap, List<Repair_Quotation__c> oldList, Map<Id, Repair_Quotation__c> oldMap) {
        System.debug('===========> start');
        List<String> rqIds = new List<String>(); 
        Map<id,Repair_Quotation__c> RqMap = new Map<id,Repair_Quotation__c>();
        if (Trigger.isUpdate) {
            Boolean flag = false;
            for(Repair_Quotation__c rq : newList){
                if(Trigger.isUpdate && rq.ListPrice__c != oldMap.get(rq.id).ListPrice__c){
                    //rqIds.add(rq.id);
                    flag = true;
                    rqIds.add(rq.id);
                    RqMap.put(rq.id, rq);
                }
            }
            if(flag == false){
                return;
            }
            for(Repair_Quotation__c rq : newList){
                if(Trigger.isUpdate && rq.ListPrice__c != oldMap.get(rq.id).ListPrice__c){
                    // rqIds.add(rq.id);
                    // RqMap.put(rq.id, rq);
                }
            }
        }else if(Trigger.isInsert){
            for(Repair_Quotation__c rq : newList){
                rqIds.add(rq.id);
                RqMap.put(rq.id, rq);
            }
        }
 
        List<Repair__c> RepairUpdateList = new List<Repair__c>();
        if(rqIds.size() > 0){
            //修理的同期中的修理报价de报价金额改变的修理
            List<Repair__c> RpList = [select id,Repair_Quotation_Id__c from Repair__c where Repair_Quotation_Id__c in :rqIds];
            if(RpList.size() > 0){
                for(Repair__c rp : RpList){
                    Repair_Quotation__c Rqa = RqMap.get(rp.Repair_Quotation_Id__c);
                    rp.Repair_Quotation_Price__c = Rqa.ListPrice__c;
                    RepairUpdateList.add(rp);
                }
                update RepairUpdateList;
            }
        }
        System.debug('===========> end');
    }
 
 
    public static void SendEmailForRentalApproval(List<Repair_Quotation__c> newList, Map<Id, Repair_Quotation__c> newMap, List<Repair_Quotation__c> oldList, Map<Id, Repair_Quotation__c> oldMap) {
        Set<Id> repairIds = new Set<Id>();
        List<Repair_Quotation__c> repairQuotationList = new List<Repair_Quotation__c>();
 
        for(Repair_Quotation__c rq : newList) {
            if(Trigger.isUpdate 
                && rq.Rental_Apply_Discount_Status__c != oldMap.get(rq.id).Rental_Apply_Discount_Status__c
                && rq.Rental_Apply_Discount_Status__c == '批准'){
                repairIds.add(rq.Repair__c);
                repairQuotationList.add(rq);
            }
        }
 
        if(repairIds.size() > 0){
            //查询修理中对应的选择的服务方式和维修中心
            Map<Id, Repair__c> repairMap = new Map<Id, Repair__c>([
                        select id,On_site_repair__c,work_location_select__c 
                        from Repair__c 
                        where Id in :repairIds]);
 
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
            // 获取Email Template
            EmailTemplate et = [Select id from EmailTemplate where name = '备品减价申请---批准通过' limit 1];
            // Email
            String userEmail = null;
 
            for (Repair_Quotation__c rqTemp :repairQuotationList) {
                String addToEmail = null;
                userEmail = rqTemp.RentalApplyDiscountApplyPersonEmail_sys__c;
                if (!repairMap.isEmpty() && repairMap.containsKey(rqTemp.Repair__c)) {
                    // 服务方式
                    String siteRepair = repairMap.get(rqTemp.Repair__c).On_site_repair__c;
                    // 维修中心
                    String workLocation = repairMap.get(rqTemp.Repair__c).work_location_select__c;
 
                    if (siteRepair == '直送OGZ修理') {
                        // 办事处通知组群设定OGZ
                        addToEmail = System.Label.EmailForRentalApproval_OGZ;
                    } else if (siteRepair == '直送SORC修理') {
                        // 办事处通知组群设定SORC
                        addToEmail = System.Label.EmailForRentalApproval_SORC;
                    } else if (siteRepair == 'RC修理') {
                        if (workLocation == '上海办事处') {
                            // 上海办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_SH;
                        } else if (workLocation == '杭州办事处') {
                            // 杭州办事处 + 上海办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_HZ + ';' + System.Label.EmailForRentalApproval_RC_SH;
                        } else if (workLocation == '广东办事处') {
                            // 广东办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_GZ;
                        } else if (workLocation == '成都办事处') {
                            // 成都办事处 + 广东办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_CD + ';' + System.Label.EmailForRentalApproval_RC_GZ;
                        } else if (workLocation == '北京办事处') {
                            // 北京办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_BJ;
                        } else if (workLocation == '沈阳办事处') {
                            // 沈阳办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_SY;
                        } else if (workLocation == '西安办事处') {
                            // 西安办事处
                            addToEmail = System.Label.EmailForRentalApproval_RC_XA;
                        }
                    }
                }
 
                if (String.isNotBlank(addToEmail)) {
                    userEmail = userEmail + ';' + addToEmail;
                }
 
                String[] toAddresses = userEmail.split(';');
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail = Messaging.renderStoredEmailTemplate(et.Id, null , rqTemp.Id);
                mail.setSaveAsActivity(false);
                mail.setToAddresses(toAddresses);
                emails.add(mail);
            }
 
            Messaging.sendEmail(emails);
        }
    }
}