<?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="net.northking.cctp.platform.db.dao.CpLogDao">


    <!-- 请在本文件内添加自定义SQL语句 -->

    <resultMap id="DetailMap" type="net.northking.cctp.platform.dto.log.CpLogDetailDto">
        <!-- 主键 -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 日志类型（1-新增 2-修改 3-删除） -->
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <!-- 所属功能 -->
        <result column="module" jdbcType="VARCHAR" property="module"/>
        <!-- 请求地址 -->
        <result column="remoteaddr" jdbcType="VARCHAR" property="remoteaddr"/>
        <!-- 逻辑删除 -->
        <result column="is_deleted" jdbcType="BIT" property="deleted"/>
        <!-- 创建人 -->
        <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 修改人 -->
        <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <!-- 修改时间 -->
        <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 日志内容 -->
        <result column="content" jdbcType="VARCHAR" property="content"/>
        <!-- 日志生成者账号 -->
        <result column="login_no" jdbcType="VARCHAR" property="loginNo"/>
        <!-- 日志生成者姓名 -->
        <result column="username" jdbcType="VARCHAR" property="username"/>
    </resultMap>

    <select id="listLog" resultMap="DetailMap" parameterType="net.northking.cctp.platform.dto.log.CpLogQueryDto">
        select p.*,user.login_no,user.user_chn_name as username from ${defaultSchema}.clt_user_login user inner join(
        select
        log.id
        ,log.type
        ,log.module
        ,log.remote_addr
        ,log.is_deleted
        ,log.created_by
        ,log.created_time
        ,log.updated_by
        ,log.updated_time
        ,log.tenant_id
        ,log_con.content
        from
        <include refid="Table_Name"/>
        log
        left join ${defaultSchema}.cp_log_content log_con
        on log.id = log_con.id
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="ids != null and ids.size()>0">
                and log.id IN
                <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="type != null">
                AND log.type = #{type,jdbcType=VARCHAR}
            </if>
            <if test="module != null">
                AND log.module = #{module,jdbcType=VARCHAR}
            </if>
            <if test="remoteAddr != null">
                AND log.remote_addr = #{remoteAddr,jdbcType=VARCHAR}
            </if>
            <if test="deleted != null">
                AND log.is_deleted = #{deleted,jdbcType=BIT}
            </if>
            <if test="tenantId != null">
                AND log.tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="content != null">
                AND locate (#{content}, log_con.content)>0
            </if>
            <if test="beginTime != null and endTime != null">
                AND log.created_time BETWEEN #{beginTime} and #{endTime}
            </if>
        </trim>
        )p on p.created_by = user.id
        <where>
        <if test="username != null">
            locate (#{username}, user.user_chn_name)>0
        </if>
        <if test="loginNo != null">
            AND locate (#{loginNo}, user.login_no)>0
        </if>
        </where>
        <choose>
            <when test="orderBy != null">
                order by ${orderBy} desc
            </when>
            <otherwise>
                order by p.created_time desc
            </otherwise>
        </choose>
    </select>

    <select id="countLog" resultType="java.lang.Integer" parameterType="net.northking.cctp.platform.dto.log.CpLogQueryDto">
        select count(p.id) from ${defaultSchema}.clt_user_login user inner join(
        select
        log.id
        ,log.type
        ,log.module
        ,log.remote_addr
        ,log.is_deleted
        ,log.created_by
        ,log.created_time
        ,log.updated_by
        ,log.updated_time
        ,log.tenant_id
        ,log_con.content
        from
        <include refid="Table_Name"/>
        log
        left join ${defaultSchema}.cp_log_content log_con
        on log.id = log_con.id
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="ids != null and ids.size()>0">
                and log.id IN
                <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="type != null">
                AND log.type = #{type,jdbcType=VARCHAR}
            </if>
            <if test="module != null">
                AND log.module = #{module,jdbcType=VARCHAR}
            </if>
            <if test="remoteAddr != null">
                AND log.remote_addr = #{remoteAddr,jdbcType=VARCHAR}
            </if>
            <if test="deleted != null">
                AND log.is_deleted = #{deleted,jdbcType=BIT}
            </if>
            <if test="tenantId != null">
                AND log.tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="beginTime != null and endTime != null">
                AND log.created_time BETWEEN #{beginTime} and #{endTime}
            </if>
            <if test="content != null">
                AND locate (#{content}, log_con.content)>0
            </if>
        </trim>
        )p on p.created_by = user.id
        <where>
            <if test="username != null">
                locate (#{username}, user.user_chn_name)>0
            </if>
            <if test="loginNo != null">
                AND locate (#{loginNo}, user.login_no)>0
            </if>
        </where>
    </select>
</mapper>