高章伟
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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
public without sharing class NFM207Handler extends Oly_TriggerHandler {
    private Map<Id, QIS_Report__c> newMap;
    private Map<Id, QIS_Report__c> oldMap;
    private List<QIS_Report__c> newList;
    private List<QIS_Report__c> oldList;
    private List<String> qisIdList;
 
    public NFM207Handler() {
        this.newMap = (Map<Id, QIS_Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, QIS_Report__c>) Trigger.oldMap;
        this.newList = (List<QIS_Report__c>) Trigger.new;
        this.oldList = (List<QIS_Report__c>) Trigger.old;
    }
    protected override void beforeInsert() {
        updateAssistantNo();
        //setUpProduct();
    }
    protected override void beforeUpdate() {
        updateAssistantNo();
    }
    protected override void afterInsert() {
        afterInsertQIS_Report();
    }
    protected override void afterUpdate() {
        updateQIS_Report();
    }
 
 
    //2019年4月2日 start 废弃不用了
    //从保有设备新建QIS,QIS来源为“保有设备”且主机/耗材选择为"耗材"时,且耗材栏为空时设置耗材数据
    //private void setUpProduct() {
    //    list<id> assetIdlist = new List<id>();
    //    for (QIS_Report__c qisReport : newList) {
    //        if (qisReport.Generation_Source__c != null &&
    //                qisReport.Generation_Source__c.equals('保有设备') &&
    //                qisReport.capital_or_consumable__c != null &&
    //                qisReport.capital_or_consumable__c.equals('耗材') &&
    //                qisreport.nonyushohin__c != null
    //           ) {
    //            assetIdlist.add(qisreport.nonyushohin__c);
    //        }
    //    }
    //    Map<id, asset> assetMap = new Map<id, Asset>();
    //    if (assetIdlist.size() > 0) {
    //        assetMap = new Map<id, Asset>(
    //            [select id, Product2Id, Asset_Owner__c, SerialNumber, Order_No__c,
    //             InstallDate, Posting_Date__c, Asset_day__c
    //             from asset
    //             where id in: assetIdlist]
    //        );
    //    }
    //    for (QIS_Report__c qisReport : newList) {
    //        if (qisReport.Generation_Source__c != null &&
    //                qisReport.Generation_Source__c.equals('保有设备') &&
    //                qisReport.capital_or_consumable__c != null &&
    //                qisReport.capital_or_consumable__c.equals('耗材') &&
    //                qisreport.nonyushohin__c != null
    //           ) {
    //            asset tempAsset = assetMap.get(qisreport.nonyushohin__c);
    //            if (tempAsset == null) {
    //                tempAsset = new asset();
    //            }
    //            if (qisReport.consumable__c == null) {
    //                qisReport.consumable__c = tempAsset.Product2Id;
    //            }
    //            if (qisReport.Lots_No__c == null) {
    //                qisReport.Lots_No__c = tempAsset.SerialNumber;
    //            }
 
    //            if (qisReport.sell__c == null) {
    //                string tempSell = null;
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('病院資産')){
    //                    tempSell = '购买';
    //                }
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('Olympus')){
    //                    tempSell = '备品';
    //                }
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('リース会社')){
    //                    tempSell = '其他';
    //                }
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('经销商资产')){
    //                    tempSell = '购买';
    //                }
                    
    //                qisReport.sell__c = tempSell;
    //            }
    //            if (qisReport.Instration_date_consumable__c == null) {
    //                Date tempdate = null;
    //                if (qisReport.InstallDate_text__c != null) {
    //                    tempdate = qisReport.InstallDate_text__c;
    //                }
    //                else if (tempAsset.InstallDate != null) {
    //                    tempdate = tempAsset.InstallDate;
    //                }
    //                else if (tempAsset.Posting_Date__c != null) {
    //                    tempdate = tempAsset.Posting_Date__c;
    //                }
    //                else if (tempAsset.Asset_day__c != null) {
    //                    tempdate = tempAsset.Asset_day__c;
    //                }
    //                qisReport.Instration_date_consumable__c =  tempdate;
    //            }
 
    //            if (qisReport.contract_number_ET__c == null) {
    //                qisReport.contract_number_ET__c = tempAsset.Order_No__c;
    //            }
            
    //        }
            
    //    }
    //}
    //2019年4月2日 end
    private void updateAssistantNo() {
 
        List<String> accIdList = new List<String>();
        for (QIS_Report__c qis : newList) {
            if (Trigger.isInsert
                    ||  (Trigger.isUpdate && (qis.Department_Class__c != oldMap.get(qis.Id).Department_Class__c))
                    ||  (Trigger.isUpdate && (oldMap.get(qis.Id).get('IsSendQIS__c') != qis.IsSendQIS__c && qis.IsSendQIS__c == true ))) {
                // 新建或者战略科室发生变化发生时,重新获取助理,对于老数据当我们点击“发送QIS到SPO”是会触发。
                accIdList.add(qis.Department_Class__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,
                                     OCM_man_province_HP__c,
                                     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 (QIS_Report__c qis : newList) {
            if (Trigger.isInsert ||
                    (Trigger.isUpdate && (qis.Department_Class__c != oldMap.get(qis.Id).Department_Class__c))
                    ||  (Trigger.isUpdate && (oldMap.get(qis.Id).get('IsSendQIS__c') != qis.IsSendQIS__c && qis.IsSendQIS__c == true ))) {
                String p = '';
                if (accountMap.get(qis.Department_Class__c) != null) {
                    p = accountMap.get(qis.Department_Class__c).OCM_man_province_HP__c;
                }
                if (qis.Salesdepartment__c == '7.能量') {
                    qis.BusinessAssistantNo__c = provinceMap.get(p) == null ? null : provinceMap.get(p).Energy_assistant__c;
                } else {
                    //QIS中战略科室名的战略科室分类
                    String c = '';
                    if (accountMap.get(qis.Department_Class__c) != null) {
                        c = accountMap.get(qis.Department_Class__c).Department_Class_Label__c;
                    }
                    if (c == '消化科' || c == '呼吸科' || c == 'ET') {
                        qis.BusinessAssistantNo__c = provinceMap.get(p) == null ? null : provinceMap.get(p).GI_assistant__c;
                    } else {
                        qis.BusinessAssistantNo__c = provinceMap.get(p) == null ? null : provinceMap.get(p).SP_assistant__c;
                    }
                }
            }
        }
    }
 
    private void afterInsertQIS_Report() {
 
        qisIdList = new List<String>();
        for (QIS_Report__c qis : newList) {
            if (qis.IsSendQIS__c == true) {
                NFM207Controller.isRunning = true;
                qisIdList.add(qis.Id);
            }
        }
        qisIdListMethod();
    }
 
    private void updateQIS_Report() {
        qisIdList = new List<String>();
        // CHAN-BC6BW9 LHJ 20190520 Start
        List<String> taskList = new List<String>();
        // CHAN-BC6BW9 LHJ 20190520 End
 
        for (QIS_Report__c qis : newList) {
            if (oldMap.get(qis.Id).get('IsSendQIS__c') != qis.IsSendQIS__c && qis.IsSendQIS__c == true ) {
                NFM207Controller.isRunning = true;
                qisIdList.add(qis.Id);
            }
            // CHAN-BC6BW9 LHJ 20190520 Start
            if (oldMap.get(qis.Id).get('QIS_Status__c') != qis.QIS_Status__c 
                    && oldMap.get(qis.Id).get('QIS_Status__c') == '草案中' ) {
                taskList.add(qis.ID);
            }
            // CHAN-BC6BW9 LHJ 20190520 End
        }
        // CHAN-BC6BW9 LHJ 20190520 Start
        if (taskList != null && taskList.size() > 0) {
            List<Task> lstTask = [select id, status from task where whatid in :taskList and status != '完了'];
 
            if (lstTask != null && lstTask.size() > 0) {
                List<Task> updTask = new List<Task>();
                for(Task tsk : lstTask) {
                    tsk.status = '完了';
                    updTask.add(tsk);
                }
                if (updTask.size() > 0) {
                    update updTask;
                }
            }
        }
        // CHAN-BC6BW9 LHJ 20190520 End
 
        qisIdListMethod();
 
    }
    private void qisIdListMethod() {
        if (qisIdList.size() > 0) {
            // MessageGroupNumber の採番
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM207';
            iflog.Log__c  = 'callout start\n';
            insert iflog;
            iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
            System.debug(Logginglevel.DEBUG, 'NFM207_' + iflog.Name + ' start');                  // callout の中 end のlogを出します
            NFM207Controller.callout(iflog.Id, qisIdList);
        }
    }
}