package cn.iocoder.yudao.module.iot.dal.mysql.devicemodel;

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.device.DeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodel.DeviceModelDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.iot.controller.admin.devicemodel.vo.*;

/**
 * 采集设备模型 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface DeviceModelMapper extends BaseMapperX<DeviceModelDO> {

    default PageResult<DeviceModelDO> selectPage(DeviceModelPageReqVO reqVO) {

        LambdaQueryWrapperX<DeviceModelDO> deviceModelDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        deviceModelDOLambdaQueryWrapperX.eqIfPresent(DeviceModelDO::getCode, reqVO.getCode())
                .likeIfPresent(DeviceModelDO::getName, reqVO.getName())
                .eqIfPresent(DeviceModelDO::getProtocol, reqVO.getProtocol())
                .eqIfPresent(DeviceModelDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(DeviceModelDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DeviceModelDO::getCreateTime);


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


        return selectPage(reqVO, deviceModelDOLambdaQueryWrapperX);


    }

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

}