liuyan
2022-11-24 8badb57ea2c82557850ad5b39281c3e8714eb119
force-app/main/default/classes/NFM624Rest.cls
@@ -18,7 +18,7 @@
    // global class GeDatasRest {
    //     public GeDatas GeDatas;
    // }
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
@@ -50,6 +50,10 @@
        public Boolean AgentFlag; //经销商标识
        public String ApproverID; //审核人员员工编码
        public String DataId;//aws存储凭据
        //邮件624接口新增字段需求 20220706 LY Start
        public String ApproveDate;                 //智慧医疗首次认证通过时间
        //邮件624接口新增字段需求 20220706 LY End
    }
 
    @HttpPost
@@ -101,7 +105,6 @@
    }
 
    global static void main(String rowData_Id) {
        // Map<String,String> RecordTypeMap = new Map<String,String>();
        // RecordTypeMap.put('','')
//List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_OTH','Department_GI', 'Department_BF','Department_GYN','Department_GS','Department_URO','Department_ENT','Department_Class_ET') order by DeveloperName desc];        
@@ -222,6 +225,7 @@
                List < Contact > peopleList = [select Id, 
                                                      Account.Name,
                                                      Department__c, 
                                                      UnifiedI_Contact_ID__c,
                                                      Account.parent.Name, 
                                                      Account.PlatformCode__c, 
                                                      Account.Management_Code__c, 
@@ -230,26 +234,51 @@
                                                      Account.Parent_Management_Code__c, 
                                                      Account.Parent_PlatformCode__c, 
                                                      CManageCode__c, 
                                                      AccountId
                                                      AccountId,
                                                      // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                                                      MobilePhone_Encrypted__c
                                                      // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                                                      ,Account.Department_Class__r.Name
                                                      from Contact 
                                                      where CManageCode__c IN: PersonManagementCodeList 
                                                      OR (Account.Parent_PlatformCode__c IN: ManagementList AND LastName_Encrypted__c IN:NameList)
                                                      OR (Account.Parent_Management_Code__c IN: SFDCCodeList AND LastName_Encrypted__c IN:NameList )
                                                      OR (Account.Parent_PlatformCode__c IN: ManagementList AND LastName_Encrypted__c IN:NameList AND UnifiedI_Contact_ID__c = null)
                                                      OR (Account.Parent_Management_Code__c IN: SFDCCodeList AND LastName_Encrypted__c IN:NameList AND UnifiedI_Contact_ID__c = null)
                                                      ORDER BY  MobilePhone ASC,CManageCode__c ASC]; //根据人员管理编码检索联系人
                Map < String, Contact > peopleMap = new Map < String, Contact > (); //联系人
                system.debug('peopleList  '+peopleList);
                for (Contact ct: peopleList) {
                    if (string.isnotblank(ct.Account.parent.Name)) {
                        Map < String, Contact > snameMap = new Map < String, Contact > ();
                        string namekey = ct.Account.Name + ' ' + ct.LastName_Encrypted__c.trim();
                        string nameKey2 = ct.Account.Management_Code__c + ' ' + ct.LastName_Encrypted__c.trim();
                        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)) {
                            snameMap = nameMap.get(ct.Account.parent.Name);
                        }
                        snameMap.put(namekey, ct);
                        snameMap.put(nameKey2,ct);
                        snameMap.put(ct.LastName_Encrypted__c.trim(), 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);
                        // } else{
                        //     snameMap.put(ct.LastName_Encrypted__c.trim() + ct.MobilePhone_Encrypted__c.trim(), ct);
                        // }
                        // // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                        nameMap.put(ct.Account.parent.Name, snameMap);
                        system.debug('snameMap'+snameMap);
                    }
