package nccloud.web.hryf.regApply.action;

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

import org.apache.commons.lang3.StringUtils;

import nc.itf.hr.frame.IPersistenceRetrieve;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.trn.regmng.Glbdef1VO;
import nc.vo.uif2.LoginContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.config.PageTemplet;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hryf.pub.PsndocTempUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;

public class RegApplyBodyAddAction extends HRCommonAction{

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		
		String page_code = param.getString("page_code", null);
		if (StringUtils.isBlank(page_code)) {
			throw new BusinessRuntimeException("Parameter Error!");
		}
		
		String area_code = param.getString("area_code", null);
		if (StringUtils.isBlank(area_code)) {
			throw new BusinessRuntimeException("Parameter Error!");
		}
		
		return bodyAddChange(param, context,page_code, area_code);
	}
	
	private Object bodyAddChange(ParamUtils param, LoginContext context, String page_code, String area_code){
		IPersistenceRetrieve service = (IPersistenceRetrieve)ServiceLocator.find(IPersistenceRetrieve.class);
		PageTemplet pt = PsndocTempUtils.getPageTemp(context, page_code);
		List<Glbdef1VO> glbdef1List = new ArrayList();
		try {
			DefdocVO[] defdocVOs = (DefdocVO[]) ServiceLocator.find(IPersistenceRetrieve.class).retrieveByClause(null, DefdocVO.class, "pk_defdoclist = (select pk_defdoclist from bd_defdoclist where code = 'HRTRN099' and nvl(dr,0) = 0) and nvl(dr,0) = 0");
			if(defdocVOs != null && defdocVOs.length > 0) {
				for(DefdocVO vo : defdocVOs) {
					Glbdef1VO glbdef1VO = new Glbdef1VO();
//					glbdef1VO.setAttributeValue("glbdef1", vo.getPk_defdoc());
//					glbdef1VO.setAttributeValue("glbdef2", vo.getDef1());
//					glbdef1VO.setAttributeValue("glbdef3", vo.getDef2());
//					glbdef1VO.setAttributeValue("glbdef4", vo.getDef3());
					glbdef1List.add(glbdef1VO);
				}
			}
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return getResultMap(glbdef1List.toArray(new Glbdef1VO[0]), page_code, area_code);
	}
	
	protected Object getResultMap(Glbdef1VO[] bills, String page_code, String area_code) {
		HashMap resultMap = new HashMap();
		resultMap.put("formData", HRNccTemplateUtils.getGridWithArea(page_code, area_code, bills));
		return resultMap;
	}
}
