高章伟
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
public without sharing class UploadOBPMController {
    private String Id { get; set; }
    private String errorStr { get; set; }
    public boolean hasError { get; set; }
    public List<OBPMInfo> OBPMInfoList { get; set; }
    public Integer lineNo { get; set; }
 
    public UploadOBPMController() {
        Id = ApexPages.currentPage().getParameters().get('id');
    }
 
    public void init(){
        hasError = false;
        OBPMInfoList = new  List<OBPMInfo>();
        
        List<Campaign> camList = [select id, Name, Shared_User__c from Campaign where id = :Id];
        if (camList.size() == 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '学会不存在。'));
            hasError = true;
            return;
        }
        Integer line = 0;
        for (Integer i = 0; i < 5; i++) {
            line += 1;
            OBPMInfo temp = new OBPMInfo(line, new Campaign());
            OBPMInfoList.add(temp);
        }
    }
 
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--无--'));
        options.add(new SelectOption('共同编辑人','共同编辑人'));
        options.add(new SelectOption('抄送人','抄送人'));
        return options;
    }
 
    public PageReference addLine() {
        Integer nowLine = (OBPMInfoList == null ? 0 : OBPMInfoList.size());
        OBPMInfo newopi = new OBPMInfo(nowLine + 1, new Campaign());
        List<OBPMInfo> tempList = new List<OBPMInfo>();
        for (OBPMInfo ob : OBPMInfoList) {
            tempList.add(ob);
        }
        tempList.add(newopi);
        OBPMInfoList = tempList.clone();
        return null;
    }
 
    public PageReference deleteLine() {
        Integer rownum = 0;
        List<OBPMInfo> tempList = new List<OBPMInfo>();
        for (OBPMInfo ob : OBPMInfoList) {
            if (ob.line != lineNo) {
                rownum += 1;
                OBPMInfo tmp = new OBPMInfo(rownum, ob.cam, ob.Role);
                tempList.add(tmp);
            }
        }
        OBPMInfoList = tempList.clone();
        return null;
    }
 
    public PageReference uploadOBPM() {
        List<String> userIdList = new List<String>();
        Map<String,String> userMap = new Map<String,String>();
        List<Campaign> camList = new List<Campaign>();
        Campaign cam = [select id, Name, Shared_User__c, Shared_Editing__c, Carbon_Copy__c from Campaign where id = :Id];
        for(OBPMInfo ob : OBPMInfoList){
            if(String.isNotBlank(ob.cam.Shared_User__c) && (ob.Role == '共同编辑人' || ob.Role =='抄送人')){
                userIdList.add(ob.cam.Shared_User__c);
            }
        }
        if(userIdList.size() > 0){
            String SharedEditing;
            String CarbonCopy;
            List<User> userList = [select id, Employee_No__c from User where id = :userIdList];
            for(User user : userList){
                userMap.put(user.id, user.Employee_No__c);
            }
            for(OBPMInfo ob : OBPMInfoList){
                if(String.isNotBlank(ob.cam.Shared_User__c)){
                    if(userMap.containsKey(ob.cam.Shared_User__c)){
                        if(ob.Role == '共同编辑人'){
                            SharedEditing += userMap.get(ob.cam.Shared_User__c) + ',';
                        } else if(ob.Role == '抄送人'){
                            CarbonCopy += userMap.get(ob.cam.Shared_User__c) + ',';
                        }
                    }
                }
            }
            //截取掉null和最后的逗号
            SharedEditing = SharedEditing.substring(4, SharedEditing.lastIndexOf(','));
            CarbonCopy = CarbonCopy.substring(4, CarbonCopy.lastIndexOf(','));
            cam.Shared_Editing__c = SharedEditing;
            cam.Carbon_Copy__c = CarbonCopy;
            camList.add(cam);
        }
        errorStr = '';
        Savepoint sp = Database.setSavepoint();
        try {
            if(camList.size() > 0){
                update camList;
            }
        } catch (System.Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
            errorStr = e.getMessage();
            hasError = true;
            return null;
        }
        return null;
    }
 
    public class OBPMInfo {
        public Integer line { get; set; }
        public Campaign cam { get; set; }
        public String Role { get; set; }
        public OBPMInfo(Integer lineInfo, Campaign camInfo){
            line = lineInfo;
            cam = camInfo;
            Role = '';
        }
        public OBPMInfo(Integer lineInfo, Campaign camInfo, String roleInfo){
            line = lineInfo;
            cam = camInfo;
            Role = roleInfo;
        }
    }
}