@@ -295,6 +324,7 @@
                List < Account > upsertAccountList = new List < Account > ();
                //联系人List(更新用) 
                List < Contact > upsertContactList = new List < Contact > ();
                List < Contact > upsertContactList1 = new List < Contact > ();
 
                for (Gedata gedata: newGeDataList) {
                    logstr += gedata.Name;
@@ -329,12 +359,20 @@
                        ct.Contact_address__c = gedata.ContactAddress;//联系地址
                        ct.Contact_address_Encrypted__c=gedata.ContactAddressEncrypted;//联系地址密文 add 20220215
                        ct.Platform_disabled_representation__c = gedata.ForbiddenStatus;//平台禁用标识
                        //LLIU-CFF7DD 【委托】客户人员信息有效/无效为“空”的处理 LY 20220616 strat
                        if (gedata.ForbiddenStatus == false) {
                            ct.Isactive__c =  '有效';
                        }
                        //LLIU-CFF7DD 【委托】客户人员信息有效/无效为“空”的处理 LY 20220616 end
                        //ct.IsFromPlatform__c = true; //来自智慧医疗创建
                        ct.Ignore_Same_Name__c = true; //不是重复的客户名 
                        //ct.SendToComPlat__c = true;
                        ct.AWS_Data_Id__c =gedata.DataId;//add 20220215 aws存储凭证
                        ct.MobilePhone_Encrypted__c =gedata.MobileEncrypted;//add 20220215 手机密文
                        ct.LastName_Encrypted__c =gedata.NameEncrypted;//add 20220215 姓名密文
                        //邮件624接口新增字段需求 20220706 LY Start
                        ct.ApproveDate__c =NFMUtil.parseDateTimeStr2Date(gedata.ApproveDate);                //智慧医疗首次认证通过时间
                        //邮件624接口新增字段需求 20220706 LY End
                    }
                    
 
@@ -346,7 +384,7 @@
                        //更新经销商联系人
                        if (!peopleMap.containsKey(personCode)) {
                            rowData.Is_Error__c = 1;
                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                            logstr += 'error:人员管理编码'+personCode+'对应的联系人不存在,请确认。';
                            continue;
                        }
                        ct.id = peopleMap.get(personCode).id;
