高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
public with sharing class SI_Opportunity_ResponseController {
    public String opporID{get;set;}
    public String MailDetail{get;set;}
    public String Cc{get;set;}
    public String MailTarget {get;set;}
    public List<Opportunity> oppoList {get;set;}
    public Opportunity oppEle {get;set;}
    public String isShowFlg {get;set;}
    public String Ccid {get;set;}
    public String mailType {get;set;}
    public String mailStatus {get;set;}
    public SI_Opportunity_ResponseController() {
        
    }
    public SI_Opportunity_ResponseController(ApexPages.StandardController stdController) {
        List<Opportunity> oppoList = new List<Opportunity>();
        opporID = stdController.getId();
        if(opporID==null||opporID == ''){
            opporID = ApexPages.currentPage().getParameters().get('id');
        }
        
        isShowFlg = ApexPages.currentPage().getParameters().get('showFlg');
        mailType = ApexPages.currentPage().getParameters().get('mailType');
    }
    public void init(){
        mailStatus = 'Fin';
        oppoList = [SELECT name,Opportunity_No__c,Owner.Email,response__c,Mail_Tar_Selector__c from Opportunity where id =:opporID];
        if(isShowFlg==''||isShowFlg==null){
            isShowFlg = 'true';
        }
        if(oppoList.size()==1){
            oppEle = oppoList[0];
        }
    }
    public void initPop(){
        oppoList = [SELECT name,Opportunity_No__c,Owner.Email,response__c,Mail_Tar_Selector__c from Opportunity where id =:opporID];
    }
    public void sendMail(){
        if(mailType=='SI'){
            MailTarget = System.label.SI_Group_MailAdd+' <SI部门> ';
        }else if(mailType=='apply'){
            MailTarget = oppEle.Owner.Email+' <申请者>';
        }
        Datetime dt = Datetime.now();
        String temp = '';
        temp += '\n      '  + ' ' + dt.format() + '       \n';
        temp += '    From:' + UserInfo.getUserEmail() +'       \n';
        temp += '    To:' + MailTarget + '\n';
        temp += '    Cc:' + Cc + '\n';
        temp += MailDetail +'\n';
        temp += '============================================================================';
        if(oppoList.size()==1){
            oppEle = oppoList[0];
            if(oppEle.response__c == null){
                oppEle.response__c = temp;
            }else{
                oppEle.response__c += temp;
            }
            
        }
        savepoint sp = Database.setsavepoint(); 
        try{
            update oppoList;
            Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
            system.debug(opporID+'233333333');
            messageNEW.Subject =  oppEle.Name + '询价应答:'+oppEle.Opportunity_No__c+' \n' ;
            messageNEW.PlainTextBody = temp;
            //set desired email addresses
            messageNEW.setCharset('UTF-8');
            List<String> idList = Ccid.split(',');
            List<user> UserEmail = [SELECT Id, Name, Email from user where id in :idList];
            List<String> MailCc = new List<String>();
            for(User us : UserEmail){
                MailCc.add(us.Email);
            }
            //messageNEW.toAddresses = toMailList;
            system.debug('==*****==' + mailType+''+oppEle.Owner.Email);
            List<String> MailAdd = new List<String>();
            String addLine = mailType=='SI'?System.label.SI_Group_MailAdd:oppEle.Owner.Email;
            MailAdd.add(addLine);
            //system.debug('==$$$$==' + mailType+''+oppEle.Owner.Email);
            messageNEW.toAddresses = MailAdd;
            //system.debug('==$$$$==' + mailType+''+oppEle.Owner.Email);
            messageNEW.ccAddresses = MailCc;
            //send the mail
            system.debug('==*****==' + messageNEW);
            Messaging.SendEmailResult[] results = messaging.sendEmail(new Messaging.SingleEmailMessage[] {messageNEW});
            if(!results[0].success){
                system.debug('=====' + results[0].errors[0].message);
                Database.rollback(sp);
                //hasError=true;
                mailStatus = 'Denied';
            }else{
                //邮件发送成功
                //hasError=false;
            }
        } catch (Exception ex) {
            Database.rollback(sp);
            mailStatus = 'Denied';
            system.debug('=====' + ex.getMessage());
            //hasError = true;
            ApexPages.addMessages(ex);
        }
    }
}