<?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.dc.mapper.InterfaceLogMapper">

    <resultMap id="interfaceLogMap" type="com.tofly.dc.entity.InterfaceLog">
        <id property="id" column="ID"/>
        <result property="interfaceId" column="INTERFACE_ID"/>
        <result property="accessTime" column="ACCESS_TIME"/>
        <result property="accessUserId" column="ACCESS_USER_ID"/>
        <result property="operationLength" column="OPERATION_LENGTH"/>
        <result property="state" column="STATE"/>
        <result property="errorCode" column="ERROR_CODE"/>
        <result property="errorNote" column="ERROR_NOTE"/>
        <association property="accessUserName" column="ACCESS_USER_ID" select="getRealName"/>
        <association property="stateName" column="STATE" select="getstateName"/>
    </resultMap>

    <select id="getstateName" resultType="string">
        SELECT CNAME
        FROM TF_YWPN_SYSCHILDCODE_D
        WHERE PCID = 50500
          AND CCODE = #{state}
    </select>
    <select id="getRealName" resultType="string" databaseId="oracle">
        select REAL_NAME createUserName
        from TF_SMPN_USER_X
        where id = #{createUser}
    </select>

    <resultMap id="interfaceLogVoMap" type="com.tofly.dc.entity.vo.InterfaceLogVo">
        <id property="id" column="ID"/>
        <result property="interfaceId" column="INTERFACE_ID"/>
        <result property="accessTime" column="ACCESS_TIME"/>
        <result property="accessUserId" column="ACCESS_USER_ID"/>
        <result property="operationLength" column="OPERATION_LENGTH"/>
        <result property="state" column="STATE"/>
        <result property="code" column="CODE"/>
        <result property="name" column="NAME"/>
        <result property="note" column="NOTE"/>
        <result property="ownershipSystem" column="OWNERSHIP_SYSTEM"/>
        <result property="ownershipBusiness" column="OWNERSHIP_BUSINESS"/>
        <result property="url" column="URL"/>
        <result property="runningStatus" column="RUNNING_STATUS"/>
        <result property="del" column="DEL"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="type" column="TYPE"/>
        <result property="contentType" column="CONTENT_TYPE"/>
        <result property="errorCode" column="ERROR_CODE"/>
        <result property="errorNote" column="ERROR_NOTE"/>
        <association property="accessUserName" column="ACCESS_USER_ID" select="getRealName"/>
        <association property="stateName" column="STATE" select="getstateName"/>
        <association property="ownershipSystemName" column="OWNERSHIP_SYSTEM"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipSystemName"/>
        <association property="ownershipBusinessName" column="OWNERSHIP_BUSINESS"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipBusinessName"/>
        <association property="runningStatusName" column="RUNNING_STATUS"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getRunningStatusName"/>
        <association property="delName" column="DEL" select="com.tofly.dc.mapper.InterfaceManageMapper.getDelName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="getRealName"/>
        <association property="typeName" column="TYPE" select="com.tofly.dc.mapper.InterfaceManageMapper.getTypeName"/>
        <association property="contentTypeName" column="CONTENT_TYPE"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getContentTypeName"/>
    </resultMap>

    <select id="getInterfaceLogVoList" resultMap="interfaceLogVoMap">
        select
        t1.*,t2.CODE,t2.NAME,t2.NOTE,t2.OWNERSHIP_SYSTEM,t2.OWNERSHIP_BUSINESS,t2.URL,t2.RUNNING_STATUS,t2.DEL,t2.CREATE_TIME,t2.CREATE_USER,t2.TYPE,t2.CONTENT_TYPE
        FROM TF_INTERFACE_LOG t1 left join TF_INTERFACE_MANAGE t2
        on t1.INTERFACE_ID = t2.ID
        WHERE '1' = '1'
        <if test="log.accessUserId !=null">
            and t1.ACCESS_USER_ID =#{log.accessUserId}
        </if>
        <if test="log.state !=null and log.state.trim() != ''">
            and t1.STATE =#{log.state}
        </if>
        <if test="log.ownershipSystem !=null and log.ownershipSystem.trim() != ''">
            and t2.OWNERSHIP_SYSTEM =#{log.ownershipSystem}
        </if>
        <if test="log.ownershipBusiness !=null and log.ownershipBusiness.trim() != ''">
            and t2.OWNERSHIP_BUSINESS =#{log.ownershipBusiness}
        </if>
        <if test="log.runningStatus !=null and log.runningStatus.trim() != ''">
            and t2.RUNNING_STATUS =#{log.runningStatus}
        </if>
        <if test="log.del !=null and log.del.trim() != ''">
            and t2.DEL =#{log.del}
        </if>
        <if test="log.type !=null and log.type.trim() != ''">
            and t2.TYPE =#{log.type}
        </if>
        <if test="log.contentType !=null and log.contentType.trim() != ''">
            and t2.CONTENT_TYPE =#{log.contentType}
        </if>
        ORDER BY t1.ACCESS_TIME desc
    </select>
    <resultMap id="sysMonitoringVoMap" type="com.tofly.dc.entity.vo.SysMonitoringVo">
        <result property="ownershipSystem" column="OWNERSHIP_SYSTEM"/>
        <result property="totalNum" column="totalNum"/>
        <result property="totalTime" column="totalTime"/>
        <result property="errorNum" column="errorNum"/>
        <result property="successfulNum" column="successfulNum"/>
        <association property="ownershipSystemName" column="OWNERSHIP_SYSTEM"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipSystemName"/>
    </resultMap>
    <select id="getSysMonitoringVoList" resultMap="sysMonitoringVoMap">
        SELECT OWNERSHIP_SYSTEM, totalNum, totalTime, errorNum, (totalNum - errorNum) successfulNum
        FROM (
        SELECT T2.OWNERSHIP_SYSTEM,
        count(1) totalNum,
        sum(T1.OPERATION_LENGTH) totalTime,
        count(T1.ERROR_CODE) errorNum
        FROM TF_INTERFACE_LOG t1
        LEFT JOIN TF_INTERFACE_MANAGE t2
        on t1.INTERFACE_ID = t2.ID
        where 1=1
        <if test="dto.sysId !=null and dto.sysId.trim() != ''">
            and t2.OWNERSHIP_SYSTEM =#{dto.sysId}
        </if>
        <if test="dto.startTime != null and dto.startTime.trim() != ''">
            and to_char(t1.ACCESS_TIME,'yyyy-MM-dd hh:mm:ss') >= #{dto.startTime}
        </if>
        <if test="dto.endTime != null and dto.endTime.trim() != ''">
            <![CDATA[ and to_char(t1.ACCESS_TIME,'yyyy-MM-dd hh:mm:ss')  <= #{dto.endTime}]]>
        </if>
        GROUP BY t2.OWNERSHIP_SYSTEM
        )
    </select>
    <resultMap id="businessMonitoringVoMap" type="com.tofly.dc.entity.vo.BusinessMonitoringVo">
        <result property="ownershipSystem" column="OWNERSHIP_SYSTEM"/>
        <result property="ownershipBusiness" column="OWNERSHIP_BUSINESS"/>
        <result property="totalNum" column="totalNum"/>
        <result property="totalTime" column="totalTime"/>
        <result property="errorNum" column="errorNum"/>
        <result property="successfulNum" column="successfulNum"/>
        <association property="ownershipSystemName" column="OWNERSHIP_SYSTEM"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipSystemName"/>
        <association property="ownershipBusinessName" column="OWNERSHIP_BUSINESS"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipBusinessName"/>
    </resultMap>
    <select id="getBusinessMonitoringVoList" resultMap="businessMonitoringVoMap">
        SELECT OWNERSHIP_SYSTEM,OWNERSHIP_BUSINESS, totalNum, totalTime, errorNum, (totalNum - errorNum) successfulNum
        FROM (
        SELECT T2.OWNERSHIP_SYSTEM,t2.OWNERSHIP_BUSINESS,
        count(1) totalNum,
        sum(T1.OPERATION_LENGTH) totalTime,
        count(T1.ERROR_CODE) errorNum
        FROM TF_INTERFACE_LOG t1
        LEFT JOIN TF_INTERFACE_MANAGE t2
        on t1.INTERFACE_ID = t2.ID
        where 1=1
        <if test="dto.sysId !=null and dto.sysId.trim() != ''">
            and t2.OWNERSHIP_SYSTEM =#{dto.sysId}
        </if>
        <if test="dto.businessId !=null and dto.businessId.trim() != ''">
            and t2.OWNERSHIP_BUSINESS =#{dto.businessId}
        </if>
        <if test="dto.startTime != null and dto.startTime.trim() != ''">
            and to_char(t1.ACCESS_TIME,'yyyy-MM-dd hh:mm:ss') >= #{dto.startTime}
        </if>
        <if test="dto.endTime != null and dto.endTime.trim() != ''">
            <![CDATA[ and to_char(t1.ACCESS_TIME,'yyyy-MM-dd hh:mm:ss')  <= #{dto.endTime}]]>
        </if>
        GROUP BY t2.OWNERSHIP_SYSTEM,t2.OWNERSHIP_BUSINESS
        )
    </select>

    <resultMap id="interfaceMonitoringVoMap" type="com.tofly.dc.entity.vo.InterfaceMonitoringVo">
        <result property="name" column="name"/>
        <result property="url" column="url"/>
        <result property="type" column="type"/>
        <result property="ownershipSystem" column="OWNERSHIP_SYSTEM"/>
        <result property="ownershipBusiness" column="OWNERSHIP_BUSINESS"/>
        <result property="totalNum" column="totalNum"/>
        <result property="totalTime" column="totalTime"/>
        <result property="errorNum" column="errorNum"/>
        <result property="successfulNum" column="successfulNum"/>
        <association property="ownershipSystemName" column="OWNERSHIP_SYSTEM"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipSystemName"/>
        <association property="ownershipBusinessName" column="OWNERSHIP_BUSINESS"
                     select="com.tofly.dc.mapper.InterfaceManageMapper.getOwnershipBusinessName"/>
        <association property="typeName" column="TYPE" select="com.tofly.dc.mapper.InterfaceManageMapper.getTypeName"/>
    </resultMap>
    <select id="getInterfaceMonitoringVoList" resultMap="interfaceMonitoringVoMap">
    SELECT  a.* ,(a.totalNum-a.errorNum) as successfulNum
        FROM (
        SELECT t1.INTERFACE_ID,
               t2.NAME                  name,
               t2.URL                   url,
               t2.type                  type,
               t2.OWNERSHIP_SYSTEM,
               t2.OWNERSHIP_BUSINESS,
               count(1)                 totalNum,
               sum(T1.OPERATION_LENGTH) totalTime,
               count(T1.ERROR_CODE)     errorNum
        FROM TF_INTERFACE_LOG t1
                 LEFT JOIN TF_INTERFACE_MANAGE t2
                           on t1.INTERFACE_ID = t2.ID
        where 1=1
        <if test="dto.sysId !=null and dto.sysId.trim() != ''">
            and t2.OWNERSHIP_SYSTEM =#{dto.sysId}
        </if>
        <if test="dto.businessId !=null and dto.businessId.trim() != ''">
            and t2.OWNERSHIP_BUSINESS =#{dto.businessId}
        </if>
        <if test="dto.typeId !=null and dto.typeId.trim() != ''">
            and t2.TYPE =#{dto.typeId}
        </if>
        <if test="dto.startTime != null and dto.startTime.trim() != ''">
            and to_char(t1.ACCESS_TIME,'yyyy-MM-dd hh:mm:ss') >= #{dto.startTime}
        </if>
        <if test="dto.endTime != null and dto.endTime.trim() != ''">
            <![CDATA[ and to_char(t1.ACCESS_TIME,'yyyy-MM-dd hh:mm:ss')  <= #{dto.endTime}]]>
        </if>
        GROUP BY t1.INTERFACE_ID, t2.NAME, t2.URL, t2.type,t2.OWNERSHIP_SYSTEM,t2.OWNERSHIP_BUSINESS
        ) a
    </select>

</mapper>
