<?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.chinaweal.aiccs.aiccs.abnormal.mapper.TSAttachmentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.abnormal.entity.TSAttachment">
        <id column="ATTACHMENTID" property="attachmentid" />
        <result column="BIZSEQ" property="bizseq" />
        <result column="REMNO" property="remNo" />
        <result column="PRIPID" property="pripid" />
        <result column="FILENAME" property="filename" />
        <result column="FILEURL" property="fileurl" />
        <result column="TEMPPATH" property="temppath" />
        <result column="UPLOADDATE" property="uploaddate" />
        <result column="USERID" property="userid" />
        <result column="USERNAME" property="username" />
        <result column="FILEEXT" property="fileext" />
        <result column="FILETYPE" property="filetype" />
        <result column="AREA_CODE" property="areaCode" />
        <result column="LAUPTIME" property="lauptime" />
        <result column="ATTACHTYPE" property="attachtype" />
        <result column="CREATETYPE" property="createtype" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ATTACHMENTID, REMNO, BIZSEQ, PRIPID, FILENAME, FILEURL, TEMPPATH, UPLOADDATE, USERID, USERNAME, FILEEXT, FILETYPE, AREA_CODE, LAUPTIME, ATTACHTYPE, CREATETYPE
    </sql>

    <select id="selectttachment" resultMap="BaseResultMap" parameterType="com.chinaweal.aiccs.aiccs.abnormal.entity.TSAttachment">
        select
            <include refid="Base_Column_List"/>
        from TSAttachment
        where bizSeq = #{tsWritModel.bizseq,jdbcType=CHAR}
        <if test="tsWritModel.pripid != null and tsWritModel.pripid != ''">
            and PRIPID = #{tsWritModel.pripid,jdbcType=CHAR}
        </if>
        <if test="tsWritModel.attachtype != null and tsWritModel.attachtype != ''">
            and ATTACHTYPE = #{tsWritModel.attachtype,jdbcType=CHAR}
        </if>
    </select>

    <insert id="saveBatchWithList" parameterType="java.util.List">
        insert into tsattachment (ATTACHMENTID,CREATETYPE,BIZSEQ,PRIPID,FILEEXT,FILENAME,UPLOADDATE,LAUPTIME,ATTACHTYPE) values
        <foreach collection="list" index="index" item="item" separator=",">
            ( #{item.attachmentid,jdbcType=VARCHAR},
            #{item.createtype,jdbcType=VARCHAR},
            #{item.bizseq,jdbcType=VARCHAR},
            #{item.pripid,jdbcType=VARCHAR},
            #{item.fileext,jdbcType=VARCHAR},
            #{item.filename,jdbcType=VARCHAR},
            #{item.uploaddate,jdbcType=TIMESTAMP},
            #{item.lauptime,jdbcType=TIMESTAMP},
            #{item.attachtype,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

     <select id="selectWritsList" parameterType="com.chinaweal.aiccs.aiccs.revoke.entity.dto.AttachmentWritDto"
            resultType="com.chinaweal.aiccs.aiccs.revoke.entity.dto.AttachmentWritDto">
        select a.ATTACHMENTID,a.BIZSEQ, a.PRIPID, a.FILENAME, a.FILEURL, a.TEMPPATH,a.UPLOADDATE, a.ATTACHTYPE,
        a.CREATETYPE,b.PRE_PUN_DEC_WRITSNO,b.PRE_HEAR_NOT_WRITSNO,b.UNISCID,b.ENTNAME,b.HEARINGPUBLISHDATE,b.DECISIONPUBLISHDATE
        from TSAttachment a
        left join biz_rev_ent b on a.bizseq = b.bizseq and a.pripid = b.pripid and b.ISREMOVE = '0'
        <where>
            <if test="dto.bizseq != null and dto.bizseq != ''">
                and b.BIZSEQ = #{dto.bizseq}
            </if>
            <if test="dto.attachtype != null and dto.attachtype != ''">
                and a.ATTACHTYPE = #{dto.attachtype}
            </if>
            <if test="dto.uniscid != null and dto.uniscid != ''">
                and b.UNISCID = #{dto.uniscid}
            </if>
            <if test="dto.entname != null and dto.entname != ''">
                and b.ENTNAME like concat('%', #{dto.entname}, '%')
            </if>
        </where>
        <if test="dto.attachtype != null and dto.attachtype != ''">
            <choose>
                <when test="dto.attachtype eq '13'">
                    order by PRE_HEAR_NOT_WRITSNO asc
                </when>
                <otherwise>
                    order by PRE_PUN_DEC_WRITSNO asc
                </otherwise>
            </choose>
        </if>
    </select>

    <select id="queryFileByHistory" resultMap="BaseResultMap">
        select
        SEVIDENCECODE as attachmentid,
        SEVIDENCEFILENAME as filename,
        SEVIDENCEFILEURL as temppath,
        '3' as createtype
        from tfdb.TB_ILC_DOCUMENT_INFO where sevidencecode in (
            SELECT fileid FROM tfdb.TB_ILC_DOCUMENT_INFO_EX WHERE KEYID = #{SAPPROVECODE}
        )
    </select>

    <select id = "queryHistoryFileById" resultMap="BaseResultMap">
        select SEVIDENCECODE as attachmentid,
        SEVIDENCEFILENAME as filename,
        SEVIDENCEFILEURL as temppath
        from tfdb.TB_ILC_DOCUMENT_INFO where sevidencecode = #{sevidencecode}
    </select>

    <select id="selectWritsListzx" parameterType="com.chinaweal.aiccs.aiccs.revoke.entity.dto.AttachmentWritDto"
            resultType="com.chinaweal.aiccs.aiccs.revoke.entity.dto.AttachmentWritDto">
        select a.ATTACHMENTID,a.BIZSEQ, a.PRIPID, a.FILENAME, a.FILEURL, a.TEMPPATH,a.UPLOADDATE, a.ATTACHTYPE,
        a.CREATETYPE
        from TSAttachment a
        <where>
            <if test="dto.bizseq != null and dto.bizseq != ''">
                and a.BIZSEQ = #{dto.bizseq}
            </if>
            <if test="dto.attachtype != null and dto.attachtype != ''">
                and a.ATTACHTYPE = #{dto.attachtype}
            </if>
        </where>
    </select>
</mapper>
