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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.application.*;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsInfo;
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 org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
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);
        try {
            IRoomChangeApplicationEntry iRoomChangeApplicationEntry = RoomChangeApplicationEntryFactory.getLocalInstance(ctx);
            CheckoutApplicationInfo checkoutApplicationInfo = (CheckoutApplicationInfo) model;
            ICheckoutApplication iCheckoutApplication = CheckoutApplicationFactory.getLocalInstance(ctx);
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("entrys.person.name");
            sic.add("entrys.occupants.id");
            checkoutApplicationInfo = iCheckoutApplication.getCheckoutApplicationInfo(new ObjectUuidPK(checkoutApplicationInfo.getId()), sic);
            CheckoutApplicationEntryCollection entrys = checkoutApplicationInfo.getEntrys();
            for (int i = 0; i < entrys.size(); i++) {
                CheckoutApplicationEntryInfo checkoutApplicationEntryInfo = entrys.get(i);
                OccupantsInfo occupants = checkoutApplicationEntryInfo.getOccupants();
                String personName = checkoutApplicationEntryInfo.getPerson().getName();
                //校验入住信息是否被换宿
                RoomChangeApplicationEntryCollection roomChangeApplicationEntryCollection =
                        iRoomChangeApplicationEntry.getRoomChangeApplicationEntryCollection("select parent.number where oldOccupants = '" + occupants.getId() + "'");
                if (roomChangeApplicationEntryCollection.size() > 0) {
                    RoomChangeApplicationEntryInfo roomChangeApplicationEntryInfo = roomChangeApplicationEntryCollection.get(0);
                    String number = roomChangeApplicationEntryInfo.getParent().getNumber();
                    throw new ShrWebBizException("员工 " + personName + "已有换宿单[ " + number + " ]，不能提交生效！");
                }
            }
        } catch (BOSException e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        } catch (EASBizException e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        }
    }
}
