buli
2023-06-09 dcd1d3017647f5f821b12be2328416d7c34617a3
force-app/main/default/classes/ContactTriggerHandler.cls
@@ -6,7 +6,9 @@
    private Map < Id, Contact > oldMap;
    private List < Contact > newList;
    private List < Contact > oldList;
    private Id AgencyId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId(); // 20220830 ljh LLIU-CHR8FF add
  private Id AgencyId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName()
    .get('Agency')
    .getRecordTypeId(); // 20220830 ljh LLIU-CHR8FF add
        
    public ContactTriggerHandler() {
        this.newMap = (Map < Id, Contact > ) Trigger.newMap;
@@ -15,7 +17,10 @@
        this.oldList = (List < Contact > ) Trigger.old;
    }
    protected override void beforeInsert() {
        if(Test.isRunningTest()||UserInfo.getUserId()!=System.Label.ByPassTrigger){ //Add by Li Jun for PIPL 20220331
    if (
      Test.isRunningTest() || UserInfo.getUserId() != System.Label.ByPassTrigger
    ) {
      //Add by Li Jun for PIPL 20220331
            mobileNumberVerification();
        }
    }
@@ -40,7 +45,6 @@
        mobileNumberVerification();
    }
    protected override void afterDelete() {
        syncToAgencyContactDelete();
        updateDealerNum();
@@ -55,13 +59,14 @@
    private void UpdateProcessingWork() {
        for (Contact temContact: newList) {
            if (System.label.UpdateServiceHistory.equals('true')) {
                string ProcessingWork = '';
                string ProcessingWorkWithoutNumber = '';
                if (temContact.ServiceBookInput__c != 0) {
                    ProcessingWork += '维修委托书填写' + temContact.ServiceBookInput__c + '次';
          ProcessingWork +=
            '维修委托书填写' +
            temContact.ServiceBookInput__c +
            '次';
                    ProcessingWorkWithoutNumber += '维修委托书填写';
                }
                if (temContact.InspectTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
@@ -70,18 +75,22 @@
                    } else {
                        ProcessingWork += ',点检' + temContact.InspectTime__c + '次';
                        ProcessingWorkWithoutNumber += ',点检';
                    }
                }
                if (temContact.InspectEquipmentTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
                        ProcessingWork += '点检设备' + temContact.InspectEquipmentTime__c + '个';
            ProcessingWork +=
              '点检设备' +
              temContact.InspectEquipmentTime__c +
              '个';
                        ProcessingWorkWithoutNumber += '点检设备';
                    } else {
                        ProcessingWork += ',点检设备' + temContact.InspectEquipmentTime__c + '个';
            ProcessingWork +=
              ',点检设备' +
              temContact.InspectEquipmentTime__c +
              '个';
                        ProcessingWorkWithoutNumber += ',点检设备';
                    }
                }
                if (temContact.VisitTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
@@ -91,7 +100,6 @@
                        ProcessingWork += ',上门' + temContact.VisitTime__c + '次';
                        ProcessingWorkWithoutNumber += ',上门';
                    }
                }
                if (temContact.pollingTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
@@ -101,7 +109,6 @@
                        ProcessingWork += ',巡检' + temContact.pollingTime__c + '次';
                        ProcessingWorkWithoutNumber += ',巡检';
                    }
                }
                if (temContact.TeachingTime__c != 0) {
                    if (String.isBlank(ProcessingWork)) {
@@ -118,11 +125,8 @@
                //}
                temContact.ProcessingWork__c = ProcessingWork;
                temContact.ProcessingWorkWithoutNumber__c = ProcessingWorkWithoutNumber;
            }
        }
    }
    // 服务技师 2018/12/24  end
    private void syncToAgencyContact() {
@@ -130,12 +134,14 @@
        Map < Id, Agency_Contact__c > targetContactMap = new Map < Id, Agency_Contact__c > ();
        Map < Id, Contact > ContactMap = new Map < Id, Contact > ();
        for (Contact nObj: newList) {
            if (nObj.RecordTypeId == '01210000000QfWdAAK' // Doctor
                &&
      if (
        nObj.RecordTypeId ==
        '01210000000QfWdAAK' && // Doctor
                String.isBlank(nObj.Strategic_dept_Class__c) == false
            ) {
                if (Trigger.isInsert ||
                    (Trigger.isUpdate
        if (
          Trigger.isInsert ||
          (Trigger.isUpdate)
                        //                        && (
                        //                            oldMap.get(nObj.Id).LastName                != nObj.LastName
                        //                         || oldMap.get(nObj.Id).FirstName               != nObj.FirstName
@@ -143,7 +149,6 @@
                        //                         || oldMap.get(nObj.Id).Type__c                 != nObj.Type__c
                        //                         || oldMap.get(nObj.Id).Doctor_Division1__c     != nObj.Doctor_Division1__c
                        //                        )
                    )
                ) {
                    //Agency_Contact__c acObj = new Agency_Contact__c(Agency_ID__c = '000000000000000', Contact__c = nObj.Id, ContactId18__c = nObj.Id, Agency_Hospital__c = null, Name = nObj.LastName + ((String.isBlank(nObj.FirstName) == false) ? ' ' + nObj.FirstName : ''), Department_Class__c = nObj.Strategic_dept_Class__c, Type__c = nObj.Type__c, Doctor_Division1__c = nObj.Doctor_Division1__c);
                    //zhj MEBG新方案改造 2022-11-27 去掉Encrypted start
@@ -174,18 +179,26 @@
        }
    }
    
    static void EncryptInsert(List<Agency_Contact__c> aclist,Map < Id, Contact > ContactMap){
  static void EncryptInsert(
    List<Agency_Contact__c> aclist,
    Map<Id, Contact> ContactMap
  ) {
        if(!(system.isFuture() || system.isBatch())){
            //zhj MEBG新方案改造 2022-11-27 start
            //AwsServiceTool2.EncryptPushFuture(Json.serialize(aclist), 'Agency_Contact__c');
            Map<String,PIHelper.PIIntegration> staticResource = new Map<String,PIHelper.PIIntegration>();
            staticResource.put('Contact',PIHelper.getPIIntegrationInfo('Contact'));
            staticResource.put('Agency_Contact__c',PIHelper.getPIIntegrationInfo('Agency_Contact__c'));
      staticResource.put(
        'Agency_Contact__c',
        PIHelper.getPIIntegrationInfo('Agency_Contact__c')
      );
            Map<String, Map<String, PI_Field_Policy_Detail__c>> mmsp = new Map<String, Map<String,PI_Field_Policy_Detail__c>>();
            for (String key : staticResource.keySet()) {
                mmsp.put(key, new Map<String,PI_Field_Policy_Detail__c>());
                for (PI_Field_Policy_Detail__c detail : staticResource.get(key).PIDetails) {
        for (
          PI_Field_Policy_Detail__c detail : staticResource.get(key).PIDetails
        ) {
                    mmsp.get(key).put(detail.SF_Field_API_Name__c, detail);
                }
            }
@@ -221,22 +234,50 @@
                res3.value = '';
                res3.table = staticResource.get('Contact').awsTableName;
                res3.dataId = ContactMap.get(ac.Contact__c).AWS_Data_Id__c;
                res3.field = mmsp.get('Contact').get('Doctor_Division1__c').AWS_Field_API__c;
        res3.field = mmsp.get('Contact')
          .get('Doctor_Division1__c')
          .AWS_Field_API__c;
                resList3.add(res3);
                System.debug('Agency_Contact__c Name = ' +mmsp.get('Agency_Contact__c').get('Name').AWS_Field_API__c);
        System.debug(
          'Agency_Contact__c Name = ' +
          mmsp.get('Agency_Contact__c').get('Name').AWS_Field_API__c
        );
                System.debug('resList = ' + resList);
                EncryptPush.fieldsMapping.put(mmsp.get('Agency_Contact__c').get('Name').AWS_Field_API__c, resList);
                EncryptPush.fieldsMapping.put(mmsp.get('Agency_Contact__c').get('Type__c').AWS_Field_API__c, resList2);
                EncryptPush.fieldsMapping.put(mmsp.get('Agency_Contact__c').get('Doctor_Division1__c').AWS_Field_API__c, resList3);
        EncryptPush.fieldsMapping.put(
          mmsp.get('Agency_Contact__c').get('Name').AWS_Field_API__c,
          resList
        );
        EncryptPush.fieldsMapping.put(
          mmsp.get('Agency_Contact__c').get('Type__c').AWS_Field_API__c,
          resList2
        );
        EncryptPush.fieldsMapping.put(
          mmsp.get('Agency_Contact__c')
            .get('Doctor_Division1__c')
            .AWS_Field_API__c,
          resList3
        );
                EncryptPushList.add(EncryptPush);
            }
            System.debug('EncryptPushListdataId = ' + JSON.serialize(EncryptPushList[0].dataId));
            System.debug('EncryptPushListsfRecordId = ' + JSON.serialize(EncryptPushList[0].sfRecordId));
            System.debug('EncryptPushListfieldsMapping = ' + JSON.serialize(EncryptPushList[0].fieldsMapping));
      System.debug(
        'EncryptPushListdataId = ' + JSON.serialize(EncryptPushList[0].dataId)
      );
      System.debug(
        'EncryptPushListsfRecordId = ' +
        JSON.serialize(EncryptPushList[0].sfRecordId)
      );
      System.debug(
        'EncryptPushListfieldsMapping = ' +
        JSON.serialize(EncryptPushList[0].fieldsMapping)
      );
            System.debug('EncryptPushList = ' + JSON.serialize(EncryptPushList));
            if(!Test.isRunningTest())
                AwsServiceTool2V2.EncryptPushFutureV2(Json.serialize(EncryptPushList),Json.serialize(aclist), 'Agency_Contact__c');
        AwsServiceTool2V2.EncryptPushFutureV2(
          Json.serialize(EncryptPushList),
          Json.serialize(aclist),
          'Agency_Contact__c'
        );
            //zhj MEBG新方案改造 2022-11-27 end
        }else{
            //Add By Li Jun for sync agency contact to aws 20220424 start
@@ -254,8 +295,10 @@
            cIdList.add(oObj.Id);
        }
        if (cIdList.size() > 0) {
            List < Agency_Contact__c > acList = [Select Id From Agency_Contact__c
                Where Contact__c =: null and Agency_ID__c = '000000000000000'
      List<Agency_Contact__c> acList = [
        SELECT Id
        FROM Agency_Contact__c
        WHERE Contact__c = :null AND Agency_ID__c = '000000000000000'
            ];
            if (acList.size() > 0) {
                delete acList;
@@ -269,7 +312,12 @@
        Map < Id, Account > acMap = new Map < Id, Account > ();
        if (Trigger.isUpdate) {
            for (Contact contactnew: newList) {
                if (contactnew.Agency_User__c != oldMap.get(contactnew.Id).Agency_User__c || (System.Label.onlyUpdate == '1' && UserInfo.getUserId() == '00510000005sEEMAA2')) {
        if (
          contactnew.Agency_User__c !=
          oldMap.get(contactnew.Id).Agency_User__c ||
          (System.Label.onlyUpdate == '1' &&
          UserInfo.getUserId() == '00510000005sEEMAA2')
        ) {
                    accountSet.add(contactnew.AccountId);
                }
            }
@@ -283,7 +331,6 @@
            }
        }
        if (Trigger.isDelete) {
            for (Contact contactold: oldList) {
                if (contactold.Agency_User__c) {
@@ -293,8 +340,14 @@
        }
        if (accountSet.size() > 0) {
            List < AggregateResult > contactList = [select count(id) ctn, AccountId accid from Contact where Agency_User__c = true and AccountId =: accountSet and RecordTypeId = '01210000000QfWi'
                group by AccountId
      List<AggregateResult> contactList = [
        SELECT count(id) ctn, AccountId accid
        FROM Contact
        WHERE
          Agency_User__c = TRUE
          AND AccountId = :accountSet
          AND RecordTypeId = '01210000000QfWi'
        GROUP BY AccountId
            ];
            for (AggregateResult ar: contactList) {
                String accid = (String) ar.get('accid');
@@ -317,15 +370,12 @@
                update acMap.values();
            }
        }
    }
    //更新经销商用户人数字段 精琢技术 pk 2021-08-26 end
    
    //zhj 新方案改造 将手机号去重及其验证规则移动到AWS和前端 start
    // 手机号去重及规则验证 及新增客户人员重名验证
    public void mobileNumberVerification(){
        // Pattern pattern = Pattern.compile('^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$');
        Map<String, Map<String,Contact>> accountContactMap = new Map<String, Map<String,Contact>>();
        Set<Id> accountIdSet = new Set<Id>();
@@ -344,7 +394,11 @@
            accIdList.add(contact1.AccountId);
        }
        if (accIdList.size()>0){
            List<Account> accList=[select ID,Is_Active__c,Is_Active_Formula__c from Account where ID in:accIdList];
      List<Account> accList = [
        SELECT ID, Is_Active__c, Is_Active_Formula__c
        FROM Account
        WHERE ID IN :accIdList
      ];
            if(accList.size()>0){
                for(Account acc:accList){
                    if (String.isNotBlank(acc.Is_Active__c)){
@@ -358,12 +412,20 @@
            }
        }
        //手机号唯一校验-医院下新建客户人员校验使用加密手机号,经销商使用普通手机号 thh 20220328 start
        ID DoctorRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();
        ID AgencyRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
    ID DoctorRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName()
      .get('Doctor')
      .getRecordTypeId();
    ID AgencyRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName()
      .get('Agency')
      .getRecordTypeId();
        for (Contact contactnew : newList) {
            // 手机号有值并且联系人有效,联系人的客户有效,进行手机号码验证
            if (('有效'.equals(contactnew.Isactive__c) || '有効'.equals(contactnew.Isactive__c))
                && ('有効'.equals(accMap.get(contactnew.AccountId)) || '有效'.equals(accMap.get(contactnew.AccountId)))) {
      if (
        ('有效'.equals(contactnew.Isactive__c) ||
        '有効'.equals(contactnew.Isactive__c)) &&
        ('有効'.equals(accMap.get(contactnew.AccountId)) ||
        '有效'.equals(accMap.get(contactnew.AccountId)))
      ) {
                // 经销商客户人员不进行手机号校验 thh 20220517 start
                if(AgencyRecordTypeId.equals(contactnew.RecordTypeId)){
                    if(String.isNotBlank(contactnew.MobilePhone)){
@@ -484,41 +546,54 @@
            if (Trigger.isUpdate) {
                old = oldMap.get(local.Id);
            }
            if (Trigger.isInsert
                        || old.LastName != local.LastName
                        || old.FirstName != local.FirstName
                        || old.Email != local.Email  //メール Email
                        || old.MobilePhone != local.MobilePhone  //手机号码 Mobile_Phone__c
                        || old.Employee_No_manual__c != local.Employee_No_manual__c  //员工号码 Employee_No__c
                        || old.Work_Location_manual__c != local.Work_Location_manual__c  //工作地 Work_Location__c
                        || old.Post_picklist__c != local.Post_picklist__c  //职位 post__c
                        || old.Job_Category_picklist__c != local.Job_Category_picklist__c  //职种 Job_Category__c
                        || old.Hire_date_text__c != local.Hire_date_text__c  //入职日 Hire_date__c
                        || old.Gender_text__c != local.Gender_text__c  //性别 Gender__c
                        || old.dept__c != local.dept__c  //本部 dept__c
                        || old.Pregnant_Rest__c != local.Pregnant_Rest__c // 是否产假 Pregnant_Rest__c
                        || old.Stay_or_not__c != local.Stay_or_not__c // 在职/离职 Stay_or_not__c
      if (
        Trigger.isInsert ||
        old.LastName != local.LastName ||
        old.FirstName != local.FirstName ||
        old.Email != local.Email || //メール Email
        old.MobilePhone != local.MobilePhone || //手机号码 Mobile_Phone__c
        old.Employee_No_manual__c != local.Employee_No_manual__c || //员工号码 Employee_No__c
        old.Work_Location_manual__c != local.Work_Location_manual__c || //工作地 Work_Location__c
        old.Post_picklist__c != local.Post_picklist__c || //职位 post__c
        old.Job_Category_picklist__c != local.Job_Category_picklist__c || //职种 Job_Category__c
        old.Hire_date_text__c != local.Hire_date_text__c || //入职日 Hire_date__c
        old.Gender_text__c != local.Gender_text__c || //性别 Gender__c
        old.dept__c != local.dept__c || //本部 dept__c
        old.Pregnant_Rest__c != local.Pregnant_Rest__c || // 是否产假 Pregnant_Rest__c
        old.Stay_or_not__c != local.Stay_or_not__c || // 在职/离职 Stay_or_not__c
                        //|| old.Salesdepartment_Text__c != local.Salesdepartment_Text__c // 销售本部 Salesdepartment__c
                        || old.AccountId != local.AccountId//客户人员换客户
                        || old.Isactive__c != local.Isactive__c//状态变更
                        || old.ServicePlatformCode__c != local.ServicePlatformCode__c//服务平台编码
                        || old.UnifiedI_Contact_ID__c != local.UnifiedI_Contact_ID__c//智慧医疗编码
                        || old.ContactType__c != local.ContactType__c//人员类型
                        || (old.ChargeState__c != local.ChargeState__c && local.RecordTypeId == AgencyId)//负责省 // 20220830 ljh LLIU-CHR8FF add
        old.AccountId != local.AccountId || //客户人员换客户
        old.Isactive__c != local.Isactive__c || //状态变更
        old.ServicePlatformCode__c != local.ServicePlatformCode__c || //服务平台编码
        old.UnifiedI_Contact_ID__c != local.UnifiedI_Contact_ID__c || //智慧医疗编码
        old.ContactType__c != local.ContactType__c || //人员类型
        (old.ChargeState__c != local.ChargeState__c &&
        local.RecordTypeId == AgencyId) //负责省 // 20220830 ljh LLIU-CHR8FF add
                        ) {
                //获取客户人员的记录类型ID thh 20220330 start
                ID InternalStaffRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Internal_staff').getRecordTypeId();
                ID DoctorRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();
                ID AgencyRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        ID InternalStaffRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName()
          .get('Internal_staff')
          .getRecordTypeId();
        ID DoctorRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName()
          .get('Doctor')
          .getRecordTypeId();
        ID AgencyRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName()
          .get('Agency')
          .getRecordTypeId();
                //获取客户人员的记录类型ID thh 20220330 end
                System.debug('local.UnifiedI_Contact_ID__c1:' + local.UnifiedI_Contact_ID__c);
        System.debug(
          'local.UnifiedI_Contact_ID__c1:' + local.UnifiedI_Contact_ID__c
        );
                System.debug('local.MobilePhone1:' + local.MobilePhone);
                // gzw 20220824 bugfix start
                // if (!local.IsFromSPO__c && !InternalStaffRecordTypeId.equals(local.RecordTypeId)) {
                if (!InternalStaffRecordTypeId.equals(local.RecordTypeId)) {
                // gzw 20220824 bugfix end
                    // 医院 客户人员 统一平台编码有值 发送 PO
                    if (DoctorRecordTypeId.equals(local.RecordTypeId) && String.isNotBlank(local.UnifiedI_Contact_ID__c)) {
          if (
            DoctorRecordTypeId.equals(local.RecordTypeId) &&
            String.isNotBlank(local.UnifiedI_Contact_ID__c)
          ) {
                        if (!NFM606_IdMap.containsKey(local.Id)) {
                            if(UserInfo.getUserId().equals(System.Label.interfaceUserID)){
                                interfaceUserUpsertContact.add(local.Id);
@@ -529,7 +604,10 @@
                        }
                    }
                    // 经销商 客户人员 手机号码有值 发送 PO
                    if (AgencyRecordTypeId.equals(local.RecordTypeId) && String.isNotBlank(local.MobilePhone)) {
          if (
            AgencyRecordTypeId.equals(local.RecordTypeId) &&
            String.isNotBlank(local.MobilePhone)
          ) {
                        if (!NFM606_IdMap.containsKey(local.Id)) {
                            if(UserInfo.getUserId().equals(System.Label.interfaceUserID)){
                                interfaceUserUpsertContact.add(local.Id);
@@ -540,7 +618,6 @@
                        }
                    }
                }
            }
        }
        //606标记
@@ -551,6 +628,7 @@
                 //添加future 判断 add for pipl  sushanhu 20220317 start
                // if (!(System.isFuture()||System.isBatch())) {
                NFM606Controller.callout('', contactIdList);
                // } else{
                    // NFM606Controller.executeNotFuture('', contactIdList);
                // }
@@ -571,7 +649,9 @@
    private void updateChargeState(){
        List<Contact> contactL = new List<Contact>();
        for (Contact nObj : newList) {
            if(nObj.RecordTypeId == AgencyId && String.isBlank(nObj.ChargeState__c)){
      if (
        nObj.RecordTypeId == AgencyId && String.isBlank(nObj.ChargeState__c)
      ) {
                Contact cnew = new Contact();
                cnew.Id = nObj.Id;
                cnew.ChargeState__c =  nObj.ChargeState_F__c;
@@ -583,6 +663,4 @@
        }
    }
    // 20220830 ljh LLIU-CHR8FF add end
}