<?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.dcs.mapper.DcsEquipmentMapper">
    
    <resultMap type="DcsEquipment" id="DcsEquipmentResult">
        <result property="equipmentId"    column="equipment_id"    />
        <result property="address"    column="address"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="dataType"    column="data_type"    />
        <result property="parentsId"    column="parents_id"    />
        <result property="flowId"    column="flow_id"    />
        <result property="equipmentCode"    column="equipment_code"    />
        <result property="registerCode"    column="register_code"    />
        <result property="unitType"    column="unit_type"    />
        <result property="equipmentType"    column="equipment_type"    />
    </resultMap>

    <sql id="selectDcsEquipmentVo">
        select equipment_id, address, equipment_name, data_type, parents_id, flow_id, equipment_code, register_code, unit_type,equipment_type from dcs_equipment
    </sql>

    <select id="selectDcsEquipmentList" parameterType="DcsEquipment" resultMap="DcsEquipmentResult">
        <include refid="selectDcsEquipmentVo"/>
        <where>
            <if test="address != null "> and address = #{address}</if>
            <if test="equipmentName != null and equipmentName != ''"> and equipment_name like concat('%', #{equipmentName}, '%')</if>
            <if test="dataType != null and dataType != ''"> and data_type = #{dataType}</if>
            <if test="parentsId != null "> and parents_id = #{parentsId}</if>
            <if test="flowId != null and flowId!='' "> and FIND_IN_SET(#{flowId}, flow_id) </if>
            <if test="equipmentCode != null and equipmentCode != ''"> and equipment_code = #{equipmentCode}</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="equipmentType != null and equipmentType != ''"> and equipment_type = #{equipmentType}</if>
        </where>
    </select>

    <select id="selectDcsEquipmentVoList" resultType="com.dcs.dcs.domain.vo.DcsEquipmentFormVo">
        SELECT
        a.*
        FROM
        dcs_equipment a
        <where>
            <if test="address != null "> and address = #{address}</if>
            <if test="equipmentName != null and equipmentName != ''"> and equipment_name like concat('%', #{equipmentName}, '%')</if>
            <if test="dataType != null and dataType != ''"> and data_type = #{dataType}</if>
            <if test="parentsId != null "> and parents_id = #{parentsId}</if>
            <if test="flowId != null and flowId!='' "> and FIND_IN_SET(#{flowId}, flow_id) </if>
            <if test="equipmentCode != null and equipmentCode != ''"> and equipment_code = #{equipmentCode}</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="equipmentType != null and equipmentType != ''"> and equipment_type = #{equipmentType}</if>
        </where>
    </select>
    
    <select id="selectDcsEquipmentByEquipmentId" parameterType="Long" resultMap="DcsEquipmentResult">
        <include refid="selectDcsEquipmentVo"/>
        where equipment_id = #{equipmentId}
    </select>
    <select id="getEquipmentIndexList" resultType="java.lang.Integer">
        SELECT equipment_id FROM dcs_equipment
        ORDER BY equipment_id
    </select>
    <select id="selectOneByEquipmentCode" resultType="com.dcs.dcs.domain.DcsEquipment">
        SELECT * FROM dcs_equipment WHERE equipment_code = #{equipmentCode}
    </select>

    <select id="selectDcsEquipmentListByDataTypes" resultType="com.dcs.dcs.domain.DcsEquipment">
        SELECT * FROM dcs_equipment
        WHERE data_type IN
        <foreach item="type" collection="list" open="(" separator="," close=")">
            #{type}
        </foreach>
    </select>

    <insert id="insertDcsEquipment" parameterType="DcsEquipment" useGeneratedKeys="true" keyProperty="equipmentId">
        insert into dcs_equipment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="address != null">address,</if>
            <if test="equipmentName != null">equipment_name,</if>
            <if test="dataType != null">data_type,</if>
            <if test="parentsId != null">parents_id,</if>
            <if test="flowId != null and flowId!=''">flow_id,</if>
            <if test="equipmentCode != null">equipment_code,</if>
            <if test="registerCode != null">register_code,</if>
            <if test="unitType != null">unit_type,</if>
            <if test="equipmentType != null">equipment_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="address != null">#{address},</if>
            <if test="equipmentName != null">#{equipmentName},</if>
            <if test="dataType != null">#{dataType},</if>
            <if test="parentsId != null">#{parentsId},</if>
            <if test="flowId != null and flowId!=''">#{flowId},</if>
            <if test="equipmentCode != null">#{equipmentCode},</if>
            <if test="registerCode != null">#{registerCode},</if>
            <if test="unitType != null">#{unitType},</if>
            <if test="equipmentType != null">#{equipmentType},</if>
        </trim>
    </insert>

    <update id="updateDcsEquipment" parameterType="DcsEquipment">
        update dcs_equipment
        <trim prefix="SET" suffixOverrides=",">
            <if test="address != null">address = #{address},</if>
            <if test="equipmentName != null">equipment_name = #{equipmentName},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="parentsId != null">parents_id = #{parentsId},</if>
            <if test="flowId != null and flowId!=''">flow_id = #{flowId},</if>
            <if test="equipmentCode != null">equipment_code = #{equipmentCode},</if>
            <if test="registerCode != null">register_code = #{registerCode},</if>
            <if test="unitType != null">unit_type = #{unitType},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
        </trim>
        where equipment_id = #{equipmentId}
    </update>

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

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