<?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.WorkFileMapper">
    
    <resultMap type="WorkFile" id="WorkFileResult">
        <result property="id"    column="id"    />
        <result property="fileBelong"    column="file_belong"    />
        <result property="fileType"    column="file_type"    />
        <result property="fileName"    column="file_name"    />
        <result property="filePath"    column="file_path"    />
        <result property="fileMd5"    column="file_md5"    />
        <result property="fileSize"    column="file_size"    />
        <result property="isDel"    column="is_del"    />
        <result property="downloadNum"    column="download_num"    />
        <result property="readNum"    column="read_num"    />
        <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="deptId"   column="dept_id"/>
        <result property="deptName" column="dept_name"/>
    </resultMap>

    <sql id="selectWorkFileVo">
        select id, file_belong, file_type, file_name, file_path, file_md5, file_size, is_del, download_num, read_num, create_by, create_time, update_by, update_time, remark,dept_id,dept_name
        from ( SELECT a.*,b.dept_name
               FROM work_file a LEFT JOIN sys_dept b ON a.dept_id=b.dept_id ) work_file
    </sql>

    <select id="selectWorkFileList" parameterType="WorkFile" resultMap="WorkFileResult">
        <include refid="selectWorkFileVo"/>
        <where>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="fileBelong != null  and fileBelong != ''"> and file_belong = #{fileBelong}</if>
            <if test="deptId != null  and deptId>0"> and dept_id = #{deptId}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="params.beginMarksTime != null and params.beginMarksTime != '' and params.endMarksTime != null and params.endMarksTime != ''"> and create_time between #{params.beginMarksTime} and #{params.endMarksTime}</if>
        </where>
        order by create_time desc
    </select>

    <select id="findCountByType" parameterType="WorkFile" resultType="map">
        select file_belong as fileBelong,count(1) as total from work_file
        <where>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="deptId != null  and deptId>0"> and dept_id = #{deptId}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="params.beginMarksTime != null and params.beginMarksTime != '' and params.endMarksTime != null and params.endMarksTime != ''"> and create_time between #{params.beginMarksTime} and #{params.endMarksTime}</if>
        </where>
        group by file_belong
    </select>

    <select id="selectWorkFileListLimit20" parameterType="WorkFile" resultMap="WorkFileResult">
        <include refid="selectWorkFileVo"/>
        <where>
            and is_del = 0
            <if test="fileBelong != null  and fileBelong != ''"> and file_belong = #{fileBelong}</if>
            <if test="deptId!=null and deptId>0">and dept_id =#{deptId}</if>
        </where>
        order by create_time desc
        Limit 20
    </select>
    
    <select id="selectWorkFileById" parameterType="Long" resultMap="WorkFileResult">
        <include refid="selectWorkFileVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertWorkFile" parameterType="WorkFile" useGeneratedKeys="true" keyProperty="id">
        insert into work_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fileBelong != null">file_belong,</if>
            <if test="fileType != null">file_type,</if>
            <if test="fileName != null">file_name,</if>
            <if test="filePath != null">file_path,</if>
            <if test="fileMd5 != null">file_md5,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="isDel != null">is_del,</if>
            <if test="downloadNum != null">download_num,</if>
            <if test="readNum != null">read_num,</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>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fileBelong != null">#{fileBelong},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="fileMd5 != null">#{fileMd5},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="downloadNum != null">#{downloadNum},</if>
            <if test="readNum != null">#{readNum},</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>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateWorkFile" parameterType="WorkFile">
        update work_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileBelong != null">file_belong = #{fileBelong},</if>
            <if test="fileType != null">file_type = #{fileType},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="fileMd5 != null">file_md5 = #{fileMd5},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="downloadNum != null">download_num = #{downloadNum},</if>
            <if test="readNum != null">read_num = #{readNum},</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>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWorkFileById" parameterType="Long">
        delete from work_file where id = #{id}
    </delete>

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