public with sharing class lexCopyProductController {
    public lexCopyProductController() {
        
    }
    // @AuraEnabled
    // public static InitData init(String recordId){
    //     InitData res = new InitData();
    //     try {
    //         List<Product_Set__c> rl=[select Name,Product_Set_CD__c,Applicable_Department__c,
    //         Product_category__c,Valid_Date__c,Invalid_Date__c,Quote_Select_Info__c,Description__c from Product_Set__c where id = :recordId];
    //         res.name=rl[0].Name;
    //         res.ProductSetCDC=rl[0].Product_Set_CD__c;
    //         res.ApplicableDepartmentC=rl[0].Applicable_Department__c;
    //         res.ProductCategoryC=rl[0].Product_category__c;
    //         res.ValidDateC=rl[0].Valid_Date__c;
    //         res.InvalidDateC=rl[0].Invalid_Date__c;
    //         res.QuoteSelectInfoC=rl[0].Quote_Select_Info__c;
    //         res.DescriptionC=rl[0].Description__c;
    //     } catch (Exception e) {
    //         throw new AuraHandledException(e.getMessage());
    //     }
    //     return res;
    // }
    @AuraEnabled
    public static InitData init(String recordId){
        InitData res = new InitData();
        String s='';
        try {
            String objectName = 'Product_Set__c'; // 要获取字段的对象名
            Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
            Schema.SObjectType objType = globalDescribe.get(objectName);
            if (objType != null) {
                Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
                s+='SELECT ';
                // 现在，fieldMap中包含了对象的所有字段信息
                for (String fieldName : fieldMap.keySet()) {
                    if(!fieldName.equals('Id')
                        ){
                        s+=fieldName+',';
                    }
                }

                s=s.removeEnd(',');
                s+=' FROM Product_Set__c where id=\''+recordId+'\'';
                system.debug('SQL:'+s);
                List<Product_Set__c> opportunitys = Database.query(s);
                s='';
                if(opportunitys.size()>0){
                    system.debug('in!');
                    for (String fieldName : fieldMap.keySet()) {
                        String formaF=fieldMap.get(fieldName).getDescribe().getName();
                        if(opportunitys.get(0).get(fieldName)!=null&&!opportunitys.get(0).get(fieldName).equals('null')){
                            if(formaF.equals('Id')
                                ||formaF.equals('OwnerId')
                                ||formaF.equals('CreatedDate')
                                ||formaF.equals('CreatedById')
                                )
                            {
                                continue;
                            }
                            Object val=opportunitys.get(0).get(fieldName);
                            if(val instanceof Date ){
                                String str=String.valueOf(val);
                                str=str.replace(' ','T');
                                str+='.000Z';
                                s+=formaF+'='+str+',';
                            }else if (val instanceof DateTime){
                                String str=String.valueOf(val);
                                str=str.replace(' ','T');
                                str+='.000Z';
                                s+=formaF+'='+str+',';
                            }else{
                                s+=formaF+'='+opportunitys.get(0).get(fieldName)+',';
                            }
                        }
                    }
                    s=s.removeEnd(',');
                }
            }
        } catch (Exception e) {
            System.debug('lexCopyToBaseController init error:'+e.getMessage());
        }
        List<Product_Set__c> rl=[select Name from Product_Set__c where id = :recordId];
        res.dataF=s;
        res.name=rl.get(0).Name;
        return res;
    }

    public class InitData{
        @AuraEnabled public String name;
        @AuraEnabled public String ProductSetCDC;
        @AuraEnabled public String ApplicableDepartmentC;
        @AuraEnabled public String ProductCategoryC;
        @AuraEnabled public Date ValidDateC;
        @AuraEnabled public Date InvalidDateC;
        @AuraEnabled public String QuoteSelectInfoC;
        @AuraEnabled public String DescriptionC;
        @AuraEnabled public String dataF;
    }
}