liuyan
2022-11-24 8badb57ea2c82557850ad5b39281c3e8714eb119
force-app/main/default/classes/NFM624Rest.cls
@@ -7,7 +7,7 @@
        
    }
*************************************************************************/
@RestResource(urlMapping = '/NFM624/*')
global with sharing class NFM624Rest {
    //add to AWS 回复 start sushanhu 20220225 
@@ -23,7 +23,7 @@
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
    }
    global class GeData {
        public String ContactId; //统一平台ID//Update to aws 存储的AWSContactId 20220225 SUSHANHU
        public String ServiceUserId; //服务平台用户ID
@@ -50,32 +50,36 @@
        public Boolean AgentFlag; //经销商标识
        public String ApproverID; //审核人员员工编码
        public String DataId;//aws存储凭据
    }
        //邮件624接口新增字段需求 20220706 LY Start
        public String ApproveDate;                 //智慧医疗首次认证通过时间
        //邮件624接口新增字段需求 20220706 LY End
    }
    @HttpPost
    global static void execute() {
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
        system.debug('ges---'+ges);
        if (ges == null) {
            return;
        }
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            return;
        }
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
        system.debug('String.isBlank(rowData.Log__c) == false?');
        if (String.isBlank(rowData.Log__c) == false) {
            System.debug('进入if方法,成功调用executefuture');
            executefuture(rowData.Id);
        }
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
@@ -92,19 +96,22 @@
         //updata response toAWS 20220225 sushanhu end
        return;
    }
    @future
    global static void executefuture(String rowData_Id) {
        system.debug('进入executefuture方法' + rowData_Id);
        main(rowData_Id);
    }
    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];
        Map < string, string > RecordTypeMap = new Map < string, string > (); //存放记录类型Id 
        //for(ID temp : rectDpt){
        //
        //}
        RecordTypeMap.put('呼吸科', '01210000000QfmR');
        RecordTypeMap.put('其他', '01210000000Qfmb');
        RecordTypeMap.put('妇科', '01210000000QfmM');
