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

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.basedata.person.PersonInfo;
import com.kingdee.eas.custom.dormitorysystem.application.CheckoutApplicationEntryInfo;
import com.kingdee.eas.custom.dormitorysystem.application.CheckoutApplicationInfo;
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.emp.PersonPositionInfo;
import com.kingdee.eas.util.app.ContextUtil;
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.Date;

/**
 * @Description 入住申请单表单Handler(员工自助)
 * @Date 2025/4/24 13:48
 * @Created by Heyuan
 */
public class CheckoutApplicationSelfEditHandler extends CheckoutApplicationEditHandler {
    private static Logger logger = Logger.getLogger(CheckoutApplicationSelfEditHandler.class);


    /**
     * 创建对象后置方法
     *
     * @param request
     * @param response
     * @param coreBaseInfo
     * @throws SHRWebException
     */
    protected void afterCreateNewModel(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo coreBaseInfo
    ) throws SHRWebException {
        OccupantsCollection occupantsCollection = null;
        Context ctx = SHRContext.getInstance().getContext();
        PersonInfo person = ContextUtil.getCurrentUserInfo(ctx).getPerson();
        try {
            IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            Date date = new Date();
            //人员
            filterItems.add(new FilterItemInfo("person", person.getId()));
            //
            filterItems.add(new FilterItemInfo("CheckInDate", date, CompareType.LESS_EQUALS));
            filterItems.add(new FilterItemInfo("CheckOutDate", date, CompareType.GREATER));
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("personPosition.person.name");
            sic.add("id");
            sic.add("CheckInDate");
            sic.add("CheckOutDate");
            sic.add("personPosition.personDep.*");
            sic.add("personPosition.primaryPosition.*");
            sic.add("Dormitory.*");
            EntityViewInfo viewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
            occupantsCollection = iOccupants.getOccupantsCollection(viewInfo);
        } catch (Exception e) {
            logger.error(e);
            throw new ShrWebBizException(e);
        }
        if (occupantsCollection.size() > 0) {
            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、自觉遵守消防安全，节约用电用水。");
            //分录
            CheckoutApplicationEntryInfo entryInfo = new CheckoutApplicationEntryInfo();
            OccupantsInfo occupantsInfo = occupantsCollection.get(0);
            entryInfo.setOccupants(occupantsInfo);
            entryInfo.setPerson(person);
            PersonPositionInfo personPosition = occupantsInfo.getPersonPosition();
            entryInfo.setAdminOrg(personPosition.getPersonDep());
            entryInfo.setPosition(personPosition.getPrimaryPosition());
            //原退宿日期
            entryInfo.setOldCheckoutDate(occupantsInfo.getCheckOutDate());
            entryInfo.setCurrentRoom(occupantsInfo.getDormitory());
            entryInfo.setCheckInDate(occupantsInfo.getCheckInDate());
            checkoutApplicationInfo.getEntrys().add(entryInfo);
        } else {
            throw new ShrWebBizException("未找到当前最新的入住信息!");
        }
    }
}
