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
public with sharing class RepairAgainAnMBCHandler extends Oly_TriggerHandler{
 
    private Map<Id, RepairAgainAn_M_BC__c> newMap;
    private Map<Id, RepairAgainAn_M_BC__c> oldMap;
    private List<RepairAgainAn_M_BC__c> newList;
    private List<RepairAgainAn_M_BC__c> oldList;
 
    public RepairAgainAnMBCHandler() {
        this.newMap = (Map<Id, RepairAgainAn_M_BC__c>) Trigger.newMap;
        this.oldMap = (Map<Id, RepairAgainAn_M_BC__c>) Trigger.oldMap;
        this.newList = (List<RepairAgainAn_M_BC__c>) Trigger.new;
        this.oldList = (List<RepairAgainAn_M_BC__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        setName();
    }
 
    protected override void afterInsert() {
    }
 
    protected override void beforeUpdate() {    
    }
 
    protected override void afterUpdate(){
        //更新成功后,需要向相关RC人员、OSH-SH/OSH-GZ品质人员发送邮件通知
        sendEmail();
    }
 
    //before insert
    private void setName() {
        //RepairAgainAn__c Set
        Set<Id> repairAnSet= new Set<Id>();
 
        for(RepairAgainAn_M_BC__c ram : newList){
            repairAnSet.add(ram.RepairAgainAn_request_name__c);
        }
 
        Map<Id,RepairAgainAn__c> repairMbcMap= new Map<Id,RepairAgainAn__c>([SELECT Id,SORC__c,(SELECT Id from RepairAgainAn_M_BC__r) from RepairAgainAn__c where Id in :repairAnSet]);
        for (RepairAgainAn_M_BC__c qisMbc : newList){
            if(repairMbcMap.containsKey(qisMbc.RepairAgainAn_request_name__c)){
                //QIS-MBC name设定规则  SORC的编号 - M-BC的选项 - 财年 - 顺序编号
                String tempName = null;
                tempName = repairMbcMap.get(qisMbc.RepairAgainAn_request_name__c).SORC__c ;
                tempName += '-' + qisMbc.M_BC__c;
                tempName += '-' + countFiscalyear();
 
                //顺序编号计算
                Integer repairSize = repairMbcMap.get(qisMbc.RepairAgainAn_request_name__c).RepairAgainAn_M_BC__r.size();
                if( repairSize == 0 ){
                     tempName += '-' + '001';
                } else {
                    Integer temp = repairMbcMap.get(qisMbc.RepairAgainAn_request_name__c).RepairAgainAn_M_BC__r.size()+1 ;
                    String repairAgainSizeString = '000' + temp ;
                    String cutString = repairAgainSizeString.substring(repairAgainSizeString.length()-3,repairAgainSizeString.length());
                    tempName += '-' + cutString;
                }
 
                qisMbc.Name = tempName;
            }
        }
    }
 
    //after update
    private void sendEmail(){
 
        EmailTemplate et=[Select id from EmailTemplate where name = '再修理分析M_BC回答内容更新' limit 1];
 
        List<Id> neadUpdateList = new List<Id>();
        for(RepairAgainAn_M_BC__c ram : newList){
            if(newMap.get(ram.Id).RepairAgainAn_M_BC_answer_detail__c != oldMap.get(ram.Id).RepairAgainAn_M_BC_answer_detail__c){
                neadUpdateList.add(ram.Id);
            }
        }
 
        String[] toAddresses = System.Label.RepairAgainAn_Sendmail.split(';');
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
        for(Id repairMbId: neadUpdateList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail = Messaging.renderStoredEmailTemplate(et.Id, null , repairMbId);
            mail.setSaveAsActivity(false);
 
            mail.setToAddresses(toAddresses);
            emails.add(mail);
        }
        
        Messaging.sendEmail(emails);
    }
 
    //计算财年
    private String countFiscalyear(){
        //财年
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        String flag = '';
        if( month < 4 ){
            year -= 1;
        }
        if( month < 4 || month > 9){
            flag = '2H\'';
        }else{
            flag = '1H\'';
        }
        Integer tempYear = year + 1;
        String currentPeriod = String.valueOf('FY' + tempYear + ' ' + flag);
 
        return currentPeriod;
    }
}