<?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.web.modules.industryservice.mapper.BizEnvMonitoringRecordMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizEnvMonitoringRecord" id="BizEnvMonitoringRecordResult">
        <id property="id" column="id"/>
        <result property="externalId" column="external_id"/>
        <result property="pastureId" column="pasture_id"/>
        <result property="farmExternalId" column="farm_external_id"/>
        <result property="farmName" column="farm_name"/>
        <result property="enclosureId" column="enclosure_id"/>
        <result property="enclosureExternalId" column="enclosure_external_id"/>
        <result property="enclosureName" column="enclosure_name"/>
        <result property="temperature" column="temperature"/>
        <result property="humidity" column="humidity"/>
        <result property="ammonia" column="ammonia"/>
        <result property="h2s" column="h2s"/>
        <result property="monitoringTime" column="monitoring_time"/>
        <result property="dataSource" column="data_source"/>
        <result property="lastSyncTime" column="last_sync_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <select id="selectByExternalId" resultMap="BizEnvMonitoringRecordResult">
        select id, external_id, pasture_id, farm_external_id, farm_name, enclosure_id, enclosure_external_id, enclosure_name,
               temperature, humidity, ammonia, h2s, monitoring_time,
               data_source, last_sync_time, del_flag, create_by, create_time, update_by, update_time, remark
        from biz_env_monitoring_record where external_id = #{externalId} limit 1
    </select>

    <select id="selectMaxMonitoringTime" resultType="java.util.Date">
        select max(monitoring_time) from biz_env_monitoring_record
    </select>

    <select id="countRecords" resultType="long">
        select count(*) from biz_env_monitoring_record
    </select>

    <insert id="insertBizEnvMonitoringRecord" parameterType="com.ruoyi.web.modules.industryservice.domain.BizEnvMonitoringRecord" useGeneratedKeys="true" keyProperty="id">
        insert into biz_env_monitoring_record (
            external_id, pasture_id, farm_external_id, farm_name, enclosure_id, enclosure_external_id, enclosure_name,
            temperature, humidity, ammonia, h2s, monitoring_time,
            data_source, last_sync_time, del_flag, create_by, create_time, remark
        ) values (
            #{externalId}, #{pastureId}, #{farmExternalId}, #{farmName}, #{enclosureId}, #{enclosureExternalId}, #{enclosureName},
            #{temperature}, #{humidity}, #{ammonia}, #{h2s}, #{monitoringTime, jdbcType=TIMESTAMP},
            #{dataSource}, #{lastSyncTime}, #{delFlag}, #{createBy}, #{createTime, jdbcType=TIMESTAMP}, #{remark}
        )
    </insert>

    <update id="updateBizEnvMonitoringRecordFromSync" parameterType="com.ruoyi.web.modules.industryservice.domain.BizEnvMonitoringRecord">
        update biz_env_monitoring_record set
            pasture_id = #{pastureId}, farm_external_id = #{farmExternalId}, farm_name = #{farmName},
            enclosure_id = #{enclosureId}, enclosure_external_id = #{enclosureExternalId}, enclosure_name = #{enclosureName},
            temperature = #{temperature}, humidity = #{humidity}, ammonia = #{ammonia}, h2s = #{h2s},
            monitoring_time = #{monitoringTime, jdbcType=TIMESTAMP},
            data_source = #{dataSource}, last_sync_time = #{lastSyncTime}, del_flag = #{delFlag},
            update_by = #{updateBy}, update_time = #{updateTime, jdbcType=TIMESTAMP}, remark = #{remark}
        where id = #{id}
    </update>
</mapper>
