彭锟
2022-03-22 2ecb63062fe10bbfb6853e37a38a74dcc10291db
force-app/main/default/classes/NFM624Rest.cls
@@ -161,8 +161,12 @@
                    StateList.add(gedata.State);
                    CityList.add(gedata.City);
                    ApproverIDList.add(gedata.ApproverID);
                    ManagementList.add(gedata.HospitalManagementCode2);
                    ManagementList.add(gedata.DepartmentManagementCode2);
                    if(string.isnotblank(gedata.HospitalManagementCode2)){
                        ManagementList.add(gedata.HospitalManagementCode2);
                    }
                    if(string.isnotblank(gedata.DepartmentManagementCode2)){
                        ManagementList.add(gedata.DepartmentManagementCode2);
                    }
                    //system.debug('AccountCodeList'+AccountCodeList);
                    if (string.isnotblank(gedata.RelatedHospital)) {
                        SFDCCodeList.add(gedata.RelatedHospital);
@@ -186,13 +190,14 @@
                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];
@@ -216,13 +221,15 @@
                                                      Account.PlatformCode__c, 
                                                      Account.Management_Code__c, 
                                                      Account.parent.Department_Class_Label__c, 
                                                      Name,
                                                      Name,
                                                      Account.Parent_Management_Code__c, 
                                                      Account.Parent_PlatformCode__c, 
                                                      CManageCode__c,
                                                      CManageCode__c,
                                                      AccountId 
                                                      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]; //根据人员管理编码检索联系人
                Map < String, Contact > peopleMap = new Map < String, Contact > (); //联系人
@@ -257,12 +264,16 @@
                                                       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 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())];
                Map <String,Account> DepartmentClassMap = new Map <String,Account>();//战略科室Map
                Map < String, Account > AccountMap = new Map < String, Account > (); //医院科室
                for (Account ac: AccountList) {
@@ -272,6 +283,9 @@
                    }
                    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);
@@ -341,14 +355,14 @@
                            dpt.Hospital__c = hospital.Id;
                            
                            //判断医院是否有效
                            if ('有效'.equals(hospital.Is_Active_Formula__c)) {
                            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)) {
                                    //人员管理编码存在
                                    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('人员管理编码存在');
@@ -436,29 +450,140 @@
                                        contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                    }
                                }
                            }else if('无效'.equals(hospital.Is_Active_Formula__c)){
                                logstr += 'error:该医院无效,此条数据跳过';
                                continue;
                            }else{
                                logstr += '该医院处于草案中,请等待审批完成';
                                logstr += '该医院处于草案中/申请中,请等待审批完成';
                                //状态------>草案中/申请中------->等待,plan1:把医院Id写到日志表上(后续检索根据医院id检索未处理过的日志),plan2:把MessageGroupNumber存到医院上
                                rowData.NFM624_Secondary_processing__c = false;
                                continue;
                            }
                        } else { //医院不存在
                            system.debug('新建医院 逻辑5逻辑10 无论人员管理编码是否有值都需要新建医院');
                            hp.Name = gedata.AccountName; //医院名赋值
                            if(ownerMap.containsKey(gedata.ApproverID)){
                                hp.OwnerId = ownerMap.get(gedata.ApproverID);//审核人员员工编码赋值给用户所有人
                        } 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;//数据需要二次处理标记
                            }
                            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;//数据需要二次处理标记
                        }
                    }
                    system.debug('upsertContactList      ' + upsertContactList);
@@ -595,55 +720,7 @@
        }
        return result;
    }
    // global static String contactAllName(String contactName){
    //     // 中国 复姓大全
    //     String CompoundSurname = '濮阳、公冶、太叔、申屠、公孙、慕容、仲孙、钟离、长孙、宇文、司徒、鲜于、司空、闾丘、子车、亓官、司寇、巫马、公西、颛孙、壤驷、公良、漆雕、乐正、宰父、谷梁、拓跋、夹谷、轩辕、令狐、段干、百里、呼延、东郭、南门、羊舌、微生、公户、公玉、公仪、梁丘、公仲、公上、公门、公山、公坚、左丘、公伯、西门、公祖、第五、公乘、贯丘、公皙、南荣、东里、东宫、仲长、子书、子桑、即墨、达奚、褚师、吴铭';
    //     // string allname = '朱启贤';
    //     // contactName = '第五惆怅';
    //     String lastName = '';
    //     String firstName = '';
    //     String result = '';
    //     if (contactName.contains('·')) {
    //         lastName = contactName.split('·')[0];
    //         firstName = contactName.split('·')[1];
    //     } else {
    //         if (contactName.length() > 2) {
    //             system.debug('CompoundSurname--->01' + contactName.subString(0, 2));
    //             if (CompoundSurname.contains(contactName.subString(0, 2))) {
    //                 lastName = contactName.substring(0,2);
    //                 firstName = contactName.substring(2,contactName.length());
    //                 system.debug('lastName--->'+lastName);
    //                 system.debug('firstName--->'+firstName);
    //             } else {
    //                 lastName = contactName.substring(0,1);
    //                 firstName = contactName.substring(1,contactName.length());
    //                 system.debug('lastName--->'+lastName);
    //                 system.debug('firstName--->'+firstName);
    //             }
    //         } else {
    //             if (contactName.length() == 2) {
    //                 lastName = contactName.substring(0,1);
    //                 firstName = contactName.substring(1,contactName.length());
    //                 system.debug('lastName--->'+lastName);
    //                 system.debug('firstName--->'+firstName);
    //             } else {
    //                 lastName = contactName;
    //                 firstName = contactName;
    //                 system.debug('lastName--->'+lastName);
    //                 system.debug('firstName--->'+firstName);
    //             }
    //         }
    //     }
    //     if (!contactName.equals(lastName+firstName)) {
    //         lastName = contactName;
    //         firstName = contactName;
    //     }
    //     result = lastName + '_' + firstName;
    //     //system.debug('CompoundSurname--->02' + CompoundSurname);
    //     return result;
    // }
    private static void supplement (String ContactId){
        //把620Batch 和 623Batch 搬过来
        //补充询问单部分 start
@@ -716,4 +793,29 @@
        }
        //补充学会部分 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;
//         }
//     }
}