<?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.sooka.system.mapper.TUInterfaceinfoMapper">

    <resultMap type="TUInterfaceinfo" id="TUInterfaceinfoResult">
        <result property="id"    column="id"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="interfaceName"    column="interface_name"    />
        <result property="infoItem"    column="info_item"    />
        <result property="usageScenarios"    column="usage_scenarios"    />
        <result property="shareType"    column="share_type"    />
        <result property="interfaceType"    column="interface_type"    />
        <result property="env"    column="env"    />
        <result property="signServeraddress"    column="sign_serveraddress"    />
        <result property="interfaceAddress"    column="interface_address"    />
        <result property="typeIam"    column="type_iam"    />
        <result property="typeAksk"    column="type_aksk"    />
        <result property="code"    column="code"    />
        <result property="callsuccnum"    column="callsuccnum"    />
        <result property="callfailnum"    column="callfailnum"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="count"    column="count"    />
        <result property="percent"    column="percent"    />
        <result property="shareType1"    column="shareType1"    />
        <result property="shareType2"    column="shareType2"    />
        <result property="shareType3"    column="shareType3"    />
        <result property="shareType4"    column="shareType4"    />
    </resultMap>

    <sql id="selectTUInterfaceinfoVo">
        select id, status, remark, del_flag, create_by, create_time, update_by, update_time, interface_name, info_item, usage_scenarios, share_type, interface_type, env, sign_serveraddress, interface_address, type_iam, type_aksk,dept_id,dept_name,code,callsuccnum,callfailnum from t_u_interfaceinfo
    </sql>

    <select id="selectTUInterfaceinfoList" parameterType="TUInterfaceinfo" resultMap="TUInterfaceinfoResult">
        <include refid="selectTUInterfaceinfoVo"/>
        <where>
            <if test="deptId != null  and deptId != ''"> and dept_id = #{deptId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="interfaceName != null  and interfaceName != ''"> and interface_name like concat('%', #{interfaceName}, '%')</if>
            <if test="infoItem != null  and infoItem != ''"> and info_item = #{infoItem}</if>
            <if test="usageScenarios != null  and usageScenarios != ''"> and usage_scenarios = #{usageScenarios}</if>
            <if test="shareType != null  and shareType != ''"> and share_type = #{shareType}</if>
            <if test="interfaceType != null  and interfaceType != ''"> and interface_type = #{interfaceType}</if>
            <if test="env != null  and env != ''"> and env = #{env}</if>
            <if test="signServeraddress != null  and signServeraddress != ''"> and sign_serveraddress = #{signServeraddress}</if>
            <if test="interfaceAddress != null  and interfaceAddress != ''"> and interface_address = #{interfaceAddress}</if>
            <if test="typeIam != null  and typeIam != ''"> and type_iam = #{typeIam}</if>
            <if test="typeAksk != null  and typeAksk != ''"> and type_aksk = #{typeAksk}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="callsuccnum != null  and callsuccnum != ''"> and callsuccnum = #{callsuccnum}</if>
            <if test="callfailnum != null  and callfailnum != ''"> and callfailnum = #{callfailnum}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTUInterfaceinfoListByIds" parameterType="String" resultMap="TUInterfaceinfoResult">
        <include refid="selectTUInterfaceinfoVo"/>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectTUInterfaceinfoById" parameterType="String" resultMap="TUInterfaceinfoResult">
        <include refid="selectTUInterfaceinfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertTUInterfaceinfo" parameterType="TUInterfaceinfo">
        insert into t_u_interfaceinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="interfaceName != null and interfaceName != ''">interface_name,</if>
            <if test="infoItem != null">info_item,</if>
            <if test="usageScenarios != null">usage_scenarios,</if>
            <if test="shareType != null">share_type,</if>
            <if test="interfaceType != null">interface_type,</if>
            <if test="env != null">env,</if>
            <if test="signServeraddress != null">sign_serveraddress,</if>
            <if test="interfaceAddress != null">interface_address,</if>
            <if test="typeIam != null">type_iam,</if>
            <if test="typeAksk != null">type_aksk,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="code != null">code,</if>
            <if test="callsuccnum != null">callsuccnum,</if>
            <if test="callfailnum != null">callfailnum,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="interfaceName != null and interfaceName != ''">#{interfaceName},</if>
            <if test="infoItem != null">#{infoItem},</if>
            <if test="usageScenarios != null">#{usageScenarios},</if>
            <if test="shareType != null">#{shareType},</if>
            <if test="interfaceType != null">#{interfaceType},</if>
            <if test="env != null">#{env},</if>
            <if test="signServeraddress != null">#{signServeraddress},</if>
            <if test="interfaceAddress != null">#{interfaceAddress},</if>
            <if test="typeIam != null">#{typeIam},</if>
            <if test="typeAksk != null">#{typeAksk},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="code != null">#{code},</if>
            <if test="callsuccnum != null">#{callsuccnum},</if>
            <if test="callfailnum != null">#{callfailnum},</if>
         </trim>
    </insert>

    <update id="updateTUInterfaceinfo" parameterType="TUInterfaceinfo">
        update t_u_interfaceinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="interfaceName != null and interfaceName != ''">interface_name = #{interfaceName},</if>
            <if test="infoItem != null">info_item = #{infoItem},</if>
            <if test="usageScenarios != null">usage_scenarios = #{usageScenarios},</if>
            <if test="shareType != null">share_type = #{shareType},</if>
            <if test="interfaceType != null">interface_type = #{interfaceType},</if>
            <if test="env != null">env = #{env},</if>
            <if test="signServeraddress != null">sign_serveraddress = #{signServeraddress},</if>
            <if test="interfaceAddress != null">interface_address = #{interfaceAddress},</if>
            <if test="typeIam != null">type_iam = #{typeIam},</if>
            <if test="typeAksk != null">type_aksk = #{typeAksk},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="code != null">code = #{code},</if>
            <if test="callsuccnum != null">callsuccnum = #{callsuccnum},</if>
            <if test="callfailnum != null">callfailnum = #{callfailnum},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTUInterfaceinfoById" parameterType="String">
        delete from t_u_interfaceinfo where id = #{id}
    </delete>

    <delete id="deleteTUInterfaceinfoByIds" parameterType="String">
        delete from t_u_interfaceinfo where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getMaxCode" resultType="java.lang.Integer" >
        SELECT max(CONVERT(code, UNSIGNED INTEGER)) from t_u_interfaceinfo
    </select>

    <select id="getShareCountByDeptId" resultMap="TUInterfaceinfoResult">
        SELECT
            COUNT(id) count,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_1' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType1,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_2' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType2,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_3' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType3,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_4' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType4
        FROM
            t_u_interfaceinfo
        <where>
            <if test="deptId != null  and deptId != ''"> and dept_id = #{deptId}</if>
        </where>
    </select>

    <select id="getCount" resultMap="TUInterfaceinfoResult">
        SELECT
            COUNT(id) count,
            b.dict_label share_type
        FROM
            t_u_interfaceinfo a
        LEFT JOIN sys_dict_data b ON a.share_type = b.dict_value
        GROUP BY
            share_type
        ORDER BY dict_value
    </select>

    <select id="getSubPercent" resultMap="TUInterfaceinfoResult" parameterType="String">
        SELECT
            CONVERT (
                (
                    COUNT(id) / (
                        SELECT
                            COUNT(id)
                        FROM
                            t_u_interfaceinfo
                    ) * 100
                ),
                DECIMAL (10, 0)
            ) percent,
            COUNT(id) count,
            b.dict_label share_type
        FROM
            t_u_interfaceinfo a
        LEFT JOIN sys_dict_data b ON a.share_type = b.dict_value
        WHERE DATE_FORMAT(a.create_time,'%Y') = #{year}
        <if test="deptId != null  and deptId != ''"> and a.dept_id = #{deptId}</if>
        GROUP BY
            share_type
        ORDER BY dict_value
    </select>

    <select id="getPercent" resultMap="TUInterfaceinfoResult" parameterType="String">
        SELECT
            CONVERT (
                (
                    COUNT(id) / (
                        SELECT
                            COUNT(id)
                        FROM
                            t_u_interfaceinfo
                    ) * 100
                ),
                DECIMAL (10, 0)
            ) percent,
            COUNT(id) count,
            b.dict_label share_type
        FROM
            t_u_interfaceinfo a
        LEFT JOIN sys_dict_data b ON a.share_type = b.dict_value
        WHERE DATE_FORMAT(a.create_time,'%Y') = #{year}
        GROUP BY
            share_type
        ORDER BY dict_value
    </select>

    <select id="getProvinceTop5Frequency" resultMap="TUInterfaceinfoResult">
        SELECT
            IFNULL(SUM(callsuccnum), 0) + IFNULL(SUM(callfailnum), 0) count,
            dept_id,
            dept_name
        FROM
            t_u_interfaceinfo
        WHERE
            dept_id != ""
        AND share_type IN (
            'share_type_3',
            'share_type_4'
        )
        GROUP BY
            dept_id
        ORDER BY
            count DESC
        LIMIT 0,
         5
    </select>

    <select id="getCityTop5Frequency" resultMap="TUInterfaceinfoResult">
        SELECT
            IFNULL(SUM(callsuccnum), 0) + IFNULL(SUM(callfailnum), 0) count,
            dept_id,
            dept_name
        FROM
            t_u_interfaceinfo
        WHERE
            dept_id != ""
        AND share_type IN (
            'share_type_1',
            'share_type_2'
        )
        GROUP BY
            dept_id
        ORDER BY
            count DESC
        LIMIT 0,
         5
    </select>

    <select id="getProvinceTop20Frequency" resultMap="TUInterfaceinfoResult">
        SELECT
            IFNULL(SUM(callsuccnum), 0) + IFNULL(SUM(callfailnum), 0) count,
            dept_id,
            dept_name
        FROM
            t_u_interfaceinfo
        WHERE
            dept_id != ""
        AND share_type IN (
            'share_type_3',
            'share_type_4'
        )
        GROUP BY
            dept_id
        ORDER BY
            count DESC
        LIMIT 0,
         20
    </select>

    <select id="getCityTop20Frequency" resultMap="TUInterfaceinfoResult">
        SELECT
            IFNULL(SUM(callsuccnum), 0) + IFNULL(SUM(callfailnum), 0) count,
            dept_id,
            dept_name
        FROM
            t_u_interfaceinfo
        WHERE
            dept_id != ""
        AND share_type IN (
            'share_type_1',
            'share_type_2'
        )
        GROUP BY
            dept_id
        ORDER BY
            count DESC
        LIMIT 0,
         20
    </select>

    <select id="getImputationInterfaceRanking" resultMap="TUInterfaceinfoResult">
        SELECT
            count('ct') count,
            dept_id,
            dept_name
        FROM
            t_u_interfaceinfo
        WHERE
            dept_id != ''
        GROUP BY
            dept_id
        ORDER BY
            count DESC
        LIMIT 0,
         10
    </select>

    <select id="getImputationDataRanking" resultMap="TUInterfaceinfoResult">
        SELECT
            SUM(callsuccnum) count,
            dept_id,
            dept_name
        FROM
            t_u_interfaceinfo
        WHERE
            dept_id != ''
        GROUP BY
            dept_id
        ORDER BY
            count DESC
        LIMIT 0,
        10
    </select>

    <select id="getFaultInterfaceCount" resultType="Integer">
        SELECT
            count(id) count
        FROM
            t_u_interfaceinfo
        WHERE
            status = 1
    </select>

    <select id="getSubJkspfx" resultMap="TUInterfaceinfoResult">
        SELECT
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_1' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType1,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_2' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType2,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_3' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType3,
            IFNULL(
                SUM(
                    CASE share_type
                    WHEN 'share_type_4' THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS shareType4
        FROM
            int_detailed d
        LEFT JOIN int_record r ON d.record_id = r.id
        LEFT JOIN t_u_interfaceinfo i ON i.id = d.int_id
        WHERE
            provide_dept_id = #{deptId}
        AND DATE_FORMAT(r.examine_time, '%Y') = #{year}
    </select>

</mapper>
