<?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.SysRollerParamMapper">

    <resultMap type="SysRollerParam" id="SysRollerParamResult">
        <result property="id"    column="id"    />
        <result property="imei"    column="imei"    />
        <result property="roller"    column="roller"    />
        <result property="refTempCode"    column="ref_temp_code"    />
        <result property="reservedLen"    column="reserved_len"    />
        <result property="manualTotalLen"    column="manual_total_len"    />
        <result property="autoTotalLen"    column="auto_total_len"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSysRollerParamVo">
        select id, imei, roller, ref_temp_code, reserved_len, manual_total_len, auto_total_len, create_by, create_time, update_by, update_time from sys_roller_param
    </sql>

    <select id="selectSysRollerParamList" parameterType="SysRollerParam" resultMap="SysRollerParamResult">
        <include refid="selectSysRollerParamVo"/>
        <where>
            <if test="imei != null  and imei != ''"> and imei = #{imei}</if>
            <if test="roller != null  and roller != ''"> and roller = #{roller}</if>
            <if test="refTempCode != null  and refTempCode != ''"> and ref_temp_code = #{refTempCode}</if>
        </where>
    </select>

    <select id="selectSysRollerParamById" parameterType="String" resultMap="SysRollerParamResult">
        <include refid="selectSysRollerParamVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysRollerParam" parameterType="SysRollerParam" useGeneratedKeys="true" keyProperty="id">
        insert into sys_roller_param
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="imei != null and imei != ''">imei,</if>
            <if test="roller != null and roller != ''">roller,</if>
            <if test="refTempCode != null and refTempCode != ''">ref_temp_code,</if>
            <if test="reservedLen != null">reserved_len,</if>
            <if test="manualTotalLen != null">manual_total_len,</if>
            <if test="autoTotalLen != null">auto_total_len,</if>
    create_by,
    create_time,
    update_by,
    update_time,
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="imei != null and imei != ''">#{imei},</if>
            <if test="roller != null and roller != ''">#{roller},</if>
            <if test="refTempCode != null and refTempCode != ''">#{refTempCode},</if>
            <if test="reservedLen != null">#{reservedLen},</if>
            <if test="manualTotalLen != null">#{manualTotalLen},</if>
            <if test="autoTotalLen != null">#{autoTotalLen},</if>
        #{createBy},
        #{createTime},
        #{updateBy},
        #{updateTime},
         </trim>
    </insert>

    <update id="updateSysRollerParam" parameterType="SysRollerParam">
        update sys_roller_param
        <trim prefix="SET" suffixOverrides=",">
            <if test="imei != null and imei != ''">imei = #{imei},</if>
            <if test="roller != null and roller != ''">roller = #{roller},</if>
            <if test="refTempCode != null and refTempCode != ''">ref_temp_code = #{refTempCode},</if>
            <if test="reservedLen != null">reserved_len = #{reservedLen},</if>
            <if test="manualTotalLen != null">manual_total_len = #{manualTotalLen},</if>
            <if test="autoTotalLen != null">auto_total_len = #{autoTotalLen},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysRollerParamById" parameterType="String">
        delete from sys_roller_param where id = #{id}
    </delete>

    <delete id="deleteSysRollerParamByIds" parameterType="String">
        delete from sys_roller_param where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="getRollerTerms" resultType="com.agri.system.domain.vo.RollerTermVO" >
        SELECT
        rp.imei,
        rp.roller,
        rp.ref_temp_code,
        rp.reserved_len,
        rp.manual_total_len,
        rp.auto_total_len,
        rp.vent_total_len,
        at.temp,
        at.vent,
        at.start_time,
        at.end_time
        FROM sys_roller_param rp
        LEFT JOIN sys_auto_term `at`
        ON rp.imei = at.imei
        AND rp.roller = at.roller  -- 关键：按imei+roller双字段关联（精准匹配）
        WHERE rp.imei IN
        <foreach collection="imeiList" item="imei" open="(" separator="," close=")">
            #{imei}
        </foreach>
    </select>
</mapper>
