<?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.cmvr.device.mapper.DeDeviceProfileMapper">
    
    <resultMap type="DeDeviceProfile" id="DeDeviceProfileResult">
        <result property="id"    column="id"    />
        <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"    />
        <result property="idDeDeviceRegistration"    column="id_de_device_registration"    />
        <result property="id_de_device_terminal_config" column="id_de_device_terminal_config"  />
        <result property="paramName"    column="param_name"    />
        <result property="defaultValue" column="default_value"  />
        <result property="paramType"    column="param_type"    />
    </resultMap>

    <sql id="selectDeDeviceProfileVo">
        select id, create_by, create_time, update_by, update_time, remark, device_model , id_de_device_terminal_config ,id_de_device_registration, param_name, default_value , param_type from de_device_profile
    </sql>

    <select id="selectDeDeviceProfileList" parameterType="DeDeviceProfile" resultMap="DeDeviceProfileResult">
        <include refid="selectDeDeviceProfileVo"/>
        <where>  
            <if test="remark != null  and remark != ''"> and remark = #{remark}</if>
            <if test="idDeDeviceRegistration != null  and idDeDeviceRegistration != ''"> and id_de_device_registration = #{idDeDeviceRegistration}</if>
            <if test="paramName != null  and paramName != ''"> and param_name like concat('%', #{paramName}, '%')</if>
            <if test="paramType != null  and paramType != ''"> and param_type = #{paramType}</if>
        </where>
    </select>
    
    <select id="selectDeDeviceProfileById" parameterType="String" resultMap="DeDeviceProfileResult">
        <include refid="selectDeDeviceProfileVo"/>
        where id_de_device_registration = #{id}
    </select>

    <select id="selectProfileById" resultType="com.cmvr.device.domain.DeDeviceProfile">
        <include refid="selectDeDeviceProfileVo"/> where id = #{id}
    </select>

    <select id="selectProfileByIdAndRemark" resultType="com.cmvr.device.domain.DeDeviceProfile">
        <include refid="selectDeDeviceProfileVo"></include>where id_de_device_registration=#{id} and remark=#{remark}
    </select>

    <select id="selectParameterByIdAndRemark" resultType="com.cmvr.device.domain.vo.ParameterVo">
        select id, param_name, default_value , param_type
        from de_device_profile where id_de_device_registration=#{id} and remark=#{remark}
    </select>





    <!-- 参数信息基础映射 -->
    <resultMap id="parameters" type="com.cmvr.device.domain.vo.ParameterVo">
        <result property="id" column="param_id"></result>
        <result property="paramName" column="param_name"></result>
        <result property="defaultValue" column="default_value"></result>
        <result property="paramType" column="param_type"></result>
    </resultMap>
    <!-- 设备信息基础映射 -->
    <resultMap id="BaseResultMap" type="com.cmvr.device.domain.vo.ProfileVo">
        <result column="id_de_device_registration" property="idDeDeviceRegistration" jdbcType="VARCHAR"/>
        <result column="device_model" property="deviceModel" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="id_de_device_terminal_config" property="id_de_device_terminal_config" jdbcType="VARCHAR"/>
        <result column="device_name" property="deviceName" jdbcType="VARCHAR"/>
        <result column="terminal_name" property="terminalName" jdbcType="VARCHAR"/>
        <collection property="parameters" ofType="com.cmvr.device.domain.vo.ParameterVo"
                    column="{id=id_de_device_registration, remark=remark}"
                    select="selectParametersByDeviceId"/>
    </resultMap>
    <!-- 查询所有设备配置信息 -->
    <select id="selectAllProfilesVo" resultMap="BaseResultMap">
            SELECT
                t1.remark,
                t1.device_model,
                t1.id_de_device_registration,
                t1.id_de_device_terminal_config,
                t2.device_name,
                t3.`name` as terminal_name
            FROM
                de_device_profile t1
                    JOIN de_device_registration t2 ON t1.id_de_device_registration = t2.id
                    JOIN de_device_terminal_config t3 ON t2.id_de_device_terminal_config = t3.id
            GROUP BY
                t1.remark,
                t1.device_model,
                t1.id_de_device_registration,
                t1.id_de_device_terminal_config,
                t2.device_name,
                t3.`name`
        </select>

        <!-- 根据设备ID和备注查询参数列表 -->
        <select id="selectParametersByDeviceId" resultMap="parameters">
            SELECT
                t1.id,
                t1.param_name,
                t1.param_type,
                t1.default_value
            FROM
                de_device_profile t1
            WHERE
                t1.id_de_device_registration = #{id}
              AND t1.remark = #{remark}
        </select>


    <insert id="insertDeDeviceProfile" parameterType="DeDeviceProfile">
        insert into de_device_profile
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="deviceModel != null">device_model,</if>
            <if test="idDeDeviceRegistration != null">id_de_device_registration,</if>
            <if test="paramName != null">param_name,</if>
            <if test="defaultValue != null">default_value,</if>
            <if test="paramType != null">param_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="deviceModel != null">#{deviceModel},</if>
            <if test="idDeDeviceRegistration != null">#{idDeDeviceRegistration},</if>
            <if test="paramName != null">#{paramName},</if>
            <if test="defaultValue != null">#{defaultValue},</if>
            <if test="paramType != null">#{paramType},</if>
         </trim>
    </insert>

    <update id="updateDeDeviceProfile" parameterType="com.cmvr.device.domain.DeDeviceProfile">
        update de_device_profile
        <trim prefix="SET" suffixOverrides=",">
            <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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="idDeDeviceRegistration != null">id_de_device_registration = #{idDeDeviceRegistration},</if>
            <if test="paramName != null">param_name = #{paramName},</if>
            <if test="defaultValue != null">default_value = #{defaultValue},</if>
            <if test="paramType != null">param_type = #{paramType},</if>
        </trim>
        where id = #{id}
    </update>


    <update id="updateProfileByIdAndByName" parameterType="com.cmvr.device.domain.vo.TeMechanicalArmVo">
        UPDATE de_device_profile set default_value=#{value}
        WHERE id_de_device_registration = #{id} AND param_name = #{name}
    </update>





    <delete id="deleteDeDeviceProfileById" parameterType="String">
        delete from de_device_profile where id = #{id}
    </delete>

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