<?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.microservices.zone.resource.mapper.ZoneResourceTypeMapper">

    <resultMap type="ZoneResourceType" id="ZoneResourceTypeResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sort" column="sort"/>
        <result property="deptId" column="dept_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="zoneId" column="zone_id"/>
        <result property="fileTypeList" column="file_type_list"/>
    </resultMap>

    <sql id="selectZoneResourceTypeVo">
        select id,
               name,
               sort,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               file_type_list,
               zone_id
        from zone_resource_type
    </sql>

    <sql id="selectZoneResourceTypeAliasVo">
        select zrt.id,
               zrt.name,
               zrt.sort,
               zrt.dept_id,
               zrt.del_flag,
               zrt.create_by,
               zrt.create_time,
               zrt.update_by,
               zrt.update_time,
               zrt.file_type_list,
               zrt.zone_id
        from zone_resource_type zrt
    </sql>

    <select id="selectZoneResourceTypeList" parameterType="ZoneResourceType" resultMap="ZoneResourceTypeResult">
        <include refid="selectZoneResourceTypeAliasVo"/>
        <include refid="selectWhere"/>
    </select>

    <sql id="selectWhere">
        <where>
            <if test="name != null  and name != ''">and zrt.name like concat('%', #{name}, '%')</if>
            <if test="deptId != null ">and zrt.dept_id = #{deptId}</if>
            <if test="zoneId != null ">and zrt.zone_id = #{zoneId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
            ORDER BY zrt.sort ASC
        </where>
    </sql>

    <select id="selectZoneResourceTypeById" parameterType="Long" resultMap="ZoneResourceTypeResult">
        <include refid="selectZoneResourceTypeVo"/>
        where id = #{id}
    </select>
    <select id="selectZoneResourceTypeByNameAndZoneId" resultMap="ZoneResourceTypeResult">
        <include refid="selectZoneResourceTypeVo"/>
        where name = #{name}
        and zone_id = #{zoneId}
    </select>
    <select id="selectZoneResourceTypeListInTypeIds" resultMap="ZoneResourceTypeResult">
        <include refid="selectZoneResourceTypeVo"/>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="selectContainFileZoneResourceTypeList" parameterType="ZoneResourceType"
            resultMap="ZoneResourceTypeResult">
        <include refid="selectZoneResourceTypeAliasVo"/>
        <include refid="selectOpenWhere"/>
    </select>
    <sql id="selectOpenWhere">
        <where>
            <if test="name != null  and name != ''">and zrt.name like concat('%', #{name}, '%')</if>
            <if test="deptId != null ">and zrt.dept_id = #{deptId}</if>
            <if test="zoneId != null ">and zrt.zone_id = #{zoneId}</if>
            and zrt.id in (select type_id from zone_resource_to_type)
            <!-- 数据范围过滤 -->
            ${params.dataScope}
            ORDER BY zrt.sort ASC
        </where>
    </sql>
    <insert id="insertZoneResourceType" parameterType="ZoneResourceType" useGeneratedKeys="true" keyProperty="id">
        insert into zone_resource_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="sort != null">sort,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="zoneId != null">zone_id,</if>
            <if test="fileTypeList != null">file_type_list,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="sort != null">#{sort},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="zoneId != null">#{zoneId},</if>
            <if test="fileTypeList != null">#{fileTypeList},</if>
        </trim>
    </insert>

    <update id="updateZoneResourceType" parameterType="ZoneResourceType">
        update zone_resource_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="zoneId != null">zone_id = #{zoneId},</if>
            <if test="fileTypeList != null">file_type_list = #{fileTypeList},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteZoneResourceTypeById" parameterType="Long">
        delete
        from zone_resource_type
        where id = #{id}
    </delete>

    <delete id="deleteZoneResourceTypeByIds" parameterType="String">
        delete from zone_resource_type where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteZoneResourceTypeByZoneId">
        delete
        from zone_resource_type
        where zone_id = #{zoneId}
    </delete>
</mapper>