@@ -122,7 +129,7 @@
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List < GeData > GeDataList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        if (GeDataList == null || GeDataList.size() == 0) {
@@ -136,7 +143,7 @@
        //         continue;
        //     }
        // }
        Savepoint sp = Database.setSavepoint();
        List<string> ApproverIDList = new List <string>();//
        List < string > PersonManagementCodeList = new List < String > (); //人员管理编码List
@@ -144,17 +151,16 @@
        List < String > ManagementList = new List < String > (); //医院、科室平台编码List
        List < String > StateList = new List < String > ();
        List < String > CityList = new List < String > ();
        List < String > NameList = new List < String >();
        try { //根据传过来的管理编码查询如果可以查询得到结果则更新,查询不到则新增
            List < Gedata > newGeDataList = new List < Gedata > ();
            for (Gedata gedata: GeDataList) {
                //for test
                gedata.AgentFlag=false;
                // string dataComplete = verify(gedata);
                string dataComplete =null;
                // test  end  for pi
                string dataComplete = verify(gedata);
                if (!String.isBlank(dataComplete)) {
                    logstr += dataComplete;
                    rowData.Is_Error__c = 1;
                    rowData.retry_cnt__c = 0;
                    continue;
                }
                if (!gedata.AgentFlag) {
@@ -178,6 +184,9 @@
                if (string.isnotblank(gedata.PersonManagementCode)) {
                    PersonManagementCodeList.add(gedata.PersonManagementCode);
                }
                if(String.isnotBlank(gedata.NameEncrypted)){
                    NameList.add(gedata.NameEncrypted);
                }
                newGeDataList.add(gedata);
                System.debug('ManagementList'+ManagementList);
                System.debug('SFDCCodeList'+SFDCCodeList);
@@ -190,62 +199,88 @@
                for (Address_Level__c temp: statetempList) {
                    StateMap.put(temp.Name, temp.Id);
                }
                system.debug('StateMap----->'+StateMap);
                //查询市区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];
                for (Address_Level2__c temp: citytempList) {
                    CityMap.put(temp.Name, temp.Id);
                }
                system.debug('CityMap----->'+CityMap);
                //查询医院所有人
                Map<string,String> ownerMap = new Map<string,String>();
                List<User> ownerList = [select Id,Employee_No__c from User where Employee_No__c  IN:ApproverIDList];
                for(User temp : ownerList){
                    ownerMap.put(temp.Employee_No__c,temp.Id);
                }
                //查询OCSM营业窗口
                Map<string,Id> OCSMMap = new Map<string,Id>();
                List<OCM_Management_Province__c> OCSMList = [select Id,Name,Window1__c from OCM_Management_Province__c];
                for(OCM_Management_Province__c ocsm : OCSMList){
                    OCSMMap.put(ocsm.name,ocsm.Window1__c);
                }
                //查询联系人
                Map < String, Map < String, Contact >> nameMap = new Map < String, Map < String, Contact >> (); //外层战略科室,内层科室名+联系人名
                List < Contact > peopleList = [select Id, 
                                                      Account.Name,
                                                      Account.Name,
                                                      Department__c, 
                                                      UnifiedI_Contact_ID__c,
                                                      Account.parent.Name, 
                                                      Account.PlatformCode__c, 
                                                      Account.Management_Code__c, 
                                                      Account.parent.Department_Class_Label__c, 
                                                      Name,
                                                      Name,LastName_Encrypted__c,
                                                      Account.Parent_Management_Code__c, 
                                                      Account.Parent_PlatformCode__c, 
                                                      CManageCode__c,
                                                      AccountId
                                                      CManageCode__c,
                                                      AccountId,
                                                      // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                                                      MobilePhone_Encrypted__c
                                                      // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                                                      ,Account.Department_Class__r.Name
                                                      from Contact 
                                                      where CManageCode__c IN: PersonManagementCodeList 
                                                      OR Account.PlatformCode__c IN: ManagementList
                                                      OR Account.Management_Code__c IN: SFDCCodeList
                                                      OR Account.Parent_PlatformCode__c IN: ManagementList
                                                      OR Account.Parent_Management_Code__c IN: SFDCCodeList]; //根据人员管理编码检索联系人
                                                      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 Name = ct.Name.replaceAll(' ','');
                        string namekey = ct.Account.Name + ' ' + Name;
                        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(ct.Name, 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);
                        // } 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);
                    }
                    peopleMap.put(ct.CManageCode__c, ct);
                    System.debug('peopleMap'+peopleMap);
@@ -253,7 +288,7 @@
                    //string temp = ct.Name + ct.Account.parent.Department_Class_Label__c;
                    // peopleMap.put(temp, ct);
                }
                //查询医院科室
                //List<Account> AccountList = [select Id,PlatformCode__c,PlatformCode__c,Department_Class_Label__c,Name,Management_Code__c,Is_Active__c,AgentCode_Ext__c,ParentId,Parent.ParentId from Account where PlatformCode__c IN :HpCodeList OR parent.PlatformCode__c IN :HpCodeList OR PlatformCode__c IN :HpCodeList OR  Management_Code__c IN :RelatedHospitalList OR Parent_Management_Code__c IN :RelatedHospitalList];
                //List < Account > AccountList = [select Id, PlatformCode__c, Department_Class_Label__c, Name, Management_Code__c, Is_Active__c, AgentCode_Ext__c, ParentId, Parent.ParentId from Account where Management_Code__c IN :AccountCodeList OR Parent_Management_Code__c IN :AccountCodeList OR PlatformCode__c IN :AccountCodeList OR Parent.Parent.PlatformCode__c IN :AccountCodeList];
@@ -264,16 +299,12 @@
                                                       OCM_man_province_HP__c, 
                                                       Name, Management_Code__c, 
                                                       Is_Active__c, AgentCode_Ext__c, 
                                                       RelatedMessageGroupNumber__c,
                                                       ParentId, 
                                                       Parent.ParentId, 
                                                       OwnerId
                                                       from Account 
                                                       where Parent_PlatformCode__c IN: ManagementList 
                                                       OR Parent_Management_Code__c IN: SFDCCodeList
                                                       OR PlatformCode__c IN: ManagementList
                                                       OR Management_Code__c IN: SFDCCodeList
                                                       OR (State_Master__c IN:StateMap.Values()  AND City_Master__c IN:CityMap.Values())];
                                                       OR Parent_Management_Code__c IN: SFDCCodeList];
                Map <String,Account> DepartmentClassMap = new Map <String,Account>();//战略科室Map
                Map < String, Account > AccountMap = new Map < String, Account > (); //医院科室
                for (Account ac: AccountList) {
@@ -284,61 +315,76 @@
                    if (string.isNotBlank(ac.PlatformCode__c)) {
                        AccountMap.put(ac.PlatformCode__c, ac);
                    }
                    if(string.isNotBlank(ac.Name) && ac.ParentId == null && ac.Parent.ParentId == null){
                        AccountMap.put(ac.name,ac);
                    }
                }
                system.debug('AccountMap       '+AccountMap);
                System.debug('DepartmentClassMap     '+DepartmentClassMap);
                //客户List(更新用)
                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;
                    logstr += gedata.AccountName;
                    logstr += gedata.DepartmentClass;
                    logstr += gedata.DepartmentName;
                    string comefrom = gedata.RegSource == '1' ? '智慧医疗' : '服务客户端'; //平台来源
                    string comefrom = gedata.RegSource == '1' ? '智慧医疗' : (gedata.RegSource == '2' ? '服务客户端' :'医拓网'); //平台来源
                    //新建医院
                    Account hp = new Account();
                    //新建联系人
                    Contact ct = new Contact();
                    ct.LastName = gedata.Name;
                    ct.FirstName = '';
                    ct.Type__c = gedata.Type;
                    ct.Type_Encrypted__c = gedata.TypeEncrypted;//add for pipl sushanhu 20220316
                    ct.MobilePhone = gedata.Mobile;
                    ct.RegSource__c = comefrom;
                    ct.UnifiedI_Contact_ID__c = gedata.ContactId; //统一平台Id
                    // //UPDATE TO AWS 存储的统一平台id
                    // ct.AWS_UnifiedI_Contact_ID__c=gedata.ContactId; //统一平台Id
                    ct.ServicePlatformCode__c = gedata.ServiceUserId; //服务平台用户Id
                    ct.Contact_address__c = gedata.ContactAddress;//联系地址
                    ct.Contact_address_Encrypted__c=gedata.ContactAddressEncrypted;//联系地址密文 add 20220215
                    ct.Platform_disabled_representation__c = gedata.ForbiddenStatus;//平台禁用标识
                    //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 姓名密文
                    if(gedata.AgentFlag){
                        //ct.FirstName = ''; 2022-4-7 yjk 注释掉
                        ct.UnifiedI_Contact_ID__c = gedata.ContactId; //统一平台Id
                        ct.ServicePlatformCode__c = gedata.ServiceUserId; //服务平台用户Id
                        //ct.CManageCode__c = gedata.PersonManagementCode;//人员管理编码
                    }else{
                        ct.LastName = gedata.Name;
                        ct.FirstName = '';
                        ct.Type__c = gedata.Type;
                        ct.Type_Encrypted__c = gedata.TypeEncrypted;//add for pipl sushanhu 20220316
                        ct.MobilePhone = gedata.Mobile;
                        ct.RegSource__c = comefrom;
                        ct.UnifiedI_Contact_ID__c = gedata.ContactId; //统一平台Id
                        // //UPDATE TO AWS 存储的统一平台id
                        // ct.AWS_UnifiedI_Contact_ID__c=gedata.ContactId; //统一平台Id
                        ct.ServicePlatformCode__c = gedata.ServiceUserId; //服务平台用户Id
                        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
                    }
                    String personCode = gedata.PersonManagementCode;
                    Map < String, Contact > contactMap = new Map < String, Contact > ();//用于暂存更新联系人
                    //1经销商true
                    if (gedata.AgentFlag == true) {
                        //更新经销商联系人
                        if (!peopleMap.containsKey(personCode)) {
                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                            rowData.Is_Error__c = 1;
                            logstr += 'error:人员管理编码'+personCode+'对应的联系人不存在,请确认。';
                            continue;
                        }
                        ct.id = peopleMap.get(personCode).id;
@@ -355,14 +401,18 @@
                            dpt.Hospital__c = hospital.Id;
                            
                            //判断医院是否有效
                            if ('有效'.equals(hospital.Is_Active_Formula__c) || '无效'.equals(hospital.Is_Active_Formula__c)) {
                            if ('有效'.equals(hospital.Is_Active_Formula__c)) {
                                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)) {
                                    //人员管理编码存在
                                    ct.AccountId = AccountMap.get(DepartmentCode).id;//科室确定,将联系人挂在该科室下
                                    ct.OwnerId = AccountMap.get(DepartmentCode).OwnerId;//科室确定,将将联系人所有人的值赋为科室所有人
                                    ct.OwnerId = AccountMap.get(DepartmentCode).OwnerId;//可是确定,将将联系人所有人的值赋为科室所有人
                                    system.debug('personCode' + personCode);
                                    if (string.isnotblank(personCode)) {
                                        system.debug('人员管理编码存在');
@@ -373,22 +423,52 @@
                                            upsertContactList.add(ct);
                                        } else { //联系人不存在 2 完成
                                            system.debug('2逻辑 有医院 有科室 有人员管理编码但查找无此人 动作:报错');
                                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                            rowData.Is_Error__c = 1;
                                            logstr += 'error:人员管理编码'+personCode+'对应的联系人不存在,请确认。';
                                            continue;
                                        }
                                    } else { //人员管理编码不存在
                                        //搜索人名/且在当前战略科室科室下
                                        System.debug('人员管理编码不存在');
                                        string namekey =  gedata.Name;
                                        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);
                                            if (sMap.containsKey(namekey)) {
                                            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) && sMap.get(namekey).UnifiedI_Contact_ID__c == null) {
                                                system.debug('Id赋值'+sMap);
                                                ct.id = sMap.get(namekey).id;
                                            } else { // 7 完成
                                            } 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);
