<?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.gz.mapper.archive.ArchiveMapper">

    <select id="searchList" resultType="com.gz.rvo.archive.ArchiveRVO"
            parameterType="com.gz.vo.archive.SearchArchiveVO">
        SELECT
        d.dict_name mj_text,
        a.*
        FROM `tab_archives` a
        LEFT JOIN tab_dict d
        ON a.mj = d.dict_code
        LEFT JOIN tab_secondary_archive sa
        ON a.id = sa.archive_id
        LEFT JOIN tab_admin u on u.id = a.created
        <where>
            a.deleted = 0
            and ml IN (
            SELECT
            `code`
            FROM
            tab_archives_tree atr
            LEFT JOIN tab_menu_role tmr ON atr.id = tmr.menu_id
            WHERE
            tmr.role_id = #{roleId}
            AND tmr.role_type = 1
            )
            and mj in (select d.dict_code from tab_dict d left join tab_menu_role mr on d.id = mr.menu_id where
            mr.role_id = #{roleId} and mr.role_type = 2 )

            <if test="deptId!=null and deptId!=''">
            and u.dept_id = #{deptId}
            </if>

            <if test="dh != null and dh != ''">
                AND a.dh like concat(concat('%',#{dh}),'%')
            </if>
            <if test="mlh != null and mlh != ''">
                AND a.mlh = #{mlh}
            </if>
            <if test="wjbh != null and wjbh != ''">
                AND (a.wjbh like concat(concat('%',#{wjbh}),'%') OR sa.wh like concat(concat('%',#{wjbh}),'%'))
            </if>
            <if test="ml != null and ml != ''">
                AND (a.ml = #{ml} or a.ml in (select `code` FROM tab_archives_tree where parent_id = (SELECT id FROM `tab_archives_tree` where code = #{ml})))
            </if>
            <if test="bgqx != null and bgqx != ''">
                <choose>
                    <when test="bgqx == 'D10' or bgqx == '005'">
                        AND a.bgqx in ('D10','005')
                    </when>
                    <when test="bgqx == 'D30' or bgqx == '004'">
                        AND a.bgqx in ('D30','004')
                    </when>
                    <when test="bgqx == 'Y'.toString() or bgqx == '001'">
                        AND a.bgqx in ('Y','001')
                    </when>
                    <otherwise>
                        AND a.bgqx = #{bgqx}
                    </otherwise>
                </choose>
            </if>
            <if test="mj != null and mj != ''">
                AND a.mj = #{mj}
            </if>
            <if test="gdnd != null and gdnd != ''">
                AND a.gdnd = #{gdnd}
            </if>
            <if test="ztc != null and ztc != ''">
                AND (a.ztc like concat(concat('%',#{ztc}),'%') OR a.tm like concat(concat('%',#{ztc}),'%') OR sa.tm like concat(concat('%',#{ztc}),'%'))
            </if>
            <if test="sql != null and sql != ''">
                ${sql}
            </if>
        </where>
        group by a.id
        <if test="field != null and field != '' and order != null and order != ''">
            order by ${field} ${order}
        </if>

    </select>


    <select id="againSearchList" resultType="com.gz.rvo.archive.ArchiveRVO"
            parameterType="com.gz.vo.archive.SearchArchiveVO">
        SELECT
        a.*
        FROM (${searchSql}) a
        <where>
            <if test="dh != null and dh != ''">
                AND a.dh like concat(concat('%',#{dh}),'%')
            </if>
            <if test="mlh != null and mlh != ''">
                AND a.mlh = #{mlh}
            </if>
            <if test="wjbh != null and wjbh != ''">
                AND (a.wjbh like concat(concat('%',#{wjbh}),'%') OR sa.wh like concat(concat('%',#{wjbh}),'%'))
            </if>
            <if test="ml != null and ml != ''">
                AND a.ml = #{ml} or a.ml in (select `code` FROM tab_archives_tree where parent_id = (SELECT id FROM `tab_archives_tree` where code = #{ml}))
            </if>
            <if test="bgqx != null and bgqx != ''">
                AND a.bgqx = #{bgqx}
            </if>
            <if test="gdnd != null and gdnd != ''">
                AND a.gdnd = #{gdnd}
            </if>
            <if test="mj != null and mj != ''">
                AND a.mj = #{mj}
            </if>
            <if test="ztc != null and ztc != ''">
                AND (a.ztc like concat(concat('%',#{ztc}),'%') OR a.tm like concat(concat('%',#{ztc}),'%'))
            </if>
            <if test="sql != null and sql != ''">
                ${sql}
            </if>
        </where>
        <if test="field != null and field != '' and order != null and order != ''">
            order by ${field} ${order}
        </if>
    </select>


    <select id="selectBorrowList" resultType="com.gz.rvo.borrow.ArchiveBorrowListRVO" parameterType="int">
        <![CDATA[
        SELECT IF
                   (
                           (SELECT count(admin_id) num
                            FROM tab_archive_borrow ab
                            WHERE ab.archive_id = a.id
                              AND ab.borrow_type = 1
                              AND ab.audit_status = 2
                              AND ab.return_status = 0) <= 0,
                           '可借阅',
                           '已借阅'
                   ) original_status,
               a.*
        FROM tab_archives a
        WHERE a.deleted = 0
          and a.ml IN (
            SELECT `code`
            FROM tab_archives_tree atr
                     LEFT JOIN tab_menu_role tmr ON atr.id = tmr.menu_id
            WHERE tmr.role_id = #{roleId}
              AND tmr.role_type = 1
        )
          and a.mj in (select d.dict_code
                       from tab_dict d
                                left join tab_menu_role mr on d.id = mr.menu_id
                       where mr.role_id = #{roleId}
                         and mr.role_type = 2)
        ]]>
    </select>


    <select id="selectMaxJH" resultType="java.lang.Integer" parameterType="string">
        SELECT max(jh) + 1
        from tab_archives
        where deleted = 0
          and ml = #{mlCode}
    </select>

    <select id="selectRoleAll" resultType="com.gz.dto.archive.ArchiveDTO" parameterType="int">
        SELECT *
        FROM tab_archives
        WHERE deleted = 0
          and ml IN (
            SELECT `code`
            FROM tab_archives_tree atr
                     LEFT JOIN tab_menu_role tmr ON atr.id = tmr.menu_id
            WHERE tmr.role_id = #{roleId}
              AND tmr.role_type = 1
        )
          and mj in (select d.dict_code
                     from tab_dict d
                              left join tab_menu_role mr on d.id = mr.menu_id
                     where mr.role_id = #{roleId}
                       and mr.role_type = 2)
        order by create_time desc
    </select>

    <select id="selectByRolePrimaryKey" resultType="com.gz.dto.archive.ArchiveDTO" parameterType="int">
        SELECT *
        FROM tab_archives
        WHERE deleted = 0
          and id = #{id}
          and ml IN (
            SELECT `code`
            FROM tab_archives_tree atr
                     LEFT JOIN tab_menu_role tmr ON atr.id = tmr.menu_id
            WHERE tmr.role_id = #{roleId}
              AND tmr.role_type = 1
        )
          and mj in (select d.dict_code
                     from tab_dict d
                              left join tab_menu_role mr on d.id = mr.menu_id
                     where mr.role_id = #{roleId}
                       and mr.role_type = 2)
        order by create_time desc
    </select>

    <select id="selectByPrimaryKeyText" resultType="com.gz.rvo.archive.ArchiveRVO">
        SELECT IF
                   (
                       atr.parent_id = - 1,
                       atr.title,
                       concat(
                               concat((SELECT c.title FROM tab_archives_tree c WHERE c.id = atr.parent_id), '-'),
                               atr.title
                           )
                   )        mlText,
               d.dict_name  mjText,
               d1.dict_name bgqxText,
               a.*
        FROM tab_archives a
                 LEFT JOIN tab_archives_tree atr ON a.ml = atr.CODE
                 left join tab_dict d on a.mj = d.dict_code
                 left join tab_dict d1 on a.bgqx = d1.dict_code
        where a.deleted = 0
          and d.deleted = 0
          and atr.deleted = 0
          and a.id = #{id}
    </select>

    <select id="selectByPk" resultType="com.gz.rvo.archive.ArchiveRVO" parameterType="int">
        SELECT
            d.dict_name mj_text,
            d1.dict_name bgqx_text,
            d2.title ml_Text,
            a.*
        FROM `tab_archives` a
         LEFT JOIN tab_dict d ON a.mj = d.dict_code
         LEFT JOIN tab_dict d1 ON a.bgqx = d1.dict_code
         LEFT JOIN tab_archives_tree d2 ON a.ml = d2.code
        where a.deleted = 0
        and a.id = #{id}
    </select>


    <select id="selectByDh"  resultType="com.gz.rvo.archive.ArchiveRVO" parameterType="string">
        SELECT
            d.dict_name mj_text,
            d1.dict_name bgqx_text,
            d2.title ml_Text,
            a.*
        FROM `tab_archives` a
                 LEFT JOIN tab_dict d ON a.mj = d.dict_code
                 LEFT JOIN tab_dict d1 ON a.bgqx = d1.dict_code
                 LEFT JOIN tab_archives_tree d2 ON a.ml = d2.code
        where a.deleted = 0
          and a.dh = #{dh}
    </select>

    <select id="getMenuId" resultType="int">
        select menu_id from tab_menu_role where role_id = #{id} and role_type = 4
    </select>

    <select id="getDeptId" resultType="string">
        select dept_id from tab_admin where id=#{id}
    </select>
</mapper>
