FUYU
2023-12-13 4488f711dbc01a8db6753907cae2ef4021dede68
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
trigger LeadTrigger on Lead (before insert, before update) {
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
    if ( StaticParameter.EscapeLeadTrigger == true) {
        System.debug( '-----:リードのトリガー動きません');
        return;
    }
    Lead old = null;
    if(Trigger.isBefore){
        if(StartTradingController.EscapeLeadUpdate == false){
            List<String> accIdList = new List<String>();
            for(Lead l : Trigger.New){
                accIdList.add(l.Hospital_Name__c);
            }
            Map<Id, Account> accountMap = new Map<Id, Account>();
            Map<String, OCM_Management_Province__c> provinceMap = new Map<String, OCM_Management_Province__c>();
            if (accIdList.size() > 0) {
                List<Account> accList = [select id, OCM_Management_Province_HP__c, Parent.Department_Class_Label__c from Account where id = :accIdList];
                for (Account acc : accList) {
                    accountMap.put(acc.id, acc);
                }
                List<OCM_Management_Province__c> provinceList = [select id, Name, GI_assistant__c, SP_assistant__c, Energy_assistant__c from OCM_Management_Province__c];
                for (OCM_Management_Province__c province : provinceList) {
                    provinceMap.put(province.Name, province);
                }
            }
            for(Lead l : Trigger.New){
                if (Trigger.isUpdate) {
                    old = Trigger.oldMap.get(l.Id);
                }
                if (Trigger.isInsert && l.Status == '確認済み') {
                    l.Status.addError('意向转询价前,状态不能为开始询价');
                }
                if (Trigger.isUpdate && l.Status != old.Status && l.Status == '確認済み') {
                    l.Status.addError('意向转询价前,状态不能为开始询价');
                }
                if (Trigger.isInsert){
                    if(l.Owner_Dep__c == '7.能量'){
                        String p = accountMap.get(l.Hospital_Name__c) == null ? '' : accountMap.get(l.Hospital_Name__c).OCM_Management_Province_HP__c;
                        l.Energy_Assistant__c = provinceMap.get(p) == null ? null : provinceMap.get(p).Energy_assistant__c;
                    }else{
                        String p = accountMap.get(l.Hospital_Name__c) == null ? '' : accountMap.get(l.Hospital_Name__c).OCM_Management_Province_HP__c;
                        String c = accountMap.get(l.Hospital_Name__c) == null ? '' : accountMap.get(l.Hospital_Name__c).Parent.Department_Class_Label__c;
                        if (c == '消化科' || c == '呼吸科' || c == 'ET') {
                            l.OCM_Oppor_Assistant__c = provinceMap.get(p) == null ? null : provinceMap.get(p).GI_assistant__c;
                        } else {
                            l.OCM_Oppor_Assistant__c = provinceMap.get(p) == null ? null : provinceMap.get(p).SP_assistant__c;
                        }
                    }
                }
            }
        }
 
    }
    // -------------------------------------------------------------
    // リードのメールアドレスを元に、取引先責任者を関連付けます。
    // すでに関連づいている場合はなにもしません。
    // -------------------------------------------------------------
    // メールアドレスが一致する contactを探します。
    // 複数見つかる場合は、最終更新日が一番新しい contactにします
    // メールアドレスが null のリードは無視します。
    
    // メアド一覧を得る
    // メアド一覧を元に contactを得る
    // 更新対象のリードに、contactへの参照をセットする
    
    // メアドの一意のリスト
    //************************************2017/08/29 DEL START [CHAN-AQN4FY]***********************************
    //Set<String> emails = new Set<String>();
    
    //// メアド一覧を得る
    //// insertの場合は、メアドが入っていれば全部対象
    //// updateの場合は、oldと newで値が違っている時だけ
    //if ( Trigger.isInsert) {
    //    for ( Lead target : Trigger.New) {
    //        if ( target.Email != null) {
    //            emails.add( target.Email);
    //        }
    //    }
    //}
    //else if ( Trigger.isUpdate) {
    //    Lead leadOld = null, leadNew = null;
    //    for ( Integer i = 0; i < Trigger.New.size(); i++) {
    //        leadOld = Trigger.Old[i];
    //        leadNew = Trigger.New[i];
    //        if ( leadOld.email != leadNew.email && leadNew.email != null) {
    //            emails.add( leadNew.email);
    //        }
    //    }
    //}
    //else {
    //    return;
    //}
 
    //// メアド一覧を元に contactを得る
    //List<Contact> contacts = [SELECT Id, Email, OwnerId FROM Contact WHERE Email IN :emails ORDER BY Email, LastModifiedDate ASC];
    //Map<String, Contact> contactMap = new Map<String, Contact>();
    //for ( Contact contact : contacts) {
    //    contactMap.put( contact.Email, contact);
    //}
    
    //// 更新対象のリードに、contactへの参照をセットする
    //Contact targetContact = null;
    //for ( Lead targetLead : Trigger.New) {
    //    targetContact = (Contact)contactMap.get( targetLead.Email);
    //    if ( targetContact != null && targetLead.contact__c == null) {
    //        targetLead.contact__c = targetContact.Id;
    //        targetLead.OwnerId = targetContact.OwnerId;
    //    }
    //}
    //************************************2017/08/29 DEL END [CHAN-AQN4FY]***********************************
    //************************************2017/08/29 ADD START [CHAN-AQN4FY]***********************************
    if ( Trigger.isInsert || Trigger.isUpdate) {
        Set<String> departmentClass = new Set<String>();
        for ( Lead target : Trigger.New) {
            departmentClass.add(target.Department_Class__c);
        }
 
        List<Account> accounts = [SELECT Id, OwnerId FROM Account WHERE Id IN : departmentClass];
        Map<String, Account> accountMap = new Map<String, Account>();
        for (Account acc : accounts) {
            accountMap.put(acc.Id, acc);
        }
 
        Account targetAccount = null;
        for ( Lead targetLead : Trigger.New) {
            if (targetLead.owner_not_automatically_update__c == true) {
                if (Trigger.isInsert) {
                    targetLead.OwnerId = UserInfo.getUserId();
                }
                else {
                    targetLead.OwnerId = targetLead.CreatedById;
                }
            } 
            else {
                targetAccount = (Account)accountMap.get(targetLead.Department_Class__c);
                if (targetAccount != null) {
                    targetLead.OwnerId = targetAccount.OwnerId;
                }
            }
        }
 
        // OBSAP 设置SI担当 2022-05-09 ssm start 
        List<String> owner_ids = new List<String>();
        for (Lead targetLead : Trigger.New) {
            owner_ids.add(targetLead.OwnerId);
        }
        List<User> owners = [select Id, ProfileId, State_Hospital__c, Salesdepartment_text__c from User where Id in :owner_ids];
        Map<String, User> owner_map = new Map<String, User>();
        for (User owner : owners) {
            owner_map.put(owner.Id, owner);
        }
        for (Lead targetLead : Trigger.New) {
            if (targetLead.SI_PromoteInquiry__c) {
                User owner = owner_map.get(targetLead.OwnerId);
                switch on owner.Salesdepartment_text__c {
                    when '1.华北' {
                        targetLead.SI_OppoLeadSec__c = System.Label.SI_Opp_Sec_1;
                    }
                    when '2.东北' {
                        targetLead.SI_OppoLeadSec__c = System.Label.SI_Opp_Sec_2;
                    }
                    when '3.西北' {
                        targetLead.SI_OppoLeadSec__c = System.Label.SI_Opp_Sec_3;
                    }
                    when '4.西南' {
                        targetLead.SI_OppoLeadSec__c = System.Label.SI_Opp_Sec_4;
                    }
                    when '5.华东' {
                        targetLead.SI_OppoLeadSec__c = System.Label.SI_Opp_Sec_5;
                    }
                    when '6.华南' {
                        targetLead.SI_OppoLeadSec__c = System.Label.SI_Opp_Sec_6;
                    }
                    when else {
                        targetLead.SI_OppoLeadSec__c = null;
                    }
                }
            } else {
                targetLead.SI_OppoLeadSec__c = null;
            }
        }
        // OBSAP 设置SI担当 2022-05-09 ssm end
 
        if( Trigger.isUpdate && Trigger.isBefore){
            System.debug(LoggingLevel.INFO, '*** beforeUpdate: ' );
           List<Date> cd = new List<Date>();
 
           for(Lead l:(List<Lead>) Trigger.new){
                String datestr =String.valueOf(l.CreatedDate);
                cd.add( Date.valueOf(datestr.substring(0,10)) );
            }
            System.debug(LoggingLevel.INFO, '*** cd: ' + cd);
            Map<Date,String> lm = new Map<Date,String>();
 
           for(OlympusCalendar__c oc:[SELECT Id,Date__c FROM OlympusCalendar__c WHERE Date__c IN:cd]){
                lm.put(oc.Date__c,oc.Id);     
           }
           System.debug(LoggingLevel.INFO, '*** map赋值: ' + lm);
 
 
 
           for(Lead l:(List<Lead>) Trigger.new){
                String datestr =String.valueOf(l.CreatedDate);
                String dt = lm.get(Date.valueOf(datestr.substring(0,10)));
                l.Lead_Olympus__c = dt;
                System.debug(LoggingLevel.INFO, '*** lead奥林巴斯赋值: ' + l.Lead_Olympus__c);
            }
            
                
        }
    }
    //************************************2017/08/29 ADD END [CHAN-AQN4FY]***********************************
}