<?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.EmsFacsMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmsFacs" id="EmsFacsResult">
        <result property="id"    column="id"    />
        <result property="facsCode"    column="facs_code"    />
        <result property="facsName"    column="facs_name"    />
        <result property="facsType"    column="facs_type"    />
        <result property="facsTypeName"    column="facs_type_name"    />
        <result property="facsSubtype"    column="facs_subtype"    />
        <result property="facsSubtypeName"    column="facs_subtype_name"    />
        <result property="enable"    column="enable"    />
        <result property="refArea"    column="ref_area"    />
        <result property="refAreaName"    column="ref_area_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectEmsFacsVo">
        select f.`id`, f.`facs_code`, f.`facs_name`, f.`facs_type`, t.`name` as facs_type_name, f.`facs_subtype`, s.`name` as facs_subtype_name, f.`enable`, f.`ref_area`, a.`area_name` as ref_area_name, f.`create_time`, f.`update_time` from adm_ems_facs f LEFT JOIN dim_ems_facs_type t ON f.`facs_type` = t.`code` LEFT JOIN dim_ems_facs_subtype s ON f.`facs_subtype`= s.`code` LEFT JOIN adm_service_area a ON f.`ref_area` = a.`area_code`
    </sql>

    <select id="selectEmsFacsList" parameterType="com.ruoyi.ems.domain.EmsFacs" resultMap="EmsFacsResult">
        <include refid="selectEmsFacsVo"/>
        <where>  
            <if test="facsCode != null  and facsCode != ''"> and f.`facs_code` = #{facsCode}</if>
            <if test="facsName != null  and facsName != ''"> and f.`facs_name` like concat('%', #{facsName}, '%')</if>
            <if test="facsType != null  and facsType != ''"> and f.`facs_type` = #{facsType}</if>
            <if test="facsSubtype != null  and facsSubtype != ''"> and f.`facs_subtype` = #{facsSubtype}</if>
            <if test="enable != null "> and f.`enable` = #{enable}</if>
            <if test="refArea != null  and refArea != ''"> and f.`ref_area` = #{refArea}</if>
        </where>
    </select>
    
    <select id="selectEmsFacsById" parameterType="Long" resultMap="EmsFacsResult">
        <include refid="selectEmsFacsVo"/>
        where f.`id` = #{id}
    </select>

    <select id="selectEmsFacsByCode" parameterType="java.lang.String" resultMap="EmsFacsResult">
        <include refid="selectEmsFacsVo"/>
        where f.`facs_code` = #{code}
    </select>

    <insert id="insertEmsFacs" parameterType="com.ruoyi.ems.domain.EmsFacs" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_facs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="facsCode != null and facsCode != ''">facs_code,</if>
            <if test="facsName != null and facsName != ''">facs_name,</if>
            <if test="facsType != null and facsType != ''">facs_type,</if>
            <if test="facsSubtype != null and facsSubtype != ''">facs_subtype,</if>
            <if test="enable != null">enable,</if>
            <if test="refArea != null and refArea != ''">ref_area,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="facsCode != null and facsCode != ''">#{facsCode},</if>
            <if test="facsName != null and facsName != ''">#{facsName},</if>
            <if test="facsType != null and facsType != ''">#{facsType},</if>
            <if test="facsSubtype != null and facsSubtype != ''">#{facsSubtype},</if>
            <if test="enable != null">#{enable},</if>
            <if test="refArea != null and refArea != ''">#{refArea},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateEmsFacs" parameterType="com.ruoyi.ems.domain.EmsFacs">
        update adm_ems_facs
        <trim prefix="SET" suffixOverrides=",">
            <if test="facsCode != null and facsCode != ''">facs_code = #{facsCode},</if>
            <if test="facsName != null and facsName != ''">facs_name = #{facsName},</if>
            <if test="facsType != null and facsType != ''">facs_type = #{facsType},</if>
            <if test="facsSubtype != null and facsSubtype != ''">facs_subtype = #{facsSubtype},</if>
            <if test="enable != null">enable = #{enable},</if>
            <if test="refArea != null and refArea != ''">ref_area = #{refArea},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsFacsById" parameterType="Long">
        delete from adm_ems_facs where id = #{id}
    </delete>

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