@@ -367,6 +405,9 @@
                                string DepartmentCode = string.isNotBlank(gedata.RelatedDepartment) ? gedata.RelatedDepartment : gedata.DepartmentManagementCode2;
                                string DepartmentClasskey = gedata.AccountName + ' ' + gedata.DepartmentClass;
                                system.debug('DepartmentClasskey------->'+DepartmentClasskey);
                                system.debug('DepartmentCode------->'+DepartmentCode);
                                system.debug('AccountMap------->'+AccountMap);
                                system.debug('AccountMap.containsKey(DepartmentCode)------->'+AccountMap.containsKey(DepartmentCode));
                                //科室存在
                                if (AccountMap.containsKey(DepartmentCode)) {
                                    //人员管理编码存在
@@ -383,26 +424,51 @@
                                        } else { //联系人不存在 2 完成
                                            system.debug('2逻辑 有医院 有科室 有人员管理编码但查找无此人 动作:报错');
                                            rowData.Is_Error__c = 1;
                                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                            logstr += 'error:人员管理编码'+personCode+'对应的联系人不存在,请确认。';
                                            continue;
                                        }
                                    } else { //人员管理编码不存在
                                        //搜索人名/且在当前战略科室科室下
                                        System.debug('人员管理编码不存在');
                                        string namekey = gedata.RelatedDepartment + ' ' + gedata.NameEncrypted;
                                        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 完成 更新操作
                                            Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                                            System.debug('sMap'+sMap);
                                            System.debug('sMap.containsKey(namekey)'+sMap.containsKey(namekey));
                                            if (sMap.containsKey(namekey)) {
                                            System.debug('sMap.containsKey(gedata.NameEncrypted)'+sMap.containsKey(gedata.NameEncrypted));
                                            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(gedata.NameEncrypted)){
                                                ct.id = sMap.get(gedata.NameEncrypted).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(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);
@@ -428,45 +494,73 @@
                                        } else { //人员管理编码查找无值 4 完成
                                            system.debug('4逻辑');
                                            rowData.Is_Error__c = 1;
                                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                            logstr += 'error:人员管理编码'+personCode+'对应的联系人不存在,请确认。';
                                            continue;
                                        }
                                    } else { //人员管理编码不存在
                                        system.debug('人员管理编码不存在');
                                        string namekey = gedata.DepartmentName+ ' ' + gedata.NameEncrypted;
                                        //string namekey = gedata.DepartmentName + ' ' + gedata.NameEncrypted + ' ' + gedata.MobileEncrypted;
                                        string namekey = DepartmentClasskey + ' ' + gedata.NameEncrypted;
                                        //string namekey = gedata.DepartmentName + ' ' + gedata.NameEncrypted;
                                        //string namekey = '8103587' + ' ' + gedata.NameEncrypted;
                                        //string namekey = gedata.AccountName + ' ' + gedata.DepartmentName + ' ' + gedata.DepartmentClass + ' ' + gedata.Name;
                                        system.debug('DepartmentClasskey'+DepartmentClasskey);
                                        system.debug('351nameMap     '+nameMap);
                                        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.containsKey(namekey)'+sMap.containsKey(namekey));
                                            system.debug('sMap.keySet'+sMap.keySet());
                                            system.debug('sMap.containsKey(namekey)'+sMap.get(namekey));
                                            //system.debug('sMap.containsKey(namekey)8103587 '+sMap.get('8103587 '+gedata.NameEncrypted));
                                            //system.debug('sMap.containsKey(namekey)北京大学第三医院 普外科 手动创建科室4'+sMap.get('北京大学第三医院 普外科 手动创建科室4 '+gedata.NameEncrypted));
                                            //if (sMap.containsKey(namekey)) { //查找存在 8 完成
                                            if (sMap.containsKey(namekey)) { //查找存在 8 完成
                                                system.debug('8逻辑');
                                                system.debug('该联系人存在');
                                                ct.id = sMap.get(namekey).id;
                                                //upsertAccountList.add(dpt);//可以注释
                                                upsertContactList.add(ct);
                                                system.debug('list里的联系人信息'+upsertContactList);
                                                // if(sMap.containsKey(gedata.Name)){
                                                //     ct.Id = sMap.get(gedata.Name).Id;
                                                // }
                                            }else if(sMap.containsKey(gedata.NameEncrypted)){
                                                system.debug('1234567890');
                                                ct.Id = sMap.get(gedata.NameEncrypted).Id;
                                                //upsertAccountList.add(dpt);
                                            }
                                            // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                                            // else if(sMap.containsKey(gedata.NameEncrypted + gedata.MobileEncrypted) && sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).UnifiedI_Contact_ID__c == null){
                                            //     system.debug('1234567890');
                                            //     ct.Id = sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).Id;
                                            //     //upsertAccountList.add(dpt);
                                            //     upsertContactList.add(ct);
                                            // }
                                            // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                                            system.debug('contactMap        '  +  contactMap);
                                            System.debug('9逻辑');
                                            contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                            system.debug('contactMap        '  +  contactMap);
                                            //upsertContactList.add(ct);
                                        }else{
                                            system.debug('创建联系人和科室');
                                            //system.debug('peopleList[0]'+peopleList[0]);
                                            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);
                                        }
                                        system.debug('创建联系人和科室');
                                        dpt.Department_Name__c = gedata.DepartmentName;
                                        upsertAccountList.add(dpt);
                                        contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                    }
                                }
                            }else if('无效'.equals(hospital.Is_Active_Formula__c)){
@@ -488,6 +582,9 @@
                            hp.OCM_Category__c = 'L';
                            hp.PlatformCode__c = gedata.HospitalManagementCode2;
                            hp.State_Master__c = StateMap.get(gedata.State); //  省
                            //Ly 省文本赋值 start
                            hp.State_Text__c = gedata.State; //  省(文本)
                            //Ly 省文本赋值 end
                            hp.City_Master__c = CityMap.get(gedata.City); //     市
                            hp.Hospital_Source__c = '智慧医疗';
                            upsertAccountList.add(hp);
@@ -498,7 +595,9 @@
                    system.debug('upsertContactList      ' + upsertContactList);
                    system.debug('upsertAccountList      ' + upsertAccountList);
                    if (upsertAccountList.size() > 0) {
                        ControllerUtil.EscapeNFM001Trigger = true;
                        //放开触发201接口条件 20221102 LY start
                        //ControllerUtil.EscapeNFM001Trigger = true;
                        //放开触发201接口条件 20221102 LY end
                        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
                        system.debug('upsertAccountList                           ' + upsertAccountList);
                        upsert upsertAccountList;
@@ -513,12 +612,15 @@
                            system.debug('ContactMap'+ContactMap);
                            system.debug('ContactMap.containsKey(ac.PlatformCode__c)'+ContactMap.containsKey(ac.PlatformCode__c));
                            if(ContactMap.containsKey(ac.PlatformCode__c)){
                                c = contactMap.get(ac.PlatformCode__c);
                                if(string.isblank(c.Id)|| c.AccountId != ac.Id){
                                    c.Accountid = ac.id;
                                }
                                c.OwnerId = ac.OwnerId;
                                upsertContactList.add(c);
                                system.debug('联系人信息'+c);
                                system.debug('upsertContactList'+upsertContactList);
                                //upsertContactList.add(c);
                                system.debug('OwnerId'+ac.OwnerId);
                                system.debug('upsertContactList'+upsertContactList);
                            }
@@ -650,19 +752,27 @@
            //List<String> contactIdList = new List<String>();
            for (Inquiry_form__c inquiryInfo : inquiryList) {
                //contactIdList.add(inquiryInfo.ContactId__c);
                inquiryMap.put(inquiryInfo.ContactId__c, inquiryInfo.Id);
                // map的k-v改造 thh 20220517 start
                inquiryMap.put(inquiryInfo.Id, inquiryInfo.ContactId__c);
                // map的k-v改造 thh 20220517 end
            }
            if (contactList.size() >0 ) {
                for (Contact contactInfo : contactList) {
            // map的k-v改造 thh 20220517 start
            if (contactList.size() >0 && inquiryMap.size() > 0) {
                for(String Id : inquiryMap.keySet()){
                    Inquiry_form__c inquiry = new Inquiry_form__c();
                    inquiry.Id = inquiryMap.get(contactInfo.UnifiedI_Contact_ID__c);      //ID
                    inquiry.Hospital_Name__c = contactInfo.Account.Id;                    //科室名
                    inquiry.Department_Class__c = contactInfo.Strategic_dept_Class__c;    //战略科室分类
                    inquiry.Contact_Name__c = contactInfo.Id;                             //客户姓名
                    inquiry.OwnerId = contactInfo.Strategic_dept_Class__r.OwnerId;        //所有人
                    inquiryFormList.add(inquiry);
                    for (Contact contactInfo : contactList) {
                        if(inquiryMap.get(Id) == contactInfo.UnifiedI_Contact_ID__c){
                            inquiry.Id = Id;      //ID
                            inquiry.Hospital_Name__c = contactInfo.Account.Id;                    //科室名
                            inquiry.Department_Class__c = contactInfo.Strategic_dept_Class__c;    //战略科室分类
                            inquiry.Contact_Name__c = contactInfo.Id;                             //客户姓名
                            inquiry.OwnerId = contactInfo.Strategic_dept_Class__r.OwnerId;        //所有人
                            inquiryFormList.add(inquiry);
                        }
                    }
                }
            }
            // map的k-v改造 thh 20220517 end
            system.debug('inquiryFormList=========>'+inquiryFormList);
            if(inquiryFormList.size()>0){
                update inquiryFormList;