<?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.ReportMapper">
    <!-- 1. 整体销售分析 基础统计 -->
    <select id="selectBasicStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
        COUNT(*) AS totalRecords,
        SUM(quantity) AS totalQuantity,
        SUM(total_amount) AS totalAmount,
        COUNT(DISTINCT product_code) AS uniqueProducts,
        COUNT(DISTINCT customer_name) AS uniqueCustomers
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND status = 1
    </select>

    <!-- 2. 去年同期统计 -->
    <select id="selectLastYearStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
            SUM(quantity) AS totalQuantity,
            SUM(total_amount) AS totalAmount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
          AND status = 1
    </select>

    <!-- 3. 上周期统计 -->
    <select id="selectPrevStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
            SUM(quantity) AS totalQuantity,
            SUM(total_amount) AS totalAmount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
          AND status = 1
    </select>

    <!-- 4. 销售趋势数据 -->
    <select id="selectTrendData" resultType="com.ruoyi.tjfx.entity.TrendDataVO">
        SELECT
        date,
        SUM(quantity) AS quantity,
        SUM(total_amount) AS amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND status = 1
        GROUP BY date
        ORDER BY date
    </select>

    <!-- 5. TOP20商品销售 -->
    <select id="selectTopProducts" resultType="com.ruoyi.tjfx.entity.TopProductVO">
        SELECT
        product_code AS productCode,
        product_name AS productName,
        SUM(quantity) AS quantity,
        SUM(total_amount) AS amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND status = 1
        GROUP BY product_code, product_name
        ORDER BY amount DESC
        LIMIT 20
    </select>

    <!-- 6. 品牌销售数据 -->
    <select id="selectBrandData" resultType="com.ruoyi.tjfx.entity.BrandDataVO">
        SELECT
        bd.brand AS brand,
        SUM(ad.quantity) AS quantity,
        SUM(ad.total_amount) AS amount
        FROM zs_tjfx_analysis_data ad
        JOIN zs_tjfx_base_data bd ON ad.product_code = bd.product_code
        WHERE ad.date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND ad.category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND ad.category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND ad.customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND ad.shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND ad.brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND ad.product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND ad.status = 1
        AND bd.brand IS NOT NULL
        AND bd.brand != ''
        GROUP BY bd.brand
        ORDER BY amount DESC
    </select>

    <!-- 7. 单品销售排行 -->
    <select id="selectProductRanking" resultType="com.ruoyi.tjfx.entity.RankingDataVO">
        SELECT
            date,
            SUM(total_amount) AS totalAmount,
            SUM(quantity) AS totalQuantity
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
          AND status = 1
          AND product_code = #{productCode}
        GROUP BY date
        ORDER BY totalAmount DESC
            LIMIT 20
    </select>

    <!-- 8. 单品销售趋势 -->
    <select id="selectProductTrend" resultType="com.ruoyi.tjfx.entity.TrendDataVO">
        SELECT
            date,
            SUM(total_amount) AS amount,
            SUM(quantity) AS quantity
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
          AND status = 1
          AND product_code = #{productCode}
        GROUP BY date
        ORDER BY date ASC
    </select>

    <!-- 9. 店铺销售基础统计 -->
    <select id="selectShopBasicStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
        SUM(quantity) AS totalQuantity,
        SUM(total_amount) AS totalAmount,
        COUNT(DISTINCT shop_name) AS totalShops,
        COUNT(DISTINCT category) AS totalCategories
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
    </select>

    <!-- 10. 各店铺销售金额 -->
    <select id="selectShopAmountData" resultType="com.ruoyi.tjfx.entity.ShopAmountVO">
        SELECT
        shop_name AS shopName,
        SUM(total_amount) AS amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
        GROUP BY shop_name
        ORDER BY amount DESC
    </select>

    <!-- 11. 各店铺销售数量 -->
    <select id="selectShopQuantityData" resultType="com.ruoyi.tjfx.entity.ShopQuantityVO">
        SELECT
        shop_name AS shopName,
        SUM(quantity) AS quantity
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
        GROUP BY shop_name
        ORDER BY quantity DESC
    </select>

    <!-- 12. 各品类销售趋势 -->
    <select id="selectCategoryTrendData" resultType="com.ruoyi.tjfx.entity.CategoryTrendVO">
        SELECT
        category,
        SUM(quantity) AS quantity,
        SUM(total_amount) AS amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
        AND category IS NOT NULL
        AND category != ''
        GROUP BY category
        ORDER BY amount DESC
    </select>

    <!-- 13. 分类规格原始数据 -->
    <select id="selectSpecsRawData" resultType="map">
        SELECT
        category_specs,
        SUM(quantity) AS quantity,
        SUM(total_amount) AS amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        AND status = 1
        AND category_specs IS NOT NULL
        AND category_specs != ''
        GROUP BY category_specs
        ORDER BY amount DESC
    </select>

    <!-- 14. 筛选选项 -->
    <select id="selectCategories" resultType="string">
        SELECT DISTINCT category
        FROM zs_tjfx_analysis_data
        WHERE category IS NOT NULL AND category != ''
        ORDER BY category
    </select>

    <select id="selectCategorySpecs" resultType="string">
        SELECT DISTINCT category_specs
        FROM zs_tjfx_analysis_data
        WHERE category_specs IS NOT NULL AND category_specs != ''
        ORDER BY category_specs
    </select>

    <select id="selectCustomers" resultType="string">
        SELECT DISTINCT customer_name
        FROM zs_tjfx_analysis_data
        WHERE customer_name IS NOT NULL AND customer_name != ''
        ORDER BY customer_name
    </select>

    <select id="selectShops" resultType="string">
        SELECT DISTINCT shop_name
        FROM zs_tjfx_analysis_data
        WHERE shop_name IS NOT NULL AND shop_name != ''
        ORDER BY shop_name
    </select>

    <select id="selectBrands" resultType="string">
        SELECT DISTINCT brand
        FROM zs_tjfx_base_data
        WHERE brand IS NOT NULL AND brand != ''
        ORDER BY brand
    </select>

    <!-- 15. 商品编码联想 -->
    <select id="selectProductCodeSuggestions" resultType="map">
        SELECT
            bd.product_code AS product_code,
            bd.product_name AS product_name,
            bd.category_specs AS category_specs,
            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 LIKE #{keyword}
        ORDER BY bd.product_code
            LIMIT 10
    </select>

    <!-- 16. 商品品牌-->
    <select id="selectBrandRawData" resultType="map">
        SELECT
        brand,
        SUM(quantity) AS quantity,
        SUM(total_amount) AS amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        AND status = 1
        AND category_specs IS NOT NULL
        AND category_specs != ''
        GROUP BY brand
        ORDER BY amount DESC
    </select>

    <!-- 1. 整体销售分析 基础统计 -->
   <!-- <select id="selectBasicStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
            COUNT(*) as totalRecords,
            SUM(quantity) as totalQuantity,
            SUM(total_amount) as totalAmount,
            COUNT(DISTINCT product_code) as uniqueProducts,
            COUNT(DISTINCT customer_name) as uniqueCustomers
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND status = 1
    </select>

    &lt;!&ndash; 2. 去年同期统计 &ndash;&gt;
    <select id="selectLastYearStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
            SUM(quantity) as totalQuantity,
            SUM(total_amount) as totalAmount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        AND status = 1
    </select>

    &lt;!&ndash; 3. 上周期统计 &ndash;&gt;
    <select id="selectPrevStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
            SUM(quantity) as totalQuantity,
            SUM(total_amount) as totalAmount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        AND status = 1
    </select>

    &lt;!&ndash; 4. 销售趋势数据 &ndash;&gt;
    <select id="selectTrendData" resultType="com.ruoyi.tjfx.entity.TrendDataVO">
        SELECT
            date,
            SUM(quantity) as dailyQuantity,
            SUM(total_amount) as dailyAmount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND status = 1
        GROUP BY date
        ORDER BY date
    </select>

    &lt;!&ndash; 5. TOP20商品销售 &ndash;&gt;
    <select id="selectTopProducts" resultType="com.ruoyi.tjfx.entity.TopProductVO">
        SELECT
            product_code as productCode,
            product_name as productName,
            SUM(quantity) as totalQuantity,
            SUM(total_amount) as totalAmount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND status = 1
        GROUP BY product_code, product_name
        ORDER BY totalAmount DESC
        LIMIT 20
    </select>

    &lt;!&ndash; 6. 品牌销售数据 &ndash;&gt;
    <select id="selectBrandData" resultType="com.ruoyi.tjfx.entity.BrandDataVO">
        SELECT
            bd.brand as brand,
            SUM(ad.quantity) as quantity,
            SUM(ad.total_amount) as amount
        FROM zs_tjfx_analysis_data ad
        JOIN zs_tjfx_base_data bd ON ad.product_code = bd.product_code
        WHERE ad.date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND ad.category = #{category}</if>
        <if test="categorySpecs != null and categorySpecs != ''">AND ad.category_specs LIKE CONCAT('%', #{categorySpecs}, '%')</if>
        <if test="customer != null and customer != ''">AND ad.customer_name = #{customer}</if>
        <if test="shop != null and shop != ''">AND ad.shop_name = #{shop}</if>
        <if test="brand != null and brand != ''">AND ad.brand = #{brand}</if>
        <if test="productCode != null and productCode != ''">AND ad.product_code LIKE CONCAT('%', #{productCode}, '%')</if>
        AND ad.status = 1
        AND bd.brand IS NOT NULL
        AND bd.brand != ''
        GROUP BY bd.brand
        ORDER BY amount DESC
    </select>

    &lt;!&ndash; 7. 单品销售排行 &ndash;&gt;
    <select id="selectProductRanking" resultType="com.ruoyi.tjfx.entity.RankingDataVO">
        SELECT
            date,
            SUM(total_amount) as totalAmount,
            SUM(quantity) as totalQuantity
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        AND status = 1
        AND product_code = #{productCode}
        GROUP BY date
        ORDER BY totalAmount DESC
        LIMIT 20
    </select>

    &lt;!&ndash; 8. 单品销售趋势 &ndash;&gt;
    <select id="selectProductTrend" resultType="com.ruoyi.tjfx.entity.TrendDataVO">
        SELECT
            date,
            SUM(total_amount) as amount,
            SUM(quantity) as quantity
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        AND status = 1
        AND product_code = #{productCode}
        GROUP BY date
        ORDER BY date ASC
    </select>

    &lt;!&ndash; 9. 店铺销售基础统计 &ndash;&gt;
    <select id="selectShopBasicStats" resultType="com.ruoyi.tjfx.entity.BasicStatsVO">
        SELECT
            SUM(quantity) as totalQuantity,
            SUM(total_amount) as totalAmount,
            COUNT(DISTINCT shop_name) as totalShops,
            COUNT(DISTINCT category) as totalCategories
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
    </select>

    &lt;!&ndash; 10. 各店铺销售金额 &ndash;&gt;
    <select id="selectShopAmountData" resultType="com.ruoyi.tjfx.entity.ShopAmountVO">
        SELECT
            shop_name as shopName,
            SUM(total_amount) as amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
        GROUP BY shop_name
        ORDER BY amount DESC
    </select>

    &lt;!&ndash; 11. 各店铺销售数量 &ndash;&gt;
    <select id="selectShopQuantityData" resultType="com.ruoyi.tjfx.entity.ShopQuantityVO">
        SELECT
            shop_name as shopName,
            SUM(quantity) as quantity
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
        GROUP BY shop_name
        ORDER BY quantity DESC
    </select>

    &lt;!&ndash; 12. 各品类销售趋势 &ndash;&gt;
    <select id="selectCategoryTrendData" resultType="com.ruoyi.tjfx.entity.CategoryTrendVO">
        SELECT
            category,
            SUM(quantity) as quantity,
            SUM(total_amount) as amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="shop != null and shop != ''">AND shop_name = #{shop}</if>
        AND status = 1
        AND category IS NOT NULL
        AND category != ''
        GROUP BY category
        ORDER BY amount DESC
    </select>

    &lt;!&ndash; 13. 分类规格原始数据 &ndash;&gt;
    <select id="selectSpecsRawData" resultType="map">
        SELECT
            category_specs,
            SUM(quantity) as quantity,
            SUM(total_amount) as amount
        FROM zs_tjfx_analysis_data
        WHERE date BETWEEN #{startDate} AND #{endDate}
        <if test="category != null and category != ''">AND category = #{category}</if>
        AND status = 1
        AND category_specs IS NOT NULL
        AND category_specs != ''
        GROUP BY category_specs
        ORDER BY amount DESC
    </select>

    &lt;!&ndash; 14. 筛选选项 &ndash;&gt;
    <select id="selectCategories" resultType="string">
        SELECT DISTINCT category
        FROM zs_tjfx_analysis_data
        WHERE category IS NOT NULL AND category != ''
        ORDER BY category
    </select>
    <select id="selectCategorySpecs" resultType="string">
        SELECT DISTINCT category_specs
        FROM zs_tjfx_analysis_data
        WHERE category_specs IS NOT NULL AND category_specs != ''
        ORDER BY category_specs
    </select>
    <select id="selectCustomers" resultType="string">
        SELECT DISTINCT customer_name
        FROM zs_tjfx_analysis_data
        WHERE customer_name IS NOT NULL AND customer_name != ''
        ORDER BY customer_name
    </select>
    <select id="selectShops" resultType="string">
        SELECT DISTINCT shop_name
        FROM zs_tjfx_analysis_data
        WHERE shop_name IS NOT NULL AND shop_name != ''
        ORDER BY shop_name
    </select>
    <select id="selectBrands" resultType="string">
        SELECT DISTINCT brand
        FROM zs_tjfx_base_data
        WHERE brand IS NOT NULL AND brand != ''
        ORDER BY brand
    </select>

    &lt;!&ndash; 15. 商品编码联想 &ndash;&gt;
    <select id="selectProductCodeSuggestions" resultType="map">
        SELECT
            bd.product_code as product_code,
            bd.product_name as product_name,
            bd.category_specs as category_specs,
            c.name as category_name
        FROM zs_tjfx_base_data bd
        LEFT JOIN categories c ON bd.category_id = c.id
        WHERE bd.product_code LIKE #{keyword}
        ORDER BY bd.product_code
        LIMIT 10
    </select>-->
</mapper>
