package com.kingdee.shr.recuritment.app;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.attachment.AttachmentInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.recuritment.task.BeisenTransferPhaseFacadeFactory;
import com.kingdee.eas.custom.recuritment.task.IBeisenTransferPhaseFacade;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.shr.attachment.ISHRAttachmentExt;
import com.kingdee.shr.attachment.SHRAttachmentExtCollection;
import com.kingdee.shr.attachment.SHRAttachmentExtFactory;
import com.kingdee.shr.attachment.SHRAttachmentExtInfo;
import com.kingdee.eas.custom.beisen.utils.HRAttachmentUploadUtil;

/**
 * @Description 录用报批扩展
 * @Date 2025/7/8 19:17
 * @Created by Heyuan
 */
public class RecApprovalControllerBeanEx extends RecApprovalControllerBean {

    @Override
    protected IObjectPK _submitEffect(Context ctx, CoreBaseInfo model) throws BOSException, EASBizException {
        IObjectPK iObjectPK = super._submitEffect(ctx, model);
        try {
            String billId = model.getId().toString();
            //同步录用报批状态至北森
            IBeisenTransferPhaseFacade iBeisenTransferPhaseFacade = BeisenTransferPhaseFacadeFactory.getLocalInstance(ctx);
            iBeisenTransferPhaseFacade.syncRecApprovalToBeisen(billId, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iObjectPK;
    }

    /**
     * 设置审批通过
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _setPassState(Context ctx, BOSUuid billId) throws BOSException, EASBizException {
        super._setPassState(ctx, billId);
        try {
            //同步录用报批状态至北森
            IBeisenTransferPhaseFacade iBeisenTransferPhaseFacade = BeisenTransferPhaseFacadeFactory.getLocalInstance(ctx);
            iBeisenTransferPhaseFacade.syncRecApprovalToBeisen(billId.toString(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反审核
     *
     * @param ctx
     * @param billIds
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _againstApprove(Context ctx, String[] billIds) throws BOSException {
        super._againstApprove(ctx, billIds);
        try {
            //同步录用报批状态至北森
            IBeisenTransferPhaseFacade iBeisenTransferPhaseFacade = BeisenTransferPhaseFacadeFactory.getLocalInstance(ctx);
            iBeisenTransferPhaseFacade.syncRecApprovalToBeisen(AtsUtil.convertArrayToString(billIds), 0);
        } catch (EASBizException e) {
            e.printStackTrace();
        }
    }

    /**
     * 重新报批
     *
     * @param ctx
     * @param billIds
     * @throws BOSException
     */
    @Override
    protected void _reApprove(Context ctx, String[] billIds) throws BOSException {
        super._reApprove(ctx, billIds);
        try {
            //同步录用报批状态至北森
            IBeisenTransferPhaseFacade iBeisenTransferPhaseFacade = BeisenTransferPhaseFacadeFactory.getLocalInstance(ctx);
            iBeisenTransferPhaseFacade.syncRecApprovalToBeisen(AtsUtil.convertArrayToString(billIds), 0);
        } catch (EASBizException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据简历id创建录用报批
     *
     * @param ctx
     * @param resumeId
     * @return
     * @throws BOSException
     */
    @Override
    protected IObjectPK _addnew(Context ctx, String resumeId) throws BOSException {
        IObjectPK iObjectPK = super._addnew(ctx, resumeId);
        //同步简历附件到录用报批
        ISHRAttachmentExt ishrAttachmentExt = SHRAttachmentExtFactory.getLocalInstance(ctx);
        SHRAttachmentExtCollection shrAttachmentExtCollection = ishrAttachmentExt.getSHRAttachmentExtCollection("select attachment.* where boID = '" + resumeId + "'");
        String uipk = "com.kingdee.shr.recuritment.app.RecApprovalSalary.form";//录用报批定薪
        String id = iObjectPK.toString();
        for (int i = 0; i < shrAttachmentExtCollection.size(); i++) {
            SHRAttachmentExtInfo shrAttachmentExtInfo = shrAttachmentExtCollection.get(i);
            AttachmentInfo attachmentInfo = shrAttachmentExtInfo.getAttachment();
            String type = attachmentInfo.getSimpleName();
            String fileName = attachmentInfo.getName();
            byte[] file = attachmentInfo.getFile();
            HRAttachmentUploadUtil.uploadAttachment(ctx, file, type, fileName, id, uipk, "interview");
        }
        return iObjectPK;
    }
}
