package com.kingdee.eas.custom.dormitorysystem.task;

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.eas.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryCollection;
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.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 org.apache.log4j.Logger;

import java.util.*;

public class DormitorysystemFacadeControllerBean extends AbstractDormitorysystemFacadeControllerBean {
    private static Logger logger = Logger.getLogger(DormitorysystemFacadeControllerBean.class);

    /**
     * 更新宿舍已入住人数
     */
    @Override
    protected void _updateDormCheckInCount(Context ctx) throws BOSException, EASBizException {
        super._updateDormCheckInCount(ctx);
        //入住人员信息
        IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
        IDormitory iDormitory = DormitoryFactory.getLocalInstance(ctx);
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        filterItems.add(new FilterItemInfo("CheckInDate", time, CompareType.LESS_EQUALS));
        filterItems.add(new FilterItemInfo("CheckOutDate", time, CompareType.GREATER_EQUALS));
        EntityViewInfo viewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
        OccupantsCollection occupantsCol = iOccupants.getOccupantsCollection(viewInfo);
        //宿舍入住情况
        Map<String, Set<String>> dormitoryInCountMap = new HashMap();
        for (int i = 0; i < occupantsCol.size(); i++) {
            OccupantsInfo occupantsInfo = occupantsCol.get(i);
            String occupantsId = occupantsInfo.getId().toString();
            //宿舍
            DormitoryInfo dormitory = occupantsInfo.getDormitory();
            String dormitoryId = dormitory.getId().toString();
            if (!dormitoryInCountMap.containsKey(dormitoryId)) {
                dormitoryInCountMap.put(dormitoryId, new HashSet());
            }
            dormitoryInCountMap.get(dormitoryId).add(occupantsId);
        }
        //宿舍
        DormitoryCollection dormitoryCollection = iDormitory.getDormitoryCollection();
        SelectorItemCollection updateSic = new SelectorItemCollection();
        updateSic.add("OccupantsNumber");
        for (int i = 0; i < dormitoryCollection.size(); i++) {
            DormitoryInfo dormitoryInfo = dormitoryCollection.get(i);
            String dormitoryId = dormitoryInfo.getId().toString();
            Set<String> set = dormitoryInCountMap.get(dormitoryId);
            if (set == null) {
                dormitoryInfo.setOccupantsNumber(0);
            } else {
                dormitoryInfo.setOccupantsNumber(set.size());
            }
            //更新入住人数
            iDormitory.updatePartial(dormitoryInfo, updateSic);
        }
    }
}