<?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.EquipmentMapper">
    
    <resultMap type="Equipment" id="EquipmentResult">
        <result property="equipmentId"    column="equipment_id"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="flowIds"    column="flow_ids"    />
        <result property="code"    column="code"    />
        <result property="equipmentType"    column="equipment_type"    />
        <result property="plcId"    column="plc_id"    />
        <result property="protocolId" column="protocol_id"/>
    </resultMap>

    <sql id="selectEquipmentVo">
        select equipment_id, equipment_name, flow_ids, code, equipment_type, plc_id from equipment
    </sql>

    <select id="selectEquipmentList" parameterType="Equipment" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        <where>  
            <if test="equipmentName != null  and equipmentName != ''"> and equipment_name like concat('%', #{equipmentName}, '%')</if>
            <if test="flowIds != null  and flowIds != ''"> and FIND_IN_SET(#{flowIds}, flow_ids) </if>
            <if test="code != null  and code != ''"> and code like concat('%', #{code}, '%')</if>
            <if test="equipmentType != null  and equipmentType != ''"> and equipment_type = #{equipmentType}</if>
            <if test="plcId != null "> and plc_id = #{plcId}</if>
            <if test="protocolId != null"> and protocol_id = #{protocolId}</if>
            <if test="status !=null and status !=''"> and status = #{status}</if>
            <if test="delFlag == null"> and del_flag = '0'</if>
        </where>
    </select>
    
    <select id="selectEquipmentByEquipmentId" parameterType="Long" resultMap="EquipmentResult">
        <include refid="selectEquipmentVo"/>
        where equipment_id = #{equipmentId}
    </select>
    <select id="selectEquipmentForm" resultType="com.dcs.hnyz.domain.vo.EquipmentFormVo">
        select * from equipment
        <where>
            <if test="equipmentName != null  and equipmentName != ''"> and equipment_name like concat('%', #{equipmentName}, '%')</if>
            <if test="flowIds != null  and flowIds != ''"> and FIND_IN_SET(#{flowIds}, flow_ids) </if>
            <if test="code != null  and code != ''"> and code like concat('%', #{code}, '%')</if>
            <if test="equipmentType != null  and equipmentType != ''"> and equipment_type = #{equipmentType}</if>
            <if test="plcId != null "> and plc_id = #{plcId}</if>
            <if test="protocolId != null"> and protocol_id = #{protocolId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="delFlag == null"> and del_flag = '0'</if>
        </where>
    </select>
    <select id="selectOneByCode" resultType="com.dcs.hnyz.domain.Equipment">
        select * from equipment where code = #{code}
    </select>

    <insert id="insertEquipment" parameterType="Equipment" useGeneratedKeys="true" keyProperty="equipmentId">
        insert into equipment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentName != null">equipment_name,</if>
            <if test="flowIds != null">flow_ids,</if>
            <if test="code != null">code,</if>
            <if test="equipmentType != null">equipment_type,</if>
            <if test="plcId != null">plc_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentName != null">#{equipmentName},</if>
            <if test="flowIds != null">#{flowIds},</if>
            <if test="code != null">#{code},</if>
            <if test="equipmentType != null">#{equipmentType},</if>
            <if test="plcId != null">#{plcId},</if>
         </trim>
    </insert>

    <update id="updateEquipment" parameterType="Equipment">
        update equipment
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentName != null">equipment_name = #{equipmentName},</if>
            <if test="flowIds != null">flow_ids = #{flowIds},</if>
            <if test="code != null">code = #{code},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
            <if test="plcId != null">plc_id = #{plcId},</if>
        </trim>
        where equipment_id = #{equipmentId}
    </update>

    <delete id="deleteEquipmentByEquipmentId" parameterType="Long">
        delete from equipment where equipment_id = #{equipmentId}
    </delete>

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