<?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.ruoyi.ems.mapper.EmsObjFlowRelMapper">

    <resultMap type="com.ruoyi.ems.domain.EmsObjFlowRel" id="flowRelResult">
        <result property="id"              column="id"    />
        <result property="exportObj"       column="export_obj"    />
        <result property="exportObjName"   column="export_obj_name"    />
        <result property="exportObjType"   column="export_obj_type"    />
        <result property="inputObj"        column="input_obj"    />
        <result property="inputObjName"    column="input_obj_name"    />
        <result property="inputObjType"    column="input_obj_type"    />
        <result property="emsCls"          column="ems_cls"    />
        <result property="emsClsName"      column="ems_cls_name" />
        <result property="flowDesc"        column="flow_desc"    />
        <result property="flowCapacity"    column="flow_capacity"    />
        <result property="flowUnit"        column="flow_unit"    />
        <result property="areaCode"        column="area_code"    />
        <result property="enableStatus"    column="enable_status"    />
        <result property="createTime"      column="create_time"    />
        <result property="updateTime"      column="update_time"    />
    </resultMap>

    <sql id="selectEmsFacsFlowRelVo">
        select
            r.`id`,
            r.`export_obj`,
            r.`export_obj_type`,
            r.`input_obj`,
            r.`input_obj_type`,
            r.`ems_cls`,
            c.`name` as ems_cls_name,
            r.`flow_desc`,
            r.`flow_capacity`,
            r.`flow_unit`,
            r.`area_code`,
            r.`enable_status`,
            r.`create_time`,
            r.`update_time`,
            COALESCE(
                    (SELECT facs.facs_name FROM adm_ems_facs facs WHERE facs.facs_code = r.export_obj AND r.export_obj_type = 1),
                    (SELECT dev.device_name FROM adm_ems_device dev WHERE dev.device_code = r.export_obj AND r.export_obj_type = 2)
            ) AS export_obj_name,
            COALESCE(
                    (SELECT facs.facs_name FROM adm_ems_facs facs WHERE facs.facs_code = r.input_obj AND r.input_obj_type = 1),
                    (SELECT dev.device_name FROM adm_ems_device dev WHERE dev.device_code = r.input_obj AND r.input_obj_type = 2)
            ) AS input_obj_name
        from adm_ems_flow_rel r
                 LEFT JOIN dim_gb_ems_cls c ON r.`ems_cls` = c.`code`
    </sql>

    <select id="selectFlowRelList" parameterType="com.ruoyi.ems.domain.EmsObjFlowRel" resultMap="flowRelResult">
        <include refid="selectEmsFacsFlowRelVo"/>
        <where>
            <if test="exportObj != null and exportObj != ''">
                and r.`export_obj` = #{exportObj}
            </if>
            <if test="exportObjType != null">
                and r.`export_obj_type` = #{exportObjType}
            </if>
            <if test="inputObj != null and inputObj != ''">
                and r.`input_obj` = #{inputObj}
            </if>
            <if test="inputObjType != null">
                and r.`input_obj_type` = #{inputObjType}
            </if>
            <if test="emsCls != null and emsCls != ''">
                and r.`ems_cls` = #{emsCls}
            </if>
            <if test="areaCode != null and areaCode != ''">
                and r.`area_code` = #{areaCode}
            </if>
            <if test="enableStatus != null">
                and r.`enable_status` = #{enableStatus}
            </if>
        </where>
        order by r.`create_time` desc
    </select>

    <select id="selectFlowRelById" parameterType="Long" resultMap="flowRelResult">
        <include refid="selectEmsFacsFlowRelVo"/>
        where r.`id` = #{id}
    </select>

    <insert id="insertFlowRel" parameterType="com.ruoyi.ems.domain.EmsObjFlowRel" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_flow_rel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="exportObj != null and exportObj != ''">export_obj,</if>
            <if test="exportObjType != null">export_obj_type,</if>
            <if test="inputObj != null and inputObj != ''">input_obj,</if>
            <if test="inputObjType != null">input_obj_type,</if>
            <if test="emsCls != null and emsCls != ''">ems_cls,</if>
            <if test="flowDesc != null and flowDesc != ''">flow_desc,</if>
            <if test="flowCapacity != null">flow_capacity,</if>
            <if test="flowUnit != null and flowUnit != ''">flow_unit,</if>
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="enableStatus != null">enable_status,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="exportObj != null and exportObj != ''">#{exportObj},</if>
            <if test="exportObjType != null">#{exportObjType},</if>
            <if test="inputObj != null and inputObj != ''">#{inputObj},</if>
            <if test="inputObjType != null">#{inputObjType},</if>
            <if test="emsCls != null and emsCls != ''">#{emsCls},</if>
            <if test="flowDesc != null and flowDesc != ''">#{flowDesc},</if>
            <if test="flowCapacity != null">#{flowCapacity},</if>
            <if test="flowUnit != null and flowUnit != ''">#{flowUnit},</if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="enableStatus != null">#{enableStatus},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateFlowRel" parameterType="com.ruoyi.ems.domain.EmsObjFlowRel">
        update adm_ems_flow_rel
        <trim prefix="SET" suffixOverrides=",">
            <if test="exportObj != null and exportObj != ''">export_obj = #{exportObj},</if>
            <if test="exportObjType != null">export_obj_type = #{exportObjType},</if>
            <if test="inputObj != null and inputObj != ''">input_obj = #{inputObj},</if>
            <if test="inputObjType != null">input_obj_type = #{inputObjType},</if>
            <if test="emsCls != null and emsCls != ''">ems_cls = #{emsCls},</if>
            <if test="flowDesc != null and flowDesc != ''">flow_desc = #{flowDesc},</if>
            <if test="flowCapacity != null">flow_capacity = #{flowCapacity},</if>
            <if test="flowUnit != null and flowUnit != ''">flow_unit = #{flowUnit},</if>
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="enableStatus != null">enable_status = #{enableStatus},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFlowRelById" parameterType="Long">
        delete from adm_ems_flow_rel where id = #{id}
    </delete>

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