<?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.AnalysisDataMapper">

    <!-- 分页查询分析数据 -->
    <select id="selectPageWithConditions" resultType="com.ruoyi.tjfx.entity.AnalysisData">
        SELECT * FROM zs_tjfx_analysis_data
        <where>
            <if test="productCode != null and productCode != ''">
                AND product_code LIKE CONCAT('%', #{productCode}, '%')
            </if>
            <if test="productName != null and productName != ''">
                AND product_name LIKE CONCAT('%', #{productName}, '%')
            </if>
            <if test="customerName != null and customerName != ''">
                AND customer_name LIKE CONCAT('%', #{customerName}, '%')
            </if>
            <if test="shopName != null and shopName != ''">
                AND shop_name LIKE CONCAT('%', #{shopName}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="startDate != null">
                AND date >= #{startDate}
            </if>
            <if test="endDate != null">
                AND date &lt;= #{endDate}
            </if>
        </where>
        ORDER BY created_at DESC
    </select>

    <!-- 根据条件查询所有数据（用于导出） -->
    <select id="selectAllWithConditions" resultType="com.ruoyi.tjfx.entity.AnalysisData">
        SELECT * FROM zs_tjfx_analysis_data
        <where>
            <if test="productCode != null and productCode != ''">
                AND product_code LIKE CONCAT('%', #{productCode}, '%')
            </if>
            <if test="productName != null and productName != ''">
                AND product_name LIKE CONCAT('%', #{productName}, '%')
            </if>
            <if test="customerName != null and customerName != ''">
                AND customer_name LIKE CONCAT('%', #{customerName}, '%')
            </if>
            <if test="shopName != null and shopName != ''">
                AND shop_name LIKE CONCAT('%', #{shopName}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="startDate != null">
                AND date >= #{startDate}
            </if>
            <if test="endDate != null">
                AND date &lt;= #{endDate}
            </if>
        </where>
        ORDER BY created_at DESC
    </select>

    <!-- 批量插入数据 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO zs_tjfx_analysis_data (
            date, shop_name, product_code, product_name, customer_name,
            category, category_specs, quantity, total_amount, source,
            status, delivery_type, destination, remarks, order_number, row_number, brand
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.date}, #{item.shopName}, #{item.productCode}, #{item.productName}, #{item.customerName},
                #{item.category}, #{item.categorySpecs}, #{item.quantity}, #{item.totalAmount}, #{item.source},
                #{item.status}, #{item.deliveryType}, #{item.destination}, #{item.remarks}, #{item.orderNumber}, #{item.rowNumber}, #{item.brand}
            )
        </foreach>
    </insert>

    <!-- 根据唯一条件查询记录 -->
    <select id="selectByUniqueCondition" resultType="com.ruoyi.tjfx.entity.AnalysisData">
        SELECT * FROM zs_tjfx_analysis_data
        WHERE date = #{date}
        AND shop_name = #{shopName}
        AND product_code = #{productCode}
        AND delivery_type = #{deliveryType}
        AND order_number = #{orderNumber}
        AND row_number = #{rowNumber}
        LIMIT 1
    </select>

</mapper>
