/**********************************************************************
 * 
 *
 * @url: /services/apexrest/NFM624Rest
 * @data:
 *  {
        
    }
*************************************************************************/
 
@RestResource(urlMapping = '/NFM624Rest2/*')
global with sharing class NFM624Rest2 {
    //add to AWS 回复 start sushanhu 20220225 
    static Boolean SFStatus=true;
    static String SFMessage='';
   //add to AWS 回复 end sushanhu 20220225
    private static final String LOG_TYPE = 'NFM624Rest2';
    // global class GeDatasRest {
    //     public GeDatas GeDatas;
    // }

    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] geData;
    }

    global class GeData {
        public String ContactId;
        public Map<String,UpsertContacts> ContactMap;
        public String[] Managements;
        public String[] SFDCCodes;
        public UpsertContacts[] UpsertContacts;
        public UpsertAccounts[] UpsertAccounts;
        public String logstr;
        public Integer isError;
        public Boolean NFM624SecondaryProcessing;
        public String nfm624RequestId;
        public String reCallNfm624RequestId;
        public String rowDataId;
        public String sourceRequestData;
    }

    global class UpsertContacts{
        public String UnifiedIContactID;
        public String ServicePlatformCode;
        public String Type;
        public String RegSource;
        public String FirstName;
        public String LastName;
        public String MobilePhone;
        public String ContactAddress;
        public Boolean PlatformDisabledRepresentation;
        public String Isactive;
        public Boolean IgnoreSameName;
        public String AWSDataId;
        public Date ApproveDate;
        public String id;
        public String AccountId;
        public String OwnerId;
        public String errorMsg;
        public Boolean AgentFlag;
    }

    global class UpsertAccounts{
        public String id;
        public String PlatformCode;
        public String Hospital;
        public String Name;
        public String RecordTypeId;
        public String DepartmentClass;
        public String ParentId;
        public String DepartmentName;
        public String OCMCategory;
        public String StateMaster;
        public String StateText;
        public String CityMaster;
        public String HospitalSource;
        public String OwnerId;
    }
 
    @HttpPost
    global static void execute() {
 
        System.debug('UserInfo.getUserId() = ' + UserInfo.getUserId());
        System.debug('System.Label.interfaceUserID = ' + System.Label.interfaceUserID);
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        system.debug('strData---'+strData);
        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;
        }
        //判断rowDataId有值没有，如果有值，从数据库里面提取出来
        BatchIF_Log__c rowData = null;
        if(ges.GeData[0].rowDataId != null && ges.GeData[0].rowDataId != ''){
            rowData = NFMUtil.updateRowData(ges.GeData[0].rowDataId, ges.GeData);
        }else {
            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' + rowData.Id);
            executefuture(rowData.Id);
        }
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        // String jsonResponse = '{"status": "Success", "Message":""}';
        // res.responseBody = blob.valueOf(jsonResponse);
         //updata response toAWS 20220225 sushanhu start
         NFMUtil.NFMResponse result = NFMUtil.getNFMResponse();
         result.SFStatus=SFStatus;
         result.SFMessage=SFMessage;
         String jsonResponse =JSON.serialize(result);
         system.debug('result---'+jsonResponse);
         res.responseBody = blob.valueOf(jsonResponse);
         //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) {
        System.debug('rowData_Id = ' + 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');
        RecordTypeMap.put('普外科', '01210000000QfmH');
        RecordTypeMap.put('泌尿科', '01210000000QfmC');
        RecordTypeMap.put('消化科', '01210000000Qfm7');
        RecordTypeMap.put('耳鼻喉科', '01210000000Qfm2');
        RecordTypeMap.put('ET', '01210000000QemQ');
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c,Is_Error__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = LOG_TYPE;
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        System.debug('iflog = ' + iflog);
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        System.debug('rowDataStr = ' + rowDataStr);
        // List < GeData > GeDataList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        // if (GeDataList == null || GeDataList.size() == 0) {
        //     return;
        // }
        List < GeData > GeDataList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        System.debug('GeDataList = ' + GeDataList);
        // System.debug('gds = ' + gds);
        // List < Account > upsertAccountList = new List < Account > ();
        // List < Contact > upsertContactList = new List < Contact > ();

        // if(gds.UpsertAccounts !=null && gds.UpsertAccounts.size() > 0){
        //     for(upsertAccounts uas : gds.UpsertAccounts){
        //         Account account = new Account();
        //         account.PlatformCode__c = uas.PlatformCode;
        //         account.Hospital__c = uas.Hospital;
        //         account.Name = uas.Name;
        //         account.RecordTypeId = uas.RecordTypeId;
        //         account.Department_Class__c = uas.DepartmentClass;
        //         account.ParentId = uas.ParentId;
        //         account.Department_Name__c = uas.DepartmentName;
        //         account.OCM_Category__c = uas.OCMCategory;
        //         account.State_Master__c = uas.StateMaster;
        //         account.State_Text__c = uas.StateText;
        //         account.City_Master__c = uas.CityMaster;
        //         account.Hospital_Source__c = uas.HospitalSource;
        //         upsertAccountList.add(account);
        //     }
        // }
        // System.debug('upsertAccountList = ' + upsertAccountList);

        // if(gds.UpsertContacts !=null && gds.UpsertContacts.size() > 0){
        //     for(UpsertContacts ucs : gds.UpsertContacts){
        //         Contact contact = new Contact();
        //         contact.UnifiedI_Contact_ID__c = ucs.UnifiedIContactID;
        //         contact.ServicePlatformCode__c = ucs.ServicePlatformCode;
        //         contact.Type__c = ucs.Type;
        //         contact.RegSource__c = ucs.RegSource;
        //         contact.Contact_address__c = ucs.ContactAddress;
        //         contact.Platform_disabled_representation__c = ucs.PlatformDisabledRepresentation;
        //         contact.Isactive__c = ucs.Isactive;
        //         contact.Ignore_Same_Name__c = ucs.IgnoreSameName;
        //         contact.AWS_Data_Id__c = ucs.AWSDataId;
        //         contact.ApproveDate__c = ucs.ApproveDate;
        //         contact.id = ucs.id;
        //         contact.AccountId = ucs.AccountId;
        //         contact.OwnerId = ucs.OwnerId;
        //         upsertContactList.add(contact);
        //     }
        // }
        // System.debug('upsertContactList = ' + upsertContactList);
 
        Savepoint sp = Database.setSavepoint();
        try { //根据传过来的管理编码查询如果可以查询得到结果则更新，查询不到则新增
            //查询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);
            }
            //判断是否有errormsg,如果有直接报错
            if(GeDataList.size() > 0){
                for (Gedata gedata: GeDataList) {
                    for(UpsertContacts ucs : gedata.UpsertContacts){
                        System.debug('ucs.errorMsg = ' + ucs.errorMsg);
                        if(ucs.errorMsg != ''){
                            throw new ControllerUtil.myException(ucs.errorMsg);
                        }
                    }
                }
            }
            if(GeDataList.size() > 0){
                System.debug('GeDataList.size() > 0');
                for (Gedata gedata: GeDataList) {

                    List<String> SFDCCodeList = new List<String>();
                    List<String> ManagementList = new List<String>();
                    List<Contact> upsertContactList = new List<Contact>();
                    List<Account> upsertAccountList = new List<Account>();
                    Map < String, Contact > contactMap = new Map < String, Contact > ();//用于暂存更新联系人

                    if(gedata.UpsertAccounts !=null && gedata.UpsertAccounts.size() > 0){
                        for(upsertAccounts uas : gedata.UpsertAccounts){
                            Account account = new Account();
                            if(String.isNotBlank(uas.id) && String.isNotEmpty(uas.id))
                                account.id = uas.id;
                            if(String.isNotBlank(uas.PlatformCode) && String.isNotEmpty(uas.PlatformCode))
                                account.PlatformCode__c = uas.PlatformCode;
                            if(String.isNotBlank(uas.Hospital) && String.isNotEmpty(uas.Hospital))
                                account.Hospital__c = uas.Hospital;
                            if(String.isNotBlank(uas.Name) && String.isNotEmpty(uas.Name))
                                account.Name = uas.Name;
                            if(String.isNotBlank(uas.RecordTypeId) && String.isNotEmpty(uas.RecordTypeId))
                                account.RecordTypeId = uas.RecordTypeId;
                            if(String.isNotBlank(uas.DepartmentClass) && String.isNotEmpty(uas.DepartmentClass))
                                account.Department_Class__c = uas.DepartmentClass;
                            if(String.isNotBlank(uas.ParentId) && String.isNotEmpty(uas.ParentId))
                                account.ParentId = uas.ParentId;
                            if(String.isNotBlank(uas.DepartmentName) && String.isNotEmpty(uas.DepartmentName))
                                account.Department_Name__c = uas.DepartmentName;
                            if(String.isNotBlank(uas.OCMCategory) && String.isNotEmpty(uas.OCMCategory))
                                account.OCM_Category__c = uas.OCMCategory;
                            if(String.isNotBlank(uas.StateMaster) && String.isNotEmpty(uas.StateMaster))
                                account.State_Master__c = uas.StateMaster;
                            if(String.isNotBlank(uas.StateText) && String.isNotEmpty(uas.StateText))
                                account.State_Text__c = uas.StateText;
                            if(String.isNotBlank(uas.CityMaster) && String.isNotEmpty(uas.CityMaster))    
                                account.City_Master__c = uas.CityMaster;
                            if(String.isNotBlank(uas.HospitalSource) && String.isNotEmpty(uas.HospitalSource))     
                                account.Hospital_Source__c = uas.HospitalSource;
                            if(String.isNotBlank(uas.OwnerId) && String.isNotEmpty(uas.OwnerId))     
                                account.OwnerId = uas.OwnerId;    
                            upsertAccountList.add(account);
                        }
                    }
                    System.debug('upsertAccountList = ' + upsertAccountList);


                    if(gedata.UpsertContacts !=null && gedata.UpsertContacts.size() > 0){
                        for(UpsertContacts ucs : gedata.UpsertContacts){
                            Contact contact = new Contact();
                            if(String.isNotBlank(ucs.UnifiedIContactID) && String.isNotEmpty(ucs.UnifiedIContactID)) 
                                contact.UnifiedI_Contact_ID__c = ucs.UnifiedIContactID;
                            if(String.isNotBlank(ucs.ServicePlatformCode) && String.isNotEmpty(ucs.ServicePlatformCode)) 
                                contact.ServicePlatformCode__c = ucs.ServicePlatformCode;
                            if(String.isNotBlank(ucs.Type) && String.isNotEmpty(ucs.Type)) 
                                contact.Type__c = ucs.Type;
                            if(String.isNotBlank(ucs.RegSource) && String.isNotEmpty(ucs.RegSource)) 
                                contact.RegSource__c = ucs.RegSource;
                            if(String.isNotBlank(ucs.FirstName) && String.isNotEmpty(ucs.FirstName)) 
                                contact.FirstName = ucs.FirstName;
                            if(String.isNotBlank(ucs.LastName) && String.isNotEmpty(ucs.LastName)) 
                                contact.LastName = ucs.LastName;
                            if(String.isNotBlank(ucs.MobilePhone) && String.isNotEmpty(ucs.MobilePhone)) 
                                contact.MobilePhone = ucs.MobilePhone;
                            if(String.isNotBlank(ucs.ContactAddress) && String.isNotEmpty(ucs.ContactAddress)) 
                                contact.Contact_address__c = ucs.ContactAddress;
                            if(ucs.PlatformDisabledRepresentation != null) 
                                contact.Platform_disabled_representation__c = ucs.PlatformDisabledRepresentation;
                            if(ucs.Isactive != null)
                                contact.Isactive__c = ucs.Isactive;
                            if(ucs.IgnoreSameName != null)
                                contact.Ignore_Same_Name__c = ucs.IgnoreSameName;
                            if(String.isNotBlank(ucs.AWSDataId) && String.isNotEmpty(ucs.AWSDataId))
                                contact.AWS_Data_Id__c = ucs.AWSDataId;
                            if(ucs.ApproveDate != null)
                                contact.ApproveDate__c = ucs.ApproveDate;
                            if(String.isNotBlank(ucs.id) && String.isNotEmpty(ucs.id))
                                contact.id = ucs.id;
                            if(String.isNotBlank(ucs.AccountId) && String.isNotEmpty(ucs.AccountId))
                                contact.AccountId = ucs.AccountId;
                            if(String.isNotBlank(ucs.OwnerId) && String.isNotEmpty(ucs.OwnerId))
                                contact.OwnerId = ucs.OwnerId;
                            upsertContactList.add(contact);
                        }
                    }
                    System.debug('upsertContactList = ' + upsertContactList);

                    if(gedata.Managements.size() > 0){
                        for(String manage : gedata.Managements){
                            ManagementList.add(manage);
                        }
                    }
                    System.debug('ManagementList = ' + ManagementList);

                    if(gedata.SFDCCodes.size() > 0){
                        for(String sfdcCode : gedata.SFDCCodes){
                            SFDCCodeList.add(sfdcCode);
                        }
                    }
                    System.debug('SFDCCodeList = ' + SFDCCodeList);

                    if(gedata.ContactMap != null){
                        for(String key : gedata.ContactMap.keySet()){ 
                            Contact contact = new Contact();
                            UpsertContacts uc = gedata.ContactMap.get(key);
                            if(String.isNotBlank(uc.UnifiedIContactID) && String.isNotEmpty(uc.UnifiedIContactID))
                                contact.UnifiedI_Contact_ID__c = uc.UnifiedIContactID;
                            if(String.isNotBlank(uc.ServicePlatformCode) && String.isNotEmpty(uc.ServicePlatformCode))
                                contact.ServicePlatformCode__c = uc.ServicePlatformCode;
                            if(String.isNotBlank(uc.Type) && String.isNotEmpty(uc.Type))
                                contact.Type__c = uc.Type;
                            if(String.isNotBlank(uc.RegSource) && String.isNotEmpty(uc.RegSource))
                                contact.RegSource__c = uc.RegSource;
                            if(String.isNotBlank(uc.FirstName) && String.isNotEmpty(uc.FirstName))
                                contact.FirstName = uc.FirstName;
                            if(String.isNotBlank(uc.LastName) && String.isNotEmpty(uc.LastName))
                                contact.LastName = uc.LastName;
                            if(String.isNotBlank(uc.MobilePhone) && String.isNotEmpty(uc.MobilePhone))
                                contact.MobilePhone = uc.MobilePhone;
                            if(String.isNotBlank(uc.ContactAddress) && String.isNotEmpty(uc.ContactAddress))
                                contact.Contact_address__c = uc.ContactAddress;
                            if(uc.PlatformDisabledRepresentation != null)
                                contact.Platform_disabled_representation__c = uc.PlatformDisabledRepresentation;
                            if(String.isNotBlank(uc.Isactive) && String.isNotEmpty(uc.Isactive))
                                contact.Isactive__c = uc.Isactive;
                            if(uc.IgnoreSameName != null)
                                contact.Ignore_Same_Name__c = uc.IgnoreSameName;
                            if(String.isNotBlank(uc.AWSDataId) && String.isNotEmpty(uc.AWSDataId))
                                contact.AWS_Data_Id__c = uc.AWSDataId;
                            if(uc.ApproveDate != null)
                                contact.ApproveDate__c = uc.ApproveDate;
                            if(String.isNotBlank(uc.id) && String.isNotEmpty(uc.id))
                                contact.id = uc.id;
                            if(String.isNotBlank(uc.AccountId) && String.isNotEmpty(uc.AccountId))
                                contact.AccountId = uc.AccountId;
                            if(String.isNotBlank(uc.OwnerId) && String.isNotEmpty(uc.OwnerId))
                                contact.OwnerId = uc.OwnerId;
                            contactMap.put(key,contact);
                        }
                    }
                    System.debug('contactMap = ' + contactMap);

                    if (upsertAccountList.size() > 0) {
                        //放开触发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('zhj Test----------------------');
                        system.debug('List1'+List1);
                        List<Account> updateHPList = new  List<Account>(); 
                        System.debug('start upsertContactList = ' + upsertContactList);
                        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);
                                //rowData.Log__c += ' test:string.isblank(c.Id) = '+string.isblank(c.Id)+'|| c.AccountId != ac.Id = '+(c.AccountId != ac.Id);
                                
                                // if(string.isblank(c.Id)|| c.AccountId != ac.Id){
                                //     system.debug('c.Id = ' + c.Id);
                                //     system.debug('c.AccountId = ' + c.AccountId);
                                //     system.debug('ac.Id = ' + ac.Id);
                                //     c.Accountid = ac.id;
                                // }
                                // c.OwnerId = ac.OwnerId;
                                if(string.isblank(c.Id)|| c.AccountId != ac.Id){
                                    for(Contact ct : upsertContactList){
                                        if(ct.AWS_Data_Id__c == c.AWS_Data_Id__c ){
                                            ct.Accountid = ac.id;
                                        }    
                                    }
                                }
                                for(Contact ct : upsertContactList){
                                    if(ct.AWS_Data_Id__c == c.AWS_Data_Id__c ){
                                        ct.OwnerId = ac.OwnerId;
                                    }    
                                }
                                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);
                            }
                            if(updateHPList.size()>0){
                                update updateHPList;
                            }
                        }
                        System.debug('end upsertContactList = ' + upsertContactList);
                    }
                    
                    upsert upsertAccountList;
                    system.debug('upsertContactList11111111111      '+upsertContactList);
                    if(upsertContactList.size()>0){
                        upsert upsertContactList;
                        system.debug('upsertContactList2222222222222      '+upsertContactList);
                        rowData.NFM624_Secondary_processing__c = true;//执行完之后修改为true，不需要再次执行
                        //2022/2/10 修改 在新建联系人之后手动执行NFM620RestBatch NFM623RestBatch
                        // Database.executeBatch(new NFM620RestBatch(), 1);
                        // Database.executeBatch(new NFM623RestBatch(), 1);
                        supplement(gedata.ContactId);
                    }
                    rowData.Is_Error__c = gedata.isError;
                    logstr += gedata.logstr;
                    rowData.NFM624_Secondary_processing__c = gedata.NFM624SecondaryProcessing;
                }
                
            }
            rowData.retry_cnt__c = 0;
            logstr += '\nend';
            if(Test.isRunningTest()){
                throw new ControllerUtil.myException('增加测试覆盖率');
            }
        } catch (Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, LOG_TYPE + '_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, LOG_TYPE + '_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                rowData.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c + '错误次数已经超过自动收信设定的最大次数，请手动收信';
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }
 
    
    private static void supplement (String ContactId){
        //把620Batch 和 623Batch 搬过来
        //补充询问单部分 start
        //更新用
        List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List<Inquiry_form__c> inquiryFormList = new List<Inquiry_form__c>();
        //根据统一用户Id查询询问单
        List<Inquiry_form__c> InquiryList = [select Id,Inquiry_No__c,ContactId__c,Hospital_Name__c,Department_Class__c,Contact_Name__c from Inquiry_form__c where ContactId__c = :ContactId and Hospital_Name__c = null and Department_Class__c = null and Contact_Name__c = null];
        system.debug('InquiryList'+InquiryList);
        //根据统一用户Id查询联系人
        List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,Account.Id,Strategic_dept_Class__c,AccountId,Account.Name,Strategic_dept_Class__r.OwnerId from Contact where UnifiedI_Contact_ID__c = :ContactId];
        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);
                // map的k-v改造 thh 20220517 start
                inquiryMap.put(inquiryInfo.Id, inquiryInfo.ContactId__c);
                // map的k-v改造 thh 20220517 end
            }
            // 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();
                    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;
            }
        }
        //补充询问单部分 end
        
        //补充学会部分 start
        //List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List<CampaignMember__c> capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ViewContactId__c = :ContactId and Contact_ID__c = null];
        if(capMemList.size() > 0){
            system.debug('capMemList=================>'+capMemList);
            Map<String, String> capMemMap = new Map<String, String>(); 
            //统一用户ID存入List,参会人员记录ID存入MAP
            //List<String> contactIdList = new List<String>();
            for (CampaignMember__c capMemInfo : capMemList) {
                //contactIdList.add(capMemInfo.ViewContactId__c);
                capMemMap.put(capMemInfo.ViewContactId__c,capMemInfo.Id);
            }
            system.debug('capMemMap======>'+capMemMap);
            //List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,Account.Id,Strategic_dept_Class__c,AccountId,Account.Name,Strategic_dept_Class__r.OwnerId,Id from Contact where UnifiedI_Contact_ID__c = ContactId];
            if (contactList.size() > 0) {
                for (Contact contactInfo : contactList) {
                    CampaignMember__c camMemberInfo = new CampaignMember__c();
                    camMemberInfo.Id = capMemMap.get(contactInfo.UnifiedI_Contact_ID__c);
                    camMemberInfo.Contact_ID__c = contactInfo.Id;                   //参会人员ID
                    camMemberInfo.Department_ID__c = contactInfo.AccountId;         //医院科室
                    camMemberInfo.Department__c = contactInfo.Account.Name;         //医院科室（文本）
                    camMemberInfo.Contact__c = contactInfo.Name;                    //客户人员姓名
                    campaignMemberList.add(camMemberInfo);
                }
            }
            system.debug('campaignMemberList================>'+campaignMemberList);
            if (campaignMemberList.size() > 0) {
                System.debug('--------1-------');
                update campaignMemberList;
            }
        }
        //补充学会部分 end
    }

    public static void improveTestRate(){
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}