package nccloud.web.hrhi.psndoc.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import nc.bs.framework.common.NCLocator;
import nc.hr.utils.ResHelper;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.bd.ref.RefInfoVO;
import nc.vo.hi.repdef.InfoSetItemVO;
import nc.vo.hr.infoset.InfoItemVO;
import nc.vo.pub.BusinessException;
import nccloud.framework.service.ServiceLocator;

public class ImportHead {
	
	public static IUAPQueryBS iUapQueryBS;
	
	public static IUAPQueryBS getIUAPQueryBS() {
		if(iUapQueryBS == null) {
			iUapQueryBS = ServiceLocator.find(IUAPQueryBS.class);
		}
		return iUapQueryBS;
	}
	
	/**
	 * 获取培训子集的表头字段
	 * @return
	 */
	public static List<InfoItemVO> getInfoSetItems(){
		String sql = "select * from hr_infoset_item where pk_infoset = (select pk_infoset from hr_infoset where infoset_code = 'hi_psndoc_train') and hided = 'N' order by showorder";
		List<InfoItemVO> result = new ArrayList<InfoItemVO>();
		try {
			result = (List<InfoItemVO>) getIUAPQueryBS().executeQuery(sql, new BeanListProcessor(InfoItemVO.class));
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static RefInfoVO getRefInfoVO(String pk_refinfo) {
		RefInfoVO vo = null;
		try {
			vo = (RefInfoVO) getIUAPQueryBS().retrieveByPK(RefInfoVO.class, pk_refinfo);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	public static List<Map<String, Object>> getHead(){
		LinkedList listBillItems = new LinkedList();
		Map<String, Object> billItem1 = new HashMap<String, Object>();
		billItem1.put("name", ResHelper.getString("common", "UC000-0000147"));
	    billItem1.put("key", "psnCode");
	    billItem1.put("dataType", Integer.valueOf(0));
	    billItem1.put("length", Integer.valueOf(40));
	    billItem1.put("width", Integer.valueOf(100));
	    billItem1.put("nullable", "N");
	    listBillItems.add(billItem1);
	     
	    Map<String, Object> billItem2 = new HashMap<String, Object>();
	    billItem2.put("name", ResHelper.getString("common", "UC000-0001403"));
	    billItem2.put("key", "psnName");
	    billItem2.put("dataType", Integer.valueOf(0));
	    billItem2.put("length", Integer.valueOf(200));
	    billItem2.put("width", Integer.valueOf(100));
	    billItem2.put("nullable", "N");
	    listBillItems.add(billItem2);
		
	    List<InfoItemVO> items = getInfoSetItems();
		if(items != null && items.size() > 0) {
			for(InfoItemVO vo : items) {
				Map<String, Object> billItem = new HashMap<String, Object>();
				billItem.put("name", vo.getItem_name());
				billItem.put("key", vo.getItem_code());
				billItem.put("dataType", vo.getData_type());
				billItem.put("length", vo.getMax_length());
				billItem.put("width", Integer.valueOf(100));
				billItem.put("nullable", vo.getNullable().toString());
				if(vo.getData_type() == 5) {
					RefInfoVO refVO = getRefInfoVO(vo.getRef_model_name());
					billItem.put("className", refVO.getRefclass());
					billItem.put("pk_refinfo",refVO.getPk_refinfo());
				}
				if(vo.getData_type() == 6) {
					billItem.put("enum_id", vo.getEnum_id());
				}
			    listBillItems.add(billItem);
			}
		}
		
		return listBillItems;
	}
}
