<?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.dcs.hnyz.mapper.EquipmentParamMapper">
    
    <resultMap type="EquipmentParam" id="EquipmentParamResult">
        <result property="registerId"    column="register_id"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="relationId"    column="relation_id"    />
        <result property="address"    column="address"    />
        <result property="registerName"    column="register_name"    />
        <result property="dataType"    column="data_type"    />
        <result property="code"    column="code"    />
        <result property="registerCode"    column="register_code"    />
        <result property="unitType"    column="unit_type"    />
        <result property="paramType"    column="param_type"    />
    </resultMap>

    <resultMap id="EquipmentFormResultMap" type="com.dcs.hnyz.domain.vo.EquipmentParamFormVo" extends="EquipmentParamResult">
        <result property="relatedEquipmentName" column="related_equipment_name"/>
        <result property="equipmentName" column="equipment_name"/>
    </resultMap>



    <sql id="selectEquipmentParamVo">
        select register_id, equipment_id, relation_id, address, register_name, data_type, code, register_code, unit_type, param_type from equipment_param
    </sql>

    <select id="selectEquipmentParamList" parameterType="EquipmentParam" resultMap="EquipmentParamResult">
        <include refid="selectEquipmentParamVo"/>
        <where>  
            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
            <if test="relationId != null "> and relation_id = #{relationId}</if>
            <if test="address != null "> and address = #{address}</if>
            <if test="registerName != null  and registerName != ''"> and register_name like concat('%', #{registerName}, '%')</if>
            <if test="dataType != null  and dataType != ''"> and data_type = #{dataType}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="registerCode != null  and registerCode != ''"> and register_code = #{registerCode}</if>
            <if test="unitType != null  and unitType != ''"> and unit_type = #{unitType}</if>
            <if test="paramType != null  and paramType != ''"> and param_type = #{paramType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="delFlag == null"> and del_flag = '0'</if>
        </where>
    </select>
    
    <select id="selectEquipmentParamByRegisterId" parameterType="Long" resultMap="EquipmentParamResult">
        <include refid="selectEquipmentParamVo"/>
        where register_id = #{registerId}
    </select>

    <select id="selectEquipmentParamForm" resultMap="EquipmentFormResultMap">
        SELECT
        a.*,
        b.equipment_name AS related_equipment_name,
        c.equipment_name AS equipment_name
        FROM
        equipment_param AS a
        INNER JOIN
        equipment AS b
        ON
        a.relation_id = b.equipment_id
        INNER JOIN
        equipment AS c
        ON
        a.equipment_id = c.equipment_id
        <where>
            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
            <if test="relationId != null "> and relation_id = #{relationId}</if>
            <if test="address != null "> and address = #{address}</if>
            <if test="registerName != null  and registerName != ''"> and register_name like concat('%', #{registerName}, '%')</if>
            <if test="dataType != null  and dataType != ''"> and data_type = #{dataType}</if>
            <if test="code != null  and code != ''"> and a.code = #{code}</if>
            <if test="registerCode != null  and registerCode != ''"> and register_code = #{registerCode}</if>
            <if test="unitType != null  and unitType != ''"> and unit_type = #{unitType}</if>
            <if test="paramType != null  and paramType != ''"> and param_type = #{paramType}</if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
            <if test="delFlag == null"> and a.del_flag = '0'</if>
        </where>
    </select>

    <insert id="insertEquipmentParam" parameterType="EquipmentParam" useGeneratedKeys="true" keyProperty="registerId">
        insert into equipment_param
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id,</if>
            <if test="relationId != null">relation_id,</if>
            <if test="address != null">address,</if>
            <if test="registerName != null and registerName != ''">register_name,</if>
            <if test="dataType != null and dataType != ''">data_type,</if>
            <if test="code != null and code != ''">code,</if>
            <if test="registerCode != null and registerCode != ''">register_code,</if>
            <if test="unitType != null and unitType != ''">unit_type,</if>
            <if test="paramType != null">param_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">#{equipmentId},</if>
            <if test="relationId != null">#{relationId},</if>
            <if test="address != null">#{address},</if>
            <if test="registerName != null and registerName != ''">#{registerName},</if>
            <if test="dataType != null and dataType != ''">#{dataType},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="registerCode != null and registerCode != ''">#{registerCode},</if>
            <if test="unitType != null and unitType != ''">#{unitType},</if>
            <if test="paramType != null">#{paramType},</if>
         </trim>
    </insert>

    <update id="updateEquipmentParam" parameterType="EquipmentParam">
        update equipment_param
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
            <if test="relationId != null">relation_id = #{relationId},</if>
            <if test="address != null">address = #{address},</if>
            <if test="registerName != null and registerName != ''">register_name = #{registerName},</if>
            <if test="dataType != null and dataType != ''">data_type = #{dataType},</if>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="registerCode != null and registerCode != ''">register_code = #{registerCode},</if>
            <if test="unitType != null and unitType != ''">unit_type = #{unitType},</if>
            <if test="paramType != null">param_type = #{paramType},</if>
        </trim>
        where register_id = #{registerId}
    </update>

    <delete id="deleteEquipmentParamByRegisterId" parameterType="Long">
        delete from equipment_param where register_id = #{registerId}
    </delete>

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