<?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.oa.mapper.CmcResourceMapper">
    
    <resultMap type="CmcResource" id="CmcResourceResult">
        <result property="resourceId"    column="resource_id"    />
        <result property="uploader"    column="uploader"    />
        <result property="uploadDept"    column="upload_dept"    />
        <result property="title"    column="title"    />
        <result property="sourcePath"    column="source_path"    />
        <result property="type"    column="type"    />
        <result property="fieldId"    column="field_id"    />
        <result property="uploadTime"    column="upload_time"    />
        <result property="hours"    column="hours"    />
        <association property="uploadUser"    javaType="SysUser"         resultMap="UploadUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
        <association property="resourceField"    javaType="CmcResourceField"         resultMap="CmcResourceFieldResult" />
    </resultMap>

    <resultMap type="SysUser" id="UploadUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <resultMap type="SysDept" id="SysDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <resultMap type="CmcResourceField" id="CmcResourceFieldResult">
        <result property="fieldId"    column="field_id"    />
        <result property="field"    column="field"    />
    </resultMap>

    <sql id="selectCmcResourceVo">
        select r.resource_id, r.uploader, r.upload_dept, r.title, r.source_path, r.type, r.field_id, rf.field, r.upload_time, r.hours from cmc_resource as r
        left join sys_user as u on u.user_id = r.uploader
        left join sys_dept as d on d.dept_id = r.upload_dept
        left join cmc_resource_field as rf on rf.field_id = r.field_id
    </sql>

    <select id="selectCmcResourceList" parameterType="CmcResource" resultMap="CmcResourceResult">
        <include refid="selectCmcResourceVo"/>
        <where>
            <if test="uploader != null "> and r.uploader = #{uploader}</if>
            <if test="uploadDept != null "> and r.upload_dept = #{uploadDept}</if>
            <if test="title != null  and title != ''"> and r.title like concat('%', #{title}, '%')</if>
            <if test="sourcePath != null  and sourcePath != ''"> and r.source_path = #{sourcePath}</if>
            <if test="type != null  and type != ''"> and r.type = #{type}</if>
            <if test="fieldId != null "> and r.field_id = #{fieldId}</if>
            <if test="uploadTime != null "> and r.upload_time = #{uploadTime}</if>
            <if test="hours != null "> and r.hours = #{hours}</if>
        </where>
    </select>

    <select id="selectCmcResourceListBelow" parameterType="CmcResource" resultMap="CmcResourceResult">
        SELECT * FROM
        ( SELECT r.resource_id, r.uploader, r.upload_dept, r.title, r.source_path, r.type, r.field_id, tree.field, r.upload_time, r.hours
        FROM cmc_resource AS r
        LEFT JOIN sys_user AS u ON u.user_id = r.uploader
        LEFT JOIN sys_dept AS d ON d.dept_id = r.upload_dept
        RIGHT JOIN (
        SELECT rf2.field_id, rf2.field FROM
        ( SELECT rf1.field_id AS sub_field_id, rf1.parent_id AS sub_parent_id, rf0.field_id, rf0.parent_id
        FROM cmc_resource_field AS rf0
        LEFT JOIN cmc_resource_field AS rf1 ON rf1.parent_id = rf0.field_id
        WHERE rf0.field_id = #{fieldId} OR rf1.field_id = #{fieldId} ) AS rf3
        RIGHT JOIN cmc_resource_field AS rf2 ON rf2.parent_id = rf3.sub_field_id
        WHERE rf3.sub_field_id = #{fieldId} OR rf2.field_id = #{fieldId} OR rf3.field_id = #{fieldId}
        ) as tree on tree.field_id = r.field_id
        UNION
        SELECT r.resource_id, r.uploader, r.upload_dept, r.title, r.source_path, r.type, r.field_id, tree.field, r.upload_time, r.hours
        FROM cmc_resource AS r
        LEFT JOIN sys_user AS u ON u.user_id = r.uploader
        LEFT JOIN sys_dept AS d ON d.dept_id = r.upload_dept
        RIGHT JOIN (
        SELECT rf5.field_id, rf5.field FROM
        ( SELECT rf2.field_id AS sub_sub_field_id, rf2.parent_id AS sub_sub_parent_id, rf3.* FROM
        ( SELECT rf1.field_id AS sub_field_id, rf1.parent_id AS sub_parent_id, rf0.field_id, rf0.parent_id
        FROM cmc_resource_field AS rf0
        LEFT JOIN cmc_resource_field AS rf1 ON rf1.parent_id = rf0.field_id
        WHERE rf0.field_id = #{fieldId} OR rf1.field_id = #{fieldId} ) AS rf3
        RIGHT JOIN cmc_resource_field AS rf2 ON rf2.parent_id = rf3.sub_field_id
        WHERE rf3.sub_field_id = #{fieldId} OR rf2.field_id = #{fieldId} OR rf3.field_id = #{fieldId} ) AS rf4
        RIGHT JOIN cmc_resource_field AS rf5 ON rf5.parent_id = rf4.sub_sub_field_id
        WHERE rf4.sub_field_id = #{fieldId} OR rf4.field_id = #{fieldId} OR rf5.field_id = #{fieldId}
        ) as tree on tree.field_id = r.field_id
        ) AS t
        <where>
            t.resource_id is not null
            <if test="uploader != null "> and t.uploader = #{uploader}</if>
            <if test="uploadDept != null "> and t.upload_dept = #{uploadDept}</if>
            <if test="title != null  and title != ''"> and t.title like concat('%', #{title}, '%')</if>
            <if test="sourcePath != null  and sourcePath != ''"> and t.source_path = #{sourcePath}</if>
            <if test="type != null  and type != ''"> and t.type = #{type}</if>
            <if test="uploadTime != null "> and t.upload_time = #{uploadTime}</if>
            <if test="hours != null "> and t.hours = #{hours}</if>
        </where>
    </select>

    <select id="selectCmcResourceByResourceId" parameterType="Integer" resultMap="CmcResourceResult">
        <include refid="selectCmcResourceVo"/>
        where resource_id = #{resourceId}
    </select>
        
    <insert id="insertCmcResource" parameterType="CmcResource" useGeneratedKeys="true" keyProperty="resourceId">
        insert into cmc_resource
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="uploader != null">uploader,</if>
            <if test="uploadDept != null">upload_dept,</if>
            <if test="title != null">title,</if>
            <if test="sourcePath != null">source_path,</if>
            <if test="type != null">type,</if>
            <if test="fieldId != null">field_id,</if>
            <if test="uploadTime != null">upload_time,</if>
            <if test="hours != null">hours,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="uploader != null">#{uploader},</if>
            <if test="uploadDept != null">#{uploadDept},</if>
            <if test="title != null">#{title},</if>
            <if test="sourcePath != null">#{sourcePath},</if>
            <if test="type != null">#{type},</if>
            <if test="fieldId != null">#{fieldId},</if>
            <if test="uploadTime != null">#{uploadTime},</if>
            <if test="hours != null">#{hours},</if>
         </trim>
    </insert>

    <update id="updateCmcResource" parameterType="CmcResource">
        update cmc_resource
        <trim prefix="SET" suffixOverrides=",">
            <if test="uploader != null">uploader = #{uploader},</if>
            <if test="uploadDept != null">upload_dept = #{uploadDept},</if>
            <if test="title != null">title = #{title},</if>
            <if test="sourcePath != null">source_path = #{sourcePath},</if>
            <if test="type != null">type = #{type},</if>
            <if test="fieldId != null">field_id = #{fieldId},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="hours != null">hours = #{hours},</if>
        </trim>
        where resource_id = #{resourceId}
    </update>

    <delete id="deleteCmcResourceByResourceId" parameterType="Integer">
        delete from cmc_resource where resource_id = #{resourceId}
    </delete>

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