<?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.EquipmentExtensionMapper">
    
    <resultMap type="EquipmentExtension" id="EquipmentExtensionResult">
        <result property="id"    column="id"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="flowId"    column="flow_id"    />
        <result property="x"    column="x"    />
        <result property="y"    column="y"    />
    </resultMap>

    <sql id="selectEquipmentExtensionVo">
        select id, equipment_id, flow_id, x, y from equipment_extension
    </sql>

    <select id="selectEquipmentExtensionList" parameterType="EquipmentExtension" resultMap="EquipmentExtensionResult">
        <include refid="selectEquipmentExtensionVo"/>
        <where>  
            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
            <if test="flowId != null "> and flow_id = #{flowId}</if>
        </where>
    </select>
    
    <select id="selectEquipmentExtensionById" parameterType="Long" resultMap="EquipmentExtensionResult">
        <include refid="selectEquipmentExtensionVo"/>
        where id = #{id}
    </select>

    <select id="getEquipmentExtensionFormList"
            resultType="com.dcs.hnyz.domain.vo.EquipmentExtensionFormVo">
        SELECT
        a.*,
        b.equipment_name,
        c.flow_name
        FROM
        equipment_extension AS a
        INNER JOIN
        equipment AS b
        ON
        a.equipment_id = b.equipment_id
        INNER JOIN
        dcs_flow AS c
        ON
        a.flow_id = c.flow_id
        <where>
            <if test="flowId != null">
                AND a.flow_id = #{flowId}
            </if>
            <if test="equipmentName != null and equipmentName != ''">
                AND b.equipment_name LIKE CONCAT('%', #{equipmentName}, '%')
            </if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
        </where>
    </select>

    <insert id="insertEquipmentExtension" parameterType="EquipmentExtension" useGeneratedKeys="true" keyProperty="id">
        insert into equipment_extension
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id,</if>
            <if test="flowId != null">flow_id,</if>
            <if test="x != null and x != ''">x,</if>
            <if test="y != null and y != ''">y,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">#{equipmentId},</if>
            <if test="flowId != null">#{flowId},</if>
            <if test="x != null and x != ''">#{x},</if>
            <if test="y != null and y != ''">#{y},</if>
         </trim>
    </insert>

    <update id="updateEquipmentExtension" parameterType="EquipmentExtension">
        update equipment_extension
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
            <if test="flowId != null">flow_id = #{flowId},</if>
            <if test="x != null and x != ''">x = #{x},</if>
            <if test="y != null and y != ''">y = #{y},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEquipmentExtensionById" parameterType="Long">
        delete from equipment_extension where id = #{id}
    </delete>

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