<?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.yunfeiyun.agmp.common.web.system.mapper.SysResMapper">

    <resultMap type="SysRes" id="SysResResult">
        <result property="resId"    column="resId"    />
        <result property="resBusId"    column="resBusId"    />
        <result property="resMediatype"    column="resMediatype"    />
        <result property="resBiztype"    column="resBiztype"    />
        <result property="resUrl"    column="resUrl"    />
        <result property="resOssUrl"    column="resOssUrl"    />
        <result property="resSeq"    column="resSeq"    />
        <result property="resCreator"    column="resCreator"    />
        <result property="resCreateddate"    column="resCreateddate"    />
        <result property="resCreateddate"    column="resCreateddate"    />
        <result property="resFormtype"    column="resFormtype"    />
        <result property="resLocalUrl"    column="resLocalUrl"    />
    </resultMap>

    <sql id="selectSysResVo">
        select
        resId,
        resBusId,
        resMediatype,
        resBiztype,
        resUrl as resLocalUrl,
        ${@com.yunfeiyun.agmp.common.constant.ResConstants$formType@QUERY.getValue()} as resFormtype,
        <choose>
            <when test="@com.yunfeiyun.agmp.common.framework.web.service.PolicyService@enabledOssCloudPolicy()==true">
                case when isNull(resOssUrl)  or resOssUrl = '' then resUrl else resOssUrl end as resUrl,
                case when !isNull(resOssUrl) and resOssUrl != '' then resOssUrl else resUrl end as resOssUrl,
            </when>
            <when test="@com.yunfeiyun.agmp.common.framework.web.service.PolicyService@enabledOssCloudPolicy()==false">
                resUrl, resUrl as resOssUrl,
            </when>
        </choose>
        resSeq,
        resCreateddate,
        resCreator
        from SysRes
    </sql>

    <select id="selectSysResList" parameterType="SysRes" resultMap="SysResResult">
         select r.resId,r.resBusId, r.resMediatype, r.resBiztype, r.resUrl, r.resSeq, r.resCreateddate, r.resCreator , u.userName as  resCreatorname from SysRes  r
         left join SysUser u on r.resCreator = u.userId
        <where>
            <if test="resId != null  and resId != ''"> and r.resId = #{resId}</if>
            <if test="resBusId != null  and resBusId != ''"> and r.resBusId like concat('%', #{resBusId}, '%')</if>
            <if test="resMediatype != null  and resMediatype != ''"> and r.resMediatype = #{resMediatype}</if>
            <if test="resBiztype != null  and resBiztype != ''"> and r.resBiztype = #{resBiztype}</if>
            <if test="resUrl != null  and resUrl != ''"> and r.resUrl = #{resUrl}</if>
            <if test="resOssUrl != null  and resOssUrl != ''"> and r.resOssUrl = #{resOssUrl}</if>
            <if test="resSeq != null "> and r.resSeq = #{resSeq}</if>
            <if test="resCreateddate != null  and resCreateddate != ''"> and r.resCreateddate = #{resCreateddate}</if>
            <if test="resCreator != null  and resCreator != ''"> and r.resCreator = #{resCreator}</if>
        </where>
    </select>

    <select id="selectSysResByResId" parameterType="String" resultMap="SysResResult">
        <include refid="selectSysResVo"/>
        where resId = #{resId}
    </select>

    <select id="selectCountResByBusId" parameterType="SysRes" resultType="Integer">
	    select count(1) from SysRes where resBusId=#{resBusId} and resBiztype = #{resBiztype}
	</select>

    <select id="selectResByResIds" parameterType="java.util.List" resultMap="SysResResult">
        select
        resId,
        resBusId,
        resMediatype,
        resBiztype,
        resUrl as resLocalUrl,
        ${@com.yunfeiyun.agmp.common.constant.ResConstants$formType@QUERY.getValue()} as resFormtype,
        <choose>
            <when test="@com.yunfeiyun.agmp.common.framework.web.service.PolicyService@enabledOssCloudPolicy()==true">
                case when isNull(resOssUrl)  or resOssUrl = '' then resUrl else resOssUrl end as resUrl,
                case when !isNull(resOssUrl) and resOssUrl != '' then resOssUrl else resUrl end as resOssUrl,
            </when>
            <when test="@com.yunfeiyun.agmp.common.framework.web.service.PolicyService@enabledOssCloudPolicy()==false">
                resUrl, resUrl as resOssUrl,
            </when>
        </choose>
        resSeq,
        resCreateddate,
        resCreator
        from SysRes where resId in
        <foreach item="resId" collection="list" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </select>

    <select id="selectResByResIdes" parameterType="String" resultMap="SysResResult">
        select
        resId,
        resBusId,
        resMediatype,
        resBiztype,
        resUrl as resLocalUrl,
        ${@com.yunfeiyun.agmp.common.constant.ResConstants$formType@QUERY.getValue()} as resFormtype,
        <choose>
            <when test="@com.yunfeiyun.agmp.common.framework.web.service.PolicyService@enabledOssCloudPolicy()==true">
                case when isNull(resOssUrl)  or resOssUrl = '' then resUrl else resOssUrl end as resUrl,
                case when !isNull(resOssUrl) and resOssUrl != '' then resOssUrl else resUrl end as resOssUrl,
            </when>
            <when test="@com.yunfeiyun.agmp.common.framework.web.service.PolicyService@enabledOssCloudPolicy()==false">
                resUrl, resUrl as resOssUrl,
            </when>
        </choose>
        resSeq,
        resCreateddate,
        resCreator
        from SysRes where resId in
        <foreach item="resId" collection="array" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </select>

    <select id="selectResByBusId" resultMap="SysResResult">
        <include refid="selectSysResVo"/>
        where resBusId = #{resBusId}
        <if test="resBiztype != null and resBiztype != ''">
            and resBiztype = #{resBiztype}
        </if>
        <if test="resMediatype != null and resMediatype != ''">
            and resMediatype = #{resMediatype}
        </if>
    </select>


    <select id="selectResByBusIds"  resultMap="SysResResult">
        <include refid="selectSysResVo"/>
        where resBusId in
        <foreach item="resBusId"  index="index" collection="resBusids" open="(" separator="," close=")">
            #{resBusId}
        </foreach>
        <if test="resBiztype != null and resBiztype != ''">
            and resBiztype = #{resBiztype}
        </if>
    </select>

    <select id="selectResByBusIdes"   resultMap="SysResResult">
        <include refid="selectSysResVo"/>
        where resBusId in
        <foreach item="resBusId"  index="index" collection="resBusids" open="(" separator="," close=")">
            #{resBusId}
        </foreach>
        <if test="resBiztype != null and resBiztype != ''">
            and resBiztype = #{resBiztype}
        </if>
    </select>

    <select id="selectSysResExpire" resultMap="SysResResult">
        <include refid="selectSysResVo"/>
        where  (resBusId = null OR resBusId = '' ) AND
         DATEDIFF(NOW(), str_to_date(resCreateddate, '%Y-%m-%d %H:%i:%s') ) > 3;
    </select>

    <insert id="insertSysRes" parameterType="SysRes">
        insert into SysRes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="resId != null">resId,</if>
            <if test="resBusId != null">resBusId,</if>
            <if test="resMediatype != null">resMediatype,</if>
            <if test="resBiztype != null">resBiztype,</if>
            <if test="resUrl != null">resUrl,</if>
            <if test="resOssUrl != null">resOssUrl,</if>
            <if test="resSeq != null">resSeq,</if>
            <if test="resCreateddate != null">resCreateddate,</if>
            <if test="resCreator != null">resCreator,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="resId != null">#{resId},</if>
            <if test="resBusId != null">#{resBusId},</if>
            <if test="resMediatype != null">#{resMediatype},</if>
            <if test="resBiztype != null">#{resBiztype},</if>
            <if test="resUrl != null">#{resUrl},</if>
            <if test="resOssUrl != null">#{resOssUrl},</if>
            <if test="resSeq != null">#{resSeq},</if>
            <if test="resCreateddate != null">#{resCreateddate},</if>
            <if test="resCreator != null">#{resCreator},</if>
         </trim>
    </insert>

    <!--    批量插入资源-->
    <insert id="insertSysReses" parameterType="java.util.List">
        insert into SysRes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="resId != null">resId,</if>
            <if test="resBusId != null">resBusId,</if>
            <if test="resMediatype != null">resMediatype,</if>
            <if test="resBiztype != null">resBiztype,</if>
            <if test="resUrl != null">resUrl,</if>
            <if test="resOssUrl != null">resOssUrl,</if>
            <if test="resSeq != null">resSeq,</if>
            <if test="resCreateddate != null">resCreateddate,</if>
            <if test="resCreator != null">resCreator,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <foreach collection='sysRes' item='item' separator=','>
                <if test="resId != null">#{resId},</if>
                <if test="resBusId != null">#{resBusId},</if>
                <if test="resMediatype != null">#{resMediatype},</if>
                <if test="resBiztype != null">#{resBiztype},</if>
                <if test="resUrl != null">#{resUrl},</if>
                <if test="resOssUrl != null">#{resOssUrl},</if>
                <if test="resSeq != null">#{resSeq},</if>
                <if test="resCreateddate != null">#{resCreateddate},</if>
                <if test="resCreator != null">#{resCreator},</if>
            </foreach>
        </trim>
    </insert>



    <update id="updateSysRes" parameterType="SysRes">
        update SysRes
        <trim prefix="SET" suffixOverrides=",">
            <if test="resBusId != null">resBusId = #{resBusId},</if>
            <if test="resMediatype != null">resMediatype = #{resMediatype},</if>
            <if test="resBiztype != null">resBiztype = #{resBiztype},</if>
            <if test="resUrl != null">resUrl = #{resUrl},</if>
            <if test="resOssUrl != null">resOssUrl = #{resOssUrl},</if>
            <if test="resSeq != null">resSeq = #{resSeq},</if>
            <if test="resCreateddate != null">resCreateddate = #{resCreateddate},</if>
            <if test="resCreator != null">resCreator = #{resCreator},</if>
        </trim>
        where resId = #{resId}  LIMIT  1
    </update>

    <update id="updateSysResV2" parameterType="SysRes">
        update SysRes
        <trim prefix="SET" suffixOverrides=",">
            <if test="resBusId != null">resBusId = #{resBusId},</if>
            <if test="resUrl != null">resUrl = #{resUrl},</if>
        </trim>
        where resId = #{resId}
        <if test="resBiztype != null">
            and resBiztype = #{resBiztype}
        </if>
        LIMIT  1
    </update>

    <update id="updateResByBusId" parameterType="SysRes">
        update SysRes
        <trim prefix="SET" suffixOverrides=",">
            <if test="resBusId != null">resBusId = #{resBusId},</if>
            <if test="resMediatype != null">resMediatype = #{resMediatype},</if>
            <if test="resBiztype != null">resBiztype = #{resBiztype},</if>
            <if test="resUrl != null">resUrl = #{resUrl},</if>
            <if test="resOssUrl != null">resOssUrl = #{resOssUrl},</if>
            <if test="resSeq != null">resSeq = #{resSeq},</if>
            <if test="resCreateddate != null">resCreateddate = #{resCreateddate},</if>
            <if test="resCreator != null">resCreator = #{resCreator},</if>
        </trim>
        where resBusId = #{resBusId}  LIMIT  1
	</update>

    <update id="updateResBusIdByResIds" parameterType="hashMap">
        update SysRes set resBusId = #{resBusId} where resId in
        <foreach item="resId" collection="list" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </update>

    <update id="updateResBusIdByResId" parameterType="String">
        update SysRes set resBusId = #{resBusId} where resId = #{resId}  LIMIT  1
    </update>

    <delete id="deleteSysResByResId" parameterType="String">
        delete from SysRes where resId = #{resId}
    </delete>

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

    <!--    批量更新资源-->
    <update id="updateSysResByResIds" parameterType="java.util.List" >
        update SysRes
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="resBusId = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.resBusId != null">
                        when resId = #{item.resId} then #{item.resBusId}
                    </if>
                </foreach>
            </trim>

            <trim prefix="resMediatype = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.resMediatype != null">
                        when resId = #{item.resId} then #{item.resMediatype}
                    </if>
                </foreach>
            </trim>

            <trim prefix="resBiztype = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.resBiztype != null">
                        when resId = #{item.resId} then #{item.resBiztype}
                    </if>
                </foreach>
            </trim>

            <trim prefix="resUrl = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.resUrl != null">
                        when resId = #{item.resId} then #{item.resUrl}
                    </if>
                </foreach>
            </trim>

            <trim prefix="resSeq = case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.resSeq != null">
                        when resId = #{item.resId} then #{item.resSeq}
                    </if>
                </foreach>
            </trim>
        </trim>
        where resId in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.resId}
        </foreach>
    </update>

    <!--    批量更新资源V2 -->
    <update id="updateSysResByResIdls" >
         update SysRes set resBusId =  #{resBusId} where  resId in
        <foreach collection="resIds" item="resId" index="index" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </update>
    <!--    批量更新资源V2 -->
    <update id="updateSysResByResIdes" >
        update SysRes set resBusId =  #{resBusId} where  resId in
        <foreach collection="resIds" index="index" item="resId" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </update>



    <!--    批量删除资源 -->
    <delete id="deleteSysReses" >
        delete from SysRes where resId in
        <foreach   collection="resIds" item="resId" index="index" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </delete>

    <!--    批量删除资源 -->
    <delete id="deleteSysResls" >
        delete from SysRes where resId in
        <foreach   collection="resIds" item="resId" index="index" open="(" separator="," close=")">
            #{resId}
        </foreach>
    </delete>

    <!--    批量删除资源 -->
    <delete id="deleteSysResbes" >
        delete from SysRes where resBusId in
        <foreach   collection="resBusIds" item="resBusId" index="index" open="(" separator="," close=")">
            #{resBusId}
        </foreach>
    </delete>

    <!--    批量删除资源 -->
    <delete id="deleteSysResbls" >
        delete from SysRes where resBusId in
        <foreach   collection="resBusIds" item="resBusId" index="index" open="(" separator="," close=")">
            #{resBusId}
        </foreach>
    </delete>


    <!--    批量删除资源 -->
    <delete id="deleteSysResess" >
        delete from SysRes where resId in
        <foreach   collection="sysReses" item="sysRes" index="index" open="(" separator="," close=")">
            #{sysRes.resId}
        </foreach>
    </delete>

    <!--    批量删除资源 -->
    <delete id="deleteSysReslss" >
        delete from SysRes where resId in
        <foreach   collection="sysResls" item="sysRes" index="index" open="(" separator="," close=")">
            #{sysRes.resId}
        </foreach>
    </delete>


    <!--    批量清理资源 -->
    <delete id="cleanSysRes" parameterType="java.util.List">
        delete from SysRes where resId in
        <foreach   collection="list" item="sysRes" index="index" open="(" separator="," close=")">
            #{sysRes.resId}
        </foreach>
        and (resBusId = null OR resBusId = '' OR resBusId = 'null')
    </delete>

    <!--    根据资源id批量查询资源-->
    <select id="selectSysResListByResId" parameterType="java.util.List" resultMap="SysResResult">
        <include refid="selectSysResVo"/>
        <where>
            <if test="list != null and list.size() > 0">
                AND resId in
                <foreach collection="list" item="resId" open="(" separator="," close=")">
                    #{resId}
                </foreach>
            </if>
        </where>
    </select>
</mapper>