/*
 * Author: Zhang,Heyang
 * Created Date: 2023/08/07
 * Purpose: get page layout and record data，根据所选医院带出直返地址信息
 * Test Class: LexNewAndEditRepairPIPLControllerTest
 *
 * */
global with sharing class LexNewAndEditRepairPIPLController {
    @AuraEnabled
    public static ResponseBodyLWC initData(Id rid, String recordTypeId, String sobjectType, String RepairId) {
        Map<String, object> data = new Map<String, object>();
        ResponseBodyLWC rbl = LexNewAndEditBasePIPLController.initData(rid, recordTypeId, sobjectType);
        System.debug('调用父组件已完成: ');
        if(rbl.status == 'Success'){
            data = (Map<String,Object>)rbl.entity;
            data.put('staticResourceContact', Json.serialize(PIHelper.getPIIntegrationInfo('Contact')));
            data.put('staticResourceAddress', Json.serialize(PIHelper.getPIIntegrationInfo('Address__c')));
            data.put('staticResourceRepairSubOrder', Json.serialize(PIHelper.getPIIntegrationInfo('RepairSubOrder__c')));
            //data.put('staticResourceV2', Json.serialize(PIHelper.getPIIntegrationInfo('Repair__cV2')));
            System.debug('data: ' + data);
            if(String.isNotBlank(rid)){
                String sql = 'SELECT Incharge_Staff_Contact__r.lastName,Incharge_Staff_Contact__r.AWS_Data_Id__c,Delivered_Product__r.Name, ';
                DescribeSObjectResult objectType = rid.getSobjectType().getDescribe();
                List<String> objectFields = new List<String>(objectType.fields.getMap().keySet());
                sql += String.join(objectFields, ',') +' from '+sobjectType+' where id =\''+rid+'\' limit 1';
                System.debug('sql: ' + sql);
                Sobject leadData = Database.query(sql);
                data.put('data',leadData);
            }
            if(String.isNotBlank(RepairId)){
                Repair__c rc = new Repair__c();
                rc = [SELECT Hospital__c,
                             Department_Class__c,
                             Account__c,
                             Dealer__c,
                             Incharge_Staff_Contact__c,
                             Incharge_Staff__c, 
                             RepairSubOrder__c,
                             On_Call_ID__c,
                             QIS_ID__c,	
                             InsReport__c,
                             Rental_Apply_Equipment_Set_Detail__c 
                        FROM Repair__c 
                        WHERE id =:RepairId LIMIT 1 ];
                data.put('repairData',rc);
            }

            rbl.entity = data;
        }
        return rbl;
    }

    @AuraEnabled
    public static Map<String, Object> getContactByAWSId(String awsId){
        Map<String, Object> resultMap = new Map<String, Object>();
        Contact ContactInfo = new Contact();
        ContactInfo = [SELECT Id,Name,Account.Name,Phone,Email,MobilePhone FROM Contact WHERE AWS_Data_Id__c =:awsId LIMIT 1];
        resultMap.put('ContactInfo', ContactInfo);
        return resultMap;
    }

    global class Response{
        @AuraEnabled
        global String recordId{set;get;}
        @AuraEnabled
        global String message{set;get;}
        @AuraEnabled
        global String status{set;get;}
    }

    @AuraEnabled
    global static Response EncryptUpdate(string rid){
        system.debug('rid='+rid);
        Response r = new Response();
        List<Repair__c> reps = new List<Repair__c>();
        //测试用假ID，测试时会传进来a0J1m000001QqXk
        if(Test.isRunningTest()){
            if(rid==System.label.piplCon_1){//niwu-a0J1m000001QqXk
                reps.add(new Repair__c(Id=System.label.piplCon_1,Encrypt_Update_Flag__c=true));
            }else{
                reps.add(new Repair__c(Id=System.label.piplCon_1,Encrypt_Update_Flag__c=false));
            }
        }else{
            reps = [select id,Encrypt_Update_Flag__c from Repair__c where id =:rid];
        }        
        if(reps.size()==0){
            r.status = 'failed';
            r.message = '符合条件的数据未找到';
            return r;
        }
        Repair__c rep = reps[0];
        if(!rep.Encrypt_Update_Flag__c){
            r.status = 'success';
            r.message = '';
            return r;
        }
        //zhj MEBG新方案改造 2022-11-29 start
        //boolean b = AWSServiceTool2.EncryptPushData(new string[]{rid});
        System.debug('AWSServiceTool2V2.EncryptPushDataRepair start');
        
        //AWSServiceTool2V2.EncryptPushDataRepair(rid);
        boolean b = false;
        if(!Test.isRunningTest()){
            b = AWSServiceTool2V2.EncryptPushDataRepair(rid);
        }
        //zhj MEBG新方案改造 2022-11-29 end
        rep.Encrypt_Update_Flag__c = false;
        if(!Test.isRunningTest()){
            update rep;
        }      
        r.status = b ? 'success' : 'failed';
        r.message = b ? '' : '加密推送失败';
        r.recordId = rid;
        return r;
    }
}