高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
force-app/main/default/classes/NFM624Rest.cls
@@ -61,6 +61,7 @@
 
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        system.debug('strData---'+strData);
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
        system.debug('ges---'+ges);
 
@@ -201,9 +202,11 @@
                }
                //查询市区Id 
                Map < String, String > CityMap = new Map < String, String > (); //市Map
                List < Address_Level2__c > citytempList = [select Id, Name from Address_Level2__c where Name IN: CityList];
                //省市对应该关系 LY 20230206 start
                List < Address_Level2__c > citytempList = [select Id, Name,Level1_Name__c  from Address_Level2__c where Name IN: CityList];
                for (Address_Level2__c temp: citytempList) {
                    CityMap.put(temp.Name, temp.Id);
                    CityMap.put(temp.Level1_Name__c+temp.Name, temp.Id);
                //省市对应该关系 LY 20230206 end
                }
 
                //查询医院所有人
@@ -252,14 +255,17 @@
                        string namekey;
                        string nameKey2;
                        string nameKey3;
                        string nameKey4;
                        if(String.isBlank(ct.MobilePhone_Encrypted__c)){
                           namekey = ct.Account.Name + ' ' + ct.LastName_Encrypted__c.trim();
                           nameKey2 = ct.Account.Management_Code__c + ' ' + ct.LastName_Encrypted__c.trim();
                           nameKey3 = ct.Account.Department_Class__r.Name + ' ' + ct.LastName_Encrypted__c.trim();
                           nameKey4 = ct.LastName_Encrypted__c.trim();
                        } else{
                           namekey = ct.Account.Name + ' ' + ct.LastName_Encrypted__c.trim() + ' ' + ct.MobilePhone_Encrypted__c.trim();
                           nameKey2 = ct.Account.Management_Code__c + ' ' + ct.LastName_Encrypted__c.trim() + ' ' + ct.MobilePhone_Encrypted__c.trim();
                           nameKey3 = ct.Account.Department_Class__r.Name + ' ' + ct.LastName_Encrypted__c.trim() + ' ' + ct.MobilePhone_Encrypted__c.trim();
                           nameKey4 = ct.LastName_Encrypted__c.trim() + ' ' + ct.MobilePhone_Encrypted__c.trim();
                        }
                        // snameMap.put(namekey,ct);
                        if (nameMap.containsKey(ct.Account.parent.Name)) {
@@ -268,6 +274,7 @@
                        snameMap.put(namekey, ct);
                        snameMap.put(nameKey2,ct);
                        snameMap.put(nameKey3,ct);
                        snameMap.put(nameKey4,ct);
                        // // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                        // if(String.isBlank(ct.MobilePhone_Encrypted__c)){
                        //     snameMap.put(ct.LastName_Encrypted__c.trim(), ct);
@@ -428,6 +435,8 @@
                                        System.debug('人员管理编码不存在');
                                        string namekey = gedata.RelatedDepartment + ' ' + gedata.NameEncrypted + ' ' + gedata.MobileEncrypted;
                                        string namekey1 = gedata.RelatedDepartment + ' ' + gedata.NameEncrypted;
                                        string namekey2 = gedata.NameEncrypted + ' ' + gedata.MobileEncrypted;
                                        string namekey3 = gedata.NameEncrypted;
                                        system.debug('DepartmentClasskey     =    '+DepartmentClasskey);
                                        system.debug('nameMap22222222    '+nameMap);
                                        if (nameMap.containskey(DepartmentClasskey)) { // 6 完成 更新操作
@@ -435,18 +444,34 @@
                                            System.debug('sMap'+sMap);
                                            System.debug('sMap.containsKey(namekey)'+sMap.containsKey(namekey));
                                            System.debug('sMap.containsKey(gedata.NameEncrypted)'+sMap.containsKey(gedata.NameEncrypted));
                                            if (sMap.containsKey(namekey)) {
                                            if (sMap.containsKey(namekey) && sMap.get(namekey).UnifiedI_Contact_ID__c == null) {
                                                system.debug('Id赋值'+sMap);
                                                ct.id = sMap.get(namekey).id;
                                            } else if (sMap.containsKey(namekey1) && sMap.get(namekey1).UnifiedI_Contact_ID__c == null) {
                                                ct.id = sMap.get(namekey1).id;
                                            
                                            // // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                                            // } else if(sMap.containsKey(gedata.NameEncrypted + gedata.MobileEncrypted) && sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).UnifiedI_Contact_ID__c == null){
                                            //     ct.id = sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).id;
                                            // // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                                            // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                                            } else if(sMap.containsKey(namekey2) && sMap.get(namekey2).UnifiedI_Contact_ID__c == null){
                                                system.debug('战略科室存在科室为空电话有值');
                                                ct.id = sMap.get(namekey2).id;
                                            }else if(sMap.containsKey(namekey3) && sMap.get(namekey3).MobilePhone_Encrypted__c == null){
                                                system.debug('战略科室存在科室为空电话无值');
                                                ct.id = sMap.get(namekey3).id;
                                            // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                                            }else { // 7 完成
                                                system.debug('7逻辑 无人员管理编码 有医院 有科室 人名查找无 动作:新建联系人 ');
                                                for ( Integer i = 0; i < peopleList.size(); i++) {
                                                    if (peopleList[i].Account.parent.Name !=DepartmentClasskey && (peopleList[i].MobilePhone_Encrypted__c ==null || peopleList[i].MobilePhone_Encrypted__c ==gedata.MobileEncrypted)) {
                                                        ct.id = peopleList[i].id;
                                                    }
                                                }
                                            }
                                        }else {
                                            //战略科室不存在,找手机号为空或相等的客户人员
                                            for ( Integer i = 0; i < peopleList.size(); i++) {
                                                if (peopleList[i].MobilePhone_Encrypted__c ==null || peopleList[i].MobilePhone_Encrypted__c ==gedata.MobileEncrypted) {
                                                    ct.id = peopleList[i].ID;
                                                }
                                            }
                                        }
                                        upsertContactList.add(ct);
@@ -487,7 +512,7 @@
                                        system.debug('nameMap.get(DepartmentClasskey)'+nameMap.get(DepartmentClasskey));
                                        if (nameMap.containsKey(DepartmentClasskey)) { //查找是否存在该战略科室 
                                            System.debug('人员管理编码不存在的情况下战略科室存在');
                                            system.debug('战略科室存在' + nameMap.get(DepartmentClasskey));
                                            System.debug('战略科室存在' + nameMap.get(DepartmentClasskey));
                                            Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                                            system.debug('namekey'+namekey);
                                            system.debug('sMap.keySet'+sMap.keySet());
@@ -522,14 +547,18 @@
                                        }else{
                                            system.debug('创建联系人和科室');
                                            //system.debug('peopleList[0]'+peopleList[0]);
                                            if (peopleList.size()>0) {
                                                ct.id = peopleList[0].ID;
                                            for ( Integer i = 0; i < peopleList.size(); i++) {
                                                if (peopleList[i].MobilePhone_Encrypted__c ==null) {
                                                    ct.id = peopleList[i].ID;
                                                }
                                            }
                                            
                                            // dpt.Department_Name__c = gedata.DepartmentName;
                                            // upsertAccountList.add(dpt);
                                            contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                            system.debug('122222222222222222222contactMap'+contactMap);
                                            upsertContactList.add(ct);
                                            system.debug('122222222222222222222upsertContactList'+upsertContactList);
                                        }
                                        dpt.Department_Name__c = gedata.DepartmentName;
@@ -559,7 +588,10 @@
                            //Ly 省文本赋值 start
                            hp.State_Text__c = gedata.State; //  省(文本)
                            //Ly 省文本赋值 end
                            hp.City_Master__c = CityMap.get(gedata.City); //     市
                            //省市对应该关系 LY 20230206 start
                            //hp.City_Master__c = CityMap.get(gedata.City); //     市
                            hp.City_Master__c = CityMap.get(gedata.State+gedata.City); //     市
                            //省市对应该关系 LY 20230206 end
                            hp.Hospital_Source__c = '智慧医疗';
                            upsertAccountList.add(hp);
                            System.debug('upsertAccountList'+upsertAccountList);
@@ -592,9 +624,11 @@
                                    c.Accountid = ac.id;
                                }
                                c.OwnerId = ac.OwnerId;
                                upsertContactList1.add(c);
                                system.debug('联系人信息'+c);
                                system.debug('upsertContactList'+upsertContactList);
                                //upsertContactList.add(c);
                                system.debug('OwnerId'+ac.OwnerId);
                                system.debug('upsertContactList1'+upsertContactList1);
                                system.debug('upsertContactList'+upsertContactList);
                            }
 
                            if(ac.RecordTypeId == '01210000000QemGAAS' && ac.Is_Active__c == '草案中' && string.isNotBlank( OCSMMap.get(ac.OCM_man_province_HP__c))){
@@ -616,10 +650,6 @@
                        // Database.executeBatch(new NFM620RestBatch(), 1);
                        // Database.executeBatch(new NFM623RestBatch(), 1);
                        supplement(gedata.ContactId);
                    }
                    if(upsertContactList1.size()>0){
                        upsert upsertContactList1;
                    }
                }
            }
