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

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

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.devicecontactmodel.DeviceContactModelDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import cn.iocoder.yudao.module.iot.controller.admin.devicecontactmodel.vo.*;

/**
 * 采集设备模型-点位管理 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface DeviceContactModelMapper extends BaseMapperX<DeviceContactModelDO> {

    default PageResult<DeviceContactModelDO> selectPageById(PageParam reqVO, DeviceContactModelPageReqVO device) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DeviceContactModelDO>()
                // 使用StringUtils.hasText()进行更严格的字符串非空判断
                .like(device.getAttributeCode() != null  && StringUtils.isNotBlank(device.getAttributeCode()), DeviceContactModelDO::getAttributeCode, device.getAttributeCode())
                .like(device.getAttributeName() != null  && StringUtils.isNotBlank(device.getAttributeName()), DeviceContactModelDO::getAttributeName, device.getAttributeName())
                // 对于枚举或数值类型，使用Objects.nonNull()判断
                .eq(Objects.nonNull(device.getAttributeType()), DeviceContactModelDO::getAttributeType, device.getAttributeType())
                // 必要的条件（如deviceModelId）保留直接eq条件
                .eq(DeviceContactModelDO::getDeviceId, device.getDeviceId())
                .orderByDesc(DeviceContactModelDO::getId));
    }


    default PageResult<DeviceContactModelDO> selectPage(DeviceContactModelPageReqVO reqVO) {

        LambdaQueryWrapperX<DeviceContactModelDO> deviceContactModelDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        deviceContactModelDOLambdaQueryWrapperX.eqIfPresent(DeviceContactModelDO::getAttributeCode, reqVO.getAttributeCode())
                .likeIfPresent(DeviceContactModelDO::getAttributeName, reqVO.getAttributeName())
                .eqIfPresent(DeviceContactModelDO::getAttributeType, reqVO.getAttributeType())
                .eqIfPresent(DeviceContactModelDO::getDataType, reqVO.getDataType())
                .eqIfPresent(DeviceContactModelDO::getAddress, reqVO.getAddress())
                .eqIfPresent(DeviceContactModelDO::getDataUnit, reqVO.getDataUnit())
                .eqIfPresent(DeviceContactModelDO::getRatio, reqVO.getRatio())
                .eqIfPresent(DeviceContactModelDO::getRemark, reqVO.getRemark())
                .eqIfPresent(DeviceContactModelDO::getDeviceId, reqVO.getDeviceId())
                .betweenIfPresent(DeviceContactModelDO::getCreateTime, reqVO.getCreateTime());

        if (reqVO.getIds()!=null && StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            deviceContactModelDOLambdaQueryWrapperX.in(DeviceContactModelDO::getId, idList);
        }

        PageResult<DeviceContactModelDO> deviceContactModelDOPageResult = selectPage(reqVO, deviceContactModelDOLambdaQueryWrapperX);

        return deviceContactModelDOPageResult;
    }

    /**
     * 根据ID查询属性名和数据单位
     * @param id 属性ID
     * @return 包含attribute_name和data_unit的Map
     */
    Map<String, String> selectAttributeNameAndUnitById(@Param("id") Long id);


}