<?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="facsCategory"    column="facs_category"    />
        <result property="facsCategoryName"    column="facs_category_name"    />
        <result property="facsSubCategory"    column="facs_subcategory"    />
        <result property="facsSubCategoryName"    column="facs_subcategory_name"    />
        <result property="enable"    column="enable"    />
        <result property="refArea"    column="ref_area"    />
        <result property="refAreaName"    column="ref_area_name"    />
        <result property="facsModel"    column="facs_model"    />
        <result property="facsModelName"    column="facs_model_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_category`, f.`facs_subcategory`, f.`enable`, f.`ref_area`, f.`facs_model`, f.`create_time`, f.`update_time`,
            t.`name` as facs_category_name,
            s.`name` as facs_subcategory_name,
            a.`area_name` as ref_area_name,
            m.`model_name` as facs_model_name
        FROM adm_ems_facs f
            LEFT JOIN dim_ems_facs_category t ON f.`facs_category` = t.`code`
            LEFT JOIN dim_ems_facs_subcategory s ON f.`facs_subcategory`= s.`code`
            LEFT JOIN adm_area a ON f.`ref_area` = a.`area_code`
            LEFT JOIN adm_ems_obj_model m ON f.`facs_model` = m.`model_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="facsCategory != null  and facsCategory != ''"> and f.`facs_category` = #{facsCategory}</if>
            <if test="facsSubCategory != null  and facsSubCategory != ''"> and f.`facs_subcategory` = #{facsSubCategory}</if>
            <if test="facsModel != null  and facsModel != ''"> and f.`facs_model` = #{facsModel}</if>
            <if test="enable != null "> and f.`enable` = #{enable}</if>
            <if test="refArea != null and refArea != '' and refArea != '-1'"> 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="facsCategory != null and facsCategory != ''">facs_category,</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">facs_subcategory,</if>
            <if test="enable != null">enable,</if>
            <if test="refArea != null and refArea != ''">ref_area,</if>
            <if test="facsModel != null and facsModel != ''">facs_model,</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="facsCategory != null and facsCategory != ''">#{facsCategory},</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">#{facsSubCategory},</if>
            <if test="enable != null">#{enable},</if>
            <if test="refArea != null and refArea != ''">#{refArea},</if>
            <if test="facsModel != null and facsModel != ''">#{facsModel},</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="facsCategory != null and facsCategory != ''">facs_category = #{facsCategory},</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">facs_subcategory = #{facsSubCategory},</if>
            <if test="enable != null">enable = #{enable},</if>
            <if test="refArea != null and refArea != ''">ref_area = #{refArea},</if>
            <if test="facsModel != null and facsModel != ''">facs_model=#{facsModel},</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>