<?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.tofly.base.mapper.LogMapper">

    <resultMap id="logMap" type="com.tofly.base.entity.Log">
        <id property="id" column="ID"/>
        <result property="tableName" column="TABLE_NAME"/>
        <result property="tableId" column="TABLE_ID"/>
        <result property="routeIp" column="ROUTE_IP"/>
        <result property="type" column="TYPE"/>
        <result property="serviceId" column="SERVICE_ID"/>
        <result property="createBy" column="CREATE_BY"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="remoteAddr" column="REMOTE_ADDR"/>
        <result property="userAgent" column="USER_AGENT"/>
        <result property="requestUri" column="REQUEST_URI"/>
        <result property="method" column="METHOD"/>
        <result property="params" column="PARAMS"/>
        <result property="time" column="TIME"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="exception" column="EXCEPTION"/>
        <result property="title" column="TITLE"/>
        <result property="operatingTime" column="OPERATING_TIME"/>
    </resultMap>

    <select id="listLogPage" resultType="map" databaseId="oracle">

        select l.TABLE_ID,l.ROUTE_IP,l.TYPE,l.SERVICE_ID,l.CREATE_TIME,l.UPDATE_TIME,l.REMOTE_ADDR,l.USER_AGENT,l.REQUEST_URI,
               l.METHOD,l.PARAMS,l.TIME,l.DEL_FLAG,l.EXCEPTION,l.TITLE,
        (select REAL_NAME from TF_SMPN_USER_X where l.CREATE_BY = USERNAME) as CREATE_BY,
        (select notes from tf_smpn_code_x where key='0001' and value=l.type ) as TABLE_NAME
        from tf_smpn_log_x l where 1=1
        <if test="map.type != null and map.type.trim() != ''">
            and l.type = #{map.type}
        </if>
        <if test="map.users != null and map.users.size() >0">
            and l.CREATE_BY in
            <foreach collection="map.users" item="ids" index="index" open="(" close=")" separator=",">
                (#{ids})
            </foreach>
        </if>
        <if test="map.startDate != null and map.startDate.trim() != ''">
            and to_char(l.CREATE_TIME,'yyyy-MM-dd hh:mm:ss') >= #{map.startDate}
        </if>
        <if test="map.endDate != null and map.endDate.trim() != ''">
            <![CDATA[       and to_char(l.CREATE_TIME,'yyyy-MM-dd hh:mm:ss') <= #{map.endDate}   ]]>
        </if>
        order by l.CREATE_TIME desc
    </select>

    <select id="countLogType" resultType="com.tofly.base.vo.CountLogVo">
        SELECT m.ID userId,m.REAL_NAME userName,m.DEPARTMENT_ID deptId,m.NAME deptName, n.type,n.typeName,sum(n.total) num FROM
            (SELECT u.REAL_NAME,u.id,u.USERNAME,u.DEPARTMENT_ID,d.NAME FROM
                TF_SMPN_USER_X u LEFT JOIN TF_SMPN_DEPARTMENT_X d ON u.DEPARTMENT_ID = d.ID
             GROUP BY u.REAL_NAME,u.id,u.USERNAME,u.DEPARTMENT_ID,d.NAME)m
                LEFT JOIN
            (SELECT l.CREATE_BY userName,l.TYPE,k.CNAME typeName,COUNT(l.ID) total
             FROM
                 TF_SMPN_LOG_X l LEFT JOIN
                 (SELECT CCODE,CNAME
                  FROM
                      TF_YWPN_SYSCHILDCODE_D
                  WHERE pcid = 18) k ON l.type = k.CCODE
        <if test="map.startDate != null and map.startDate.trim() != ''">
            and to_char(l.CREATE_TIME,'yyyy-MM-dd hh:mm:ss') >= #{map.startDate}
        </if>
        <if test="map.endDate != null and map.endDate.trim() != ''">
            <![CDATA[       and to_char(l.CREATE_TIME,'yyyy-MM-dd hh:mm:ss') <= #{map.endDate}   ]]>
        </if>
             GROUP BY l.CREATE_BY,l.TYPE,k.cname
            )n
            ON m.USERNAME = n.USERNAME
        WHERE
            M .DEPARTMENT_ID is not null
          and n. TYPE is not null
          and n.typeName is not null
        GROUP BY m.ID,m.REAL_NAME,m.DEPARTMENT_ID,m.NAME,n.type,n.typeName
    </select>

    <select id="listLogPage" resultType="map" databaseId="mysql">

        select l.*,
        (select notes from TF_SMPN_CODE_X where `key`='0007' and `value`=l.type ) as type_name
        from TF_SMPN_LOG_X l where 1=1
        <if test="map.type != null and map.type.trim() != ''">
            and type = #{map.type}
        </if>
        <if test="map.startDate != null and map.startDate.trim() != ''">
            date_format(CREATE_TIME,'%Y-%m-%d %H:%i:%s') >= #{map.startDate}
        </if>
        <if test="map.endDate != null and map.endDate.trim() != ''">
            <![CDATA[       date_format(CREATE_TIME,'%Y-%m-%d %H:%i:%s') <= #{map.endDate}   ]]>
        </if>
    </select>

    <select id="getOperationRecords" resultType="com.tofly.base.vo.OperationRecordsVo">
        SELECT w1.id deptId,w1.name deptName,w2.id userId,w2.REAL_NAME realName,w2.SERVICE_ID serviceId,w2.SERVICE_NAME serviceName,w2.NUM num,w2.operatingTime
        FROM
            ((SELECT id,name
              FROM TF_SMPN_DEPARTMENT_X) w1 LEFT JOIN
            (SELECT q1.id,Q1.REAL_NAME,Q1.DEPARTMENT_ID,Q2.SERVICE_ID,Q2.SERVICE_NAME,Q2.NUM,Q2.operatingTime
             FROM
                 (SELECT id,USERNAME,REAL_NAME,DEPARTMENT_ID
                  FROM TF_SMPN_USER_X)q1 LEFT JOIN
                 (SELECT  t1.SERVICE_ID,t1.SERVICE_NAME,T2.CREATE_BY,T2.NUM,T2.operatingTime
                  FROM
                      (SELECT SERVICE_ID,SERVICE_NAME
                       FROM TF_YWPN_OPERATING_CONTROLS_D) t1 LEFT JOIN
                      (SELECT SERVICE_ID,CREATE_BY,count(REQUEST_URI) num,sum(OPERATING_TIME) operatingTime
                       FROM TF_SMPN_LOG_X
                       where CREATE_TIME between #{startTime}  and #{endTime}
                       GROUP BY SERVICE_ID,CREATE_BY) t2
                      on t1.SERVICE_Id = t2.SERVICE_id) q2
                 on q1.USERNAME = q2.CREATE_BY) w2
            on w1.id = W2.DEPARTMENT_ID)
    </select>

    <select id="getOneLogInfo" resultType="com.tofly.base.entity.Log">
        SELECT * FROM
            (SELECT *
             FROM TF_SMPN_LOG_X
             WHERE SERVICE_ID = 'tofly-auth'
        <if test="map.username != null and map.username.trim() != ''">
            and CREATE_BY = #{username}
        </if>
               and METHOD = 'POST'
               and EXCEPTION is NULL
             ORDER BY create_time desc)
        WHERE rownum = 1
    </select>
</mapper>
