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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.BOSObjectFactory;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.AbstractObjectCollection;
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.Genders;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.application.*;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryFactory;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryInfo;
import com.kingdee.eas.custom.dormitorysystem.dormitory.IDormitory;
import com.kingdee.eas.custom.dormitorysystem.enumerationset.DormitoryClassification;
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.custom.dormitorysystem.otherbasicdata.DormitoryTypeInfo;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.PersonDormitoryTypeInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.CoreBillBaseCollection;
import com.kingdee.eas.framework.ICoreBillBase;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.eas.hr.base.HRBillBaseEntryInfo;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.shr.base.syssetting.BasicItemStateEnum;
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 com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @author qingwu
 * @date 2025/4/27
 * @apiNote ABK_换宿申请单
 */
public class RoomChangeApplicationEditHandler extends HRBillBaseEditHandler {
    private Logger logger = Logger.getLogger(RoomChangeApplicationEditHandler.class);
    private Context ctx = SHRContext.getInstance().getContext();

    @Override
    protected void beforeSubmit(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws SHRWebException {
        logger.error("beforeSubmit--");
        HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
        String billIds = hrBillBaseInfo.getId().toString();
        Map result = null;
        try {
            result = validateRoomAvailableByBillIds(billIds);
        } catch (Exception e) {
            logger.error(e);
            throw new ShrWebBizException("调用方法validateRoomAvailableByBillIds报错: " + 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());
        }
        // 设置单据状态为未审批
        // model.setInt("billState", 1);
        super.beforeSubmit(request, response, model);
    }


    @Override
    protected void afterCreateNewModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo coreBaseInfo) throws SHRWebException {
        super.afterInitModel(request, response, coreBaseInfo);
        RoomChangeApplicationInfo roomChangeApplicationInfo = (RoomChangeApplicationInfo) coreBaseInfo;
        try {
            RoomChangeApplicationEntryCollection entrys = roomChangeApplicationInfo.getEntrys();
            RoomChangeApplicationEntryInfo roomChangeApplicationEntryInfo = new RoomChangeApplicationEntryInfo();
            roomChangeApplicationEntryInfo.setCheckoutDate(DateTimeUtils.parseDate("2199-12-31", "yyyy-MM-dd"));
            // managementNotes  宿舍管理说明
            StringBuilder managementNotes = new StringBuilder();
            managementNotes.append("本人愿意遵守以下条款：").append("\n");
            managementNotes.append("1、自觉遵守公司《宿舍管理制度》和员工宿舍区有关管理办法，不私自调换宿舍和床位。").append("\n");
            managementNotes.append("2、自觉维护宿舍公共区域的卫生，爱护公物，损坏公物自觉赔偿。").append("\n");
            managementNotes.append("3、自觉遵守消防安全，节约用电用水。").append("\n");
            roomChangeApplicationInfo.setManagementNotes(managementNotes.toString());
            entrys.add(roomChangeApplicationEntryInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
    }


}
