package com.kingdee.eas.custom.handover.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.*;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.handover.*;
import com.kingdee.eas.custom.handover.utils.WorkHandoverUtil;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.affair.ResignBizBillEntryInfo;
import com.kingdee.eas.hr.affair.ResignBizBillFactory;
import com.kingdee.eas.hr.affair.ResignBizBillInfo;
import com.kingdee.eas.hr.base.*;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 工作交接人表单EditHandler
 * @Date 2025/5/30 17:46
 * @Created by Heyuan
 */
public class WorkHandoverEditHandler extends EditHandler {
    private static Logger logger = Logger.getLogger(WorkHandoverEditHandler.class);

    @Override
    protected void afterCreateNewModel(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo coreBaseInfo
    ) throws SHRWebException {
        super.afterCreateNewModel(request, response, coreBaseInfo);
        //录用审批单据
        WorkHandoverInfo info = (WorkHandoverInfo) coreBaseInfo;
        HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
        hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
        info.setHrOrgUnit(hrOrgUnitInfo);
        info.setId(BOSUuid.create(info.getBOSType()));
    }

    /**
     * 通过单据id获取离职单数据
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public void getResignBizBillInfoByBillIdAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        String resignBizBillId = request.getParameter("resignBizBillId");
        if (StringUtils.isEmpty(resignBizBillId)) {
            throw new ShrWebBizException("离职单id不能为空!");
        }
        try {
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("entrys.*");
            sic.add("entrys.person.*");
            sic.add("entrys.adminOrg.*");
            sic.add("entrys.hrBizDefine.*");
            sic.add("entrys.position.*");
            sic.add("entrys.jobGrade.*");
            sic.add("entrys.oldEmpType.*");
            sic.add("entrys.empType.*");
            sic.add("entrys.variationReason.*");
            ResignBizBillInfo resignBizBillInfo =
                    ResignBizBillFactory.getRemoteInstance().getResignBizBillInfo(new ObjectUuidPK(resignBizBillId), sic);
            ResignBizBillEntryInfo resignBizBillEntryInfo = resignBizBillInfo.getEntrys().get(0);
            String personId = resignBizBillEntryInfo.getPerson().getId().toString();
            //是否为领导
            boolean isLeader = WorkHandoverUtil.isLeader(ctx, personId);
            resignBizBillEntryInfo.put("isLeader", isLeader);
            JSONUtils.SUCCESS(resignBizBillEntryInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ShrWebBizException(e);
        }
    }

    @Override
    protected void beforeSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo model
    ) throws SHRWebException {
        //设置单据状态为未审批
        model.setInt("billState", 1);
        super.beforeSubmit(request, response, model);
    }


    /**
     * 提交生效
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     */
    public void submitEffectAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        CoreBaseInfo model = (CoreBaseInfo) request.getAttribute("dynamic_model");
        this.beforeSave(request, response, model);
        HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
        hrBillBaseInfo.setApproveType(ApproveTypeEnum.DIRECT);
        try {
            IHRBillBase ihrBillBase = (IHRBillBase) this.getBizInterface();
            IObjectPK objectPK = ihrBillBase.submitEffect(model);
            model.setId(BOSUuid.read(objectPK.toString()));
        } catch (EASBizException var8) {
            logger.error(var8);
            throw new ShrWebBizException(var8.getMessage(), var8);
        } catch (BOSException var9) {
            logger.error(var9);
            throw new ShrWebBizException(var9.getMessage(), var9);
        } catch (Exception var10) {
            logger.error(var10);
            throw new ShrWebBizException(var10);
        }
        this.afterSave(request, response, model);
        this.saveAttachment(request, response, model);
        this.writeSuccessData(model.getId().toString());
    }
}
