Liu Cheng
2022-11-21 8c8f69c3b5bf76f23e6f5399bf0a0c4138a75a63
DB202211029119 申请单延期后最新预计归还日未更新对应
2个文件已修改
285 ■■■■■ 已修改文件
force-app/main/default/classes/RentalApplyTriggerHandler.cls 178 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
force-app/main/default/classes/RentalApplyTriggerHandlerTest.cls 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
force-app/main/default/classes/RentalApplyTriggerHandler.cls
@@ -11,6 +11,7 @@
    private List<Rental_Apply__c> oldList;
    private static Date td = Date.today();
    private static Map<Id, Rental_Apply__c> oldRaMap = new Map<Id, Rental_Apply__c>();
    public static Boolean isFirst = true;
    //update      wangweipeng                             2021/11/25                   start
    private static Map<String, String> approver_of_Service_DepartmentMap = new Map<String, String>();
@@ -49,9 +50,11 @@
    }
    protected override void afterInsert() {
        // Check本部是否可以选择
        checkbenbu();
        checkbenbu();// 20220909 ljh 恢复代码
        // 共享设定
        setShare();
        decryptInsert(newList);
    }
    protected override void beforeUpdate() {
        setManager();
@@ -64,7 +67,7 @@
    protected override void afterUpdate() {
        // Check本部是否可以选择
        checkbenbu();
        checkbenbu();// 20220909 ljh 恢复代码
        cancelRa();
        // before では数式項目がnullの場合があります
        formulaToTextCheck();
@@ -75,6 +78,15 @@
        // 取消申请单的审批
        removedProcessRequest();
        System.debug('---------------newList--------------' + newList);
        // add by lc 2022/11/15 DB202211029119 start
        if (isFirst) {
            // 主从申请单,只延主单的情况,走单独的处理逻辑,并且只执行一次
            synchRentalApplyDataMaster();
        }
        // add by lc 2022/11/15 DB202211029119 end
        synchRentalApplyData2();
        // 延期审批后需要更新一览
        setAppExtensionRaes();
@@ -82,10 +94,50 @@
        setShare();
        // 办事处分单的装机确认
        setAgencyHPReceived();
        //批量审批时,需要把主单和从单的延期字段信息同步
        synchRentalApplyData();
    }
    // add by lc 2022/11/15 DB202211029119 start
    // 主从申请单,只延主单的情况,走单独的处理逻辑
    private void synchRentalApplyDataMaster() {
        List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
        // 批准只能一条一条的批准
        for (Rental_Apply__c nObj : newList) {
            Rental_Apply__c oObj = oldMap.get(nObj.Id);
            if (oObj.ExtensionApprovalTime_Initial__c != nObj.ExtensionApprovalTime_Initial__c
                && nObj.ExtensionApprovalTime_Initial__c != null && oObj.ExtensionApprovalTime_Initial__c == null
                && String.isNotBlank(nObj.Extension_Type__c) && nObj.Extension_Type__c == '批量延期'
                && (nObj.demo_purpose2__c == '试用(无询价)' || nObj.demo_purpose2__c == '试用(有询价)')
                && String.isBlank(oObj.Extension_Much_ID__c)
                && String.isBlank(oObj.Root_Rental_Apply__c)) {
                isFirst = false;
                List<Rental_Apply__c> checkRentalApply = new List<Rental_Apply__c>();
                checkRentalApply.add(oObj);
                try {
                    System.debug('========================checkRentalApply=========================' + checkRentalApply);
                    for (Rental_Apply_Equipment_Set__c raes : getCan_Extend_RequestList(checkRentalApply)) {
                        System.debug('raes.Id=========================' + raes.Id);
                        //判断是此申请单是否存在 ok并且回寄时间不为空的一览,
                        if ((raes.Received_Confirm__c == 'OK' || raes.Received_Confirm__c == '默认签收-OK') && raes.Asset_return_time__c != null) {
                        }else{
                            raes.RcUnexpectExpiryDelay__c = raes.Rental_Apply__r.RcUnexpectExpiryDelay__c;
                            raesList.add(raes);
                        }
                    }
                }
                catch (Exception e) {
                    nObj.addError(e.getMessage() + ',请操作驳回。');
                }
            }
        }
        if (0 < raesList.size()) {
            update raesList;
        }
    }
    // add by lc 2022/11/15 DB202211029119 end
    private void setAgencyHPReceived() {
        Set<Id> raIdSet = new Set<Id>();
@@ -332,8 +384,11 @@
                    //如果为批量延期,那么这个集合里面会存放 原单+原单下所有的从单
                    //如果为从单,并且目的2为询价,那么会存放 当前从单的原单+从单原单下所有的从单(包括当前从单)
                    List<Rental_Apply__c> checkRentalApply = new List<Rental_Apply__c>();
                    // 只有产品试用会存在批量延期
                    if(String.isNotBlank(nObj.Extension_Type__c) && nObj.Extension_Type__c == '批量延期'){
                        System.debug('========================1=========================');
                        if(String.isNotBlank(nObj.Extension_Much_ID__c)){
                            System.debug('========================2=========================');
                            String parentId = nObj.Id;
                            parentId = parentId.substring(0,15);
                            String likeParentId = parentId+'%';
@@ -363,6 +418,7 @@
                                            order by CreatedDate asc];
                        }
                    }else{
                        System.debug('========================3=========================');
                        //如果延期的是从单,那么需要特殊处理
                        if(String.isNotBlank(nObj.Root_Rental_Apply__c) && (nObj.demo_purpose2__c == '试用(无询价)' || nObj.demo_purpose2__c == '试用(有询价)')){
                            String likeParentId = nObj.Root_Rental_Apply__c+'%';
@@ -395,6 +451,8 @@
                        }
                        checkRentalApply.add(oObj);
                    }
                    System.debug('========================checkRentalApply=========================' + checkRentalApply);
                    for (Rental_Apply_Equipment_Set__c raes : getCan_Extend_RequestList(checkRentalApply)) {
                        //延期批准时间(最初)或延期批准时间(最终) 值都有变动,那么证明此次延期已经批准了,那么需要给申请单的一览赋值
                        if (oObj.ExtensionApprovalTime_Initial__c != nObj.ExtensionApprovalTime_Initial__c
@@ -458,7 +516,7 @@
            update raesList;
        }
    }
    // 20220909 ljh 恢复代码
    private void checkbenbu() {
        for (Rental_Apply__c nObj : newList) {
            if (nObj.DataMigration_Flag__c == false) {
@@ -983,6 +1041,116 @@
        }
    }
    public static void decryptInsert(List<Rental_Apply__c> newList){
        if(!system.isFuture()){
            List<Rental_Apply__c> fendanList = new List<Rental_Apply__c>();
            for(Rental_Apply__c ra : newList){
                if(ra.Old_Rental_Apply__c != null){
                    fendanList.add(ra);
                }
            }
            if(fendanList.size() == 0){
                system.debug('no need split');
                return;
            }
           decryptInsertFuture(JSON.serialize(fendanList));
        }
    }
    @future(callout=true)
    public static void decryptInsertFuture(string json_list){
        decryptInsertCore(json_list);
    }
    // List<Rental_Apply__c> temps = [select id,AWS_Data_Id__c,name, direct_shippment_address__c,  Direct_Shippment_Address_Encrypt__c, Phone_number__c,  Phone_Number_Encrypt__c,CreatedDate   from Rental_Apply__c where AWS_Data_Id__c != null order by CreatedDate desc limit 2];
    public static void decryptInsertCore(string json_list){
        system.debug('enter decryptInsertCore');
        //调用滨璜接口更新
        PIHelper.PIIntegration staticResource =  PIHelper.getPIIntegrationInfo('Rental_Apply__c');
        system.debug('staticResource.token='+staticResource.token);
        if(String.isBlank(staticResource.token)){
            System.debug('获取aws token 失败');
            return;
        }
        List<Rental_Apply__c> newList = (List<Rental_Apply__c>)Json.deserialize(json_list, List<Rental_Apply__c>.class);
        Map<Id,Rental_Apply__c> newMap = new Map<Id,Rental_Apply__c>(newList);
        List<Map<string,object>> lmso = new List<Map<string,object>>();
        for(Rental_Apply__c ra : newList){
            Map<string,object> mso = new Map<string,object>();
            /*if(!string.isBlank(ra.AWS_Data_Id__c)){
                continue;
            }*/
            for(PI_Field_Policy_Detail__c detail : staticResource.PIDetails){
                if(ra.isSet(detail.SF_Field_API_Name__c)){
                    mso.put(detail.AWS_Field_API__c,ra.get(detail.SF_Field_API_Name__c));
                    mso.put(detail.AWS_Encrypted_Field_API__c,ra.get(detail.SF_Field_Encrypted_API__c));
                }
            }
            mso.put('sfRecordId',ra.Id);
            lmso.add(mso);
        }
        if(lmso.size()==0){
            system.debug('lmso.size()='+lmso.size());
            return;
        }
        string payload = Json.serialize(lmso);
        system.debug('payload='+payload);
        String awsApi = staticResource.viewUnifiedContactUrl;
        NFMUtil.response response = NFMUtil.sendToPiAWS(payload, awsApi,staticResource.token);
        system.debug(response);
        Map<string,object> res_obj = (Map<string,object>)Json.deserializeUntyped(response.responseBody);
        if(res_obj == null || !res_obj.containsKey('object') ){
            System.debug('res_obj == null || !res_obj.containsKey(\'object\')');
            return;
        }
        List<object> objList = (List<object>)res_obj.get('object');
        if(objList == null){
            System.debug('objList == null');
            return;
        }
        List<Rental_Apply__c> updateList = new List<Rental_Apply__c>();
        for(object obj : objList){
            Map<string,object> obj_map = (Map<string,object>)obj;
            string sfRecordId = null;
            string dataId = null;
            if(obj_map.containsKey('sfRecordId')){
                sfRecordId = string.valueOf(obj_map.get('sfRecordId'));
            }else{
                system.debug('obj_map.containsKey(\'sfRecordId\')='+obj_map.containsKey('sfRecordId'));
                continue;
            }
            if(obj_map.containsKey('dataId')){
                dataId = string.valueOf(obj_map.get('dataId'));
            }else{
                system.debug('obj_map.containsKey(\'dataId\')='+obj_map.containsKey('dataId'));
                continue;
            }
            if(newMap.containsKey(sfRecordId)){
                Rental_Apply__c ra = newMap.get(sfRecordId);
                ra.AWS_Data_Id__c = dataId;
                updateList.add(ra);
            }else{
                system.debug('newMap.containsKey('+sfRecordId+')='+newMap.containsKey(sfRecordId));
                continue;
            }
        }
        system.debug('updateList.size='+updateList.size());
        if(updateList.size()>0){
            update updateList;
        }
    }
    @future
    public static void doUnlockByFuture(List<ID> idList) {
        // Unlock操作
@@ -1259,6 +1427,8 @@
                            }
                        }
                    }
                    System.debug('raes==============' + raes);
                    System.debug('raes1==============' + checkCan_Extend_Request(raes, false));
                    if (checkCan_Extend_Request(raes, false)) {
                        raesList.add(raes);
                    }
