//package cn.iocoder.yudao.module.iot.dal.mysql.recipedeviceattribute;
//
//import java.util.*;
//
//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.recipedeviceattribute.RecipeDeviceAttributeDO;
//import org.apache.ibatis.annotations.Mapper;
//import cn.iocoder.yudao.module.iot.controller.admin.recipedeviceattribute.vo.*;
//
///**
// * 配方配置（关联采集设备模型-点位管理） Mapper
// *
// * @author 内蒙必硕
// */
//@Mapper
//public interface RecipeDeviceAttributeMapper extends BaseMapperX<RecipeDeviceAttributeDO> {
//
//    default PageResult<RecipeDeviceAttributeDO> selectPage(RecipeDeviceAttributePageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<RecipeDeviceAttributeDO>()
//                .eqIfPresent(RecipeDeviceAttributeDO::getRecipeId, reqVO.getRecipeId())
//                .eqIfPresent(RecipeDeviceAttributeDO::getAttributeId, reqVO.getAttributeId())
//                .likeIfPresent(RecipeDeviceAttributeDO::getAttributeName, reqVO.getAttributeName())
//                .eqIfPresent(RecipeDeviceAttributeDO::getAttributeType, reqVO.getAttributeType())
//                .eqIfPresent(RecipeDeviceAttributeDO::getDataType, reqVO.getDataType())
//                .eqIfPresent(RecipeDeviceAttributeDO::getDataUnit, reqVO.getDataUnit())
//                .betweenIfPresent(RecipeDeviceAttributeDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(RecipeDeviceAttributeDO::getId));
//    }
//
//
//
//}

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

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.recipedeviceattribute.RecipeDeviceAttributeDO;
import cn.iocoder.yudao.module.iot.controller.admin.recipedeviceattribute.vo.RecipeDeviceAttributePageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.recipedeviceattribute.vo.RecipeDeviceAttributePageRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 配方配置（关联采集设备模型-点位管理） Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface RecipeDeviceAttributeMapper extends BaseMapperX<RecipeDeviceAttributeDO> {

    // 原有分页方法（保留）
    default PageResult<RecipeDeviceAttributeDO> selectPage(RecipeDeviceAttributePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RecipeDeviceAttributeDO>()
                .eqIfPresent(RecipeDeviceAttributeDO::getRecipeId, reqVO.getRecipeId())
                .eqIfPresent(RecipeDeviceAttributeDO::getAttributeId, reqVO.getAttributeId())
                .likeIfPresent(RecipeDeviceAttributeDO::getAttributeName, reqVO.getAttributeName())
                .eqIfPresent(RecipeDeviceAttributeDO::getAttributeType, reqVO.getAttributeType())
                .eqIfPresent(RecipeDeviceAttributeDO::getDataType, reqVO.getDataType())
                .eqIfPresent(RecipeDeviceAttributeDO::getDataUnit, reqVO.getDataUnit())
                .betweenIfPresent(RecipeDeviceAttributeDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RecipeDeviceAttributeDO::getId));
    }

    // 修复后：关联分页查询（符合MyBatis-Plus分页规范）
    @Select({
            "<script>",
            "SELECT rda.id, rda.attribute_id, dcm.attribute_name, dcm.attribute_type, dcm.data_type, dcm.data_unit, dcm.address",
            "FROM iot_recipe_device_attribute rda",
            "LEFT JOIN iot_device_contact_model dcm ON rda.attribute_id = dcm.id",
            "WHERE 1=1",
            "  AND rda.deleted = 0",
            "<if test='reqVO.recipeId != null'>",
            "  AND rda.recipe_id = #{reqVO.recipeId}",
            "</if>",
            "<if test='reqVO.attributeName != null and reqVO.attributeName != \"\"'>",
            "  AND dcm.attribute_name LIKE CONCAT('%', #{reqVO.attributeName}, '%')",
            "</if>",
            "ORDER BY rda.id DESC",
            "</script>"
    })
    List<RecipeDeviceAttributePageRespDTO> selectPageWithAttribute(
            Page<RecipeDeviceAttributePageRespDTO> page, // 分页参数（由插件自动填充）
            @Param("reqVO") RecipeDeviceAttributePageReqVO reqVO);

    // 封装成芋道的PageResult（默认方法，复用框架逻辑）
    default PageResult<RecipeDeviceAttributePageRespDTO> selectPageWithAttributeWrap(RecipeDeviceAttributePageReqVO reqVO) {
        Page<RecipeDeviceAttributePageRespDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        List<RecipeDeviceAttributePageRespDTO> list = selectPageWithAttribute(page, reqVO);
        return new PageResult<>(list, page.getTotal());
    }


    default List<RecipeDeviceAttributePageRespDTO> getList(RecipeDeviceAttributePageReqVO reqVO) {
        List<RecipeDeviceAttributePageRespDTO> list = selectPageWithAttribute(null, reqVO);
        return list;
    }

    /**
     * 根据配方ID查询设备属性记录
     * @param recipeId 配方ID
     * @return 设备属性记录列表
     */
    default List<RecipeDeviceAttributeDO> selectByRecipeId(@Param("recipeId") Long recipeId) {
        return selectList(RecipeDeviceAttributeDO::getRecipeId, recipeId);
    };

    // 分页关联查询
    PageResult<RecipeDeviceAttributePageRespDTO> selectPageWithAttribute(RecipeDeviceAttributePageReqVO reqVO);
    // 根据RecipeId删除
    int deleteByRecipeId(@Param("recipeId") Long recipeId);

}