<?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.fibo.ddp.common.dao.datax.datamanage.FieldCallLogMapper">

    <resultMap type="com.fibo.ddp.common.model.datax.datamanage.FieldCallLog" id="FieldCallLogMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="fieldId" column="field_id" jdbcType="INTEGER"/>
        <result property="inputParam" column="input_param" jdbcType="VARCHAR"/>
        <result property="fieldValue" column="field_value" jdbcType="VARCHAR"/>
        <result property="duration" column="duration" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <select id="findFieldCallList" parameterType="com.fibo.ddp.common.model.datax.datamanage.FieldCallLog"
            resultType="com.fibo.ddp.common.model.datax.datamanage.FieldCall">
        select f.id
        , r.id as fieldRelId
        , f.field_en as fieldEn
        , f.field_cn as fieldCn
        , f.field_typeid as fieldTypeId
        , f.value_type as valueType
        , f.value_scope as valueScope
        , p.field_type as fieldType
        , f.is_derivative
        , f.is_output
        , f.is_common
        , f.formula
        , f.formula_show
        , f.used_fieldid
        , f.orig_fieldid
        , f.author
        , f.is_use_sql
        , f.data_source_id
        , f.sql_statement
        , f.sql_variable
        , f.is_interface
        , f.interface_id
        , f.interface_parse_field
        , f.json_value
        ,f.dict_variable
        ,IFNULL(fclog.callCount,0) AS call_count
        from t_field f
        inner JOIN t_field_user_rel r on f.id = r.field_id and r.status = 1 and r.organ_id = #{organId}
        inner JOIN t_field_type p on f.field_typeid = p.id
        left JOIN (select field_id,count(1) AS callCount from t_field_call_log
        <where>
            <if test="fieldId!=null">
                and field_id = #{fieldId}
            </if>
            <if test="queryTimeStart!=null">
                and create_time &gt;= #{queryTimeStart}
            </if>
            <if test="queryTimeEnd!=null">
                and create_time &lt;= #{queryTimeEnd}
            </if>
        </where>
        group by field_id) fclog on f.id = fclog.field_id
        <where>
            <if test="searchKey != null and searchKey != ''">
                and f.field_cn like '%${searchKey}%'
            </if>
            <if test="fieldId != null">
                and f.id != #{fieldId}
            </if>
            <if test="fieldType == 1">
                AND f.is_use_sql = FALSE
                AND f.is_derivative = FALSE
                AND f.is_interface = FALSE
            </if>
            <if test="fieldType == 2">
                AND f.is_use_sql = TRUE
            </if>
            <if test="fieldType == 3">
                AND f.is_derivative = TRUE
            </if>
            <if test="fieldType == 4">
                AND f.is_interface = TRUE
            </if>
            <if test="fieldType == 5">
                AND p.type = 5
            </if>
        </where>

        ORDER BY call_count DESC, f.created DESC
    </select>
    <select id="findFieldCallLogList" parameterType="com.fibo.ddp.common.model.datax.datamanage.FieldCallLog"
            resultType="com.fibo.ddp.common.model.datax.datamanage.FieldCallLog">
        select id, field_id,field_type,source_type,source_id,field_value,input_param,duration,create_time,update_time
        from t_field_call_log
        <where>
            <if test="fieldId!=null">
                and field_id = #{fieldId}
            </if>
            <if test="queryTimeStart!=null">
                and create_time &gt;= #{queryTimeStart}
            </if>
            <if test="queryTimeEnd!=null">
                and create_time &lt;= #{queryTimeEnd}
            </if>
        </where>
        ORDER BY id DESC
    </select>
    <select id="findFieldCallCountList" parameterType="com.fibo.ddp.common.model.datax.datamanage.FieldCallLog"
            resultType="com.fibo.ddp.common.model.datax.datamanage.FieldCall">
        select fclog.callCount,f.field_cn,f.field_en,fclog.field_id from (select field_id,count(1) AS callCount from
        t_field_call_log
        <where>
            <if test="fieldId!=null">
                and field_id = #{fieldId}
            </if>
            <if test="queryTimeStart!=null">
                and create_time &gt;= #{queryTimeStart}
            </if>
            <if test="queryTimeEnd!=null">
                and create_time &lt;= #{queryTimeEnd}
            </if>
        </where>
        group by field_id limit 10)fclog inner join t_field f on f.id = fclog.field_id
        ORDER BY fclog.callCount DESC
    </select>
</mapper>

