package nccloud.web.hryf.regApply.action;

import java.util.ArrayList;
import java.util.Arrays;

import nccloud.commons.lang.ArrayUtils;

import nc.bs.logging.Logger;
import nc.hr.utils.ResHelper;
import nc.itf.hr.frame.IHrBillCode;
import nc.itf.trn.regmng.IRegmngManageService;
import nc.pub.billcode.itf.IBillcodeManage;
import nc.vo.hi.pub.BillCodeRepeatBusinessException;
import nc.vo.pub.BusinessException;
import nc.vo.trn.regmng.AggRegapplyVO;
import nc.vo.trn.regmng.RegapplyVO;
import nc.vo.uif2.LoginContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.itf.hryf.pub.IHRYFTempletConst;
import nccloud.web.hr.pub.HRNccPubEnv;
import nccloud.web.hr.pub.HRNccQueryHelper;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrhi.pub.PsndocTempUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 转正申请-批量保存操作
 * @author lbt 20190323
 *
 */
public class RegApplyBatchSaveAction extends RegBaseAction {



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

		//1：解析请求中得参数，放入map中，并根据参数获取用户的登录上下文信息。
		HRNccQueryHelper helper = new HRNccQueryHelper(request);
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		//2：接收转正人员并校验
		String[] listpks = param.getStrings("listpks");
		if (listpks==null || listpks.length == 0) {
			throw new BusinessException(ResHelper.getString("6009item","06009item0002")/*@res "请选择要转正的人员!"*/);
		}
		//3：接收卡片数据并校验
		AggRegapplyVO aggvo = new AggRegapplyVO();

		RegapplyVO regapplyVO = HRNccTemplateUtils.getVOFromForm(RegapplyVO.class, PsndocTempUtils.getModelStr(param, "formData"));
		if (regapplyVO == null) {
			throw new BusinessException(ResHelper.getString("6008hrgx","06008hrgx0021")
/*@res "form 数据没传！"*/);
		}

		//5：设置默认值信息
		regapplyVO.setAttributeValue(RegapplyVO.PK_BILLTYPE,IHRYFTempletConst.REGMNG_APPLY_BILLTYPE );
		regapplyVO.setAttributeValue(RegapplyVO.APPLY_DATE, HRNccPubEnv.getBizLiteralDate());
		regapplyVO.setAttributeValue(RegapplyVO.PK_GROUP, context.getPk_group());
		regapplyVO.setAttributeValue(RegapplyVO.PK_ORG, context.getPk_org());
		regapplyVO.setAttributeValue(RegapplyVO.BILL_CODE, "~");
		aggvo.setParentVO(regapplyVO);
		//6:前台自动生成编码
		String[] billCodes = null;
		boolean isAuto = isAutoGenerateBillCode(context);
		if (isAuto){
			billCodes =ServiceLocator.find(IHrBillCode.class).getBillCode(getBillType(), context.getPk_group(), context.getPk_org(), listpks.length);
		}
		try{
			ServiceLocator.find(IRegmngManageService.class).batchSaveBill(aggvo,new ArrayList<String>(Arrays.asList(listpks)), context, billCodes);
		}
		catch (Exception e){
			String[] codes = null;
			// 如果是重号异常 ,则将重复的号Abandon掉
			if (e instanceof BillCodeRepeatBusinessException){
				codes = ((BillCodeRepeatBusinessException) e).getRepeatCodes();
				if (isAuto && codes != null){
					for (int i = 0; i < codes.length; i++){
						try{
							ServiceLocator.find(IBillcodeManage.class).AbandonBillCode_RequiresNew(getBillType(), context.getPk_group(), context.getPk_org(), codes[i]);
						}
						catch (Exception e2){
							Logger.error(e2.getMessage(), e2);
						}
					}
				}
			}
			// 如果发生异常 则回滚单据号
			if (billCodes != null && billCodes.length > 0) {
				for (String billno : billCodes) {
					if (codes != null && ArrayUtils.contains(codes, billno)) {
						// 背放弃的号就不再回滚了
						continue;
					}
					ServiceLocator.find(IBillcodeManage.class).rollbackPreBillCode(getBillType(), context.getPk_group(), context.getPk_org(), billno);
				}
			}
			throw new BusinessException(e.getMessage(), e);
		}
		return "success";
	}
}