package nccloud.web.hrhi.psndoc.action;

import nc.bs.logging.Logger;
import nc.hr.utils.ResHelper;
import nc.itf.bd.psn.psndoc.IPsndocQueryService;
import nc.itf.hi.IPsndocService;
import nc.itf.hr.frame.IPersistenceRetrieve;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.hi.psndoc.PsnCreateVO;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.sm.UserVO;
import nc.vo.uif2.LoginContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.itf.hrhi.pub.IHRHITempletConst;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;
import nccloud.commons.lang.StringUtils;

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

/**
 * 生成用户初始化对话框
 * 
 * @author sunpeng
 *
 */
public class PsndocCreateUserInitAction extends HRCommonAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {

		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);

		// hr组织id，
		if (StringUtils.isBlank(context.getPk_org())) {
			throw new BusinessRuntimeException("Parameter Error!");
		}

		// 只有维护节点有这个按钮
		String pageCode = IHRHITempletConst.TEMPLET_EMPLOYEE;

		String paramStr = param.getString("param_str", null);
		if (StringUtils.isBlank(paramStr)) {
			throw new BusinessException(ResHelper.getString("6007psn", "06007psn0103")/* @res "请选择要批改的人员!" */);
		}

		String[] psnjobPks = paramStr.split(",");
		Object[] objects = ServiceLocator.find(IMDPersistenceQueryService.class).queryBillOfVOByPKsWithOrder(PsnJobVO.class, psnjobPks, true);
		if (objects == null || objects.length == 0) {
			return null;
		}

		ArrayList<String> docPK = new ArrayList<String>();
		for (int i = 0; i < objects.length; i++) {
			if(objects[i]==null){
				continue;
			}
			if (!docPK.contains(((PsnJobVO) objects[i]).getPk_psndoc())) {
				docPK.add(((PsnJobVO) objects[i]).getPk_psndoc());
			}

		}
		PsndocVO[] vos = ServiceLocator.find(IPsndocQueryService.class).queryPsndocByPks(docPK.toArray(new String[0]));

		ArrayList<PsndocVO> al = new ArrayList<PsndocVO>();

		for (int i = 0; vos != null && i < vos.length; i++) {
			if (vos[i].getPsnjobs() == null || vos[i].getPsnjobs().length == 0) {
				continue;
			}
			al.add(vos[i]);
		}

		vos = al.toArray(new PsndocVO[0]);

		PsnCreateVO[] psnCreateVOs = psndocVO2PsnCreateVO(vos);

		return HRNccTemplateUtils.getGridWithArea(pageCode, "createuser", psnCreateVOs);
	}

	public PsnCreateVO[] psndocVO2PsnCreateVO(PsndocVO[] psndocvos) throws BusinessException {
		if (psndocvos == null || psndocvos.length == 0)
			return null;
		PsnCreateVO[] result = new PsnCreateVO[psndocvos.length];

		ArrayList<String> al = new ArrayList<String>();
		for (int i = 0; i < psndocvos.length; i++) {
			if (!al.contains(psndocvos[i].getPrimaryKey())) {
				al.add(psndocvos[i].getPrimaryKey());
			}
		}
		List<String> list = new ArrayList<String>();
		try {
			list = ServiceLocator.find(IPsndocService.class).queryUserDocPK(al.toArray(new String[0]));
		} catch (BusinessException e) {
			Logger.error(e.getMessage(), e);
		}

		for (int i = 0; i < psndocvos.length; i++) {
			if (list.contains(psndocvos[i].getPk_psndoc())) {
				UserVO[] userVO = getUserVO(psndocvos[i].getPk_psndoc());
				result[i] = new PsnCreateVO(psndocvos[i].getPk_psndoc(), userVO[0].getUser_code(), psndocvos[i].getName(), true);
			} else {
				//modify by@chenzf 修改用户编码为手机号 2021-1-5 11:39:12 begin
//				result[i] = new PsnCreateVO(psndocvos[i].getPk_psndoc(), psndocvos[i].getCode(), psndocvos[i].getName(), false);
				result[i] = new PsnCreateVO(psndocvos[i].getPk_psndoc(), psndocvos[i].getMobile(), psndocvos[i].getName(), false);
				//modify by@chenzf 修改用户编码为手机号 2021-1-5 11:39:12 end
			}
		}
		return result;
	}

	private UserVO[] getUserVO(String pk_psndoc) throws BusinessException {
		String cond = " pk_base_doc = '"+pk_psndoc+"'";
		return (UserVO[]) ServiceLocator.find(IPersistenceRetrieve.class).retrieveByClause(null, UserVO.class, cond);
	}

}
