package com.kingdee.eas.custom.hireapproval.app;

import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.base.codingrule.ICodingRuleManager;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.custom.hireapproval.bizEnum.ExecuteStateEnum;
import com.kingdee.eas.utils.StringExUtils;
import com.kingdee.util.DateTimeUtils;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;

import com.kingdee.bos.*;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.common.EASBizException;

import java.lang.String;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.kingdee.eas.custom.hireapproval.OAHireSalaryDataInfo;
import com.kingdee.eas.custom.hireapproval.OAHireSalaryDataCollection;

public class OAHireSalaryDataControllerBean extends AbstractOAHireSalaryDataControllerBean {
    private static Logger logger = Logger.getLogger(OAHireSalaryDataControllerBean.class);

    /**
     * @param ctx
     * @param idNumber 身份证号码
     * @param passport 其他证件号码
     * @return
     * @throws BOSException
     */
    @Override
    protected OAHireSalaryDataInfo _getOAHireSalaryDataByIdNumber(
            Context ctx,
            String idNumber,
            String passport
    ) throws BOSException {
        if (!StringExUtils.isAnyNotEmpty(idNumber, passport)) {
            throw new BOSException("身份证号码和其他证件号不能全为空!");
        }
        try {
            StringBuilder sql = new StringBuilder("select *,entrys.* where ");
            Set<String> set = new HashSet<String>();
            if (!StringUtils.isEmpty(idNumber)) {
                set.add(" idNumber ='" + idNumber + "'");
            }
            if (!StringUtils.isEmpty(passport)) {
                set.add(" passport ='" + passport + "'");
            }
            sql.append(String.join(" or ", set));
            logger.error("_getOAHireSalaryDataByIdNumber sql = " + sql);
            return getOAHireSalaryDataInfo(ctx, sql.toString());
        } catch (EASBizException e) {
            e.printStackTrace();
            throw new BOSException("没有匹配到身份证号码[" + idNumber + "],其他证件号[" + passport + "]的OA录用定薪数据表");
        }
    }

    /**
     * 通过证件号码获取单据信息 或创建
     *
     * @param ctx
     * @param idNumber 身份证号码
     * @param passport 其他证件号码
     * @return
     * @throws BOSException
     */
    @Override
    protected OAHireSalaryDataInfo _getOrCreateOAHireSalaryDataByIdNumber(
            Context ctx,
            String idNumber,
            String passport
    ) throws BOSException {
        if (!StringExUtils.isAnyNotEmpty(idNumber, passport)) {
            throw new BOSException("身份证号码和其他证件号不能全为空!");
        }
        StringBuilder sql = new StringBuilder("select *,entrys.* where ");
        Set<String> set = new HashSet<String>();
        if (!StringUtils.isEmpty(idNumber)) {
            set.add(" idNumber ='" + idNumber + "'");
        }
        if (!StringUtils.isEmpty(passport)) {
            set.add(" passport ='" + passport + "'");
        }
        sql.append(String.join(" or ", set));
        logger.error("_getOrCreateOAHireSalaryDataByIdNumber sql = " + sql);
        OAHireSalaryDataCollection oaHireSalaryDataCollection = getOAHireSalaryDataCollection(ctx, sql.toString());
        OAHireSalaryDataInfo info = null;
        if (oaHireSalaryDataCollection.isEmpty()) {
            logger.error("没有匹配到身份证号码[" + idNumber + "],其他证件号[" + passport + "]的OA录用定薪数据表");
            info = new OAHireSalaryDataInfo();
            HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
            hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
            info.setHrOrgUnit(hrOrgUnitInfo);
            info.setExecuteStatus(ExecuteStateEnum.UNEXECUTE);
        } else {
            info = oaHireSalaryDataCollection.get(0);
        }
        return info;
    }

    /**
     * 是否可以执行生成入职定薪
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     */
    @Override
    protected boolean _isCanExecute(Context ctx, String billId) throws BOSException {
        super._isCanExecute(ctx, billId);
        if (StringUtils.isEmpty(billId)) {
            throw new BOSException("单据id不能为空!");
        }
        try {
            OAHireSalaryDataInfo info = getOAHireSalaryDataInfo(ctx, new ObjectUuidPK(billId));
            ExecuteStateEnum executeStatus = info.getExecuteStatus();
            if (ExecuteStateEnum.SUCCESS.equals(executeStatus) || ExecuteStateEnum.EXECUTEING.equals(executeStatus)) {
                //执行成功和执行中不能执行
                return false;
            }
            return true;
        } catch (EASBizException e) {
            throw new BOSException(e);
        }
    }

    /**
     * 设置执行中
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     */
    @Override
    protected void _setExecuting(Context ctx, String billId) throws BOSException {
        if (StringUtils.isEmpty(billId)) {
            throw new BOSException("单据id不能为空!");
        }
        super._setExecuting(ctx, billId);
        try {
            OAHireSalaryDataInfo info = getOAHireSalaryDataInfo(ctx, new ObjectUuidPK(billId));
            ExecuteStateEnum executeStatus = info.getExecuteStatus();
            if (ExecuteStateEnum.SUCCESS.equals(executeStatus)) {
                throw new BOSException("执行状态已经为成功，不能重复执行");
            }
            if (ExecuteStateEnum.EXECUTEING.equals(executeStatus)) {
                //执行中忽略
                return;
            }
            info.setExecuteStatus(ExecuteStateEnum.EXECUTEING);
            info.setExecuteResult(null);
            save(ctx, info);
        } catch (EASBizException e) {
            throw new BOSException(e);
        }
    }

    @Override
    protected IObjectPK _addnew(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        //设置编码
        this.setNumberFromCodingRule(ctx, (OAHireSalaryDataInfo) model);
        //设置业务时间
        ((OAHireSalaryDataInfo) model).setBizDate(DateTimeUtils.truncateDate(new Date()));
        return super._addnew(ctx, model);
    }


    /**
     * 根据编码规则设置编码
     *
     * @param ctx
     * @param model
     * @throws BOSException
     * @throws EASBizException
     */
    protected void setNumberFromCodingRule(Context ctx, OAHireSalaryDataInfo model) throws BOSException, EASBizException {
        String orgID = null;
        if (model.getHrOrgUnit() != null) {
            orgID = model.getHrOrgUnit().getId().toString();
        }
        String number = null;
        ICodingRuleManager iCodingRuleManager = CodingRuleManagerFactory.getLocalInstance(ctx);
        boolean codeRuleisExist = orgID != null && iCodingRuleManager.isExist(model, orgID);
        boolean isSetNumber = true;
        if (codeRuleisExist && iCodingRuleManager.getRuleStatus(model, orgID).isModifiable() && !StringUtils.isEmpty(model.getNumber())) {
            isSetNumber = false;
        }
        if (codeRuleisExist && isSetNumber) {
            int numberCount = 0;

            for (number = iCodingRuleManager.getNumber(model, orgID); super._exists(ctx, "where number = '" + number + "'") && numberCount < 5000; ++numberCount) {
                number = iCodingRuleManager.getNumber(model, orgID);
            }
        }
        if (number != null) {
            model.setNumber(number);
        }
    }
}