<?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="pageIds" column="page_ids"/>
        <result property="code" column="code"/>
        <result property="equipmentType" column="equipment_type"/>
        <result property="plcId" column="plc_id"/>
        <result property="ipAddress" column="ip_address"/>
        <result property="port" column="port"/>
        <result property="protocolId" column="protocol_id"/>
    </resultMap>

    <sql id="selectEquipmentVo">
        select equipment_id,
               equipment_name,
               flow_ids,
               page_ids,
               code,
               title,
               equipment_type,
               plc_id,
               ip_address,
               port,
               protocol_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="pageIds != null  and pageIds != ''">and FIND_IN_SET(#{pageIds}, page_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="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="pageIds != null  and pageIds != ''">and FIND_IN_SET(#{pageIds}, page_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="getEquipmentsByParentId" resultType="com.dcs.hnyz.domain.Equipment">
        <include refid="selectEquipmentVo"/>
        WHERE equipment_id IN (
        SELECT relation_id FROM equipment_param WHERE equipment_id = #{equipmentId}
        )
    </select>

    <update id="updateEquipmentByPlcId">
        UPDATE equipment e
            JOIN plc_info p
        ON e.plc_id = p.plc_id
            SET e.protocol_id = p.protocol_id,
                e.port = p.port,
                e.ip_address = p.ip_address
        WHERE e.plc_id = #{plcId}
    </update>


</mapper>