<?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">
<!-- 牦牛疾病预警 biz_yak_disease_warning：列表/详情查询、插入、按来源设备去重删除 -->
<mapper namespace="com.ruoyi.web.modules.industryservice.mapper.BizYakDiseaseWarningMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning" id="BizYakDiseaseWarningResult">
        <id     property="id"               column="id"/>
        <result property="yakNo"            column="yak_no"/>
        <result property="pastureId"        column="pasture_id"/>
        <result property="pastureName"      column="pasture_name"/>
        <result property="alertTime"        column="alert_time"/>
        <result property="alertMessage"     column="alert_message"/>
        <result property="warningType"      column="warning_type"/>
        <result property="measuredValue"    column="measured_value"/>
        <result property="referenceRange"   column="reference_range"/>
        <result property="deviceNo"         column="device_no"/>
        <result property="collectTime"      column="collect_time"/>
        <result property="yakAvatarUrl"     column="yak_avatar_url"/>
        <result property="dataSource"       column="data_source"/>
        <result property="lastSyncTime"     column="last_sync_time"/>
        <result property="createTime"       column="create_time"/>
        <result property="updateTime"       column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, yak_no, pasture_id, pasture_name, alert_time, alert_message,
               warning_type, measured_value, reference_range, device_no, collect_time, yak_avatar_url,
               data_source, last_sync_time, create_time, update_time
        from biz_yak_disease_warning
    </sql>

    <select id="selectBizYakDiseaseWarningById" parameterType="long" resultMap="BizYakDiseaseWarningResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectBizYakDiseaseWarningList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning"
            resultMap="BizYakDiseaseWarningResult">
        <include refid="selectVo"/>
        <where>
            <if test="pastureId != null">
                and pasture_id = #{pastureId}
            </if>
            <if test="alertTimeBegin != null">
                and alert_time &gt;= #{alertTimeBegin}
            </if>
            <if test="alertTimeEnd != null">
                and alert_time &lt;= #{alertTimeEnd}
            </if>
            <if test="allowedPastureIds != null and allowedPastureIds.size() > 0">
                and pasture_id in
                <foreach collection="allowedPastureIds" item="pid" open="(" separator="," close=")">
                    #{pid}
                </foreach>
            </if>
        </where>
        order by alert_time desc, id desc
    </select>

    <insert id="insertBizYakDiseaseWarning" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_yak_disease_warning (
            yak_no, pasture_id, pasture_name, alert_time, alert_message,
            warning_type, measured_value, reference_range, device_no, collect_time, yak_avatar_url,
            data_source, last_sync_time, create_time, update_time
        ) values (
            #{yakNo}, #{pastureId}, #{pastureName}, #{alertTime}, #{alertMessage},
            #{warningType}, #{measuredValue}, #{referenceRange}, #{deviceNo}, #{collectTime}, #{yakAvatarUrl},
            #{dataSource}, #{lastSyncTime}, #{createTime}, #{updateTime}
        )
    </insert>

    <select id="selectMonthlyWarningByPastureName" resultMap="BizYakDiseaseWarningResult">
        <include refid="selectVo"/>
        where pasture_name = #{pastureName}
          and warning_type = #{warningType}
          and alert_time &gt;= #{monthStart}
          and alert_time &lt;= #{monthEnd}
        order by alert_time desc, id desc
        limit 1
    </select>

    <select id="selectByDataSourceAndDeviceNo" resultMap="BizYakDiseaseWarningResult">
        <include refid="selectVo"/>
        where data_source = #{dataSource} and device_no = #{deviceNo}
        limit 1
    </select>

    <update id="updateAlertContent" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning">
        update biz_yak_disease_warning
        set alert_time = #{alertTime},
            alert_message = #{alertMessage},
            measured_value = #{measuredValue},
            reference_range = #{referenceRange},
            pasture_name = #{pastureName},
            warning_type = #{warningType},
            last_sync_time = #{lastSyncTime},
            update_time = #{updateTime}
        where id = #{id}
    </update>

    <delete id="deleteByDataSourceAndDeviceNo">
        delete from biz_yak_disease_warning
        where data_source = #{dataSource} and device_no = #{deviceNo}
    </delete>

    <delete id="deleteByDataSourceAndDeviceNos">
        delete from biz_yak_disease_warning
        where data_source = #{dataSource}
          and device_no in
        <foreach collection="deviceNos" item="no" open="(" separator="," close=")">#{no}</foreach>
    </delete>
</mapper>
