<?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="cn.iocoder.yudao.module.product.dal.mysql.harvestinfo.HarvestInfoMapper">

    <!--
        一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="getNumStatisticsOfYear" resultType="BigDecimal">
        SELECT
            SUM(phb.goods_num * bgi.conversion_value)
        FROM
            product_harvest_batch phb
            LEFT JOIN product_harvest_info phi ON phb.harvest_id = phi.harverst_id
            LEFT JOIN basic_goods_info bgi ON bgi.goods_id = phb.goods_id
        WHERE
            YEAR ( phi.harverst_time ) = YEAR (NOW())
    </select>

    <select id="getMonthYOYDatas" resultType="cn.iocoder.yudao.module.product.controller.admin.statistics.vo.MonthYOYRespVO">
        SELECT
            YEAR ( phi.harverst_time ) year,
            MONTH ( phi.harverst_time ) month,
            SUM( phb.goods_num * bgi.conversion_value ) sumNum,
            CONCAT(ifnull(round((sum( phb.goods_num * bgi.conversion_value ) - ss )/ ss * 100, 2 ), 0 ), '%') rateData
        FROM
            product_harvest_info phi
            LEFT JOIN product_harvest_batch phb ON phb.harvest_id = phi.harverst_id
            LEFT JOIN basic_goods_info bgi ON bgi.goods_id = phb.goods_id
            LEFT JOIN (
                SELECT
                    MONTH( phi.harverst_time ) mm,
                    YEAR( phi.harverst_time ) yy,
                    SUM( phb.goods_num * bgi.conversion_value ) ss
                FROM
                    product_harvest_info phi
                LEFT JOIN product_harvest_batch phb ON phb.harvest_id = phi.harverst_id
                LEFT JOIN basic_goods_info bgi ON bgi.goods_id = phb.goods_id
                WHERE phi.deleted = 0 AND phb.deleted = 0
                GROUP BY
                    mm, yy
            ) tongbi
            ON MONTH ( phi.harverst_time ) = tongbi.mm
            AND tongbi.yy = YEAR ( phi.harverst_time ) - 1
        WHERE
            YEAR(phi.harverst_time) = YEAR(now()) AND phi.deleted = 0 AND phb.deleted = 0
        GROUP BY
            year, month
        ORDER BY
            year DESC, month DESC;
    </select>

    <select id="getWeekYOYDatas" resultType="cn.iocoder.yudao.module.product.controller.admin.statistics.vo.WeekYOYRespVO">
        SELECT
            YEAR ( phi.harverst_time ) year,
            MONTH ( phi.harverst_time ) month,
            WEEK ( phi.harverst_time ) week,
            sum( phb.goods_num * bgi.conversion_value ) sumNum,
            concat(ifnull( round(( sum( phb.goods_num * bgi.conversion_value ) - ss )/ ss * 100, 2 ), 0 ), '%') rateData
        FROM
            product_harvest_info phi
            LEFT JOIN product_harvest_batch phb ON phb.harvest_id = phi.harverst_id
            LEFT JOIN basic_goods_info bgi ON bgi.goods_id = phb.goods_id
            LEFT JOIN (
                SELECT
                    MONTH( phi.harverst_time ) mm,
                    YEAR( phi.harverst_time ) yy,
                    WEEK( phi.harverst_time ) ww,
                    sum( phb.goods_num * bgi.conversion_value ) ss
                FROM
                    product_harvest_info phi
                LEFT JOIN product_harvest_batch phb ON phb.harvest_id = phi.harverst_id
                LEFT JOIN basic_goods_info bgi ON bgi.goods_id = phb.goods_id
                WHERE phi.deleted = 0 AND phb.deleted = 0
                GROUP BY
                    mm,
                    yy,
                    ww
            ) tongbi ON MONTH ( phi.harverst_time ) = tongbi.mm
            AND WEEK ( phi.harverst_time ) = tongbi.ww
            AND tongbi.yy = YEAR ( phi.harverst_time ) - 1
        WHERE
            YEAR(phi.harverst_time) = YEAR(now()) AND phi.deleted = 0 AND phb.deleted = 0
        GROUP BY
            year, month, week
        ORDER BY
            year DESC, month DESC, week DESC;
    </select>

    <select id="getDatasGroupByGoodsId" resultType="cn.iocoder.yudao.module.product.controller.admin.statistics.vo.GoodsStatisticsDataRespVO">
        SELECT
            phb.goods_id,
            bgi.goods_name,
            SUM( phb.goods_num ) sumNum
        FROM
            product_harvest_info phi
        LEFT JOIN product_harvest_batch phb ON phb.harvest_id = phi.harverst_id
        LEFT JOIN basic_goods_info bgi ON bgi.goods_id = phb.goods_id
        WHERE
            phi.deleted = 0 AND phb.deleted = 0
            <if test="type = 'month'">
                AND MONTH ( phi.harverst_time ) = MONTH (NOW())
            </if>
            <if test="type = 'month'">
                AND WEEK ( phi.harverst_time ) = WEEK (NOW())
            </if>
        GROUP BY
            phb.goods_id
    </select>

    <select id="getTodayHarvestCount" resultType="BigDecimal">
        SELECT
            IFNULL(SUM( goods_num ), 0)
        FROM
            product_harvest_info phi
                LEFT JOIN product_harvest_batch phb ON phb.harvest_id = phi.harverst_id
        WHERE
            phi.deleted = 0
        AND phb.deleted = 0
        AND TO_DAYS(phi.harverst_time) = TO_DAYS(NOW())
    </select>
</mapper>