package cn.iocoder.yudao.module.mes.dal.mysql.energydevice;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.*;

/**
 * 能源设备 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface EnergyDeviceMapper extends BaseMapperX<EnergyDeviceDO> {

    default PageResult<EnergyDeviceDO> selectPage(EnergyDevicePageReqVO reqVO) {


        LambdaQueryWrapperX<EnergyDeviceDO> energyDeviceDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        energyDeviceDOLambdaQueryWrapperX
                .likeIfPresent(EnergyDeviceDO::getName, reqVO.getName())
                .eqIfPresent(EnergyDeviceDO::getCode, reqVO.getCode())
                .eqIfPresent(EnergyDeviceDO::getInfo, reqVO.getInfo())
                .eqIfPresent(EnergyDeviceDO::getCheckCron, reqVO.getCheckCron())
                .betweenIfPresent(EnergyDeviceDO::getLastCheckTime, reqVO.getLastCheckTime())
                .eqIfPresent(EnergyDeviceDO::getLastCheckValue, reqVO.getLastCheckValue())
                .likeIfPresent(EnergyDeviceDO::getUnitName, reqVO.getUnitName())
                .eqIfPresent(EnergyDeviceDO::getIsEnable, reqVO.getIsEnable())
                .betweenIfPresent(EnergyDeviceDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(EnergyDeviceDO::getId);

        // 单独处理 ids 条件
        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            energyDeviceDOLambdaQueryWrapperX.in(EnergyDeviceDO::getId, idList);
        }


        return selectPage(reqVO, energyDeviceDOLambdaQueryWrapperX);
    }

}