package com.kingdee.shr.ats.web.handler;


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

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.AtsLeaveBillCollection;
import com.kingdee.eas.hr.ats.AtsLeaveBillFactory;
import com.kingdee.eas.hr.ats.AtsLeaveBillInfo;
import com.kingdee.shr.ats.web.handler.util.AtsLeaveBillOperationUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;

public class AtsLeaveBillListHandlerEx extends AtsLeaveBillListHandler {

    private static Logger logger =
            Logger.getLogger("com.kingdee.shr.ats.web.handler.AtsLeaveBillListHandlerEx");

    /**
     * 提交工作流
     */
    @Override
    protected void doBeforeSubmitForWF(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) {
        // TODO 自动生成的方法存根
        logger.info("AtsLeaveBillListHandlerEx.....doBeforeSubmitForWF...start");
        super.doBeforeSubmitForWF(request, response, model);
        try {
            Context ctx = SHRContext.getInstance().getContext();
            AtsLeaveBillInfo info = (AtsLeaveBillInfo) model;
            AtsLeaveBillCollection atsCollection = AtsLeaveBillFactory.getLocalInstance(ctx).getAtsLeaveBillCollection(" where id='" + info.getId().toString() + "'");
            if (atsCollection != null && atsCollection.size() > 0) {
                for (int i = 0; i < atsCollection.size(); ++i) {
                    AtsLeaveBillInfo atsLeaveBillInfo = atsCollection.get(i);
                    logger.info("AtsLeaveBillListHandlerEx.....number..." + atsLeaveBillInfo.getNumber());

                    logger.info("AtsLeaveBillListHandlerEx.....BillID..." + atsLeaveBillInfo.getId().toString());
                    //先删除拆分数据
                    String billFormatter = "('" + atsLeaveBillInfo.getId().toString() + "')";
                    AtsLeaveBillOperationUtil.deleteInfo(ctx, billFormatter);
                    //审批中的数据存入拆分表
                    AtsLeaveBillOperationUtil.saveInfo(ctx, atsLeaveBillInfo);
                }
            }

            logger.info("AtsLeaveBillListHandlerEx.....doBeforeSubmitForWF...end");
        } catch (Exception e) {

        }
    }


    /**
     * 撤销方法
     */
    @Override
    public void abortBillAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
            throws SHRWebException, EASBizException, BOSException {
        // TODO 自动生成的方法存根
        logger.info("AtsLeaveBillListHandlerEx.....abortBillAction...start");
        super.abortBillAction(request, response, modelMap);
        Context ctx = SHRContext.getInstance().getContext();
        String billId = request.getParameter("billId");
        if (StringUtils.isNotBlank(billId)) {
            String billFormatter = "('" + billId.replaceAll(",", "','") + "')";
            AtsLeaveBillOperationUtil.deleteInfo(ctx, billFormatter);
        }
        logger.info("AtsLeaveBillListHandlerEx.....abortBillAction...start");

    }

    /**
     * 反审核
     */
    @Override
    public void againstApproveAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
            throws SHRWebException {
        logger.info("AtsLeaveBillListHandlerEx.....againstApproveAction...start");
        // TODO 自动生成的方法存根
        super.againstApproveAction(request, response, modelMap);

        String billId = request.getParameter("billId");
        Context ctx = SHRContext.getInstance().getContext();
        if (StringUtils.isNotBlank(billId)) {
            String billFormatter = "('" + billId.replaceAll(",", "','") + "')";
            AtsLeaveBillOperationUtil.deleteInfo(ctx, billFormatter);
        }
    }


}
