<?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.envdata.EnvDataMapper">

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

    <select id="selectEnvDataPage" resultType="cn.iocoder.yudao.module.product.controller.admin.envdata.vo.EnvDataRespVO">
        SELECT ped.*, pet.env_type_name
        FROM product_env_data ped
        LEFT JOIN product_env_type pet ON pet.id = ped.env_type_id
        <where>
            ped.deleted = 0
            <if test="pageReqVO.envTypeId != null">and ped.env_type_id = #{pageReqVO.envTypeId}</if>
            <if test="pageReqVO.belongBusinessId != null">and ped.belong_business_id = #{pageReqVO.belongBusinessId}</if>
        </where>
        order by ped.create_time desc
    </select>

    <select id="getLastOne" resultType="cn.iocoder.yudao.module.product.dal.dataobject.envdata.EnvDataDO">
        SELECT * FROM product_env_data WHERE deleted = 0 ORDER BY create_time DESC LIMIT 1
    </select>

    <select id="getLastEnvDatas" resultType="cn.iocoder.yudao.module.product.controller.admin.envdata.vo.EnvDataRespVO">
        SELECT ped.*, pet.unit FROM product_env_data ped
        left join product_env_type pet on ped.env_type_id = pet.id
        WHERE ped.deleted = 0
            <if test="typeIds != null and typeIds.size() != 0">
                AND ped.env_type_id IN
                <foreach collection="typeIds" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            AND ped.create_time = #{date}
        ORDER BY ped.create_time DESC
    </select>

    <select id="getAVGEnvDatas" resultType="cn.iocoder.yudao.module.product.controller.admin.envdata.vo.EnvDataRespVO">
        SELECT
            ped.id, ped.env_type_id, ped.env_name, ped.belong_business_id, ped.tenant_id, ROUND(AVG(ped.value), 2) value,
            date_format(ped.create_time, "%Y-%m-%d") create_time, pet.unit
        FROM
            product_env_data ped
        LEFT JOIN product_env_type pet ON ped.env_type_id = pet.id
        WHERE
            ped.deleted = 0
            <if test="typeIds != null and typeIds.size() != 0">
                AND ped.env_type_id IN
                <foreach collection="typeIds" open="(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </if>
            AND ped.create_time >= NOW() - INTERVAL #{day} DAY
        GROUP BY
            ped.env_type_id,
            date_format(ped.create_time, "%y-%m-%d")
        ORDER BY
            ped.create_time, ped.env_type_id
    </select>

    <delete id="cleanDataWhenOverXDays">
        DELETE FROM product_env_data WHERE DATEDIFF(NOW(), create_time) > #{day}
    </delete>

</mapper>
