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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.custom.dormitorysystem.application.CheckInApplicationEntryInfo;
import com.kingdee.eas.custom.dormitorysystem.application.CheckInApplicationInfo;
import com.kingdee.eas.custom.dormitorysystem.application.utils.CheckDuplicateCheckInByBillIdUtil;
import com.kingdee.eas.custom.dormitorysystem.application.utils.ValidateRoomAvailableByBillIdsUtil;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
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.util.DateTimeUtils;
import org.apache.log4j.Logger;

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

/**
 * @Description 入住申请单表单Handler
 * @Date 2025/4/24 13:48
 * @Created by Heyuan
 */
public class CheckInApplicationEditHandler extends DormitoryApplicationEditHandler {
    private static Logger logger = Logger.getLogger(CheckInApplicationSelfEditHandler.class);
    private Context ctx = SHRContext.getInstance().getContext();


    @Override
    protected void beforeSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo model
    ) throws SHRWebException {
        try {
            HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
            String billIds = hrBillBaseInfo.getId().toString();
            //根据单据id校验房间是否入住(批量)
            ValidateRoomAvailableByBillIdsUtil.startValidate(ctx, billIds);
            //检查入住申请单是否重复、入住信息是否重复,时间是否交叉
            CheckDuplicateCheckInByBillIdUtil.startValidate(ctx, billIds);
            //设置单据状态为未审批
            model.setInt("billState", 1);
            super.beforeSubmit(request, response, model);
        } catch (BOSException e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        }
    }


    /**
     * 创建对象后置方法
     *
     * @param request
     * @param response
     * @param coreBaseInfo
     * @throws SHRWebException
     */
    protected void afterCreateNewModel(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo coreBaseInfo
    ) throws SHRWebException {
        try {
            Context ctx = SHRContext.getInstance().getContext();
            CheckInApplicationInfo checkInApplicationInfo = (CheckInApplicationInfo) coreBaseInfo;
            HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
            hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
            checkInApplicationInfo.setHrOrgUnit(hrOrgUnitInfo);
            checkInApplicationInfo.setManagementNotes("本人愿意遵守以下条款：" +
                    "\n1、自觉遵守公司《宿舍管理制度》和员工宿舍区有关管理办法，不私自调换宿舍和床位。" +
                    "\n2、自觉维护宿舍公共区域的卫生，爱护公物，损坏公物自觉赔偿。 3、自觉遵守消防安全，节约用电用水。" +
                    "\n3、自觉遵守消防安全，节约用电用水。");
            //分录
            CheckInApplicationEntryInfo entryInfo = new CheckInApplicationEntryInfo();
            entryInfo.setCheckoutDate(DateTimeUtils.parseDate("2199-12-31", "yyyy-MM-dd"));
            checkInApplicationInfo.getEntrys().add(entryInfo);
        } catch (Exception e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        }
    }
}
