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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.ctrl.reportone.data.framework.bos.BosUtil;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.base.codingrule.ICodingRuleManager;
import com.kingdee.eas.basedata.org.CtrlUnitFactory;
import com.kingdee.eas.basedata.org.ICtrlUnit;
import com.kingdee.eas.basedata.org.OrgUnitInfo;
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.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.framework.Result;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.util.DateTimeUtils;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class RoomChangeApplicationControllerBean extends AbstractRoomChangeApplicationControllerBean {
    private static Logger logger = Logger.getLogger(RoomChangeApplicationControllerBean.class);

    @Override
    protected void _submitEffect(Context ctx, String billId) throws BOSException, EASBizException {
        super._submitEffect(ctx, billId);
        RoomChangeApplicationInfo roomChangeApplicationInfo = this.getRoomChangeApplicationInfo(ctx, new ObjectUuidPK(billId));
        this._submitEffect(ctx, roomChangeApplicationInfo);
    }

    @Override
    protected void _setAudited(Context ctx, BOSUuid billID) throws BOSException, EASBizException {
        logger.error("_setAudited-----");
        //RoomChangeApplicationInfo info = (RoomChangeApplicationInfo) model;
        RoomChangeApplicationInfo info = getRoomChangeApplicationInfo(ctx, new ObjectUuidPK(billID));
        //人员入住信息
        IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
        //人员入住信息修改字段
        SelectorItemCollection sicOccupants = new SelectorItemCollection();
        sicOccupants.add("CheckOutDate");
        //换宿单分录
        SelectorItemCollection sicEntry = new SelectorItemCollection();
        sicEntry.add("occupants");
        //生成入住人员信息
        for (int i = 0; i < info.getEntrys().size(); i++) {
            RoomChangeApplicationEntryInfo entryInfo = info.getEntrys().get(i);
            //原入住人员信息
            OccupantsInfo oldOccupants = entryInfo.getOldOccupants();
            //原退宿日期
            entryInfo.setOldCheckoutDate(oldOccupants.getCheckOutDate());
            //新增新的入住人员信息
            oldOccupants = iOccupants.getOccupantsInfo(new ObjectUuidPK(oldOccupants.getId()));
            //修改原入住人员信息的宿时间等于换宿单入住时间的前一天。
            Date checkInDate = entryInfo.getCheckInDate();
            oldOccupants.setCheckOutDate(DateTimeUtils.addDay(checkInDate, -1));
            iOccupants.updatePartial(oldOccupants, sicOccupants);
            //创建一张新的人员入住信息入住时间等于换宿单的入住时间
            //生成入住人员信息
            OccupantsInfo occupantsInfo = new OccupantsInfo();
            //入住人员
            occupantsInfo.setPerson(entryInfo.getPerson());
            //入住日期
            occupantsInfo.setCheckInDate(entryInfo.getCheckInDate());
            //退宿日期
            occupantsInfo.setCheckOutDate(entryInfo.getCheckoutDate());
            //入住房间
            occupantsInfo.setDormitory(entryInfo.getAdjustRoom());
            //人员宿舍分类
            occupantsInfo.setPersonDormitoryType(oldOccupants.getPersonDormitoryType());
            //宿舍
            //occupantsInfo.setDormitory(oldOccupants.getDormitory());
            //职位
            occupantsInfo.setPersonPosition(oldOccupants.getPersonPosition());
            //occupantsInfo.setPersonD
            //状态:启用
            //occupantsInfo.setState(BasicItemStateEnum.ENABLE);
            iOccupants.addnew(occupantsInfo);
            // 保存新的入住人员信息到
            entryInfo.setOccupants(occupantsInfo);

        }
        update(ctx, new ObjectUuidPK(info.getId()), info);
        //校验时间是否存在交叉
        super._setAudited(ctx, billID);
    }

    /**
     * 提交生效
     *
     * @param ctx
     * @param model
     * @return
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected IObjectPK _submitEffect(
            Context ctx,
            CoreBaseInfo model
    ) throws BOSException, EASBizException {
        logger.error("_submitEffect-----");
        //RoomChangeApplicationInfo info = (RoomChangeApplicationInfo) model;
        //info = getRoomChangeApplicationInfo(ctx, new ObjectUuidPK(info.getId()));
        ////人员入住信息
        //IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
        ////人员入住信息修改字段
        //SelectorItemCollection sicOccupants = new SelectorItemCollection();
        //sicOccupants.add("CheckOutDate");
        ////换宿单分录
        //SelectorItemCollection sicEntry = new SelectorItemCollection();
        //sicEntry.add("occupants");
        ////生成入住人员信息
        //for (int i = 0; i < info.getEntrys().size(); i++) {
        //    RoomChangeApplicationEntryInfo entryInfo = info.getEntrys().get(i);
        //    //原入住人员信息
        //    OccupantsInfo oldOccupants = entryInfo.getOldOccupants();
        //    //原退宿日期
        //    entryInfo.setOldCheckoutDate(oldOccupants.getCheckOutDate());
        //    //新增新的入住人员信息
        //    oldOccupants = iOccupants.getOccupantsInfo(new ObjectUuidPK(oldOccupants.getId()));
        //    //修改原入住人员信息的宿时间等于换宿单入住时间的前一天。
        //    Date checkInDate = entryInfo.getCheckInDate();
        //    oldOccupants.setCheckOutDate(DateTimeUtils.addDay(checkInDate, -1));
        //    iOccupants.updatePartial(oldOccupants, sicOccupants);
        //    //创建一张新的人员入住信息入住时间等于换宿单的入住时间
        //    //生成入住人员信息
        //    OccupantsInfo occupantsInfo = new OccupantsInfo();
        //    //入住人员
        //    occupantsInfo.setPerson(entryInfo.getPerson());
        //    //入住日期
        //    occupantsInfo.setCheckInDate(entryInfo.getCheckInDate());
        //    //退宿日期
        //    occupantsInfo.setCheckOutDate(entryInfo.getCheckoutDate());
        //    //入住房间
        //    occupantsInfo.setDormitory(entryInfo.getAdjustRoom());
        //    //人员宿舍分类
        //    occupantsInfo.setPersonDormitoryType(oldOccupants.getPersonDormitoryType());
        //    //宿舍
        //    //occupantsInfo.setDormitory(oldOccupants.getDormitory());
        //    //职位
        //    occupantsInfo.setPersonPosition(oldOccupants.getPersonPosition());
        //    //occupantsInfo.setPersonD
        //    //状态:启用
        //    //occupantsInfo.setState(BasicItemStateEnum.ENABLE);
        //    iOccupants.addnew(occupantsInfo);
        //    // 保存新的入住人员信息到
        //    entryInfo.setOccupants(occupantsInfo);
        //
        //}
        //update(ctx, new ObjectUuidPK(info.getId()), info);
        //IObjectPK iObjectPK = super._submitEffect(ctx, info);
        return super._submitEffect(ctx, model);
    }


    /**
     * 反审批单据
     *
     * @param ctx
     * @param billId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _untiCheckBizBill(
            Context ctx,
            String billId
    ) throws BOSException, EASBizException {
        super._untiCheckBizBill(ctx, billId);
        IRoomChangeApplicationEntry iEntry = RoomChangeApplicationEntryFactory.getLocalInstance(ctx);
        //人员入住信息
        IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
        //换宿单
        RoomChangeApplicationInfo roomChangeApplicationInfo = this.getRoomChangeApplicationInfo(ctx, new ObjectUuidPK(billId));
        RoomChangeApplicationEntryCollection entrys = roomChangeApplicationInfo.getEntrys();
        //人员入住信息修改字段
        SelectorItemCollection sicOccupants = new SelectorItemCollection();
        sicOccupants.add("CheckOutDate");
        for (int i = 0; i < entrys.size(); i++) {
            RoomChangeApplicationEntryInfo roomChangeApplicationEntryInfo = entrys.get(i);
            //入住人员信息
            OccupantsInfo occupants = roomChangeApplicationEntryInfo.getOccupants();
            //删除
            iOccupants.delete(new ObjectUuidPK(occupants.getId()));
            //原入住人员信息
            OccupantsInfo oldOccupants = roomChangeApplicationEntryInfo.getOldOccupants();
            try {
                oldOccupants.setCheckOutDate(DateTimeUtils.parseDate("2199-12-31", "yyyy-MM-dd"));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            //oldOccupants.setCheckOutDate(roomChangeApplicationEntryInfo.getOldCheckoutDate());
            iOccupants.updatePartial(oldOccupants, sicOccupants);
            //清空原入住信息 和原退宿日期
            roomChangeApplicationEntryInfo.setOccupants(null);
            iEntry.update(new ObjectUuidPK(roomChangeApplicationEntryInfo.getId()), roomChangeApplicationEntryInfo);
        }
    }
}
