涂煌豪
2022-05-23 78ac43791ac9e9d75ce4c33e9dddbdffa88fec2f
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
trigger UserBefore on User (before insert, before update, after insert, after update) {
    Map<Id, User> userMap = new Map<Id, User>();
    List<String> userIdUpList = new List<String>();
    Map<Id,user> userIdUpMap = new Map<Id,user>();
    List<String> userIdInsList = new List<String>();
    List<String> uIds = new List<String>(); //zhaohui 2020-08-18
    if (Trigger.isBefore) {
        for(User usr : Trigger.new) {
            //获取需要更新联盟ID的用户
            if(Trigger.isUpdate && usr.IsActive == true
            && usr.Test_staff__c == false && usr.Employee_No__c <> null
            && usr.FederationIdentifier == null){
                userIdUpList.add(usr.Id);
            }
            // Alias__c
            usr.Alias__c = usr.LastName + usr.FirstName;
 
            // Phote__c
            if (Trigger.isInsert && String.isBlank(usr.Photo__c) == false
                    || (Trigger.isUpdate && ((User) Trigger.oldMap.get(usr.Id)).Photo__c != usr.Photo__c)
                    || (Trigger.isUpdate && String.isBlank(usr.Photo__c) == false && String.isBlank(usr.Photo_Text__c))
            ) {
                String phtTxt = '';
                System.debug('UserBefore Photo__c=' + usr.Photo__c);
                if (String.isBlank(usr.Photo__c) == false) {
                    Integer pStt = usr.Photo__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = usr.Photo__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = usr.Photo__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = usr.Photo__c.substring(pStt + 5, pEnd);
                                System.debug('UserBefore phpTxt1=' + phtTxt);
                                phtTxt = phtTxt.unescapeHtml4();
                                System.debug('UserBefore phpTxt2=' + phtTxt);
                            }
                        }
                    }
                }
                usr.Photo_Text__c = phtTxt;
            }
        }
        if(userIdUpList.size() > 0){
            List<user> updateList = [
                            select id,Name,Employee_No__c,FederationIdentifier
                            from user
                            where id in :userIdUpList
                            and Profile.UserLicense.Name= 'salesforce'];
            for (user us : updateList) {
                userIdUpMap.put(us.Id,us);
            }
            for(User usr : Trigger.new) {
                if(userIdUpMap.containsKey(usr.Id)){
                    usr.FederationIdentifier = usr.Employee_No__c + '@olympus.com.cn';
                }
            }
        }
    }
// CIC 130175 start
    else {
        // Trigger.isAfter
        for(User usr : Trigger.new) {
            // MB関連
            if (Trigger.isInsert && String.isBlank(usr.Province__c) == false
                    || (Trigger.isUpdate && (Trigger.oldMap.get(usr.Id)).Province__c != usr.Province__c)
            ) {
                userMap.put(usr.Id, usr);
            }
            ////获取需要更新联盟ID的用户
            if (Trigger.isInsert && usr.IsActive == true
            && usr.Test_staff__c == false && usr.Employee_No__c <> null
            && usr.FederationIdentifier == null){
                userIdInsList.add(usr.Id);
            }
 
            //zhaohui 2020-08-18 start
            if (Trigger.isUpdate && !usr.IsActive && (Trigger.oldMap.get(usr.Id)).IsActive != usr.IsActive) {
                uIds.add(usr.Id);
            }
            //zhaohui 2020-08-18 end
        }
        // MBこどもを更新
        //ControllerUtil.updMBChildFromUser(userMap);
 
        if(userIdInsList.size() > 0){
            List<user> updateList = [
                            select id,Name,Employee_No__c,FederationIdentifier
                            from user
                            where IsActive = true
                            and Test_staff__c = false
                            and Employee_No__c <> null
                            and id in :userIdInsList
                            and Profile.UserLicense.Name= 'salesforce'];
            for (user us : updateList) {
                us.FederationIdentifier = us.Employee_No__c + '@olympus.com.cn';
                //userIdInsMap.put(us.Id,us);
            }
            ControllerUtil.upUserFederationIdentifier(updateList);
 
        }
 
        //zhaohui 2020-08-18 start
        if (uIds.size() > 0) {
            Database.executeBatch(new CloseTaskUpdateBatch(uIds));
        }
        //zhaohui 2020-08-18 end
    }
// CIC 130175 end
}