/*
 * Author: 	Zhang,Heyang
 * Created Date: 06/06/2023
 * Purpose: When copying QIS, check whether the current record status allows replication
 * Test Class: LexQISCloneButtonControllerTest
 *
 * */
public with sharing class LexQISCloneButtonController {
    public LexQISCloneButtonController() {

    }
    @AuraEnabled
    public static Boolean checkStatus(String recordId){
        QIS_Report__c qis = [SELECT Id,QIS_Status__c FROM QIS_Report__c WHERE id =: recordId LIMIT 1];
        return qis.QIS_Status__c == '取消'? true : false;
    }

    @AuraEnabled
    public static sObject getClonedValues(Id recordId, String objectAPIName) {
        try {
            Map<String, Schema.SobjectType> schemaMap = Schema.getGlobalDescribe();
            Map<String, Schema.SObjectField> fieldMap = schemaMap.get(objectAPIName).getDescribe().fields.getMap();
            String soqlQuery = 'Select ';
            Schema.DescribeFieldResult fieldResult;
            Set<String> ignoreFields = new Set<String>{'createdbyid', 'lastmodifiedbyid', 'lastmodifieddate', 'createddate'};
            for(String s : fieldMap.keySet()) {
                if(!ignoreFields.contains(s)) {
                    fieldResult = fieldMap.get(s).getDescribe();
                    if(fieldResult.isCreateable() && !fieldResult.isUnique()) {
                        soqlQuery += s + ',';
                    }
                }
            }
        
            soqlQuery = soqlQuery.removeEnd(',');
            soqlQuery += ' FROM ' + objectAPIName + ' WHERE ID = \'' + recordId + '\'';
            System.debug('soqlQuery=======>' + soqlQuery);
            Sobject record = Database.query(soqlQuery);
            return record;
        } catch(Exception e) {
            return null;
        }
    }
}