package nccloud.web.hryf.regApply.action;

import nc.itf.hr.pf.IHrPf;
import nc.vo.hr.pf.PFQueryParams;
import nc.vo.hr.tools.pub.HRConstEnum;
import nc.vo.om.pub.SuperVOHelper;
import nc.vo.pub.BusinessException;
import nc.vo.trn.regmng.AggRegapplyVO;
import nc.vo.trn.regmng.RegapplyVO;
import nc.vo.uif2.LoginContext;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.model.PageInfo;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.itf.hryf.pub.IHRYFTempletConst;
import nccloud.web.hr.pub.HRNccQueryHelper;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hryf.pub.utils.BillQueryUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 转正申请查询
 * @author lbt 20190314 
 *
 */
public class RegApplyQueryAction extends RegBaseAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		
		// 解析请求中得参数，放入map中，并根据参数获取用户的登录上下文信息。
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		// 不选主组织 查询不出数据
        if (context.getPk_org() == null){
            return null;
        }
        // 获取查询面板SQL
        HRNccQueryHelper helper = new HRNccQueryHelper(request);
 		String condition = helper.getWhereSql();
		if(StringUtils.isBlank(condition)){
			condition="1=1 ";
		}
        // 组装申请单状态数组 sql
		String[] billStateArr = param.getStrings("billStatus");
		String billStateSqlWhere = this.getBillStateSqlByStateArr(billStateArr);
		// 组装申请单日期sql
		String billTime = param.getString("time",null);
		String billTimeSqlWhere = this.getBillTimeSqlByTime(billTime,param);
		// 组装 试用类型Sql
		String  probationType = param.getString("probationType", "3");
		String probationTypeSqlWhere = this.getProbationTypeSqlByTime(probationType);
		
		// SQL总装
		condition = condition + billStateSqlWhere + billTimeSqlWhere + probationTypeSqlWhere;
		return getResData(context,condition,helper);
	}
	
	private Grid getResData(LoginContext context,String condition,HRNccQueryHelper helper) throws BusinessException  {
		// 查询操作,NC方法返回聚合VO
		AggRegapplyVO[] aggRegApps = (AggRegapplyVO[])ServiceLocator.find(IHrPf.class).queryBill(context, AggRegapplyVO.class, condition, false, getQueryParams(), getOrderBySQL());
		
		if (aggRegApps == null || aggRegApps.length == 0) {
			return null;
		}
		
		// 取出聚合VO中的CARD数据，返回数组
		RegapplyVO[] regapps = SuperVOHelper.getParentVOArrayFromAggVOs(aggRegApps, RegapplyVO.class);
		
		String[] allPks = BillQueryUtils.getAllPks(regapps);
		// 分页信息
		PageInfo pageInfo =helper.getPageInfo(allPks);
		String[] pks = helper.getCurrPagePKs(pageInfo,allPks);
		RegapplyVO[] resApply = BillQueryUtils.getBillVOArr(pks,regapps);
		// 根据数据创建GRID，并返回。
		Grid retGrid  = HRNccTemplateUtils.getGridWithArea(IHRYFTempletConst.TEMPLET_REGAPPLY, "list", resApply);
		retGrid.getModel().setPageinfo(pageInfo);
		retGrid.getModel().setAllpks(allPks);
		return retGrid;
	}

	// 拼装查询条件
	private PFQueryParams getQueryParams() {
		PFQueryParams queryParams = new PFQueryParams();
		queryParams.setApproveSite(IHRYFTempletConst.APPROVE_SITE_FALSE);
		queryParams.setBillState(HRConstEnum.ALL_INTEGER);
		return queryParams;
	}

	// 状态SQL条件拼接
	private String getBillStateSqlByStateArr(String[] billstateArr){
		return BillQueryUtils.getBillStateSqlByStateArr(billstateArr);
	}
	// 时间SQL条件拼接
	private String getBillTimeSqlByTime(String billTime,ParamUtils param){
		return BillQueryUtils.getBillTimeSqlByTime(billTime,param);
	}

	// 拼装试用类型条件
	private String getProbationTypeSqlByTime(String proType){
		StringBuilder proTypeSqlWhere = new StringBuilder();
		if (!"3".equals(proType)){
			proTypeSqlWhere.append(" and probation_type =" +proType);
			return proTypeSqlWhere.toString();
		}
		return "";
	}

	// 排序字段
	private String getOrderBySQL(){
		return " apply_date desc, bill_code ";
	}
	
}
