binxie
2023-06-19 41625c74ad92b72151f4d1009ea0c791635d9dac
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
public without sharing class LightningUtil {
    public LightningUtil() {
        
    }
    // 精琢科技  zxk 查询用户的用户产品区分字段  start
    public static User loginUserId(){
        system.debug('UserInfo.getUserId()================>'+UserInfo.getUserId());
        return [select UserPro_Type__c from User where id =: UserInfo.getUserId()];
    }
    // 精琢科技  zxk 查询用户的用户产品区分字段  end
    public static List<Agency_Contact__c> selectOCMAgencyContact(Set<String> hospitalSet, Set<String> ahospitalSet) {
        return [select Id, Agency_Hospital__c, IsOlympusContact__c, Name, Doctor_Division1__c, Type__c, Hospital_DC_Name__c,Department_Cateogy_F__c,Hospital_Name__c,
                AWS_Data_Id__c  // 20220222 PI改造 by Bright
                from Agency_Contact__c 
                    where Department_Class__r.Hospital_Department_Class__c in :hospitalSet or (Department_Class__c = null and Agency_Hospital__c in :aHospitalSet) order by Name];
    }
    public static List<Agency_Contact__c> searchOCMAgencyContact(Set<String> hospitalSet, Set<String> aHospitalSet, String hosStr, String conStr) {
        return [select Id, Agency_Hospital__c, IsOlympusContact__c, Name, Doctor_Division1__c, Type__c, Hospital_DC_Name__c,Department_Cateogy_F__c,Hospital_Name__c from Agency_Contact__c 
                    where (Department_Class__r.Hospital_Department_Class__c in :hospitalSet or (Department_Class__c = null and Agency_Hospital__c in :aHospitalSet)) and Name like :conStr and Hospital_DC_Name__c like :hosStr order by Name];
    }
 
    // 20220222 PI改造 by Bright--start
    public static List<Agency_Contact__c> searchOCMAgencyContact(Set<String> hospitalSet, Set<String> aHospitalSet, String hosStr, List<string> awsids) {
 
        string sql = 'select Id, Agency_Hospital__c, IsOlympusContact__c, Name, Doctor_Division1__c, Type__c, Hospital_DC_Name__c,Department_Cateogy_F__c,Hospital_Name__c,AWS_Data_Id__c from Agency_Contact__c';
        sql += ' where (Department_Class__r.Hospital_Department_Class__c in :hospitalSet or (Department_Class__c = null and Agency_Hospital__c in :aHospitalSet)) and Hospital_DC_Name__c like :hosStr';
 
        if (awsids!=null && awsids.size()>0) {
            sql += ' and AWS_Data_Id__c in :awsids ';
        }
        sql += ' order by Hospital_DC_Name__c';
        system.debug('sql='+sql);
        return Database.query(sql);
    }
    // 20220222 PI改造 by Bright--end
    public static List<Contact> selectContact(String cId) {
        return [select Id, AccountId, Name from Contact where id=:cId];
    }
 
    public static List<Agency_Contact__c> selectAgencyContactList(String hospital_id) {
        //return [select id,Name,Doctor_Division1__c FROM Contact WHERE Strategic_dept_Class__c in (select Id From Account where Hospital_Department_Class__c=:hospital_id and RecordTypeId=:record_type_id)];
        Agency_Hospital_Link__c ahl = [select Hospital__c from Agency_Hospital_Link__c where id = :hospital_id];
        return [select id,Name,Doctor_Division1__c,Type__c,Agency_Hospital__c FROM Agency_Contact__c WHERE Hospital_ID18__c=:ahl.Hospital__c];
    }
 
    public static List<contact> selectAgencyPerson() {
        String login_user_id = UserInfo.getUserId();
    // PIPL update Yin Mingjie 21/02/2022 start
    // return [select id, Name, Agency_User__c from contact where  Agency_User__c = true and  Isactive__c = '有效' and AccountId in 
        // (select AccountId from User where id=:login_user_id)];
        return [select id, Name, Agency_User__c, AWS_Data_Id__c from contact where  Agency_User__c = true and  Isactive__c = '有效' and AccountId in 
        (select AccountId from User where id=:login_user_id)];
        // PIPL update Yin Mingjie 21/02/2022 end
    }
    public static List<contact> selectAgencyPerson01() {
        String login_user_id = UserInfo.getUserId();
        system.debug('login_user_id=============>'+login_user_id);
        return [select id, Name,Agency_User__c from contact where  Agency_User__c = false and  Isactive__c = '有效'  and id in 
        (select ContactId from User where id =: login_user_id)];
    }
 
    public static List<contact> selectAgencyPerson02() {
        String login_user_id = UserInfo.getUserId();
        return [select id, Name, Agency_User__c from contact where Isactive__c = '有效' and AccountId in 
        (select AccountId from User where id=:login_user_id)];
    }
 
    public static List<contact> selectAgencyPerson03() {
        String login_user_id = UserInfo.getUserId();
        system.debug('login_user_id=============>'+login_user_id);
        return [select id, Name,Agency_User__c from contact where   Isactive__c = '有效'  and id in 
        (select ContactId from User where id =: login_user_id)];
    }
    
    public static void updateAccMaxActivityDate(String accId, Date week) {
        Agency_Hospital_Link__c account = [select Id,MaxActivityDate__c from Agency_Hospital_Link__c where id=:accId];
        account.MaxActivityDate__c = week;
        update account;
    }
    
    public static Agency_Report_Header__c upsertAgencyReportHeader(Agency_Report_Header__c data) {
        upsert data Agency_Report_Header__c.HeaderInputKey__c;
        return data;
    }
 
    public static void upsertMAgencyReportHeader(List<Agency_Report_Header__c> data) {
        upsert data Agency_Report_Header__c.HeaderInputKey__c;
        // return data;
    }
 
    // PIPL update Yin Mingjie 21/02/2022 start
    public static Map<String, String> insertAgencyContact(Agency_Contact__c data) {
        Map<String, String> acmap = new Map<String, String>();
        acmap.put('AgencyContactId', '');
        acmap.put('errormsg', '');
        try {
            insert data;
            acmap.put('AgencyContactId', data.Id);
        } catch (Exception e) {
            acmap.put('errormsg', e.getMessage());
        }
        return acmap;
    }
    // PIPL update Yin Mingjie 21/02/2022 end
 
    public static Agency_Report__c insertAgencyReport(Agency_Report__c data) {
        if (String.isBlank(data.Hospital__c)) {
            List<Agency_Hospital_Link__c> agency_hospital_link = [select Id, Hospital__c from Agency_Hospital_Link__c where Id=:data.Agency_Hospital__c];
            data.Hospital__c = agency_hospital_link[0].Hospital__c;
        }
        if (String.isBlank(data.Department_Class__c)) {
            String record_type_id = getRecordTypeId(data.Department_Cateogy__c);
            List<Account> account = [select Id, RecordTypeId from account where Hospital_Department_Class__c=:data.Hospital__c and RecordTypeId=:record_type_id];
            data.Department_Class__c = account[0].Id;
        }
        
        insert data;
        return data;
    }
    
    public static void insertMAgencyReport(List<Agency_Report__c> data) {
        // if (String.isBlank(data.Hospital__c)) {
        //     List<Agency_Hospital_Link__c> agency_hospital_link = [select Id, Hospital__c from Agency_Hospital_Link__c where Id=:data.Agency_Hospital__c];
        //     data.Hospital__c = agency_hospital_link[0].Hospital__c;
        // }
        // if (String.isBlank(data.Department_Class__c)) {
        //     String record_type_id = getRecordTypeId(data.Department_Cateogy__c);
        //     List<Account> account = [select Id, RecordTypeId from account where Hospital_Department_Class__c=:data.Hospital__c and RecordTypeId=:record_type_id];
        //     data.Department_Class__c = account[0].Id;
        // }
        
        insert data;
        // return data;
    }
 
    public static Agency_Report__c updateAgencyReport(Agency_Report__c data) {
        if (String.isBlank(data.Hospital__c)) {
            List<Agency_Hospital_Link__c> agency_hospital_link = [select Id, Hospital__c from Agency_Hospital_Link__c where Id=:data.Agency_Hospital__c];
            data.Hospital__c = agency_hospital_link[0].Hospital__c;
        }
        if (String.isBlank(data.Department_Class__c)) {
            String record_type_id = getRecordTypeId(data.Department_Cateogy__c);
            List<Account> account = [select Id, RecordTypeId from account where Hospital_Department_Class__c=:data.Hospital__c and RecordTypeId=:record_type_id];
            data.Department_Class__c = account[0].Id;
        }
        
        update data;
        return data;
    }
 
    public static List<Account> getDeptListByRecordType(String hospital_id, Set<String> recordIds) {
        return [select Id, RecordTypeId, Name from account where Hospital_Department_Class__c = :hospital_id and RecordTypeId in :recordIds];
    }
 
    public static List<RecordType> getAccRecordType(String[] deptTypes) {
        return [select Id, Name, DeveloperName from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN :deptTypes];
    }
 
    public static void upsertAgencyOpportunity(List<Agency_Opportunity__c> upsertList) {
        upsert upsertList Agency_Opportunity__c.TargetInputKey__c;
    }
    //经销商系统
    public static List<Agency_Report__c> selectAgencyReport(Date week, String person_str) {
        if (String.isBlank(person_str)) {
            List<Contact> person_list = selectAgencyPerson();//fy Department_Cateogy_text__c
            return [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,Department_Cateogy_text__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c, productCategories__c,DealerPersonnel__c,WorkRecord__c,Purpose_Type__c,SupportNeeds__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,Person_In_Charge2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name    
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c in :person_list and WeeklyReportClassification__c = ''  order by LastModifiedDate desc];
        } else {//fy Department_Cateogy_text__c
            return [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,Department_Cateogy_text__c,DealerPersonnel__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,WorkRecord__c,Purpose_Type__c,SupportNeeds__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,Person_In_Charge2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name   
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c=:person_str and WeeklyReportClassification__c = ''  order by LastModifiedDate desc];
        }
    }
    //追溯系统
    public static List<Agency_Report__c> selectAgencyReport01(Date week, String person_str) {
        system.debug('person_strperson_str=================>'+person_str);
        if (String.isBlank(person_str)) {
            List<Contact> person_list = selectAgencyPerson01();//fy Department_Cateogy_text__c  doctor3__c
            return [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,Department_Cateogy_text__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,  DealerPersonnel__c,WorkRecord__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c,doctor3__c, doctor2__r.Name, Submit_date__c,
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name,  
                            UseProduct1__r.Asset_Model_No__c,UseProduct2__r.Asset_Model_No__c,UseProduct3__r.Asset_Model_No__c   
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c in :person_list and WeeklyReportClassification__c != '' order by LastModifiedDate desc];
        } else {//fy Department_Cateogy_text__c  doctor3__c
            return [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,Department_Cateogy_text__c,DealerPersonnel__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c, WorkRecord__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c,doctor3__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name,
                            UseProduct1__r.Asset_Model_No__c,UseProduct2__r.Asset_Model_No__c,UseProduct3__r.Asset_Model_No__c   
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c=:person_str and WeeklyReportClassification__c != '' order by LastModifiedDate desc];
        }
    }
    //经销商系统
    public static List<Agency_Report__c> selectMAgencyReport(Date date1, Date date2 ,List<Contact> conMList) {//fy Department_Cateogy_text__c
        List<Agency_Report__c> reportlist = [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c,ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,Department_Cateogy__c,Department_Cateogy_text__c, DealerPersonnel__c,WorkRecord__c, ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name,Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,//支援需求SupportNeeds__c
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,SupportNeeds__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name  
                            From Agency_Report__c
                            where Person_In_Charge2__c in :conMList and Report_Date__c >= :date1 and Report_Date__c <= :date2 and WeeklyReportClassification__c = '' ];
        return reportlist;
    }
    //追溯系统
    public static List<Agency_Report__c> selectMAgencyReport01(Date date1, Date date2 ,List<Contact> conMList) {//fy Department_Cateogy_text__c  doctor3__c
        List<Agency_Report__c> reportlist = [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c,ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,Department_Cateogy__c,Department_Cateogy_text__c, DealerPersonnel__c,WorkRecord__c, ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name,Person_In_Charge2__r.Name, doctor2__c,doctor3__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name,  
                            UseProduct1__r.Asset_Model_No__c,UseProduct2__r.Asset_Model_No__c,UseProduct3__r.Asset_Model_No__c 
                            From Agency_Report__c
                            where Person_In_Charge2__c in :conMList and Report_Date__c >= :date1 and Report_Date__c <= :date2 and WeeklyReportClassification__c != '' ];
        return reportlist;
    }
    //经销商系统的
    public static List<Agency_Report__c> selectAgencyReportById(String report_id) {//fy Department_Cateogy_text__c
        return [Select Id, Name, Department_Cateogy__c,Department_Cateogy_text__c,Purpose_Type__c, ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,WorkRecord__c,EffectProgress__c,ProductClassification__c,ProductCcategory__c,productCategories__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c , Opportunity__c, Opportunity__r.Name
                            From Agency_Report__c
                            where id=:report_id ];
    }
    //追溯系统的
    public static List<Agency_Report__c> selectAgencyReportById01(String report_id) {//fy Department_Cateogy_text__c doctor3__c
        return [Select Id, Name, Department_Cateogy__c,Department_Cateogy_text__c, ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,WorkRecord__c,EffectProgress__c,ProductClassification__c,ProductCcategory__c,productCategories__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c,doctor3__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c 
                            From Agency_Report__c
                            where id=:report_id];
    }
    
    public static List<Agency_Opportunity__c> selectOpportunityByIdAndHospitalLinkId(String opportunity_id, String agency_hospital_link_id) {
        return [select Id, Name, StageName__c, StageName2__c, RecordTypeId, Amount__c, OCMSale_Price__c, Close_Forecasted_Date__c, TargetInputKey__c, Agency_Hospital__c, Agency_Hospital__r.Name, Department_Cateogy__c, Product_Category__c, Product_Category__r.Name
                from Agency_Opportunity__c where id=:opportunity_id and Agency_Hospital__c=:agency_hospital_link_id];
    }
    
    public static String getRecordTypeId(String department_str) {
        String record_type_id = '';
        if (department_str == 'GI') {
            // 消化科
            record_type_id = '01210000000QemLAAS';
        } else if(department_str == 'BF') {
            // 呼吸科
            record_type_id = '01210000000QezZAAS';
        } else if(department_str == 'GYN') {
            // 婦人科
            record_type_id = '01210000000QezoAAC';
        } else if(department_str == 'GS') {
            // 普外科
            record_type_id = '01210000000QezeAAC';
        } else if(department_str == 'URO') {
            // 泌尿科
            record_type_id = '01210000000QezjAAC';
        } else if(department_str == 'ENT') {
            // 耳鼻喉科
            record_type_id = '01210000000QeztAAC';
        } else if(department_str == 'ET') {
            record_type_id = '01210000000QemQAAS';
        } else if(department_str == 'OTH') {
            // その他
            record_type_id = '01210000000QezyAAC';
        }
        
        return record_type_id;
    }
 
    public static void updateAgencyOpportunity(Agency_Opportunity__c updaterecord) {
        upsert updaterecord;
    }
 
    public static Integer ControllerUtil() {
        Integer i = 0;
        
        return i;
    }
    
}