<?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.yanzhu.jh.work.mapper.WorkFileUserMapper">
    
    <resultMap type="WorkFileUser" id="WorkFileUserResult">
        <result property="id"    column="id"    />
        <result property="fileId"    column="file_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="isDel"    column="is_del"    />
        <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="remark"    column="remark"    />
        <result property="deptType"    column="deptType"    />
        <result property="deptTypeName"    column="deptTypeName"    />
        <result property="deptName"    column="deptName"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="nickName"    column="nickName"    />
        <result property="userName"    column="userName"    />
        <result property="num"    column="num"    />
    </resultMap>

    <resultMap id="WorkFileUserWorkFileUserLogResult" type="WorkFileUser" extends="WorkFileUserResult">
        <collection property="workFileUserLogList" notNullColumn="sub_id" javaType="java.util.List" resultMap="WorkFileUserLogResult" />
    </resultMap>

    <resultMap type="WorkFileUserLog" id="WorkFileUserLogResult">
        <result property="id"    column="sub_id"    />
        <result property="fileUserId"    column="sub_file_user_id"    />
        <result property="time"    column="sub_time"    />
        <result property="isDel"    column="sub_is_del"    />
    </resultMap>

    <sql id="selectWorkFileUserVo">
        select id, file_id, project_id, dept_id, user_id, is_del, create_by, create_time, update_by, update_time, remark from work_file_user
    </sql>

    <select id="selectWorkFileUserList" parameterType="WorkFileUser" resultMap="WorkFileUserResult">
        select wfu.id, wfu.file_id, wfu.project_id, wfu.dept_id, sd.type_flag as deptType, sdd.dict_label as deptTypeName, sd.dept_name as deptName, sd.ancestors, su.user_name as userName, su.nick_name as nickName, wfu.user_id, wfu.is_del, wfu.create_by, wfu.create_time, wfu.update_by, wfu.update_time, wfu.remark from work_file_user wfu
        left join sys_user su on su.user_id = wfu.user_id
        left join sys_dept sd on sd.dept_id = wfu.dept_id
        left join sys_dict_data sdd on sdd.dict_type='sys_dept_type' and sdd.dict_value=sd.type_flag
        <where>
            wfu.is_del=0
            <if test="fileId != null "> and wfu.file_id = #{fileId}</if>
            <if test="deptType != null "> and sd.type_flag = #{deptType}</if>
            <if test="projectId != null "> and wfu.project_id = #{projectId}</if>
            <if test="deptId != null "> and wfu.dept_id = #{deptId}</if>
            <if test="userId != null "> and wfu.user_id = #{userId}</if>
        </where>
        order by wfu.create_by desc
    </select>

    <select id="queryCountByDeptType" parameterType="WorkFileUser" resultType="map">
        select sd.type_flag as deptType, sdd.dict_label as deptTypeName, count(1) as total from work_file_user wfu
        left join sys_user su on su.user_id = wfu.user_id
        left join sys_dept sd on sd.dept_id = wfu.dept_id
        left join sys_dict_data sdd on sdd.dict_type='sys_dept_type' and sdd.dict_value=sd.type_flag
        <where>
            wfu.is_del=0
            <if test="fileId != null "> and wfu.file_id = #{fileId}</if>
        </where>
        group by sd.type_flag
    </select>
    
    <select id="selectWorkFileUserById" parameterType="Long" resultMap="WorkFileUserWorkFileUserLogResult">
        select a.id, a.file_id, a.project_id, a.dept_id, a.user_id, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
 b.id as sub_id, b.file_user_id as sub_file_user_id, b.time as sub_time, b.is_del as sub_is_del
        from work_file_user a
        left join work_file_user_log b on b.file_user_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertWorkFileUser" parameterType="WorkFileUser" useGeneratedKeys="true" keyProperty="id">
        insert into work_file_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fileId != null">file_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="isDel != null">is_del,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fileId != null">#{fileId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="isDel != null">#{isDel},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateWorkFileUser" parameterType="WorkFileUser">
        update work_file_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileId != null">file_id = #{fileId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="isDel != null">is_del = #{isDel},</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="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWorkFileUserById" parameterType="Long">
        delete from work_file_user where id = #{id}
    </delete>

    <delete id="deleteWorkFileUserByIds" parameterType="String">
        delete from work_file_user where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteWorkFileUserLogByFileUserIds" parameterType="String">
        delete from work_file_user_log where file_user_id in 
        <foreach item="fileUserId" collection="array" open="(" separator="," close=")">
            #{fileUserId}
        </foreach>
    </delete>

    <delete id="deleteWorkFileUserLogByFileUserId" parameterType="Long">
        delete from work_file_user_log where file_user_id = #{fileUserId}
    </delete>

    <insert id="batchWorkFileUserLog">
        insert into work_file_user_log( id, file_user_id, time, is_del) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.fileUserId}, #{item.time}, #{item.isDel})
        </foreach>
    </insert>
</mapper>