public with sharing class LexUtils {
    //获取记录类型Id
    @AuraEnabled
    public static string getRecordTypeIdByName(String Name,String DeveloperName){
        return [SELECT Id FROM RecordType where name =: Name and DeveloperName =:DeveloperName].Id;

    }
    //获取当前用户信息
    @AuraEnabled(cacheable=true)
    public static Sobject getCurrentUserInfo(){
        return getRecord(UserInfo.getUserId(),'User');
    }
    //根据用户名获取用户信息    
    @AuraEnabled(cacheable=true)
    public static User getUserByName(String name){
        if(String.isBlank(name)){
            return null;
        }
        List<User> listUser = [select id, Name from User where Name=:name];
        return listUser.size()>0 ? listUser[0] : null;
    }
	//获取全字段记录信息（记录ID，记录ApiName）
    @AuraEnabled(cacheable=false)
    public static Sobject getRecord(String recordId,String objectApiName){
        SObjectType sobjType = Schema.getGlobalDescribe().get(objectApiName);
        Map<String,Schema.SObjectField> mfields = sobjType.getDescribe().fields.getMap();
        List<String> listAllFields = new List<String>();
        listAllFields.addAll(mfields.keyset());
        for(String fieldApi : mfields.keyset() ){
            Schema.DescribeFieldResult sFieldDescribe = mfields.get(fieldApi).getDescribe();
            if( sFieldDescribe.getType() ==  DisplayType.Reference && String.isNotBlank(sFieldDescribe.getRelationshipName())){
                //system.debug('###fieldApi='+fieldApi+' ##sFieldDescribe.getReferenceTargetField()='+sFieldDescribe.getReferenceTargetField()+' ##sFieldDescribe='+sFieldDescribe);
                if(sFieldDescribe.getRelationshipName() == 'LastAmountChangedHistory' || sFieldDescribe.getRelationshipName() == 'QuoteLineItem' ||
                 sFieldDescribe.getRelationshipName() == 'LastCloseDateChangedHistory' || sFieldDescribe.getRelationshipName() == 'OriginalOrderItem'){
                    continue;
                }
                listAllFields.add(sFieldDescribe.getRelationshipName() +'.Name');
            }
        }
        system.debug('#####'+String.join(listAllFields, ','));
        String queryString = 'select ' + String.join(listAllFields, ',') + ' from ' + objectApiName + ' where id=:recordId';
        List<Sobject> listSobject = Database.query(queryString);
        return listSobject.size() > 0? listSobject[0]:null;
    }
    //提交审批
    @AuraEnabled
    public static String submitApproval(String recordId){
        Savepoint sp = Database.setSavepoint();
        try {
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(recordId);
            Approval.ProcessResult submitResult = Approval.process(req1);
            return 'OK';
        }
        catch (Exception e) {
            Database.rollback(sp);
            String chineseErrorMessage = '';
            String errorMessage = e.getMessage();
            
            String chinesePattern = '[\\u4e00-\\u9fa5]+';
            Pattern p = Pattern.compile(chinesePattern);
            Matcher matcher = p.matcher(errorMessage);
            
            while (matcher.find()) {
                chineseErrorMessage += matcher.group();
            }
            return chineseErrorMessage;
        }
    }
}