<?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="cn.roadm.mapper.NullMobileCheckMapper">
    <select id="listPageQueryChargeList" resultType="cn.roadm.bo.NullMobileCheckChargeModal">
        select a.id, a.spId, b.sp_name as spName, a.chargeValue, a.chargeAfter, a.operator, DATE_FORMAT(a.createTime, '%Y-%m-%d %H:%i:%s') as createTime, chargeType,
        price, remark
        from sms_null_mobile_check_charge a left join sms_sp_info b on a.spId = b.id
        <where>
            <if test="spId != null and spId != ''">
                and spId = #{spId}
            </if>
            <if test="operator != null and operator != ''">
                and operator like CONCAT('%', #{operator}, '%')
            </if>
            <if test="startTime != null and startTime != ''">
                and a.createTime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.createTime &lt;= DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
            <if test="chargeType != null">
                and chargeType = #{chargeType}
            </if>
        </where>
        order by a.createTime desc
    </select>

    <insert id="addChargeRecord">
        insert into sms_null_mobile_check_charge (spId, chargeValue, chargeAfter, operator, createTime, chargeType, price, remark)
        VALUES (#{spId}, #{chargeValue}, #{chargeAfter}, #{operator}, #{createTime}, #{chargeType}, #{price}, #{remark})
    </insert>

    <select id="listPageQuerySummaryList" resultType="cn.roadm.bo.NullMobileCheckSummaryModal">
        select  a.spId, b.sp_name as spName,
        sum(IFNULL(a.success,0)) as success,
        sum(IFNULL(a.fail, 0)) as fail,
        a.summary_type as queryType,
        sum(IFNULL(a.hitNum, 0)) as hitNum,
        sum(IFNULL(a.unHitNum, 0)) as unHitNum,
        sum(IFNULL(a.cacheNum, 0)) as cacheNum
         <if test="byDay != null and byDay == 1">
        	, DATE_FORMAT(a.monitorTime, '%Y-%m-%d') as createTime
        </if>
        <if test="(bySupplier != null and bySupplier == 1) or (bySupplierAccount != null and bySupplierAccount == 1)">
            , a.supplier, c.supplier_name as supplierName, c.name as supplierAccountName
        </if>
        from sms_null_mobile_check_summary a 
        left join sms_sp_info b on a.spId = b.id
        <if test="(bySupplier != null and bySupplier == 1) or (bySupplierAccount != null and bySupplierAccount == 1)">
            left join sms_null_mobile_check_service c on a.supplier COLLATE utf8mb4_unicode_ci = c.service_name
        </if>
        <where>
            <if test="spId != null and spId != ''">
                and spId = #{spId}
            </if>
            <if test="supplier != null and supplier != ''">
                and supplier = #{supplier}
            </if>
            <if test="queryType != null and queryType != ''">
                and summary_type = #{queryType}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.monitorTime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.monitorTime &lt;= DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by a.spId, b.sp_name, a.summary_type
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT( a.monitorTime, '%Y-%m-%d' )
        </if>
        <if test="bySupplier != null and bySupplier == 1">
            , a.supplier, c.supplier_name, c.name
        </if>
        <if test="bySupplierAccount != null and bySupplierAccount == 1">
            , a.supplier, c.supplier_name, c.name
        </if>
<!--         根据日期倒序排序-->
<!--        order by success desc, createTime desc-->
        order by createTime desc
    </select>

    <select id="queryAllSummaryList" resultType="cn.roadm.bo.NullMobileCheckSummaryModal">
        select  a.spId, b.sp_name as spName,
        sum(IFNULL(a.success,0)) as success,
        sum(IFNULL(a.fail, 0)) as fail,
        a.summary_type as queryType,
        sum(IFNULL(a.hitNum, 0)) as hitNum,
        sum(IFNULL(a.unHitNum, 0)) as unHitNum,
        sum(IFNULL(a.cacheNum, 0)) as cacheNum
         <if test="byDay != null and byDay == 1">
        	, DATE_FORMAT(a.monitorTime, '%Y-%m-%d') as createTime
        </if>
        <if test="(bySupplier != null and bySupplier == 1) or (bySupplierAccount != null and bySupplierAccount == 1)">
            , a.supplier, c.supplier_name as supplierName, c.name as supplierAccountName
        </if>
        from sms_null_mobile_check_summary a 
        left join sms_sp_info b on a.spId = b.id
        <if test="(bySupplier != null and bySupplier == 1) or (bySupplierAccount != null and bySupplierAccount == 1)">
            left join sms_null_mobile_check_service c on a.supplier COLLATE utf8mb4_unicode_ci = c.service_name
        </if>
        <where>
            <if test="spId != null and spId != ''">
                and spId = #{spId}
            </if>
            <if test="supplier != null and supplier != ''">
                and supplier = #{supplier}
            </if>
            <if test="queryType != null and queryType != ''">
                and summary_type = #{queryType}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.monitorTime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.monitorTime &lt;= DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by a.spId, b.sp_name, a.summary_type
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT( a.monitorTime, '%Y-%m-%d' )
        </if>
        <if test="bySupplier != null and bySupplier == 1">
            , a.supplier, c.supplier_name, c.name
        </if>
        <if test="bySupplierAccount != null and bySupplierAccount == 1">
            , a.supplier, c.supplier_name, c.name
        </if>
    </select>

    <select id="listPageQuerySpSummaryList" resultType="cn.roadm.bo.SpNullMobileCheckSummaryModal">
        select  a.spId, b.sp_name as spName,
        sum(IFNULL(a.success,0)) as success,
        sum(IFNULL(a.fail, 0)) as fail,
        a.summary_type as queryType,
        sum(IFNULL(a.hitNum, 0)) as hitNum,
        sum(IFNULL(a.unHitNum, 0)) as unHitNum
         <if test="byDay != null and byDay == 1">
        	, DATE_FORMAT(a.monitorTime, '%Y-%m-%d') as createTime
        </if>
        from sp_null_mobile_check_summary a left join sms_sp_info b on a.spId = b.id
        <where>
            <if test="spId != null and spId != ''">
                and a.spId = #{spId}
            </if>
            <if test="queryType != null and queryType != ''">
                and a.summary_type = #{queryType}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.monitorTime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.monitorTime &lt; DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by a.spId, b.sp_name, a.summary_type
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT( a.monitorTime, '%Y-%m-%d' )
        </if>
        <!--         根据日期倒序排序-->
        <!--        order by success desc, createTime desc-->
        order by createTime desc
    </select>

    <select id="queryAllSpSummaryList" resultType="cn.roadm.bo.SpNullMobileCheckSummaryModal">
        select  a.spId, b.sp_name as spName,
        sum(IFNULL(a.success,0)) as success,
        sum(IFNULL(a.fail, 0)) as fail,
        a.summary_type as queryType,
        sum(IFNULL(a.hitNum, 0)) as hitNum,
        sum(IFNULL(a.unHitNum, 0)) as unHitNum
         <if test="byDay != null and byDay == 1">
        	, DATE_FORMAT(a.monitorTime, '%Y-%m-%d') as createTime
        </if>
        from sp_null_mobile_check_summary a left join sms_sp_info b on a.spId = b.id
        <where>
            <if test="spId != null and spId != ''">
                and a.spId = #{spId}
            </if>
            <if test="queryType != null and queryType != ''">
                and a.summary_type = #{queryType}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.monitorTime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and a.monitorTime &lt; DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        group by a.spId, b.sp_name, a.summary_type
        <if test="byDay != null and byDay == 1">
            , DATE_FORMAT( a.monitorTime, '%Y-%m-%d' )
        </if>
    </select>

    <select id="getSupplierListByType" resultType="cn.roadm.bo.NullMobileCheckServiceAccountVO">
        select id, name, handler, config, otype, type, supplier_name as supplierName, service_name as serviceName from sms_null_mobile_check_service where `type` = #{type}
    </select>

    <select id="getAllSupplierList" resultType="cn.roadm.bo.NullMobileCheckServiceAccountVO">
        select id, name, handler, config, otype, type, supplier_name as supplierName, service_name as serviceName from sms_null_mobile_check_service
    </select>

    <select id="getSupplierListByTypeAndOtype" resultType="cn.roadm.bo.NullMobileCheckServiceAccountVO">
        select id, name, handler, config, otype, type, supplier_name as supplierName, service_name as serviceName 
        from sms_null_mobile_check_service 
        where type = #{type}
        <if test="otype != null">
            and (otype = 0 or otype = #{otype})
        </if>
    </select>

    <select id="queryTestConfigParams" resultType="cn.roadm.bo.NullMobileCheckServiceAccountVO">
        select id, name, handler, config, otype, type from sms_null_mobile_check_service where type = #{type} and id = #{supplierId}
    </select>

    <select id="getTestUserNamePassword" resultType="java.lang.String">
        select CONCAT(username, '_', password) from sms_sp_user_info where sp_id = #{innerTestNullCheckSpId} and role_id = 0
    </select>
</mapper>
