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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.custom.dormitorysystem.application.*;
import com.kingdee.eas.custom.dormitorysystem.occupants.IOccupants;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsCollection;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsFactory;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.shr.base.syssetting.BaseItemException;
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.shr.base.syssetting.web.json.JSONUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

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

    /**
     * 创建对象后置方法
     *
     * @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();
            CheckoutApplicationInfo checkoutApplicationInfo = (CheckoutApplicationInfo) coreBaseInfo;
            HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
            hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
            checkoutApplicationInfo.setHrOrgUnit(hrOrgUnitInfo);
//            checkoutApplicationInfo.setManagementNotes("本人愿意遵守以下条款：" +
//                    "\n1、自觉遵守公司《宿舍管理制度》和员工宿舍区有关管理办法，不私自调换宿舍和床位。" +
//                    "\n2、自觉维护宿舍公共区域的卫生，爱护公物，损坏公物自觉赔偿。 3、自觉遵守消防安全，节约用电用水。" +
//                    "\n3、自觉遵守消防安全，节约用电用水。");
        } catch (Exception e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        }
    }

    /**
     * 获取可选入住人员信息
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     */
    public void getOptionalOccupantsAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        try {
            Context ctx = SHRContext.getInstance().getContext();
            IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            Date date = new Date();
            filterItems.add(new FilterItemInfo("CheckInDate", date, CompareType.LESS_EQUALS));
            filterItems.add(new FilterItemInfo("CheckOutDate", date, CompareType.GREATER));
            EntityViewInfo viewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
            OccupantsCollection occupantsCollection = iOccupants.getOccupantsCollection(viewInfo);
            Set<String> occupantsIds = new HashSet<String>();
            for (int i = 0; i < occupantsCollection.size(); i++) {
                OccupantsInfo occupantsInfo = occupantsCollection.get(i);
                String occupantsId = occupantsInfo.getId().toString();
                occupantsIds.add(occupantsId);
            }
            JSONUtils.SUCCESS(AtsUtil.convertSetToString(occupantsIds));
        } catch (BOSException e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        }
    }

    @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;
            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);
        }
    }
}
