package com.seeyon.apps.src_saomaoyi.utils;

import com.seeyon.v3x.services.form.bean.RecordExport;
import com.seeyon.v3x.services.form.bean.SubordinateFormExport;
import com.seeyon.v3x.services.form.bean.ValueExport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

//创建无流程表单数据处理工具类
public class FormExportUtil {

	/**
	 * 	设置主表信息
	 * @param map 设置主表字段。Map<主表显示名称,主表数据>
	 * @return
	 */
	public List<ValueExport> setFormValue(Map<String, Object> map ){
//		创建返回值对象
		List<ValueExport> valueExports = new ArrayList<ValueExport>();
		ValueExport valueExport ;
//		获取参数信息（显示名称）
		Set<String> keys = map.keySet();
		if(keys.size()>0) {
//			对控件赋值
			for (String key : keys) {
				if(map.get(key) != null ){
				valueExport = new ValueExport();
				valueExport.setDisplayName(key);
				valueExport.setValue(map.get(key).toString());
		    	valueExports.add(valueExport);
				}
				System.out.println(key+":"+map.get(key));
			}
		}
		return valueExports;
	}
	
	/**
	 * 设置从表信息
	 * @param lists 设置主表字段。List<Map<显示名称,数据值>>
	 */
	public List<SubordinateFormExport> setSubordinateFormValue(List<Map<String, Object>> lists){
		List<SubordinateFormExport> subordinateFormExports = new ArrayList<SubordinateFormExport>();
    	SubordinateFormExport subordinateFormExport = new SubordinateFormExport();
    	List<RecordExport> recordExports = new ArrayList<RecordExport>();
    	List<ValueExport> valueExports;
    	RecordExport recordExport;
    	for(int i = 0 ; i < lists.size() ; i++) {
    		recordExport = new RecordExport();
        	valueExports = setFormValue(lists.get(i));
        	recordExport.setRecord(valueExports);
        	recordExports.add(recordExport);
    	}
    	subordinateFormExport.setValues(recordExports);
    	subordinateFormExports.add(subordinateFormExport);
	
    	return subordinateFormExports;
	}

	public List<SubordinateFormExport> setAllSubordinateFormValue(List<Map<String,Object>> lists){
		List<SubordinateFormExport> subordinateFormExports = new ArrayList<SubordinateFormExport>();
		for (Map<String, Object> list : lists) {
			SubordinateFormExport subordinateFormExport = new SubordinateFormExport();
			List<RecordExport> recordExports = new ArrayList<RecordExport>();
			for (String key : list.keySet()) {
				List<Map<String,Object>> columns = (List<Map<String, Object>>) list.get(key);
				for(int i = 0 ; i < columns.size() ; i++) {
					List<ValueExport> valueExports = setFormValue(columns.get(i));
					RecordExport recordExport = new RecordExport();
					recordExport.setRecord(valueExports);
					recordExports.add(recordExport);
				}
			}
			subordinateFormExport.setValues(recordExports);
			subordinateFormExports.add(subordinateFormExport);
		}
		return subordinateFormExports;
	}


}
