global without sharing class CampaignMemberUploadFileController {
    public String staticResource {get; set;}
    public static String sobjectTypeValue = 'CampaignMember';
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public CampaignMemberUploadFileController(ApexPages.StandardController controller) {
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Document'));
    }
    global class Response{
        public String message{set;get;}
        public String status{set;get;}
    }
    @RemoteAction
    global static Response saveFile(String CampaignMemberDatas,String transId){
        Response res = new Response();
        Savepoint sp = Database.setSavepoint();
        try{
            System.debug('CampaignMemberDatas:' + JSON.serialize(CampaignMemberDatas));
            Schema.SObjectType CampaignMemberSchema = schemaMap.get(sobjectTypeValue);
            Map<String, Schema.SObjectField> fieldAPIToTypeMap = CampaignMemberSchema.getDescribe().fields.getMap();
            Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeUntyped(CampaignMemberDatas);
            List<Map<String,Object>> datas = (List<Map<String,Object>>)fieldValueMap.get('idList');
            List<CampaignMember> CMList = new List<CampaignMember>();
            List<String> awsIdList = new List<String>();
            List<String> sfIdList = new List<String>();
            List<String> contentList = new List<String>();
            for (Integer i = 0; i < datas.size(); i++) {
                CampaignMember cm = new CampaignMember();
                for(String fieldAPI : datas[i].keySet()){ 
                    if(!fieldAPIToTypeMap.containskey(fieldAPI)){
                        continue;
                    }
                    Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();  
                    String fieldValue = String.valueOf(datas[i].get(fieldAPI)); 
                    if(fieldAPI == 'AWS_Data_Id__c'){
                        awsIdList.add(fieldValue);
                    }
                    if(String.valueOf(fielddataType)=='DATE'){
                        cm.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-')));              
                    }else if(String.valueOf(fielddataType)=='DATETIME'){
                        if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){
                            fieldValue = fieldValue.replace('T',' ');
                            cm.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
                        }else if(String.isNotBlank(fieldValue))  {
                            fieldValue = fieldValue.replace('/', '-') + ':00';
                            cm.put(fieldAPI, Datetime.valueOf(fieldValue));
                        }else{
                            cm.put(fieldAPI, null);
                        }                    
                    }else if(String.valueof(fielddataType)=='CURRENCY'|| String.valueof(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
                        cm.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?null:Decimal.valueOf(fieldValue.replace(',', ''))); 
                    } else if(String.valueof(fielddataType)=='BOOLEAN'){
                        cm.put(fieldAPI, datas[i].get(fieldAPI));
                    }else {
                        cm.put(fieldAPI,fieldValue);
                    }  
                }
                CMList.add(cm);
            }
            insert CMList;
            for(CampaignMember cm : CMList){
                contentList.add(cm+'');
                sfIdList.add(cm.id);
            }
            //记录日志
            res.message = 'success';
            res.status = 'success';
            PIHelper.saveTransLogBatch(sobjectTypeValue,awsIdList,sfIdList,transId, contentList ,res.status,'');
            return res;
        }catch(DmlException e){
            Integer index = 0;
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            res.message = '保存失败，原因:'+ e.getDmlMessage(index);
            res.status = 'Exception';
            PIHelper.saveTransLog(sobjectTypeValue,'','',transId, '' ,res.status,e.getMessage()+e.getStackTraceString());
            return res;
        }catch(Exception e) {
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);     
            res.message = e.getMessage()+e.getStackTraceString();  
            res.status = 'Exception';
            PIHelper.saveTransLog(sobjectTypeValue,'','',transId, '' ,res.status,res.message);
            return res;
        }
    }
}