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

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.iot.dal.dataobject.deviceattributetype.DeviceAttributeTypeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energytype.EnergyTypeDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.mes.controller.admin.energytype.vo.*;

/**
 * 能耗类型 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface EnergyTypeMapper extends BaseMapperX<EnergyTypeDO> {

    default PageResult<EnergyTypeDO> selectPage(EnergyTypePageReqVO reqVO) {
        LambdaQueryWrapperX<EnergyTypeDO> wrapper = new LambdaQueryWrapperX<EnergyTypeDO>()
                .eqIfPresent(EnergyTypeDO::getCode, reqVO.getCode())
                .likeIfPresent(EnergyTypeDO::getName, reqVO.getName())
                .eqIfPresent(EnergyTypeDO::getUnit, reqVO.getUnit())
                .eqIfPresent(EnergyTypeDO::getIsEnable, reqVO.getIsEnable())
                .eqIfPresent(EnergyTypeDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(EnergyTypeDO::getCreateTime, reqVO.getCreateTime());

        // 只有在ids不为空时才加入过滤条件
        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .filter(StringUtils::isNotBlank)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());

            if (!idList.isEmpty()) {
                wrapper.in(EnergyTypeDO::getId, idList);
            }
        }

        return selectPage(reqVO, wrapper.orderByDesc(EnergyTypeDO::getId));
    }

    default List<EnergyTypeDO> select() {
        return selectList();
    }
}