@@ -716,7 +746,7 @@
        List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List<Inquiry_form__c> inquiryFormList = new List<Inquiry_form__c>();
        //根据统一用户Id查询询问单
        List<Inquiry_form__c> InquiryList = [select Id,Inquiry_No__c,ContactId__c,Hospital_Name__c,Department_Class__c,Contact_Name__c from Inquiry_form__c where ContactId__c = :ContactId];
        List<Inquiry_form__c> InquiryList = [select Id,Inquiry_No__c,ContactId__c,Hospital_Name__c,Department_Class__c,Contact_Name__c from Inquiry_form__c where ContactId__c = :ContactId and Hospital_Name__c = null and Department_Class__c = null and Contact_Name__c = null];
        system.debug('InquiryList'+InquiryList);
        //根据统一用户Id查询联系人
        List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,Account.Id,Strategic_dept_Class__c,AccountId,Account.Name,Strategic_dept_Class__r.OwnerId from Contact where UnifiedI_Contact_ID__c = :ContactId];
@@ -758,7 +788,7 @@
        
        //补充学会部分 start
        //List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List<CampaignMember__c> capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where Contact_ID__c = :ContactId];
        List<CampaignMember__c> capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ViewContactId__c = :ContactId and Contact_ID__c = null];
        if(capMemList.size() > 0){
            system.debug('capMemList=================>'+capMemList);
            Map<String, String> capMemMap = new Map<String, String>();