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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.custom.compensation.ISubmitShemeTemplate;
import com.kingdee.eas.custom.compensation.SubmitShemeTemplateCollection;
import com.kingdee.eas.custom.compensation.SubmitShemeTemplateFactory;
import com.kingdee.eas.custom.compensation.SubmitShemeTemplateInfo;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.shr.base.syssetting.BaseItemStateEnum;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

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

/**
 * @Description 提报套打模板ListHandler
 * @Date 2025/9/15 17:24
 * @Created by heyuan
 */
public class SubmitShemeTemplateListHandler extends ListHandler {
    /**
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     * @Description 删除
     */
    @Override
    public String deleteAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        // 获取删除的ID
        Context ctx = getContext();
        String billId = this.getBillId(request);
        if (StringUtils.isEmpty(billId)) {
            //throw new ShrWebBizException(SHRWebResource.getString("EAS_BIZ_NOT_EXIST"));
            throw new ShrWebBizException("单据id为空!  </br> The document ID is empty!");
        }
        SubmitShemeTemplateCollection submitShemeTemplateCol = null;
        try {
            ISubmitShemeTemplate iSubmitShemeTemplate = SubmitShemeTemplateFactory.getLocalInstance(ctx);
            submitShemeTemplateCol = iSubmitShemeTemplate
                    .getSubmitShemeTemplateCollection("where id in (" + AtsUtil.getStrFromString(billId) + ")");
        } catch (BOSException e) {
            throw new ShrWebBizException(e);
        }
        for (int i = 0; i < submitShemeTemplateCol.size(); i++) {
            SubmitShemeTemplateInfo info = submitShemeTemplateCol.get(i);
            BaseItemStateEnum state = info.getState();
            if (BaseItemStateEnum.ENABLE.equals(state)) {
                // 状态为启用的，不能删除
                throw new ShrWebBizException("状态为启用的，不能删除 </br> Items in the enabled state cannot be deleted");
            }
        }
        return super.deleteAction(request, response, modelMap);
    }
}
