<?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.force.cancel.mapper.CancelRevokeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.force.cancel.entity.CancelRevoke">
        <id column="BIZ_ID" property="bizId"/>
        <result column="BIZ_SEQ" property="bizSeq"/>
        <result column="PRIPID" property="pripid"/>
        <result column="UNISCID" property="uniscid"/>
        <result column="REGNO" property="regno"/>
        <result column="ENTNAME" property="entname"/>
        <result column="REVOKE_REASON" property="revokeReason"/>
        <result column="REVOKE_DATE" property="revokeDate"/>
        <result column="CANCEL_REVOKE_DATE" property="cancelRevokeDate"/>
        <result column="CANCEL_REVOKE_REASON" property="cancelRevokeReason"/>
        <result column="APPROVAL_DOC_NO" property="approvalDocNo"/>
        <result column="APPROVAL_DEPT" property="approvalDept"/>
        <result column="APPROVAL_DEPT_ID" property="approvalDeptId"/>
        <result column="STATUS" property="status"/>
        <result column="CURRENT_NODE" property="currentNode"/>
        <result column="REMARKS" property="remarks"/>
        <result column="HANDLER_ID" property="handlerId"/>
        <result column="HANDLER_NAME" property="handlerName"/>
        <result column="HANDLER_DATE" property="handlerDate"/>
        <result column="CREATE_TIME" property="createTime"/>
        <result column="UPDATE_TIME" property="updateTime"/>
        <result column="AREA_CODE" property="areaCode"/>
        <result column="WORKFLOWID" property="workflowid"/>
        <result column="TASKLISTID" property="tasklistid"/>
        <result column="REVOKE_LIST_ID" property="revokeListId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        BIZ_ID, BIZ_SEQ, PRIPID, UNISCID, REGNO, ENTNAME, REVOKE_REASON, REVOKE_DATE,
        CANCEL_REVOKE_DATE, CANCEL_REVOKE_REASON, APPROVAL_DOC_NO, APPROVAL_DEPT, APPROVAL_DEPT_ID,
        STATUS, CURRENT_NODE, REMARKS, HANDLER_ID, HANDLER_NAME, HANDLER_DATE, CREATE_TIME, UPDATE_TIME,
        AREA_CODE, WORKFLOWID, TASKLISTID, REVOKE_LIST_ID
    </sql>

    <!-- 新增 -->
    <insert id="insert" parameterType="com.chinaweal.aiccs.aiccs.force.cancel.entity.CancelRevoke">
        INSERT INTO AICCS.TS_BIZ_CANCEL_REVOKE (
            BIZ_ID, BIZ_SEQ, PRIPID, UNISCID, REGNO, ENTNAME, REVOKE_REASON, REVOKE_DATE,
            CANCEL_REVOKE_DATE, CANCEL_REVOKE_REASON, APPROVAL_DOC_NO, APPROVAL_DEPT, APPROVAL_DEPT_ID,
            STATUS, CURRENT_NODE, REMARKS, HANDLER_ID, HANDLER_NAME, HANDLER_DATE, CREATE_TIME, UPDATE_TIME,
            AREA_CODE, WORKFLOWID, TASKLISTID, REVOKE_LIST_ID
        ) VALUES (
            #{bizId}, #{bizSeq}, #{pripid}, #{uniscid}, #{regno}, #{entname}, #{revokeReason}, #{revokeDate},
            #{cancelRevokeDate}, #{cancelRevokeReason}, #{approvalDocNo}, #{approvalDept}, #{approvalDeptId},
            #{status}, #{currentNode}, #{remarks}, #{handlerId}, #{handlerName}, #{handlerDate}, #{createTime}, #{updateTime},
            #{areaCode}, #{workflowid}, #{tasklistid}, #{revokeListId}
        )
    </insert>

    <!-- 更新 -->
    <update id="update" parameterType="com.chinaweal.aiccs.aiccs.force.cancel.entity.CancelRevoke">
        UPDATE AICCS.TS_BIZ_CANCEL_REVOKE
        <set>
            <if test="bizSeq != null and bizSeq != ''">BIZ_SEQ = #{bizSeq},</if>
            <if test="pripid != null and pripid != ''">PRIPID = #{pripid},</if>
            <if test="uniscid != null and uniscid != ''">UNISCID = #{uniscid},</if>
            <if test="regno != null and regno != ''">REGNO = #{regno},</if>
            <if test="entname != null and entname != ''">ENTNAME = #{entname},</if>
            <if test="revokeReason != null and revokeReason != ''">REVOKE_REASON = #{revokeReason},</if>
            <if test="revokeDate != null">REVOKE_DATE = #{revokeDate},</if>
            <if test="cancelRevokeDate != null and cancelRevokeDate != ''">CANCEL_REVOKE_DATE = #{cancelRevokeDate},</if>
            <if test="cancelRevokeReason != null and cancelRevokeReason != ''">CANCEL_REVOKE_REASON = #{cancelRevokeReason},</if>
            <if test="approvalDocNo != null and approvalDocNo != ''">APPROVAL_DOC_NO = #{approvalDocNo},</if>
            <if test="approvalDept != null and approvalDept != ''">APPROVAL_DEPT = #{approvalDept},</if>
            <if test="approvalDeptId != null and approvalDeptId != ''">APPROVAL_DEPT_ID = #{approvalDeptId},</if>
            <if test="status != null and status != ''">STATUS = #{status},</if>
            <if test="currentNode != null and currentNode != ''">CURRENT_NODE = #{currentNode},</if>
            <if test="remarks != null and remarks != ''">REMARKS = #{remarks},</if>
            <if test="handlerId != null and handlerId != ''">HANDLER_ID = #{handlerId},</if>
            <if test="handlerName != null and handlerName != ''">HANDLER_NAME = #{handlerName},</if>
            <if test="handlerDate != null">HANDLER_DATE = #{handlerDate},</if>
            <if test="areaCode != null and areaCode != ''">AREA_CODE = #{areaCode},</if>
            <if test="workflowid != null and workflowid != ''">WORKFLOWID = #{workflowid},</if>
            <if test="tasklistid != null and tasklistid != ''">TASKLISTID = #{tasklistid},</if>
            <if test="revokeListId != null and revokeListId != ''">REVOKE_LIST_ID = #{revokeListId},</if>
            UPDATE_TIME = CURRENT_TIMESTAMP
        </set>
        WHERE BIZ_ID = #{bizId}
    </update>

    <!-- 根据ID查询 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM AICCS.TS_BIZ_CANCEL_REVOKE
        WHERE BIZ_ID = #{bizId}
    </select>

    <!-- 根据业务编号查询 -->
    <select id="selectByBizSeq" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM AICCS.TS_BIZ_CANCEL_REVOKE
        WHERE BIZ_SEQ = #{bizSeq}
    </select>

    <!-- 分页查询列表 -->
    <select id="selectList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM AICCS.TS_BIZ_CANCEL_REVOKE
        <where>
            <if test="status != null and status != ''">
                AND STATUS = #{status}
            </if>
            <if test="statusIn != null and statusIn != ''">
                AND STATUS IN (${statusIn})
            </if>
            <if test="entname != null and entname != ''">
                AND ENTNAME LIKE '%' || #{entname} || '%'
            </if>
            <if test="uniscid != null and uniscid != ''">
                AND UNISCID = #{uniscid}
            </if>
            <if test="regno != null and regno != ''">
                AND REGNO = #{regno}
            </if>
            <if test="bizSeq != null and bizSeq != ''">
                AND BIZ_SEQ = #{bizSeq}
            </if>
            <if test="handlerId != null and handlerId != ''">
                AND HANDLER_ID = #{handlerId}
            </if>
        </where>
        ORDER BY CREATE_TIME DESC
    </select>

    <!-- 分页查询数量 -->
    <select id="selectCount" resultType="int">
        SELECT COUNT(1)
        FROM AICCS.TS_BIZ_CANCEL_REVOKE
        <where>
            <if test="status != null and status != ''">
                AND STATUS = #{status}
            </if>
            <if test="statusIn != null and statusIn != ''">
                AND STATUS IN (${statusIn})
            </if>
            <if test="entname != null and entname != ''">
                AND ENTNAME LIKE '%' || #{entname} || '%'
            </if>
            <if test="uniscid != null and uniscid != ''">
                AND UNISCID = #{uniscid}
            </if>
            <if test="regno != null and regno != ''">
                AND REGNO = #{regno}
            </if>
        </where>
    </select>

    <!-- 查询待审批列表 -->
    <select id="selectApproveList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM AICCS.TS_BIZ_CANCEL_REVOKE
        <where>
            STATUS IN ('2', '3', '4')
            <if test="currentNode != null and currentNode != ''">
                AND CURRENT_NODE = #{currentNode}
            </if>
            <if test="entname != null and entname != ''">
                AND ENTNAME LIKE '%' || #{entname} || '%'
            </if>
            <if test="handlerId != null and handlerId != ''">
                AND HANDLER_ID = #{handlerId}
            </if>
        </where>
        ORDER BY CREATE_TIME DESC
    </select>

    <!-- 查询待审批数量 -->
    <select id="selectApproveCount" resultType="int">
        SELECT COUNT(1)
        FROM AICCS.TS_BIZ_CANCEL_REVOKE
        WHERE STATUS IN ('2', '3', '4')
        <if test="currentNode != null and currentNode != ''">
            AND CURRENT_NODE = #{currentNode}
        </if>
    </select>

    <!-- 查询吊销主体列表（根据吊销状态查询） -->
    <select id="selectRevokedEntList" resultType="map">
        SELECT
            r.REVLISTID AS revListId,
            r.PRIPID AS pripid,
            r.UNISCID AS uniscid,
            r.REGNO AS regno,
            r.ENTNAME AS entname,
            r.REVOKEDATE AS revokeDate,
            r.REVOKEREA AS revokeReason
        FROM AICCS.REVOKELIST r
        WHERE r.STATE = '1'
        <if test="params.entname != null and params.entname != ''">
            AND r.ENTNAME LIKE '%' || #{params.entname} || '%'
        </if>
        <if test="params.uniscid != null and params.uniscid != ''">
            AND r.UNISCID = #{params.uniscid}
        </if>
        <if test="params.regno != null and params.regno != ''">
            AND r.REGNO = #{params.regno}
        </if>
        ORDER BY r.REVOKEDATE DESC
    </select>

    <!-- 分页查询吊销主体列表 -->
    <select id="selectRevokedEntPage" resultType="map">
        SELECT
            r.REVLISTID AS revListId,
            r.PRIPID AS pripid,
            r.UNISCID AS uniscid,
            r.REGNO AS regno,
            r.ENTNAME AS entname,
            r.REVOKEDATE AS revokeDate,
            r.REVOKEREA AS revokeReason
        FROM AICCS.REVOKELIST r
        WHERE r.STATE = '1'
        <if test="params.entname != null and params.entname != ''">
            AND r.ENTNAME LIKE '%' || #{params.entname} || '%'
        </if>
        <if test="params.uniscid != null and params.uniscid != ''">
            AND r.UNISCID = #{params.uniscid}
        </if>
        <if test="params.regno != null and params.regno != ''">
            AND r.REGNO = #{params.regno}
        </if>
        ORDER BY r.REVOKEDATE DESC
    </select>

    <!-- 查询吊销主体数量 -->
    <select id="selectRevokedEntCount" resultType="int">
        SELECT COUNT(1)
        FROM AICCS.REVOKELIST r
        WHERE r.STATE = '1'
        <if test="entname != null and entname != ''">
            AND r.ENTNAME LIKE '%' || #{entname} || '%'
        </if>
        <if test="uniscid != null and uniscid != ''">
            AND r.UNISCID = #{uniscid}
        </if>
        <if test="regno != null and regno != ''">
            AND r.REGNO = #{regno}
        </if>
    </select>

    <!-- 更新状态 -->
    <update id="updateStatus">
        UPDATE AICCS.TS_BIZ_CANCEL_REVOKE
        SET STATUS = #{status},
            CURRENT_NODE = #{currentNode},
            UPDATE_TIME = CURRENT_TIMESTAMP
        WHERE BIZ_ID = #{bizId}
    </update>

</mapper>
