<?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.agri.system.mapper.SysAgriInfoMapper">

    <resultMap type="SysAgriInfo" id="SysAgriInfoResult">
        <result property="id"    column="id"    />
        <result property="imei"    column="imei"    />
        <result property="agriName"    column="agri_name"    />
        <result property="workMode"    column="work_mode"    />
        <result property="userId"    column="user_id"    />
        <result property="alarmStatus"    column="alarm_status"    />
        <result property="installTime"    column="install_time"    />
        <result property="location"    column="location"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="isDeleted"    column="is_deleted"    />
    </resultMap>

    <sql id="selectSysAgriInfoVo">
        select id, imei, agri_name, user_id, alarm_status, install_time, quilt_num, film_num, blind_num, location, remark, create_time, create_by, update_time, update_by, is_deleted from sys_agri_info
    </sql>

    <sql id="agriUserVo">
        SELECT
            agri.id,
            agri.imei,
            agri.agri_name,
            agri.alarm_status,
            agri.install_time,
            agri.location,
            agri.remark,
            agri.create_time,
            agri.create_by,
            agri.update_time,
            agri.update_by,
            agri.is_deleted,
            agri.quilt_num,
            agri.film_num,
            agri.blind_num,
            user_agri.user_id
        FROM
            sys_agri_info agri
            LEFT JOIN sys_user_agri user_agri ON user_agri.agri_id = agri.imei
    </sql>

    <sql id="agriInfoVo">
        SELECT
            agri.id,
            agri.imei,
            agri.agri_name,
            agri.work_mode,
            agri.quilt_num,
            agri.film_num,
            agri.blind_num,
            user_agri.user_id,
            dtu_last.time,
            dtu_last.ts,
            dtu_last.temp1,
            dtu_last.temp2,
            dtu_last.temp3,
            dtu_last.temp4
        FROM
            sys_agri_info agri
                LEFT JOIN sys_user_agri user_agri ON user_agri.agri_id = agri.imei
                <if test="userId == null"> and user_agri.user_id is null </if>
                LEFT JOIN (
                SELECT
                    dtu.imei,
                    dtu.time,
                    dtu.ts,
                    dtu.temp1,
                    dtu.temp2,
                    dtu.temp3,
                    dtu.temp4,
                    ROW_NUMBER() OVER (PARTITION BY dtu.imei ORDER BY dtu.create_time DESC, dtu.id DESC) AS rn
                FROM sys_dtu_data dtu
                WHERE dtu.create_time >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
            ) dtu_last ON dtu_last.imei = agri.imei AND dtu_last.rn = 1
    </sql>

    <select id="selectSysAgriInfoList" parameterType="SysAgriInfo" resultMap="SysAgriInfoResult">
        <include refid="selectSysAgriInfoVo"/>
        <where>
            <if test="imei != null  and imei != ''"> and imei = #{imei}</if>
            <if test="agriName != null  and agriName != ''"> and agri_name like concat('%', #{agriName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="alarmStatus != null "> and alarm_status = #{alarmStatus}</if>
            <if test="workMode != null  and workMode != ''"> and work_mode = #{workMode}</if>
            <if test="installTime != null "> and install_time = #{installTime}</if>
            <if test="location != null  and location != ''"> and location = #{location}</if>
            <if test="isDeleted != null "> and is_deleted = #{isDeleted}</if>
        </where>
    </select>

    <select id="selectSysAgriInfoById" parameterType="Long" resultMap="SysAgriInfoResult">
        <include refid="selectSysAgriInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysAgriInfo" parameterType="SysAgriInfo" useGeneratedKeys="true" keyProperty="id">
        insert into sys_agri_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="imei != null and imei != ''">imei,</if>
            <if test="agriName != null and agriName != ''">agri_name,</if>
            <if test="workMode != null and workMode != ''">work_mode,</if>
            <if test="userId != null">user_id,</if>
            <if test="alarmStatus != null">alarm_status,</if>
            <if test="installTime != null">install_time,</if>
            <if test="location != null">location,</if>
            <if test="remark != null">remark,</if>
            create_time,
            create_by,
            update_time,
            update_by,
            <if test="isDeleted != null">is_deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="imei != null and imei != ''">#{imei},</if>
            <if test="agriName != null and agriName != ''">#{agriName},</if>
            <if test="workMode != null and workMode != ''">#{workMode},</if>
            <if test="userId != null">#{userId},</if>
            <if test="alarmStatus != null">#{alarmStatus},</if>
            <if test="installTime != null">#{installTime},</if>
            <if test="location != null">#{location},</if>
            <if test="remark != null">#{remark},</if>
            #{createTime},
            #{createBy},
            #{updateTime},
            #{updateBy},
            <if test="isDeleted != null">#{isDeleted},</if>
         </trim>
    </insert>

    <update id="updateSysAgriInfo" parameterType="SysAgriInfo">
        update sys_agri_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="imei != null and imei != ''">imei = #{imei},</if>
            <if test="workMode != null  and workMode != ''"> and work_mode = #{workMode}</if>
            <if test="agriName != null and agriName != ''">agri_name = #{agriName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="alarmStatus != null">alarm_status = #{alarmStatus},</if>
            <if test="installTime != null">install_time = #{installTime},</if>
            <if test="location != null">location = #{location},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSysAgriInfoById" parameterType="Long">
        update sys_agri_info set is_deleted = 1 where id = #{id}
    </update>

    <update id="deleteSysAgriInfoByIds" parameterType="String">
        update sys_agri_info set is_deleted = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="findAgriByUser" parameterType="SysAgriInfo" resultMap="SysAgriInfoResult">
        <include refid="agriUserVo"/>
        <if test="userId == null"> and user_agri.user_id is null </if>
        <where>
            is_deleted = 0
            <if test="userId != null"> and user_agri.user_id = #{userId}</if>
            <if test="imei != null  and imei != ''"> and agri.imei = #{imei}</if>
            <if test="workMode != null  and workMode != ''"> and agri.work_mode = #{workMode}</if>
            <if test="agriName != null  and agriName != ''"> and agri.agri_name like concat('%', #{agriName}, '%')</if>
            <if test="alarmStatus != null "> and agri.alarm_status = #{alarmStatus}</if>
            <if test="installTime != null "> and agri.install_time = #{installTime}</if>
            <if test="location != null  and location != ''"> and agri.location = #{location}</if>
            <if test="isDeleted != null "> and agri.is_deleted = #{isDeleted}</if>
        </where>
    </select>

    <select id="findAgriInfoByUser" parameterType="SysAgriInfo" resultType="com.agri.system.domain.vo.AgriInfoView">
        <include refid="agriInfoVo"/>
        <where>
            is_deleted = 0
            <if test="userId != null"> and user_agri.user_id = #{userId}</if>
            <if test="imei != null  and imei != ''"> and agri.imei = #{imei}</if>
            <if test="workMode != null  and workMode != ''"> and agri.work_mode = #{workMode}</if>
            <if test="agriName != null  and agriName != ''"> and agri.agri_name like concat('%', #{agriName}, '%')</if>
            <if test="alarmStatus != null "> and agri.alarm_status = #{alarmStatus}</if>
            <if test="installTime != null "> and agri.install_time = #{installTime}</if>
            <if test="location != null  and location != ''"> and agri.location = #{location}</if>
            <if test="isDeleted != null "> and agri.is_deleted = #{isDeleted}</if>
        </where>
    </select>

    <!--  查询自动模式下的所有imei以及卷膜参数  -->
    <select id="findAgriOfAutoInfo" resultType="com.agri.system.domain.vo.AgriAutoInfoVo">
        SELECT
            agri.`imei`,
            agri.`agri_name`,
            agri.`work_mode`,
            agri.`is_deleted`,
            agri.quilt_num,
            agri.film_num,
            agri.blind_num,
            roller_param.`roller`,
            roller_param.`ref_temp_code`,
            roller_param.`reserved_len`,
            roller_param.`manual_total_len`,
            roller_param.`auto_total_len`,
            roller_param.`vent_total_len`
        FROM
            sys_agri_info agri
                LEFT JOIN sys_roller_param roller_param ON agri.imei = roller_param.imei
        WHERE
            agri.`is_deleted` = 0
          and agri.work_mode = 1
    </select>

</mapper>
