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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.custom.handover.*;
import com.kingdee.eas.hr.affair.ResignBizBillInfo;
import com.kingdee.shr.affair.web.handler.hrman.ResignBizBillHrManListHandler;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
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 离职单列表扩展
 * @Date 2025/6/5 18:45
 * @Created by Heyuan
 */
public class ResignBizBillHrManListHandlerEx extends ResignBizBillHrManListHandler {
    private static Logger logger = Logger.getLogger(ResignBizBillHrManListHandlerEx.class);

    /**
     * 反审批
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     */
    @Override
    public void untiCheckAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        //校验是否绑定工作交接单
        //如果绑定工作交接单,不允许反审批
        String billId = this.getBillId(request);
        if (StringUtils.isEmpty(billId)) {
            throw new ShrWebBizException("单据id为空!");
        }
        WorkHandoverEntryCollection workHandoverEntryCol = null;
        try {
            Context ctx = this.getContext();
            IWorkHandoverEntry iWorkHandoverEntry = WorkHandoverEntryFactory.getLocalInstance(ctx);
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            filterItems.add(new FilterItemInfo("resignBizBill.id", billId, CompareType.INCLUDE));
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("resignBizBill.number");
            sic.add("parent.number");
            EntityViewInfo viewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
            workHandoverEntryCol = iWorkHandoverEntry.getWorkHandoverEntryCollection(viewInfo);
        } catch (BOSException e) {
            logger.error(e.getMessage(), e);
            throw new ShrWebBizException(e);
        }
        StringBuilder errorMsg = new StringBuilder();
        for (int i = 0; i < workHandoverEntryCol.size(); i++) {
            WorkHandoverEntryInfo workHandoverEntryInfo = workHandoverEntryCol.get(i);
            //工作交接单编码
            String workHandoverNumber = workHandoverEntryInfo.getParent().getNumber();
            ResignBizBillInfo resignBizBill = workHandoverEntryInfo.getResignBizBill();
            //离职单编码
            String resignBizBillNumber = resignBizBill.getNumber();
            errorMsg.append("离职单[").append(resignBizBillNumber).append("]已绑定工作交接单[").append(workHandoverNumber).append("]，不允许反审批！");
            throw new ShrWebBizException(errorMsg.toString());
        }
        super.untiCheckAction(request, response, modelMap);
    }
}
