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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.bos.metadata.data.SortType;
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.IDormitory;
import com.kingdee.eas.custom.dormitorysystem.waterpower.IWaterPowerMeterReading;
import com.kingdee.eas.custom.dormitorysystem.waterpower.WaterPowerMeterReadingCollection;
import com.kingdee.eas.custom.dormitorysystem.waterpower.WaterPowerMeterReadingFactory;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Map;

/**
 * 获取水电表读数服务
 */
public class GetRoomListService implements IHRMsfService {

    private static final Logger logger = LoggerFactory.getLogger(GetRoomListService.class);
    // 定义日期格式化器，避免重复创建

    @Override
    public Object process(Context ctx, Map<String, Object> param) throws EASBizException, BOSException {

        if (ctx == null) {
            throw new BOSException("上下文Context不能为空");
        }
        FilterInfo filterInfo = new FilterInfo();
        // 3. 处理房间号参数
        Object roomNumberObj = param.get("roomNumber");
        if (roomNumberObj != null && !StringUtils.isEmpty(roomNumberObj.toString())) {
            String roomNumber = roomNumberObj.toString().trim();
            filterInfo.getFilterItems().add(new FilterItemInfo("dormitory.number", "%" + roomNumber+"%", CompareType.LIKE));
            filterInfo.getFilterItems().add(new FilterItemInfo("dormitory.name", "%" + roomNumber+"%", CompareType.LIKE));
            logger.info("添加房间号过滤条件：dormitory.id = {}", roomNumber);
        }
        // 4. 构建查询结果并返回
        return buildWaterPowerCollection(ctx, filterInfo);
    }

    /**
     * 构建排序条件并查询水电表读数集合
     * 抽离通用逻辑，提高代码复用性
     */
    private DormitoryCollection buildWaterPowerCollection(Context ctx, FilterInfo filterInfo) throws BOSException {
        // 构建排序条件
        SorterItemCollection sorterItemCollection = new SorterItemCollection();
        SorterItemInfo longNum = new SorterItemInfo("longNumber"); //长编码
        sorterItemCollection.add(longNum);
        sorterItemCollection.add(new SorterItemInfo("level")); // 层级
        // 构建视图并查询
        EntityViewInfo entityViewInfo = new EntityViewInfo();
        entityViewInfo.setFilter(filterInfo);
        entityViewInfo.setSorter(sorterItemCollection);
        IDormitory wpIns = DormitoryFactory.getLocalInstance(ctx);
        DormitoryCollection wpCol = wpIns.getDormitoryCollection(entityViewInfo);
        logger.info("查询到水电表读数记录数：{}", wpCol.size());
        return wpCol;
    }
}