<?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.nbport.zgwl.contractlog.mapper.ContractLogMapper">

    <resultMap id="ContractLogResult" type="com.nbport.zgwl.contractlog.dto.ContractLogDto">
        <id property="id" column="ID"/>
        <result property="contractId" column="CONTRACT_ID"/>
        <result property="contractCode" column="CONTRACT_CODE"/>
        <result property="contractName" column="CONTRACT_NAME"/>
        <result property="contractStatus" column="CONTRACT_STATUS"/>
        <result property="actionType" column="ACTION_TYPE"/>
        <result property="actionName" column="ACTION_NAME"/>
        <result property="actionStatus" column="ACTION_STATUS"/>
        <result property="operatorName" column="OPERATOR_NAME"/>
        <result property="operatorPhone" column="OPERATOR_PHONE"/>
        <result property="actionTime" column="ACTION_TIME"/>
        <result property="actionContent" column="ACTION_CONTENT"/>
        <result property="callbackPayload" column="CALLBACK_PAYLOAD"/>
        <result property="remark" column="REMARK"/>
    </resultMap>

    <sql id="logQuerySql">
        from SEAL_CONTRACT_LAUNCH_LOG log
        left join SEAL_CONTRACT_LAUNCH_MAIN main on main.ID = log.CONTRACT_ID
        <where>
            <if test="query.contractId != null">
                and log.CONTRACT_ID = #{query.contractId}
            </if>
            <if test="query.contractCode != null and query.contractCode != ''">
                and main.CONTRACT_CODE like CONCAT(CONCAT('%', #{query.contractCode}), '%')
            </if>
            <if test="query.contractName != null and query.contractName != ''">
                and main.CONTRACT_NAME like CONCAT(CONCAT('%', #{query.contractName}), '%')
            </if>
            <if test="query.operatorName != null and query.operatorName != ''">
                and log.OPERATOR_NAME like CONCAT(CONCAT('%', #{query.operatorName}), '%')
            </if>
            <if test="query.actionType != null and query.actionType != ''">
                and log.ACTION_TYPE = #{query.actionType}
            </if>
            <if test="query.actionStatus != null and query.actionStatus != ''">
                and log.ACTION_STATUS = #{query.actionStatus}
            </if>
            <if test="query.dateStart != null and query.dateStart != ''">
                and log.ACTION_TIME >= TO_DATE(#{query.dateStart} || ' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
            </if>
            <if test="query.dateEnd != null and query.dateEnd != ''">
                and log.ACTION_TIME &lt;= TO_DATE(#{query.dateEnd} || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
            </if>
        </where>
    </sql>

    <select id="selectLogPage" resultMap="ContractLogResult">
        select log.ID, log.CONTRACT_ID, main.CONTRACT_CODE, main.CONTRACT_NAME, main.STATUS as CONTRACT_STATUS,
               log.ACTION_TYPE, log.ACTION_NAME, log.ACTION_STATUS, log.OPERATOR_NAME, log.OPERATOR_PHONE,
               log.ACTION_TIME, log.ACTION_CONTENT, log.CALLBACK_PAYLOAD, log.REMARK
        <include refid="logQuerySql"/>
        order by log.ACTION_TIME desc, log.ID desc
    </select>

    <select id="selectLogList" resultMap="ContractLogResult">
        select log.ID, log.CONTRACT_ID, main.CONTRACT_CODE, main.CONTRACT_NAME, main.STATUS as CONTRACT_STATUS,
               log.ACTION_TYPE, log.ACTION_NAME, log.ACTION_STATUS, log.OPERATOR_NAME, log.OPERATOR_PHONE,
               log.ACTION_TIME, log.ACTION_CONTENT, log.CALLBACK_PAYLOAD, log.REMARK
        <include refid="logQuerySql"/>
        order by log.ACTION_TIME desc, log.ID desc
    </select>

</mapper>