force-app/main/default/classes/RentalApplyTriggerHandlerTest.cls
@@ -1,5 +1,103 @@
@isTest
public class RentalApplyTriggerHandlerTest {
    // 202220909 ljh end
    // @testSetup
    // static void setUp(){
    //     TestDataUtility.CreatePIPolicyConfigurations( new string[]{'Rental_Apply__c'});
    // }
    @TestSetup
    static void setup(){
        TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Agency_Contact__c','Contact'});
    }
    @isTest
    static void Test1(){
        Test.setMock(HttpCalloutMock.class, new HttpMock());
        List<Agency_Contact__c> lra = new List<Agency_Contact__c>();
        lra.add(new Agency_Contact__c(
        ));
        insert lra;
        Test.startTest();
        //system.debug(PIHelper.getPIIntegrationInfo('Agency_Contact__c').newEncryptUrl);
        AWSServiceTool2.EncryptPushCore(Json.serialize(lra),'Agency_Contact__c');
        AWSServiceTool2.EncryptPushFuture(null,null);
        Test.stopTest();
    }
   @isTest
    static void Test2(){
        Test.setMock(HttpCalloutMock.class, new HttpMock());
        List<Agency_Contact__c> lra = new List<Agency_Contact__c>();
        lra.add(new Agency_Contact__c(
            Aws_Data_Id__c = '123456'
        ));
        insert lra;
        Test.startTest();
        //system.debug(PIHelper.getPIIntegrationInfo('Agency_Contact__c').newEncryptUrl);
        AWSServiceTool2.EncryptPushCore(Json.serialize(lra),'Agency_Contact__c');
        Test.stopTest();
    }
    @isTest
    static void Test3(){
        Test.setMock(HttpCalloutMock.class, new HttpMock());
        Test.startTest();
        AWSServiceTool2.EncryptPushData(new string[]{'0031000000O4Cff'});
        Test.stopTest();
    }
  //@isTest
  //  static void Test2(){
  //      Test.setMock(HttpCalloutMock.class, new HttpMock());
  //      List<Agency_Contact__c> lra = new List<Agency_Contact__c>();
  //      lra.add(new Agency_Contact__c(
  //          Id = 'a2R1m0000007BPD',
  //          Aws_Data_Id__c = '123456'
  //      ));
  //      Test.startTest();
  //      //system.debug(PIHelper.getPIIntegrationInfo('Agency_Contact__c').newEncryptUrl);
  //      AWSServiceTool2.EncryptPushCore(Json.serialize(lra),'Agency_Contact__c');
  //      Test.stopTest();
  //  }
    class HttpMock implements HttpCalloutMock{
            public HTTPResponse respond(HTTPRequest request) {
            // 创建一个假的回应
            System.debug('------------------------------------------------------');
            HttpResponse response = new HttpResponse();
            string body = '';
            system.debug(request.getEndpoint());
            if(request.getEndpoint().contains('token')){
                system.debug('url=token');
                response.setHeader('Content-Type', 'application/json');
          body='{ "message": "", "object": "freqfewqfewewfewfew", "status": "", "success": true, "timestamp": 0, "txId": "" }';
            } else if(request.getEndpoint().contains('insert')){
                system.debug('url=Insert');
                response.setHeader('Content-Type', 'application/json');
          body='{ "message": "", "object": [ { "dataId": "123456", "directShippmentAddress": "", "directShippmentAddressEncrypt": "", "isDelete": 0, "phoneNumber": "", "phoneNumberEncrypt": "", "sfRecordId": "a2R1m0000007BPD" } ], "status": "", "success": true, "timestamp": 0, "txId": "" }';
            } else if(request.getEndpoint().contains('update')){
                system.debug('url=update');
                response.setHeader('Content-Type', 'application/json');
          body='{ "message": "", "object": [ { "dataId": "123456", "directShippmentAddress": "", "directShippmentAddressEncrypt": "", "isDelete": 0, "phoneNumber": "", "phoneNumberEncrypt": "", "sfRecordId": "a2R1m0000007BPD" } ], "status": "", "success": true, "timestamp": 0, "txId": "" }';
            } else{
            }
            response.setBody(body);
            response.setStatus('OK');
            response.setStatusCode(200);
            return response;
            // }
        }
    }
    // 202220909 ljh end
    /**
     * 新增申请修理关联表数据
     * RentalApplyTriggerHandler的createRentalApplyRepairLinkData()测试
@@ -669,7 +767,8 @@
            System.assertEquals(mgtProList[1].Agency_assistant2__c, raShareList[1].UserOrGroupId, ra.ToAgency__c + '的 User2, 但是实际是:' + raShareList[1].UserOrGroup.Name);
        }
    }
    @isTest
    // 202020909 ljh 为了恢复代码 注释
    /*@isTest
    static void test_copyToChild() {
                //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
@@ -718,7 +817,7 @@
        raObj.HP_received_sign_day__c = System.today();
        raObj.HP_received_sign_rich__c = '123';
        update raObj;
    }
    }*/
    // 河北, 天津
    private static List<OCM_Management_Province__c> setupOCMManagementProvince() {
        ControllerUtil.EscapeNFM001Trigger = true;
@@ -1949,8 +2048,9 @@
                //延期内容
                raListID[2].ExtensionContent__c = '申请延期从';
                raListID[2].Extension_Type__c = '批量延期';
                try {
                update raListID;
                } catch (Exception e) {}
                raListID[0].ExtensionStatus__c = '已批准';
                raListID[0].Extension_NewStep_AppTime__c = DateTime.now();
@@ -1960,6 +2060,7 @@
                raListID[0].ExtensionApplicationTime_Initial__c = null;
                raListID[0].Extension_NewStep_AppTime__c = DateTime.now();
                update raListID[0];
        }
    }