@@ -413,201 +493,138 @@
                                            //upsertContactList.add(ct);
                                        } else { //人员管理编码查找无值 4 完成
                                            system.debug('4逻辑');
                                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                            rowData.Is_Error__c = 1;
                                            logstr += 'error:人员管理编码'+personCode+'对应的联系人不存在,请确认。';
                                            continue;
                                        }
                                    } else { //人员管理编码不存在
                                        system.debug('人员管理编码不存在');
                                        string namekey = gedata.DepartmentName+ ' ' + gedata.Name;
                                        //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.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);
                                                //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.Name)){
                                                system.debug('1234567890');
                                                ct.Id = sMap.get(gedata.Name).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)){
                                rowData.Is_Error__c = 1;
                                logstr += 'error:该医院无效,此条数据跳过';
                                continue;
                            }else{
                                logstr += '该医院处于草案中/申请中,请等待审批完成';
                                //状态------>草案中/申请中------->等待,plan1:把医院Id写到日志表上(后续检索根据医院id检索未处理过的日志),plan2:把MessageGroupNumber存到医院上
                                logstr += '该医院处于草案中,请等待审批完成';
                                rowData.NFM624_Secondary_processing__c = false;
                                continue;
                            }
                        } else { //医院不存在 则需要增加判断根据名判重
                            if(accountMap.containskey(gedata.AccountName)){//如果根据名字判断有重复则数据新建指向这个医院
                                Account dpt = new Account();
                                Account hospital = AccountMap.get(gedata.AccountName);
                                dpt.PlatformCode__c = gedata.DepartmentManagementCode2;
                                dpt.Hospital__c = hospital.Id;
                                //医院存在↓取状态,有效无效都新建
                                if ('有效'.equals(hospital.Is_Active_Formula__c) || '无效'.equals(hospital.Is_Active_Formula__c)) {
                                    string DepartmentCode = string.isNotBlank(gedata.RelatedDepartment) ? gedata.RelatedDepartment : gedata.DepartmentManagementCode2;
                                    string DepartmentClasskey = gedata.AccountName + ' ' + gedata.DepartmentClass;
                                    //科室存在
                                    if (AccountMap.containsKey(DepartmentCode)) {//因为SOQL没有检索到科室所以没有判定为没有科室
                                        //人员管理编码存在
                                        ct.AccountId = AccountMap.get(DepartmentCode).id;//科室确定,将联系人挂在该科室下
                                        ct.OwnerId = AccountMap.get(DepartmentCode).OwnerId;//可是确定,将将联系人所有人的值赋为科室所有人
                                        system.debug('personCode' + personCode);
                                        if (string.isnotblank(personCode)) {
                                            system.debug('人员管理编码存在');
                                            //联系人存在 11 医院名字排重存在,科室存在,更新联系人, 完成
                                            if (peopleMap.containsKey(personCode)) {
                                                system.debug('11逻辑 名字查重医院 有科室 有人员管理编码且查找有此人 动作:更新');
                                                ct.Id = peopleMap.get(personCode).Id;
                                                upsertContactList.add(ct);
                                            } else { //联系人不存在 12 完成
                                                system.debug('12逻辑 名字查重医院 有科室 有人员管理编码但查找无此人 动作:报错');
                                                logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                                continue;
                                            }
                                        } else { //人员管理编码不存在
                                            //搜索人名/且在当前战略科室科室下
                                            System.debug('人员管理编码不存在');
                                            string namekey =  gedata.Name;
                                            system.debug('DepartmentClasskey     =    '+DepartmentClasskey);
                                            system.debug('nameMap22222222    '+nameMap);
                                            if (nameMap.containskey(DepartmentClasskey)) { // 16 完成 更新操作
                                                Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                                                if (sMap.containsKey(namekey)) {
                                                    system.debug('Id赋值'+sMap);
                                                    ct.id = sMap.get(namekey).id;
                                                    system.debug('16逻辑 无人员管理编码 医院查重存在 有科室 人名查找有 动作:更新联系人 ');
                                                } else { // 17 完成
                                                    system.debug('17逻辑 无人员管理编码 医院查重存在 有科室 人名查找无 动作:新建联系人 ');
                                                }
                                            }
                                            upsertContactList.add(ct);
                                        }
                                        dpt.Id = AccountMap.get(DepartmentCode).Id;
                                        upsertAccountList.add(dpt);
                                    } else { //科室不存在
                                        //为科室赋值
                                        dpt.Name = gedata.DepartmentName;
                                        dpt.RecordTypeId = RecordTypeMap.get(gedata.DepartmentClass);
                                        //因为没有医院管理编码,无法检索到战略科室,所以在给科室的战略科室字段赋值时空指针
                                        dpt.Department_Class__c = DepartmentClassMap.get(gedata.DepartmentClass).Id;
                                        dpt.ParentId = DepartmentClassMap.get(gedata.DepartmentClass).Id;
                                        if (string.isnotblank(personCode)) { //人员管理编码存在
                                            if (peopleMap.containsKey(personCode)) { //人员管理编码查找有值 13 完成
                                                System.debug('13逻辑');
                                                //新建科室+新建联系人,
                                                ct.id = peopleMap.get(personCode).id;
                                                contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                                system.debug('contactMap        '  +  contactMap);
                                                dpt.Department_Name__c = gedata.DepartmentName;
                                                upsertAccountList.add(dpt);
                                                //upsertContactList.add(ct);
                                            } else { //人员管理编码查找无值 14 完成
                                                system.debug('14逻辑');
                                                logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                                continue;
                                            }
                                        } else { //人员管理编码不存在
                                            system.debug('人员管理编码不存在');
                                            string namekey = gedata.DepartmentName+ ' ' + gedata.Name;
                                            //string namekey = gedata.AccountName + ' ' + gedata.DepartmentName + ' ' + gedata.DepartmentClass + ' ' + gedata.Name;
                                            system.debug('DepartmentClasskey'+DepartmentClasskey);
                                            system.debug('351nameMap     '+nameMap);
                                            if (nameMap.containsKey(DepartmentClasskey)) { //查找是否存在该战略科室
                                                System.debug('人员管理编码不存在的情况下战略科室存在');
                                                system.debug('战略科室存在' + nameMap.get(DepartmentClasskey));
                                                Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                                                if (sMap.containsKey(namekey)) { //查找存在 18 完成
                                                    system.debug('18逻辑');
                                                    system.debug('该联系人存在');
                                                    ct.id = sMap.get(namekey).id;
                                                    upsertAccountList.add(dpt);
                                                    upsertContactList.add(ct);
                                                    // if(sMap.containsKey(gedata.Name)){
                                                    //     ct.Id = sMap.get(gedata.Name).Id;
                                                    // }
                                                }else if(sMap.containsKey(gedata.Name)){
                                                    system.debug('1234567890');
                                                    ct.Id = sMap.get(gedata.Name).Id;
                                                    //upsertAccountList.add(dpt);
                                                }
                                                system.debug('contactMap        '  +  contactMap);
                                                System.debug('19逻辑');
                                                contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                            }
                                            system.debug('创建联系人和科室');
                                            dpt.Department_Name__c = gedata.DepartmentName;
                                            upsertAccountList.add(dpt);
                                            contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                        }
                                    }
                                }else{
                                    logstr += '该医院处于草案中/申请中,请等待审批完成';
                                    //状态------>草案中/申请中------->等待,plan1:把医院Id写到日志表上(后续检索根据医院id检索未处理过的日志),plan2:把MessageGroupNumber存到医院上
                                    hospital.RelatedMessageGroupNumber__c += iflog.MessageGroupNumber__c + ';';
                                    rowData.NFM624_Secondary_processing__c = false;
                                    upsertAccountList.add(hospital);
                                }
                            }else{
                                //↓新建医院
                                system.debug('新建医院 逻辑5逻辑10逻辑15逻辑20 无论人员管理编码是否有值都需要新建医院');
                                hp.Name = gedata.AccountName; //医院名赋值
                                if(ownerMap.containsKey(gedata.ApproverID)){
                                    hp.OwnerId = ownerMap.get(gedata.ApproverID);//审核人员员工编码赋值给用户所有人
                                }
                                hp.RecordTypeId = '01210000000QemG';
                                hp.OCM_Category__c = 'L';
                                hp.PlatformCode__c = gedata.HospitalManagementCode2;
                                hp.State_Master__c = StateMap.get(gedata.State); //  省
                                hp.City_Master__c = CityMap.get(gedata.City); //     市
                                hp.Hospital_Source__c = '智慧医疗';
                                upsertAccountList.add(hp);
                                System.debug('upsertAccountList'+upsertAccountList);
                                rowData.NFM624_Secondary_processing__c = false;//数据需要二次处理标记
                        } else { //医院不存在
                            system.debug('新建医院 逻辑5逻辑10 无论人员管理编码是否有值都需要新建医院');
                            hp.Name = gedata.AccountName; //医院名赋值
                            if(ownerMap.containsKey(gedata.ApproverID)){
                                hp.OwnerId = ownerMap.get(gedata.ApproverID);//审核人员员工编码赋值给用户所有人
                            }
                            hp.RecordTypeId = '01210000000QemG';
                            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);
                            System.debug('upsertAccountList'+upsertAccountList);
                            rowData.NFM624_Secondary_processing__c = false;//数据需要二次处理标记
                        }
                    }
                    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;
                        system.debug('SFDCCodeList'+SFDCCodeList);
                        system.debug('SFDCCodeList'+ManagementList);
                        List < Account > List1 = [select Id,RecordTypeId,Is_Active__c,PlatformCode__c,OCM_man_province_HP__c,OwnerId from Account where PlatformCode__c In: SFDCCodeList OR PlatformCode__c IN: ManagementList];//客户所有人修改11.18 11.35 未完成
                        system.debug('List1'+List1);
                        List<Account> updateHPList = new  List<Account>(); 
                        for (Account ac: List1) {
                            Contact c = new contact();
                            system.debug('ac.PlatformCode__c'+ac.PlatformCode__c);
                            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);
                            }
                            if(ac.RecordTypeId == '01210000000QemGAAS' && ac.Is_Active__c == '草案中' && string.isNotBlank( OCSMMap.get(ac.OCM_man_province_HP__c))){
                                ac.OwnerId = OCSMMap.get(ac.OCM_man_province_HP__c);
                                updateHPList.add(ac);
@@ -659,42 +676,37 @@
        }
        update iflog;
    }
    private static String verify(GeData gda) {
        String result = '';
        if (string.isblank(gda.ContactId)) {
            result += 'DataError: ContactId 统一平台ID为空\n';
        }
        if (gda.AgentFlag == null) {
            result += 'DataError:经销商标识为空\n';
        }
        if (string.isblank(gda.Name)) {
            result += 'DataError:姓名为空\n';
        }
        if (string.isblank(gda.NameEncrypted)) {
            result += 'DataError:姓名密文为空\n';
        }
        if (string.isblank(gda.Mobile)) {
            result += 'DataError:手机号为空\n';
        }
        if (string.isblank(gda.MobileEncrypted)) {
            result += 'DataError:手机号密文为空\n';
        }
        if (string.isblank(gda.RegSource)) {
            result += 'DataError:注册平台来源为空\n';
        }if (string.isblank(gda.DataId)) {
            result += 'DataError:AWS加密凭据为空\n';
        }
        if (gda.AgentFlag) {
            if (string.isblank(gda.ContactId)) {
                result += 'DataError: ContactId 统一平台ID为空\n';
            }
            if (string.isblank(gda.PersonManagementCode)) {
                result += 'DataError:PersonManagementCode 人员管理编码为空\n';
            }
        } else {
            // if (string.isnotblank(gda.PersonManagementCode) && string.isBlank(gda.RelatedHospital)) {
            //     result += 'DataError: 关联医院编码为空\n';
            // }
            if(string.isBlank(gda.RelatedDepartment) && String.isBlank(gda.ApproverID)){
                result += 'DataError: 在关联SFDC科室[RelatedDepartment]为空时,审核人员员工编码[ApproverID]必填\n';
            if (string.isblank(gda.ServiceUserId)) {
                result += 'ServiceUserId 服务平台用户Id为空\n';
            }
        }else{
            if (string.isblank(gda.ContactId)) {
                result += 'DataError: ContactId 统一平台ID为空\n';
            }
            if (gda.AgentFlag == null) {
                result += 'DataError:经销商标识为空\n';
            }
            if (string.isblank(gda.Name)) {
                result += 'DataError:姓名为空\n';
            }
            if (string.isblank(gda.Mobile)) {
                result += 'DataError:手机号为空\n';
            }
            if (string.isblank(gda.RegSource)) {
                result += 'DataError:注册平台来源为空\n';
            }
            if(string.isBlank(gda.RelatedHospital) && String.isBlank(gda.HospitalManagementCode2) && String.isBlank(gda.ApproverID)){
                result += 'DataError: 新建医院时,审核人员员工编码[ApproverID]必填\n';
            }
            if (string.isblank(gda.HospitalManagementCode2)) {
                result += 'DataError:医院平台编码为空\n';
@@ -733,26 +745,34 @@
        //根据统一用户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];
        system.debug('contactList' + contactList);
        if(InquiryList.size()>0){
            //统一用户ID存入List、询问单ID存Map
            Map<String, String> inquiryMap = new Map<String, String>();
            //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;
@@ -793,29 +813,4 @@
        }
        //补充学会部分 end
    }
    //前置条件,医院管理编码不存在的时候执行这个操作
//     private static Account duplicate_removal (Map<String,ACcount> accountNameMap,Gedate ged,BatchIF_Log__c iflog){//医院名Map,接口数据,日志表
//         //查询医院是新增条件OR (省 = 传入省 AND 市 = 传入市),增加一个一名字为key的map,根据传入数据的名字判断是否存在
//         //不存在———>新建
//         //存在——>取状态
//         //状态------>草案中/申请中------->等待,plan1:把医院Id写到日志表上(后续检索根据医院id检索未处理过的日志),plan2:把MessageGroupNumber存到医院上
//         //状态------>有效,数据新建到该医院下,发送联系人信息
//         //状态------>无效,数据新建到该医院下,先发送联系人信息,再发送医院信息
//         if(accountNameMap.containskey(ged.AccountName)){//根据医院名判断医院是否存在
//             //医院存在
//             Account tempHp = accountNameMap.get(ged.AccountName);
//             if('草案中'.equals(tempHp.Is_Active_Formula__c) || '申请中'.equals(tempHp.Is_Active_Formula__c)){//判断医院状态
//                 //草案中/申请中状态
//                 tempHp.RelatedMessageGroupNumber__c += iflog.MessageGroupNumber__c + ';';
//                 //continue;
//                 return tempHp;
//             }else{
//             }
//         }else{
//             //医院不存在
//             return hp;
//         }
//     }
}