<?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.TSBizRemListMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.abnormal.entity.TSBizRemList">
        <id column="REMLISTID" property="remlistid" />
        <result column="BIZSEQ" property="bizseq" />
        <result column="ABNLISTID" property="abnlistid" />
        <result column="PRIPID" property="pripid" />
        <result column="REGNO" property="regno" />
        <result column="ENTNAME" property="entname" />
        <result column="SPECAUSE" property="specause" />
        <result column="ABNTIME" property="abntime" />
        <result column="SPEREASONS" property="spereasons" />
        <result column="REMEXCPRES" property="remexcpres" />
        <result column="REMREASONS" property="remreasons" />
        <result column="APPROVALDATE" property="approvaldate" />
        <result column="DECORG" property="decorg" />
        <result column="DECORGNAME" property="decorgName" />
        <result column="AREA_CODE" property="areaCode" />
        <result column="LAUPTIME" property="lauptime" />
        <result column="ReconsiderationOrg" property="reconsiderationOrg" />
        <result column="GovernmentOrg" property="governmentOrg" />
        <result column="LitiOrg" property="litiOrg" />
        <result column="remSponsor" property="remSponsor" />
        <result column="remSponsorID" property="remSponsorID" />
        <result column="remRemark" property="remRemark" />
        <result column="status" property="status" />
        <result column="applicant" property="applicant" />
        <result column="applicantNo" property="applicantNo" />
        <result column="TYPE" property="type" />
        <result column="handleStatus" property="handleStatus" />
        <result column="backProposal" property="backProposal" />
        <result column="applyTime" property="applyTime" />
        <result column="isAgreeRepair" property="isAgreeRepair" />
        <result column="isAccept" property="isAccept" />
        <result column="appOrPc" property="appOrPc" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        REMLISTID, BIZSEQ, ABNLISTID, PRIPID, REGNO, ENTNAME, SPECAUSE, ABNTIME, SPEREASONS, REMEXCPRES, REMREASONS, APPROVALDATE,
        DECORG, DECORGNAME, AREA_CODE, LAUPTIME, ReconsiderationOrg, GovernmentOrg, LitiOrg, remSponsor, remSponsorID, remRemark, status,applicant,applicantNo, TYPE,
        handleStatus, backProposal, applyTime, isAgreeRepair, isAccept,appOrPc
    </sql>

    <select id="selectbizRembizseq" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from TSBizRemList where bizseq = #{bizseq}
    </select>

    <select id="selectBRMexptlist" parameterType="com.chinaweal.aiccs.aiccs.abnormal.entity.TSBizRemList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from TSBizRemList
        <where>
            <if test=" tsBizRemListModel.pripid != null and tsBizRemListModel.pripid != ''">
                and pripid = #{tsBizRemListModel.pripid}
            </if>
            <if test=" tsBizRemListModel.bizseq != null and tsBizRemListModel.bizseq != ''">
                and bizseq = #{tsBizRemListModel.bizseq}
            </if>
            <if test=" tsBizRemListModel.abnlistid != null and tsBizRemListModel.abnlistid != ''">
                and abnlistid = #{tsBizRemListModel.abnlistid}
            </if>
        </where>
    </select>

    <select id="selectbizRemByPagelist" parameterType="com.chinaweal.aiccs.aiccs.abnormal.entity.TSBizRemList"
            resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from TSBizRemList
        <where>
            <if test="bizseq != null and bizSeq != ''">
                and bizseq = #{bizseq}
            </if>
        </where>
    </select>

    <select id="selectbizRemByPriPid"  parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from TSBizRemList
        <where>
            <if test="params.priPid != null and params.priPid != ''">
                and pripid = #{params.priPid}
            </if>
        </where>
        order by abntime desc
    </select>

    <select id="selectbizRemByPriPidAndBIZSEQ"  parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from TSBizRemList
        <where>
            <if test="params.priPid != null and params.priPid != ''">
                and pripid = #{params.priPid}
            </if>
            and BIZSEQ is not null
            and BIZSEQ != ''
            and handlestatus != '5'
        </where>
        order by abntime desc
    </select>

    <select id="selectbizRem"  parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from TSBizRemList
        <where>
            <if test="params.priPid != null and params.priPid != ''">
                and pripid = #{params.priPid}
            </if>
            <if test="params.bizseq != null and params.bizseq != ''">
                and BIZSEQ = #{params.bizseq}
            </if>
        </where>
        order by abntime desc
    </select>

    <select id="queryByxyh" resultMap="BaseResultMap">
        select * from tsbizremlist where remSponsorID = #{remSponsorID} and applytime &lt;= '2021-10-20 11:05:00'
    </select>

    <insert id="insertIdTobak" parameterType="String">
        insert into tasklistid_bak value (#{tasklistid})
    </insert>

    <select id="checkTaskList" resultMap="BaseResultMap">
        select * from aiccs.tsbizremlist where BIZSEQ is not null and bizseq != '' and ABNLISTID = #{ABNLISTID} and handleStatus not in ('4','5')
    </select>

    <select id="queryLerep" resultType="String">
        select eb.lerep from aiccs.tsbizremlist ts left join aiccs.e_baseinfo eb on ts.pripid = eb.pripid
        where bizseq = #{bizseq}
    </select>

    <select id="queryHistoryInfo" resultMap="BaseResultMap">
        select
        tsa.SAPPROVECODE as bizseq,
        tsa.SAUTOID as abnlistid,
        tsae.sentid as pripid,
        tsae.sregno as regno,
        tsae.sentname as entname,
        tsa.sreasontypename as spereasons,
        tsa.SREASONCONTENT as remreasons,
        tsa.DTINPUTTIME as applyTime
        from tfdb.tb_shs_approve tsa
        left join tfdb.tb_shs_approve_entity tsae on tsa.sapprovecode = tsae.sapprovecode
        where tsa.SAPPROVECODE = #{SAPPROVECODE}
    </select>

    <select id="queryHistoryContent" resultType="java.lang.String">
        select SCONTENT from tfdb.tb_shs_decision tsd where SAPPROVECODE = #{SAPPROVECODE}
    </select>

    <select id="queryxyxfCqYzfwList" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.vo.XyxfCqDto">
        WITH FilteredTask AS (
        SELECT
        a.ACCEPTNO,
        a.BUSNAME,
        a.bustype,
        a.CREATETIME,
        a.LAUPTIME,
        a.DEADLINEDATE,
        a.WORKFLOWID
        FROM aiccs.TSTaskList a
        WHERE a.DEADLINEDATE IS NOT NULL
        AND a.busstatus = '2'
        AND a.bustype = '33'
        AND a.LAUPTIME > a.DEADLINEDATE
        <if test="paramMap.startIntoDate != null and paramMap.startIntoDate != '' ">
            AND a.CREATETIME >= #{paramMap.startIntoDate}
        </if>
        <if test="paramMap.endIntoDate != null and paramMap.endIntoDate != '' ">
            AND a.CREATETIME &lt; #{paramMap.endIntoDate}
        </if>
        ),
        RankedData AS (
        SELECT
        ft.ACCEPTNO AS "ACCEPTNO",
        ft.BUSNAME AS "BUSNAME",
        eb.UNISCID AS "UNISCID",
        eb.regno AS "regno",
        eb.regulatory_name_temp AS "regulatoryNameTemp",
        abl.DECORGCN AS "DECORGCN",
        abl.REMDECORGCN AS "REMDECORGCN",
        ft.bustype AS "bustype",
        op.HANDLER AS "HANDLER",
        ft.CREATETIME AS "CREATETIME",
        ft.LAUPTIME AS "LAUPTIME",
        ft.DEADLINEDATE AS "DEADLINEDATE",
        bc.name AS "name",
        ROW_NUMBER() OVER(PARTITION BY ft.ACCEPTNO ORDER BY ft.CREATETIME DESC) AS rn
        FROM FilteredTask ft
        INNER JOIN aiccs.e_baseinfo eb
        ON ft.BUSNAME = eb.ENTNAME
        INNER JOIN aiccs.tsopinion op
        ON ft.WORKFLOWID = op.WORKFLOWID
        AND op.OPINIONTYPE = '0'
        LEFT JOIN aiccs.tsserillegallist abl
        ON eb.ENTNAME = abl.ENTNAME
        LEFT JOIN CXAICORG.T_BASECODE bc
        ON bc.CODE = SUBSTRING(eb.suporg, 1, 6)
        WHERE abl.DECORG LIKE #{paramMap.regorg}
        )
        SELECT
        "ACCEPTNO",
        "BUSNAME",
        "UNISCID",
        "regno",
        "regulatoryNameTemp",
        "DECORGCN",
        "REMDECORGCN",
        "bustype",
        "HANDLER",
        "CREATETIME",
        "LAUPTIME",
        "DEADLINEDATE",
        "name"
        FROM RankedData
        WHERE rn = 1
        ORDER BY SUBSTRING("name", 1, 6)
    </select>


    <select id="queryxyxfCqList" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.vo.XyxfCqDto">
        WITH FilteredTask AS (
        SELECT
        a.ACCEPTNO,
        a.BUSNAME,
        a.bustype,
        a.CREATETIME,
        a.LAUPTIME,
        a.DEADLINEDATE,
        a.WORKFLOWID
        FROM aiccs.TSTaskList a
        WHERE a.DEADLINEDATE IS NOT NULL
        AND a.busstatus = '2'
        AND a.LAUPTIME > a.DEADLINEDATE
        <if test="paramMap.startIntoDate != null and paramMap.startIntoDate != '' ">
            AND a.CREATETIME >= #{paramMap.startIntoDate}
        </if>
        <if test="paramMap.endIntoDate != null and paramMap.endIntoDate != '' ">
            AND a.CREATETIME &lt; #{paramMap.endIntoDate}
        </if>
        ),
        RankedData AS (
        SELECT
        ft.ACCEPTNO AS "ACCEPTNO",
        ft.BUSNAME AS "BUSNAME",
        eb.UNISCID AS "UNISCID",
        eb.regno AS "regno",
        eb.regulatory_name_temp AS "regulatoryNameTemp",
        abl.intoDecorg AS "DECORGCN",
        abl.REDECORG AS "REMDECORGCN",
        ft.bustype AS "bustype",
        op.HANDLER AS "HANDLER",
        ft.CREATETIME AS "CREATETIME",
        ft.LAUPTIME AS "LAUPTIME",
        ft.DEADLINEDATE AS "DEADLINEDATE",
        bc.name AS "name",
        ROW_NUMBER() OVER(PARTITION BY ft.ACCEPTNO ORDER BY ft.CREATETIME DESC) AS rn
        FROM FilteredTask ft
        INNER JOIN aiccs.e_baseinfo eb
        ON ft.BUSNAME = eb.ENTNAME
        INNER JOIN aiccs.tsopinion op
        ON ft.WORKFLOWID = op.WORKFLOWID
        AND op.OPINIONTYPE = '0'
        LEFT JOIN aiccs.tsabnlist abl
        ON eb.ENTNAME = abl.ENTNAME
        LEFT JOIN CXAICORG.T_BASECODE bc
        ON bc.CODE = SUBSTRING(eb.suporg, 1, 6)
        WHERE abl.DECORG LIKE #{paramMap.regorg}
        )
        SELECT
        "ACCEPTNO",
        "BUSNAME",
        "UNISCID",
        "regno",
        "regulatoryNameTemp",
        "DECORGCN",
        "REMDECORGCN",
        "bustype",
        "HANDLER",
        "CREATETIME",
        "LAUPTIME",
        "DEADLINEDATE",
        "name"
        FROM RankedData
        WHERE rn = 1
        ORDER BY SUBSTRING("name", 1, 6)
    </select>


    <select id="exportqueryxyxfCqYzfwList" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.vo.XyxfCqDto">
        WITH FilteredTask AS (
        SELECT
        a.ACCEPTNO,
        a.BUSNAME,
        a.bustype,
        a.CREATETIME,
        a.LAUPTIME,
        a.DEADLINEDATE,
        a.WORKFLOWID
        FROM aiccs.TSTaskList a
        WHERE a.DEADLINEDATE IS NOT NULL
        AND a.busstatus = '2'
        AND a.bustype = '33'
        AND a.LAUPTIME > a.DEADLINEDATE
        <if test="paramMap.startIntoDate != null and paramMap.startIntoDate != '' ">
            AND a.CREATETIME >= #{paramMap.startIntoDate}
        </if>
        <if test="paramMap.endIntoDate != null and paramMap.endIntoDate != '' ">
            AND a.CREATETIME &lt; #{paramMap.endIntoDate}
        </if>
        ),
        RankedData AS (
        SELECT
        ft.ACCEPTNO AS "ACCEPTNO",
        ft.BUSNAME AS "BUSNAME",
        eb.UNISCID AS "UNISCID",
        eb.regno AS "regno",
        eb.regulatory_name_temp AS "regulatoryNameTemp",
        abl.DECORGCN AS "DECORGCN",
        abl.REMDECORGCN AS "REMDECORGCN",
        ft.bustype AS "bustype",
        op.HANDLER AS "HANDLER",
        ft.CREATETIME AS "CREATETIME",
        ft.LAUPTIME AS "LAUPTIME",
        ft.DEADLINEDATE AS "DEADLINEDATE",
        bc.name AS "name",
        ROW_NUMBER() OVER(PARTITION BY ft.ACCEPTNO ORDER BY ft.CREATETIME DESC) AS rn
        FROM FilteredTask ft
        INNER JOIN aiccs.e_baseinfo eb
        ON ft.BUSNAME = eb.ENTNAME
        INNER JOIN aiccs.tsopinion op
        ON ft.WORKFLOWID = op.WORKFLOWID
        AND op.OPINIONTYPE = '0'
        LEFT JOIN aiccs.tsserillegallist abl
        ON eb.ENTNAME = abl.ENTNAME
        LEFT JOIN CXAICORG.T_BASECODE bc
        ON bc.CODE = SUBSTRING(eb.suporg, 1, 6)
        WHERE abl.DECORG LIKE #{paramMap.regorg}
        )
        SELECT
        "ACCEPTNO",
        "BUSNAME",
        "UNISCID",
        "regno",
        "regulatoryNameTemp",
        "DECORGCN",
        "REMDECORGCN",
        "bustype",
        "HANDLER",
        "CREATETIME",
        "LAUPTIME",
        "DEADLINEDATE",
        "name"
        FROM RankedData
        WHERE rn = 1
        ORDER BY SUBSTRING("name", 1, 6)
    </select>


    <select id="exportqueryxyxfCqList" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.vo.XyxfCqDto">
        WITH FilteredTask AS (
        SELECT
        a.ACCEPTNO,
        a.BUSNAME,
        a.bustype,
        a.CREATETIME,
        a.LAUPTIME,
        a.DEADLINEDATE,
        a.WORKFLOWID
        FROM aiccs.TSTaskList a
        WHERE a.DEADLINEDATE IS NOT NULL
        AND a.busstatus = '2'
        AND a.LAUPTIME > a.DEADLINEDATE
        <if test="paramMap.startIntoDate != null and paramMap.startIntoDate != '' ">
            AND a.CREATETIME >= #{paramMap.startIntoDate}
        </if>
        <if test="paramMap.endIntoDate != null and paramMap.endIntoDate != '' ">
            AND a.CREATETIME &lt; #{paramMap.endIntoDate}
        </if>
        ),
        RankedData AS (
        SELECT
        ft.ACCEPTNO AS "ACCEPTNO",
        ft.BUSNAME AS "BUSNAME",
        eb.UNISCID AS "UNISCID",
        eb.regno AS "regno",
        eb.regulatory_name_temp AS "regulatoryNameTemp",
        abl.intoDecorg AS "DECORGCN",
        abl.REDECORG AS "REMDECORGCN",
        ft.bustype AS "bustype",
        op.HANDLER AS "HANDLER",
        ft.CREATETIME AS "CREATETIME",
        ft.LAUPTIME AS "LAUPTIME",
        ft.DEADLINEDATE AS "DEADLINEDATE",
        bc.name AS "name",
        ROW_NUMBER() OVER(PARTITION BY ft.ACCEPTNO ORDER BY ft.CREATETIME DESC) AS rn
        FROM FilteredTask ft
        INNER JOIN aiccs.e_baseinfo eb
        ON ft.BUSNAME = eb.ENTNAME
        INNER JOIN aiccs.tsopinion op
        ON ft.WORKFLOWID = op.WORKFLOWID
        AND op.OPINIONTYPE = '0'
        LEFT JOIN aiccs.tsabnlist abl
        ON eb.ENTNAME = abl.ENTNAME
        LEFT JOIN CXAICORG.T_BASECODE bc
        ON bc.CODE = SUBSTRING(eb.suporg, 1, 6)
        WHERE abl.DECORG LIKE #{paramMap.regorg}
        )
        SELECT
        "ACCEPTNO",
        "BUSNAME",
        "UNISCID",
        "regno",
        "regulatoryNameTemp",
        "DECORGCN",
        "REMDECORGCN",
        "bustype",
        "HANDLER",
        "CREATETIME",
        "LAUPTIME",
        "DEADLINEDATE",
        "name"
        FROM RankedData
        WHERE rn = 1
        ORDER BY SUBSTRING("name", 1, 6)
    </select>
</mapper>
