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

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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.iocoder.yudao.module.iot.dal.dataobject.recipeplandetail.RecipePlanDetailDO;
import cn.iocoder.yudao.module.iot.controller.admin.recipeplandetail.vo.RecipePlanDetailPageRespDTO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.iot.controller.admin.recipeplandetail.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


/**
 * 配方计划详情表（配方库） Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface RecipePlanDetailMapper extends BaseMapperX<RecipePlanDetailDO> {

    default PageResult<RecipePlanDetailDO> selectPage(RecipePlanDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RecipePlanDetailDO>()
                .eqIfPresent(RecipePlanDetailDO::getCode, reqVO.getCode())
                .likeIfPresent(RecipePlanDetailDO::getName, reqVO.getName())
                .eqIfPresent(RecipePlanDetailDO::getRecipeId, reqVO.getRecipeId())
                .eqIfPresent(RecipePlanDetailDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(RecipePlanDetailDO::getSource, reqVO.getSource())
                .eqIfPresent(RecipePlanDetailDO::getIsEnable, reqVO.getIsEnable())
                .betweenIfPresent(RecipePlanDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RecipePlanDetailDO::getId));
    }
    // 核心：联表分页查询（包含配方名称、计划编码）
    @Select({
            "<script>",
            "SELECT rpd.*, ",
            "       r.name AS recipeName, ", // 关联iot_recipe表的名称
            "       p.code AS planCode,      ", // 关联mes_plan表的编码
            "       d.id AS deviceId         ", // 新增：关联iot_device表的id作为deviceId
            "FROM iot_recipe_plan_detail rpd",
            "LEFT JOIN iot_recipe r ON rpd.recipe_id = r.id   ", // 左关联配方表（避免配方ID不存在时数据丢失）
            "LEFT JOIN mes_plan p ON rpd.plan_id = p.id     ", // 左关联计划表
            "LEFT JOIN iot_device d ON r.machine_id = d.id ", // 新增：左关联设备表，通过machine_name匹配device_name
            "WHERE rpd.deleted = 0 ", // 过滤已删除数据
            "<if test='reqVO.code != null and reqVO.code != \"\"'>",
            "  AND rpd.code LIKE CONCAT('%', #{reqVO.code}, '%')",
            "</if>",
            "<if test='reqVO.name != null and reqVO.name != \"\"'>",
            "  AND rpd.name LIKE CONCAT('%', #{reqVO.name}, '%')",
            "</if>",
            "<if test='reqVO.recipeId != null'>",
            "  AND rpd.recipe_id = #{reqVO.recipeId}",
            "</if>",
            "<if test='reqVO.planId != null'>",
            "  AND rpd.plan_id = #{reqVO.planId}",
            "</if>",
            "<if test='reqVO.source != null and reqVO.source != \"\"'>",
            "  AND rpd.source = #{reqVO.source}",
            "</if>",
            "<if test='reqVO.isEnable != null'>",
            "  AND rpd.is_enable = #{reqVO.isEnable}",
            "</if>",
            "ORDER BY rpd.id DESC",
            "</script>"
    })
    List<RecipePlanDetailPageRespDTO> selectPageWithRelations(
            Page<RecipePlanDetailPageRespDTO> page, // MyBatis-Plus分页插件自动填充页码/页大小
            @Param("reqVO") RecipePlanDetailPageReqVO reqVO);

    // 封装成芋道框架的PageResult（复用框架分页逻辑）
    default PageResult<RecipePlanDetailPageRespDTO> selectPageWithRelationsWrap(RecipePlanDetailPageReqVO reqVO) {
        Page<RecipePlanDetailPageRespDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        List<RecipePlanDetailPageRespDTO> list = selectPageWithRelations(page, reqVO);
        return new PageResult<>(list, page.getTotal());
    }


}