<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.tjfx.mapper.BaseDataMapper">

    <!-- 根据商品编号和分类ID查询基准数据 -->
    <select id="selectByProductCodeAndCategoryId" resultType="com.ruoyi.tjfx.entity.BaseData">
        SELECT bd.*, c.name as category_name
        FROM zs_tjfx_base_data bd
        LEFT JOIN zs_tjfx_categories c ON bd.category_id = c.id
        WHERE bd.product_code = #{productCode}
        <if test="categoryId != null">
            AND bd.category_id = #{categoryId}
        </if>
        LIMIT 1
    </select>
    <select id="findByCategoryId" resultType="com.ruoyi.tjfx.entity.BaseDataVO">
        SELECT bd.*, c.name as category_name
        FROM zs_tjfx_base_data bd
        LEFT JOIN zs_tjfx_categories c ON bd.category_id = c.id
        WHERE 1=1
        <if test="categoryId != null">
            AND bd.category_id = #{categoryId}
        </if>
    </select>
    <select id="selectByProductCode" resultType="com.ruoyi.tjfx.entity.BaseDataVO">
        SELECT bd.*, c.name as category_name
        FROM zs_tjfx_base_data bd
        LEFT JOIN zs_tjfx_categories c ON bd.category_id = c.id
        WHERE bd.product_code = #{productCode}
        LIMIT 1
    </select>

    <select id="selectExistingProductCodes" resultType="string">
        SELECT product_code
        FROM zs_tjfx_base_data
        WHERE product_code IN
        <foreach collection="codes" item="code" open="(" separator="," close=")">
            #{code}
        </foreach>
    </select>
    <select id="selectPageWithJoin" resultType="com.ruoyi.tjfx.entity.BaseDataVO">
        SELECT
        bd.id, bd.product_code, bd.product_name, bd.brand,
        bd.category_id, c.name AS category_name,
        bd.category_specs, bd.created_at, bd.updated_at
        FROM zs_tjfx_base_data bd
        LEFT JOIN zs_tjfx_categories c ON bd.category_id = c.id
        <where>
            <if test="productCode != null and productCode != ''">
                AND bd.product_code LIKE CONCAT('%', #{productCode}, '%')
            </if>
            <if test="productName != null and productName != ''">
                AND bd.product_name LIKE CONCAT('%', #{productName}, '%')
            </if>
            <if test="categoryId != null">
                AND bd.category_id = #{categoryId}
            </if>
        </where>
        ORDER BY bd.created_at DESC
    </select>
    <insert id="insertBatch">
        INSERT INTO zs_tjfx_base_data (
        product_code, product_name, brand,
        category_id, category_specs,
        created_at, updated_at
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.productCode},
            #{item.productName},
            #{item.brand},
            #{item.categoryId},
            #{item.categorySpecs},
            #{item.createdAt},
            #{item.updatedAt}
            )
        </foreach>
    </insert>
</mapper>
