package nccloud.web.hrhi.psndoc.action;

import java.util.List;
import java.util.Map;

import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.uif2.LoginContext;
import nccloud.framework.core.io.WebFile;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrhi.psndoc.vo.ExportFile;
import nccloud.web.hrhi.psndoc.vo.ImportHead;
import nccloud.web.hrhi.psndoc.vo.TrainPsndocXlsExporter;
import nccloud.web.uapbd.commons.web.ParamUtils;
import nccloud.base.exception.ExceptionUtils;
import nccloud.commons.lang.ArrayUtils;

public class TrainExportAction implements ICommonAction{

	@Override
	public Object doAction(IRequest request) {
//		Map<String, String[]> readParameters = request.readParameters();
//		ParamUtils param = new ParamUtils(request);
//        LoginContext context = HRNccUtils.getLoginContext(param);
		Map<String, String[]> readParameters = request.readParameters();
		if (readParameters.isEmpty()) {
			return null;
		}
		String[] querys = (String[])readParameters.get("querystr");
		if (ArrayUtils.isEmpty(querys)) {
			return null;
		}
		String jsonStr = ((String[])readParameters.get("querystr"))[0];
		Map<String, String> map = (Map)JsonFactory.create().fromJson(jsonStr, Map.class);
		map.put("node_type", NODE_TYPE.ORG_NODE.toString());
		List<Map<String, Object>> headList = ImportHead.getHead();
		
		TrainPsndocXlsExporter exporter = new TrainPsndocXlsExporter();
		try {
			ExportFile importFile = exporter.exportToFile(null, headList, UFBoolean.TRUE, map.get("pk_org"), map.get("pk_group"));
			return new WebFile(importFile.getFileName(), importFile.getInputStream());
		} catch (Exception e) {
			ExceptionUtils.wrapBusinessException(e.getMessage());
			return null;
		}
	}

}
