package nccloud.web.hrzz.psninfo.action;

import nc.bs.logging.Logger;
import nc.hr.utils.HRFileUtils;
import nc.hr.utils.ResHelper;
import nc.itf.hi.IPsndocQryService;
import nc.md.model.IBean;
import nc.vo.hi.psndoc.PsndocAggVO;
import nc.vo.hi.psndoc.PsndocVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.uif2.LoginContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.itf.hryf.psninfoapprove.IPsninfoApproveService;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrhi.pub.MetaDataUtils;
import nccloud.web.hrhi.pub.PsndocTempUtils;
import nccloud.web.hrzz.psninfo.util.PsndocValidator;
import nccloud.web.hrzz.pub.util.HrzzPubUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;
import nccloud.commons.lang.StringUtils;

/**
 * 个人信息填写功能-【保存】操作
 *
 * @author tianxfc
 */
public class PsninfoSaveAction extends HRCommonAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		// 获取区域编码：区域编码和信息集编码、信息集对应的表名code一致
		String strTabCode = param.getString("areaCode", null);
		if (StringUtils.isEmpty(strTabCode)) {
			throw new BusinessRuntimeException(ResHelper.getString("6065hrzz",
					"06065hrzz0017")/* @res "区域编码不能为空！" */);
		}
		String needCheckStr = param.getString("needCheck", "Y");
		boolean needCheck = "Y".equals(needCheckStr);
		IBean bean = MetaDataUtils.getBeanByFullName("hrhi." + strTabCode);
		Class<?> clazzVO = null;
		try {
			clazzVO = Class.forName(bean.getFullClassName());
		} catch (ClassNotFoundException ex) {
			Logger.error(ex);
		}
		PsndocAggVO psndocAgg = HrzzPubUtils.queryPsndocByUserID(context.getPk_loginUser());
		if (psndocAgg == null) {
			throw new BusinessRuntimeException(ResHelper.getString("6065hrzz",
					"06065hrzz0018")/* @res "未找到用户对应的人员基本信息！" */);
		}

		PsndocVO oldPsndocVO = psndocAgg.getParentVO();
		String pk_org = oldPsndocVO.getPsnJobVO().getPk_hrorg();
		context.setPk_org(pk_org);
		String modelStr = PsndocTempUtils.getModelStr(param, strTabCode);
		if (StringUtils.isBlank(modelStr)) {
			throw new BusinessRuntimeException(ResHelper.getString("6065hrzz",
					"06065hrzz0019")/* @res "数据不能为空！" */);
		}
		// 修改人员基本信息
		String checkInfo = null;
		if (PsndocVO.getDefaultTableName().equals(strTabCode)) {
			PsndocVO psndocVO = HRNccTemplateUtils.getVOFromForm(PsndocVO.class, modelStr);
			resetPhoto(param, psndocVO, oldPsndocVO);
			checkInfo = ServiceLocator.find(IPsninfoApproveService.class).savePsndocVO(pk_org, psndocVO, needCheck);
		} else {
			SuperVO[] subVOs = (SuperVO[]) HRNccTemplateUtils.getVOFromGrid(clazzVO, modelStr);
			// 如果子集数据未做修改保存时直接返回，不做处理
			if (subVOs != null && subVOs.length > 0) {
				boolean changeflag = false;
				for (int i = 0; i < subVOs.length; i++) {
					if (VOStatus.UNCHANGED != subVOs[i].getStatus()) {
						changeflag = true;
					}
				}
				if (!changeflag) {
					return null;
				}
			}
			psndocAgg.setTableVO(strTabCode, subVOs);
			new PsndocValidator().validate(psndocAgg);
			checkInfo = ServiceLocator.find(IPsninfoApproveService.class).savePsnSubInfo(pk_org, oldPsndocVO, subVOs,
					needCheck);
		}
		return StringUtils.isNotBlank(checkInfo) ? ResHelper.getString("6065hrzz", "06065hrzz0020")
				/* @res "保存的信息需要审核，是否继续？" */ : null;
	}

	/**
	 * 重置人员头像
	 *
	 * @param param
	 * @param psndocVO
	 */
	private void resetPhoto(ParamUtils param, PsndocVO psndocVO, PsndocVO oldPsndocVO) {
		// 获取头像是否修改标识，Y：修改；N：未修改
		String isPhotoChange = param.getString("isphotochange", "N");
		if (!"Y".equals(isPhotoChange)) {
			PsndocAggVO psndocAggVO = null;
			try {
				psndocAggVO = ServiceLocator.find(IPsndocQryService.class).queryPsndocVOByPk(oldPsndocVO.getPk_psndoc(), true, true);
			} catch (BusinessException e) {
				e.printStackTrace();
			}
			if(psndocVO!=null&&psndocAggVO!=null&&psndocAggVO.getParentVO()!=null){
				psndocVO.setPhoto(psndocAggVO.getParentVO().getPhoto());
				psndocVO.setPreviewphoto(oldPsndocVO.getPreviewphoto());
			}
			return;
		}
		String image = param.getString("image", null);
		if (StringUtils.isBlank(image)) {
			// 删除头像,存储cleanphoto标识用
			String cleanphoto = "cleanphoto";
			psndocVO.setPhoto(cleanphoto.getBytes());
			psndocVO.setPreviewphoto(null);
			return;
		}
		// 去掉头像base64流的前缀image/png;base64,
		if (image.split(",") != null && image.split(",").length > 1) {
			image = image.split(",")[1];
			byte[] b = HRFileUtils.base64Decode(image);
			psndocVO.setPhoto(b);
		}
	}
}