package nccloud.web.hrwa.pubinfo.wadataexplain.action;

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

import nc.vo.hrwa.wadataexplain.Wadataexplain;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.convert.translate.Translator;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridFormulaHandler;
import nccloud.itf.bd.wadataexplain.IWadataexplainService;
import nccloud.web.action.NCCAction;
import nccloud.web.utils.GridModelConvertUtils;


public class WadataexplainBatchCommitAction extends NCCAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		Grid grid = (Grid) para;
		GridModelConvertUtils gridConvUtl = new GridModelConvertUtils();
		Wadataexplain[] vos = gridConvUtl.toVOs(Wadataexplain.class, grid.getModel());
		StringBuffer pk_wa_class_buf = new StringBuffer();
		StringBuffer pk_buf = new StringBuffer();
		StringBuffer cyearperiodbuff = new StringBuffer();
		List<String> cyplist = new ArrayList<String>();
		List<String> waclasslist = new ArrayList<String>();
		HashMap<String,String> psndocMap = new HashMap<String,String>();
		for (Wadataexplain vo : vos) {
			String pk_psndoc = vo.getPk_psndoc();
			String pk_wa_data_explain = vo.getPk_wa_data_explain();
			String pk_wa_class = vo.getPk_wa_class();
			String cyp = vo.getDef1();
			pk_buf.append("'").append(pk_wa_data_explain).append("',");
			if(!waclasslist.contains(pk_wa_class))
				pk_wa_class_buf.append("'").append(pk_wa_class).append("',");
			if(!cyplist.contains(cyp))
				cyearperiodbuff.append("'").append(cyp).append("',");
			psndocMap.put(pk_psndoc, pk_psndoc);
			cyplist.add(cyp);
			waclasslist.add(pk_wa_class);
		}
		List<Wadataexplain> result  =null;
		if(pk_wa_class_buf.length()>0 && pk_buf.length()>0){
			 result = ServiceLocator.find(IWadataexplainService.class).batchCommit(pk_wa_class_buf, pk_buf, psndocMap,cyearperiodbuff);
		}
		if(null!=result) {
			
			grid = gridConvUtl.toGridOpe(WadataexplainConsts.pagecode, result.stream().toArray(Wadataexplain[]::new));
		}
		// 处理显示公式
		GridFormulaHandler gridFormulaHandler = new GridFormulaHandler(grid);
		gridFormulaHandler.handleLoadFormula();
		// 翻译器，将主键翻译成名称
		Translator translator = new Translator();
		translator.translate(grid);
		return grid;
	}

	@Override
	protected Class getParaClass() {
		return Grid.class;
	}

}
