<?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.jeethink.receive.mapper.ArchivesMapper">
    <sql id="selectArchives">
        select a.archives_id, a.name, a.archive_no, a.type, a.people_type, a.tier, a.classified, a.duration, a.if_paper, a.gd_people_id, a.now_status,
               a.user_id, u.nick_name, u.user_num as user_number, d.dept_id, d.dept_name, a.create_user, a.create_time, a.update_user, su.nick_name as updater_name, a.update_time,a.warehouse_type
        from archives_master a
        left join sys_user u on a.user_id = u.user_id
        left join sys_dept d on u.dept_id = d.dept_id
        left join sys_user su on a.update_user = u.user_id
        where a.deleted = '0'
    </sql>
    <insert id="insertArchives" parameterType="ArchivesMaster">
        insert into archives_master (archives_id, name, archive_no, type, people_type, tier, classified, duration, now_status, if_paper, gd_people_id, user_id, create_time, update_time, create_user, update_user,warehouse_type)
            VALUES (#{archivesId}, #{name}, #{archiveNo}, #{type}, #{peopleType}, #{tier}, #{classified}, #{duration}, #{nowStatus}, #{ifPaper}, #{gdPeopleId}, #{userId}, now(), now(), #{createUser}, #{updateUser}, #{warehouseType})
    </insert>
    <update id="updateArchive" parameterType="ArchivesMaster">
        update archives_master set duration = #{duration}, now_status = #{nowStatus}, if_paper = #{ifPaper}, update_user = #{updateUser}, update_time = now(),warehouse_type = #{warehouseType}
        where archives_id = #{archivesId}
    </update>
    <update id="changeNowStatus">
        update archives_master set now_status = #{nowStatus} where archives_id = #{archivesId}
    </update>
    <update id="romoveById">
        update archives_master set deleted = '1' where archives_id = #{0}
    </update>
    <update id="postponeArchives" parameterType="ArchivesMaster">
        update archives_master set duration = #{duration}, update_user = #{updateUser}, update_time = now() where archives_id = #{archivesId}
    </update>
    <select id="selectById" resultType="ArchivesMaster">
        <include refid="selectArchives"/>
        and a.archives_id = #{0}
    </select>
    <select id="selectArchivesList" parameterType="ArchivesMaster" resultType="ArchivesMaster">
        <include refid="selectArchives"/>
        <if test="stage == 'receive'"> and a.now_status in ('0', '1', '2', '3', '9', '10') </if>
        <if test="stage == 'destroy'"> and a.now_status in ('31', '32', '33', '39') </if>
        <if test="type != null and type != ''"> and a.type = #{type} </if>
        <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>
        <if test="archiveNo != null  and archiveNo != ''"> and a.archive_no like concat('%', #{archiveNo}, '%')</if>
        <if test="peopleType != null  and peopleType != ''"> and a.people_type = #{peopleType}</if>
        <if test="userId != null  and userId != ''"> and a.user_id = #{userId}</if>
        <if test="deptId != null  and deptId != '' and deptId != 1"> and (d.dept_id = #{deptId} or d.parent_id=#{deptId})</if>
        <if test="nowStatus != null and nowStatus != ''"> and a.now_status = #{nowStatus} </if>
        order by update_time desc,u.user_num
    </select>
    <select id="selectExpireArchivesList" resultType="ArchivesMaster">
        select a.archives_id, a.name, a.archive_no, a.type, a.people_type, a.tier, a.classified, a.duration, a.if_paper, a.gd_people_id, a.now_status,
            a.user_id, u.nick_name, a.create_user, a.create_time, a.update_user, a.update_time, ae.expire
        from archives_master a
        left join (
            select archives_id,
               (case when duration = '1' then DATEDIFF(DATE_ADD(update_time, INTERVAL  30 year ), NOW())
                when duration = '2' then  DATEDIFF(DATE_ADD(update_time, INTERVAL  20 year ), NOW())
                when duration = '3' then DATEDIFF(DATE_ADD(update_time, INTERVAL  10 year ), NOW())
                else 500 end) as expire
            from archives_master
        ) ae on a.archives_id = ae.archives_id
        left join sys_user u on a.user_id = u.user_id
        where a.deleted = '0' and a.now_status = '10' and ae.expire &lt;= 30
        <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>
        <if test="peopleType != null  and peopleType != ''"> and a.people_type = #{peopleType}</if>
        <if test="userId != null  and userId != ''"> and a.user_id = #{userId}</if>
        order by ae.expire
    </select>
    <select id="selectArchivesByUser" resultType="com.jeethink.bean.domain.ArchivesMaster">
        select a.archives_id, name, archive_no, type, people_type, tier, classified, duration, if_paper, gd_people_id, a.user_id,
               a.create_time, a.update_time, create_user, update_user, deleted, now_status
        from archives_master a left join sys_user u on a.user_id = u.user_id where a.type = #{type} and u.user_num = #{userNumber}
    </select>

    <select id="selectJk" parameterType="String" resultType="ArchivesMaster">
        SELECT am.* from archives_master am
                             LEFT JOIN sys_user u on u.user_id=am.user_id
        WHERE u.user_num=#{userNum} and type='1'
    </select>
</mapper>