//拆分选项列表的共通方法
public without sharing class SplitOptionListUtil {
    public SplitOptionListUtil() {
        
    }

    //传递一个map进来  key是sobj对象 value是拼接好的选项列表api
    //1.根据sobj的id获取对象名 -> Id.getSObjectType().getDescribe().getName()
    //2.根据value获取选项列表对应的lable名 -> map<sobj,map<api,lable>>
    //3.查询当前的"报告书状况统计"表中是否存在对应的值 如果有全删除再添加
    //4.获取对象上的字段值并创建map <sobj对象,map<lable名称,内容集合>>
    ////key -> sobj对象  value: -> key :api对应的lable名称 value:内容集合
    public void makeObject(Map<Sobject,List<String>> data){
    	System.debug('14:::data::'+data);
    	//1.获取对象名
    	String objName = data.keySet().iterator().next().Id.getSObjectType().getDescribe().getName();
    	//2.获取对象上的 api->lable标签名的对应关系
    	Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    	Schema.SObjectType leadSchema = schemaMap.get(objName);
		Map<String, Schema.SObjectField> apiLableMap = leadSchema.getDescribe().fields.getMap();
		//3.全删全加
		List<Id> soIds = new List<Id>();
		Set<String> columns = new Set<String>();
    	for (SObject sobj : data.keySet()) {
    		soIds.add(sobj.Id);
    		for (String str : data.get(sobj)) {
    			columns.add(str);
    		}
    	}
    	//拼接待查询的字段
    	String columnStr = '';
    	if (columns.size()>0) {
    		for (String str : columns) {
    			columnStr += str +',';
    		}
    	}
    	if (columnStr != '') {
    		columnStr = columnStr.substring(0,columnStr.length() - 1);	
    	}

    	//取得查找字段名
    	objName = objName.endsWith('__c')? objName : objName+'__c';
		//3.1 获取所有存在的报告书状况统计对象
		List<Sobject> delRpmObjList = new List<SObject>();
		String soql ='select id,Category__c,'+objName+' from ReportMemo__c where ' +objName + ' in :soIds';
		List<SObject> rmList = Database.query(soql);
		if (rmList != null && rmList.size() > 0) {
			for (SObject rep : rmList) {
				for (Sobject sobj : data.keySet()) {
					if (rep.get(objName) == sobj.Id) {
						for (String str : data.get(sobj)) {
							if (rep.get('Category__c') == apiLableMap.get(str).getDescribe().getLabel()) {
								delRpmObjList.add(rep);
							}
						}
					}
				}
			}
		}
		List<ReportMemo__c> delrpmList = (List<ReportMemo__c>)delRpmObjList;
		System.debug('delrpmList::'+delrpmList);
		if (delrpmList != null && delrpmList.size() > 0) {
			delete delrpmList;
		}

    	//4.获取对象上的字段值
    	//创建集合
    	//key -> sobj对象  value: -> key :api对应的lable名称 value:内容集合
    	Map<SObject,Map<String,List<String>>> fieldMap = new Map<SObject,Map<String,List<String>>>();
    	for (SObject sobj : data.keySet()) {
    		String key = '';
    		String value = '';
			Map<String,Object> fieldsToValue = sobj.getPopulatedFieldsAsMap();
			Map<String,List<String>> sobjFieldMap = new Map<String,List<String>>();

			for (String fieldName : fieldsToValue.keySet()){
				for (String apiName : data.get(sobj)) {
					if (fieldName == apiName) {
						System.debug('79::::apiName:'+apiName);
						value = (String) fieldsToValue.get(fieldName);
						key = apiLableMap.get(fieldName).getDescribe().getLabel();
						sobjFieldMap.put(key, value.split(';'));
					}
				}
			}
			fieldMap.put(sobj, sobjFieldMap);
    	}

    	System.debug('fieldMap::'+fieldMap);
    	
    	//添加
    	List<SObject> insrpmObjList = new List<SObject>();

    	//key -> sobj对象 value: -> key :api对应的lable名称 value:内容集合
    	for (SObject sobj : fieldMap.keySet()) {
    		Map<String,List<String>> apiValMap = fieldMap.get(sobj);
    		for (String category : apiValMap.keySet()) {
    			for (String str : apiValMap.get(category)) {
    				SObject rmc = new ReportMemo__c();
					rmc.put('Category__c', category);
					rmc.put('Memo__c', str);
					rmc.put(objName, sobj.Id);
					insrpmObjList.add(rmc);
					
				}
    		}
    	}

    	List<ReportMemo__c> insrpmList = (List<ReportMemo__c>)insrpmObjList;
    	if (insrpmList != null && insrpmList.size() > 0) {
    		insert insrpmList;
    	}
    }

}