package nccloud.web.hrwa.wagrade.action;

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

import nccloud.commons.collections.CollectionUtils;
import nccloud.commons.lang.ArrayUtils;

import nc.hr.utils.StringPiecer;
import nc.itf.hr.wa.IWaGradeQueryService;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.uif2.LoginContext;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.IWaGradeCommonDef;
import nc.vo.wa.grade.WaGradeVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrwa.pub.IWATempletConst;
import nccloud.web.hrwa.wagrade.action.util.WaGradeGrid;
import nccloud.web.hrwa.wagrade.action.util.WaGradeQueryHelper;
import nccloud.web.uapbd.commons.web.ParamUtils;
/**
 * 
 * 2019年7月13日下午3:19:55
 * msw：
 * 刷新列表界面
 */
public class WaGradeRefreshAction extends HRCommonAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		String areaCode = param.getString("area_code", null);
		String appCode = NODE_TYPE.ORG_NODE.equals(context.getNodeType()) ? IWATempletConst.APPCODE_WAGRADE_ORG
				: IWATempletConst.APPCODE_WAGRADE_GRP;
		//modify by@陈志锋 没有分配下来的不允许查询到集团级的数据 2021-3-10 17:09:35 begin
		AggWaGradeVO[] aggVOs = (AggWaGradeVO[]) ServiceLocator.find(IWaGradeQueryService.class).queryWaGradeVO(context,"pk_org='"+context.getPk_org()+"'");
//		AggWaGradeVO[] aggVOs = (AggWaGradeVO[]) ServiceLocator.find(IWaGradeQueryService.class).queryWaGradeVO(context,null);
		//modify by@陈志锋 没有分配下来的不允许查询到集团级的数据 2021-3-10 17:09:35 end
		WaGradeVO[] vos = formatToVOs(aggVOs);
		if (ArrayUtils.isEmpty(vos))
			return null;
		WaGradeQueryHelper queryHelper = new WaGradeQueryHelper(request,vos.length);
		WaGradeVO[] currVOs = queryHelper.getCurrPageVOs(vos);
		Grid grid = WaGradeGrid.getGridWithArea(appCode, areaCode, currVOs);
		grid.getModel().setPageinfo(queryHelper.getPageInfo());
		grid.getModel().setAllpks(StringPiecer.getStrArray(currVOs, WaGradeVO.PK_WA_GRD));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Parent", grid);
		return map;
	}
	/**
	 * 转换为VO数组 并计算子集数
	 * @param aggVOs
	 * @return
	 */
	private WaGradeVO[] formatToVOs(AggWaGradeVO[] aggVOs){
		if(ArrayUtils.isEmpty(aggVOs))
			return null;
		List<WaGradeVO> voList = new ArrayList<WaGradeVO>();
		for(AggWaGradeVO aggVO:aggVOs) {
			WaGradeVO vo = aggVO.getParentVO();
			if(vo == null)
				continue;
			vo.setSeclvcnt(String.valueOf(ArrayUtils.getLength(aggVO.getTableVO(IWaGradeCommonDef.WA_SECLV))));
			vo.setPrmlvcnt(String.valueOf(ArrayUtils.getLength(aggVO.getTableVO(IWaGradeCommonDef.WA_PRMLV))));
			voList.add(vo);
		}
		return CollectionUtils.isEmpty(voList) ? null : voList.toArray(new WaGradeVO[0]);
	}
}
