
package nccloud.web.hryf.regApply.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.hr.utils.ResHelper;
import nc.itf.hr.frame.IPersistenceRetrieve;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.hi.trnstype.TrnstypeFlowVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.billtype.BilltypeVO;
import nc.vo.trn.pub.TRNConst;
import nc.vo.trn.regitem.TrnRegItemVO;
import nc.vo.trn.regmng.AggRegapplyVO;
import nc.vo.trn.regmng.Glbdef1VO;
import nc.vo.trn.regmng.Glbdef2VO;
import nc.vo.trn.regmng.RegapplyVO;
import nc.vo.uif2.LoginContext;
import nccloud.commons.lang.StringUtils;
import nccloud.dto.hryf.tools.HRYFConst;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.meta.PageMeta;
import nccloud.web.hr.pub.HRNccPubEnv;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hryf.regApply.utils.RegPageMetaUtil;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 转正申请-新增按钮
 * @author lbt 20190314
 *
 */
public class RegApplyAddAction extends RegBaseAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		// 解析请求中得参数，放入map中，并根据参数获取用户的登录上下文信息。
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		//  获取页面参数
		Integer probationType = param.getInt("probationType", null);
		if(probationType == null){
			throw new BusinessException(ResHelper.getString("6009reg","06009reg0017")/*@res "当前选择的试用期类型为空，不能进行后续操作！"*/);
		}
		// 查询出转正前/后 项目信息
		TrnRegItemVO[] regItemSet = super.probationTypeNotNullValidate(context,param,probationType);
		AggRegapplyVO aggVO = super.getDefaultValue(context,probationType+"",false);
		String trnstype = TRNConst.TRANSTYPE_REG;
		TrnstypeFlowVO[] flow =  (TrnstypeFlowVO[]) ServiceLocator.find(IPersistenceRetrieve.class).retrieveByClause(null, TrnstypeFlowVO.class,
	                        " pk_group = '" + HRNccPubEnv.getPk_group() + "' and pk_trnstype = '" + trnstype + "'");
		// 业务类型配置
		if (flow != null && flow.length > 0) {
             ((RegapplyVO)aggVO.getParentVO()).setTranstypeid(flow[0].getPk_transtype());
             ((RegapplyVO)aggVO.getParentVO()).setTranstype(null);
             if (StringUtils.isNotEmpty(flow[0].getPk_transtype())){
                 BilltypeVO billtype =  (BilltypeVO) ServiceLocator.find(IPersistenceRetrieve.class) .retrieveByPk(null, BilltypeVO.class, flow[0].getPk_transtype());
                 ((RegapplyVO)aggVO.getParentVO()).setTranstype(billtype.getPk_billtypecode());
             }
             ((RegapplyVO)aggVO.getParentVO()).setBusiness_type(flow[0].getPk_businesstype());
        }
		
		((RegapplyVO)aggVO.getParentVO()).setAttributeValue("glbdef5", "总分90分及以上为优秀；80分（含）-90分为良好；70分（含）-80分为称职；60分（含）-70分为合格；60分以下为不合格");
		
		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 order by code");
		if(defdocVOs != null && defdocVOs.length > 0) {
			List<Glbdef2VO> glbdef1List = new ArrayList();
			for(DefdocVO vo : defdocVOs) {
				Glbdef2VO glbdef1VO = new Glbdef2VO();
				glbdef1VO.setAttributeValue("glbdef1", vo.getPk_defdoc());
				glbdef1VO.setAttributeValue("glbdef2", vo.getDef1());
				glbdef1VO.setAttributeValue("glbdef3", vo.getDef2());
				glbdef1VO.setAttributeValue("glbdef4", vo.getDef3());
				glbdef1VO.setAttributeValue("glbdef5", vo.getDef4());
				glbdef1List.add(glbdef1VO);
			}
			aggVO.setChildrenVO(glbdef1List.toArray(new Glbdef2VO[0]));
		}
		
		PageMeta pm = super.showAllSetItems(context, HRYFConst.APPCODE_REGAPPLY, HRYFConst.PAGECODE_REGAPPLY, true, regItemSet,TRNConst.TRNS_OLDINFO_TAB,TRNConst.TRNS_NEWINFO_TAB);
		pm = RegPageMetaUtil.getPageMetaByEditType( context, pm,HRYFConst.APPCODE_REGAPPLY, "add",(RegapplyVO)aggVO.getParentVO());
		
		//返回页面默认数据
		Map<String,Object> result = new HashMap<>();
		result.put("formData", HRNccTemplateUtils.getFormWithArea(HRYFConst.PAGECODE_REGAPPLY, "card", (RegapplyVO)aggVO.getParentVO()));
		result.put("formBodyData", HRNccTemplateUtils.getGridWithArea(HRYFConst.PAGECODE_REGAPPLY, "card_body", (Glbdef2VO[])aggVO.getChildrenVO()));
		result.put("isBillCodeEditable", super.isBillCodeEditable(context));
		// 返回前后项目模板
		
		result.put(TRNConst.TRNS_OLDINFO_TAB, pm.getAreaMeta(TRNConst.TRNS_OLDINFO_TAB));
		result.put(TRNConst.TRNS_NEWINFO_TAB, pm.getAreaMeta(TRNConst.TRNS_NEWINFO_TAB));
		result.put(TRNConst.TRNS_PSNINFO_TAB, pm.getAreaMeta(TRNConst.TRNS_PSNINFO_TAB));
		result.put("card", pm.getAreaMeta("card"));
		
		result.put("card_body", pm.getAreaMeta("card_body"));
		return result;
	}
	
}
