package com.kingdee.eas.custom.dormitorysystem.application.handler;

import com.kingdee.bos.Context;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.shr.base.syssetting.IMSFServiceFacade;
import com.kingdee.shr.base.syssetting.MSFServiceFacadeFactory;
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 org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 退宿申请单列表Handler
 * @Date 2025/4/24 13:47
 * @Created by Heyuan
 */
public class CheckoutApplicationListHandler extends DormitoryApplicationListHandler {
    private static Logger logger = Logger.getLogger(CheckoutApplicationListHandler.class);
    private Context ctx = SHRContext.getInstance().getContext();

    @Override
    protected void beforeSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo model
    ) throws SHRWebException {
        super.beforeSubmit(request, response, model);
        HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
        String billIds = hrBillBaseInfo.getId().toString();
        Map result = null;
        Map<String, String> params = new HashMap<String, String>();
        params.put("billIds", billIds);
        //校验入住申请单是否重复提交、校验入住信息时间交叉
        try {
            IMSFServiceFacade imsfServiceFacade = MSFServiceFacadeFactory.getLocalInstance(ctx);
            result = (Map) imsfServiceFacade.processService("checkDuplicateCheckInByBillIdService", params);
        } catch (Exception e) {
            logger.error(e);
            throw new ShrWebBizException("调用接口checkDuplicateCheckInByBillIdService报错: " + e.getMessage());
        }
        Integer code = (Integer) result.get("code");
        if (500 == code) {
            List<String> errorMsgList = (List<String>) result.get("errorMsg");
            StringBuilder errorMsgBuilder = new StringBuilder();
            for (String errorMsg : errorMsgList) {
                errorMsgBuilder.append(errorMsg).append("\n");
            }
            throw new ShrWebBizException(errorMsgBuilder.toString());
        }
    }
}
