<?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.ZoneResourceMapper">

    <resultMap type="com.microservices.zone.resource.domain.ZoneResource" id="ZoneResourceResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="introduction" column="introduction"/>
        <result property="fileIds" column="file_ids"/>
        <result property="domainId" column="domain_id"/>
        <result property="sort" column="sort"/>
        <result property="isHomepage" column="is_homepage"/>
        <result property="zoneId" column="zone_id"/>
        <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="editorType" column="editor_type"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="auditTime" column="audit_time"/>
        <result property="auditBy" column="audit_by"/>
        <result property="summary" column="summary"/>
        <result property="remark" column="remark"/>
        <result property="keywords" column="keywords"/>
        <result property="extendData" column="extend_data"/>
        <association property="zoneResourceDomain" column="domain_id"
                     javaType="com.microservices.zone.resource.domain.ZoneResourceDomain"
                     resultMap="domainResult"/>
    </resultMap>


    <resultMap id="domainResult" type="com.microservices.zone.resource.domain.ZoneResourceDomain">
        <id property="id" column="domain_id"/>
        <result property="name" column="domain_name"/>
    </resultMap>

    <sql id="selectZoneResourceVo">
        select id,
               name,
               file_ids,
               domain_id,
               sort,
               is_homepage,
               zone_id,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               editor_type,
               audit_status,
               audit_time,
               audit_by,
               summary,
               remark,
               update_time,
               keywords,
               extend_data
        from zone_resource
    </sql>

    <sql id="selectZoneResourceAliasVo">
        select zr.id,
               zr.name,
               zr.file_ids,
               zr.domain_id,
               zr.sort,
               zr.is_homepage,
               zr.zone_id,
               zr.dept_id,
               zr.del_flag,
               zr.create_by,
               zr.create_time,
               zr.update_by,
               zr.update_time,
               zr.editor_type,
               zr.audit_status,
               zr.audit_time,
               zr.audit_by,
               zr.summary,
               zr.remark,
               zr.keywords,
               zr.extend_data,
               zrd.name as domain_name
        from zone_resource zr
                 left join zone_resource_domain zrd on zr.domain_id = zrd.id
    </sql>

    <sql id="selectZoneResourceAndIntroductionAliasVo">
        select zr.id,
               zr.introduction,
               zr.name,
               zr.file_ids,
               zr.domain_id,
               zr.sort,
               zr.is_homepage,
               zr.zone_id,
               zr.dept_id,
               zr.del_flag,
               zr.create_by,
               zr.create_time,
               zr.update_by,
               zr.update_time,
               zr.editor_type,
               zr.audit_status,
               zr.audit_time,
               zr.audit_by,
               zr.summary,
               zr.remark,
               zr.keywords,
               zr.extend_data,
               zrd.name as domain_name
        from zone_resource zr
                 left join zone_resource_domain zrd on zr.domain_id = zrd.id
    </sql>

    <sql id="selectWhere">
        <where>
            <if test="name != null  and name != ''">and zr.name like concat('%', #{name}, '%')</if>
            <if test="domainId != null ">and zr.domain_id = #{domainId}</if>
            <if test="isHomepage != null  and isHomepage != ''">and zr.is_homepage = #{isHomepage}</if>
            <if test="zoneId != null ">and zr.zone_id = #{zoneId}</if>
            <if test="deptId != null ">and zr.dept_id = #{deptId}</if>
            <if test="createBy != null and createBy != ''">and zr.create_by = #{createBy}</if>
            <if test="keywords != null and keywords != ''">and zr.keywords like concat('%', #{keywords}, '%')</if>
            <choose>
                <when test="auditStatus != null and auditStatus !=''">
                    and zr.audit_status = #{auditStatus}
                </when>
                <otherwise>
                    and zr.audit_status = '1'
                </otherwise>
            </choose>
            <if test="searchTypeId != null ">
                and zr.id in (select resource_id from zone_resource_to_type where type_id=#{searchTypeId})
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
            ORDER BY zr.sort DESC
        </where>
    </sql>

    <sql id="selectWhereBySearchVo">
        <where>
            <if test="name != null  and name != ''">and zr.name like concat('%', #{name}, '%')</if>
            <if test="domainId != null ">and zr.domain_id = #{domainId}</if>
            <if test="isHomepage != null  and isHomepage != ''">and zr.is_homepage = #{isHomepage}</if>
            <if test="zoneId != null ">and zr.zone_id = #{zoneId}</if>
            <if test="deptId != null ">and zr.dept_id = #{deptId}</if>
            <if test="createBy != null and createBy != ''">and zr.create_by = #{createBy}</if>
            <if test="keywords != null and keywords != ''">and zr.keywords like concat('%', #{keywords}, '%')</if>
            <if test="searchTypeId != null ">
                and zr.id in (select resource_id from zone_resource_to_type where type_id=#{searchTypeId})
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
            <choose>
                <when test="orderByDownloadCount != null and orderByDownloadCount">
                    order by (select sum(download_count) from sys_file_info sf where FIND_IN_SET(sf.file_id,zr.file_ids)) desc
                </when>
                <when test="orderByDownloadCount = null and orderByColumn = null and orderByColumn =''">
                    order by zr.sort DESC
                </when>
            </choose>
        </where>
    </sql>

    <select id="selectZoneResourceList" parameterType="ZoneResource" resultMap="ZoneResourceResult">
        <include refid="selectZoneResourceAliasVo"/>
        <include refid="selectWhere"/>
    </select>

    <select id="selectZoneResourceListBySearchVo"
            resultMap="ZoneResourceResult">
        <include refid="selectZoneResourceAliasVo"/>
        <include refid="selectWhereBySearchVo"/>
    </select>

    <select id="selectZoneResourceById" parameterType="Long" resultMap="ZoneResourceResult">
        <include refid="selectZoneResourceAndIntroductionAliasVo"/>
        where zr.id = #{id}
    </select>
    <select id="selectZoneResourceByNameAndZoneId" resultType="com.microservices.zone.resource.domain.ZoneResource">
        <include refid="selectZoneResourceVo"/>
        where name = #{name}
        and zone_id = #{zoneId}
    </select>
    <select id="selectZoneResourceCount" resultType="java.lang.Integer">
        select count(0)
        from zone_resource zr
        left join zone_resource_domain zrd on zr.domain_id = zrd.id
        <include refid="selectWhere"/>
    </select>
    <select id="selectAllZoneResourceList" resultMap="ZoneResourceResult">
        <include refid="selectZoneResourceAliasVo"/>
    </select>

    <insert id="insertZoneResource" parameterType="ZoneResource" useGeneratedKeys="true" keyProperty="id">
        insert into zone_resource
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="introduction != null and introduction != ''">introduction,</if>
            <if test="fileIds != null">file_ids,</if>
            <if test="domainId != null">domain_id,</if>
            <if test="sort != null">sort,</if>
            <if test="isHomepage != null">is_homepage,</if>
            <if test="zoneId != null">zone_id,</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="editorType != null">editor_type,</if>
            <if test="summary != null">summary,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="auditBy != null">audit_by,</if>
            <if test="remark != null">remark,</if>
            <if test="keywords != null">keywords,</if>
            <if test="extendData != null">extend_data,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="introduction != null and introduction != ''">#{introduction},</if>
            <if test="fileIds != null">#{fileIds},</if>
            <if test="domainId != null">#{domainId},</if>
            <if test="sort != null">#{sort},</if>
            <if test="isHomepage != null">#{isHomepage},</if>
            <if test="zoneId != null">#{zoneId},</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="editorType != null">#{editorType},</if>
            <if test="summary != null">#{summary},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="auditBy != null">#{auditBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="keywords != null">#{keywords},</if>
            <if test="extendData != null">#{extendData},</if>
        </trim>
    </insert>

    <update id="updateZoneResource" parameterType="ZoneResource">
        update zone_resource
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="introduction != null and introduction != ''">introduction = #{introduction},</if>
            <if test="fileIds != null">file_ids = #{fileIds},</if>
            <if test="domainId != null">domain_id = #{domainId},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="isHomepage != null">is_homepage = #{isHomepage},</if>
            <if test="zoneId != null">zone_id = #{zoneId},</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="summary != null">summary = #{summary},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="auditBy != null">audit_by = #{auditBy},</if>
            <if test="keywords != null">keywords = #{keywords},</if>
            <if test="extendData != null">extend_data = #{extendData},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteZoneResourceById" parameterType="Long">
        delete
        from zone_resource
        where id = #{id}
    </delete>

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