<%--
  Created by IntelliJ IDEA.
  User: wuxuan
  Date: 2023/5/6
  Time: 10:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page pageEncoding="UTF-8" language="java" %>
<%
    String path = request.getContextPath();
%>
<%@ include file="../common/common.jsp" %>
<html>
<head>
    <title>空号检测调用统计</title>
</head>
<body>
<div class="page_container">
    <div class="page_head">
        <h3>供应商空号检测调用统计</h3>
        <div class="breadcrumb">
            <a href="#">首页</a> > <a href="#">企业管理</a> > 供应商空号检测调用统计
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <form id="form" action="nullMobileCheckSummary.html" method="post">
                <div class="panel">
                    <div class="table_operate">
                        <label>企业ID:
<%--                            <input type="text" name="spId" value="${modal.spId}" class="form_control form_white"/>--%>
                            <select id="spid" name="spId" class="form_control form_white singleSelect">
                                <option value="">请选择</option>
                                <c:forEach items="${spListOptions}" var="option">
                                    <option value="${option.value}" <c:if test="${option.value eq modal.spId}">selected</c:if>>${option.value} ${option.label}</option>
                                </c:forEach>
                            </select>
                        </label>
                        <c:if test="${modal.bySupplier != null && modal.bySupplier == 1}">
                            <label>供应商:
                                <select name="supplier" class="form_control form_white">
                                    <option value="">全部</option>
                                    <c:forEach items="${supplierMap}" var="item">
                                        <option value="${item.value}" <c:if test="${not empty modal.supplier && item.value eq modal.supplier}">selected="selected"</c:if>>${item.key}</option>
                                    </c:forEach>
                                </select>
                            </label>
                        </c:if>
                        <label name="queryTypeLabel">
                            查询类型:
                            <select name="queryType" class="form_control form_white">
                                <option value="">全部</option>
                                <option value="0" <c:if test="${not empty modal.queryType && modal.queryType == 0}">selected="selected"</c:if>>空号检测</option>
                                <option value="1" <c:if test="${not empty modal.queryType && modal.queryType == 1}">selected="selected"</c:if>>携号转网</option>
                            </select>
                        </label>
                        <label>创建时间:</label>&nbsp;<input
                            id="d12" name="startTime" type="text" style="width: 200px"
                            value="${modal.startTime}" pattern="yyyy-MM-dd"
                            class="Wdate form_control form_white Wdate1"
                            onfocus="WdatePicker({maxDate:'#F{$dp.$D(\'d13\')||\'%y-%M-%d\'}',dateFmt:'yyyy-MM-dd',isShowClear:false,readOnly:true})"/>--
                        <input id="d13" name="endTime" type="text" style="width: 200px"
                               value="${modal.endTime}" pattern="yyyy-MM-dd"
                               class="Wdate form_control form_white Wdate1"
                               onfocus="WdatePicker({minDate:'#F{$dp.$D(\'d12\',{d:0});}',maxDate:'%y-%M-%d',dateFmt:'yyyy-MM-dd',isShowClear:false,readOnly:true})"/>
                        <label>&nbsp;</label>
                        <label>
                            是否分天汇总:
                            <input type="checkbox" value="1" name="byDay" <c:if test="${modal.byDay != null && modal.byDay == 1}">checked="checked"</c:if>
                                                    style="margin-left: 10px">
                        </label>
                        <label>
                            是否分供应商汇总:
                            <input type="checkbox" value="1" name="bySupplier" <c:if test="${modal.bySupplier != null && modal.bySupplier == 1}">checked="checked"</c:if>
                                style="margin-left: 10px">
                        </label>
                        <label>
                            是否分供应商账号汇总:
                            <input type="checkbox" value="1" name="bySupplierAccount" <c:if test="${modal.bySupplierAccount != null && modal.bySupplierAccount == 1}">checked="checked"</c:if>
                                style="margin-left: 10px">
                        </label>
                        <button type="button" class="btn btn_orange btn_icon" onclick="submitForm()">
                            <span class="ico_16px ico_search"></span>查询历史
                        </button>
                        <button type="button" class="btn btn_blue btn_icon" onclick="queryTodayData()">
                            <span class="ico_16px ico_search"></span>查询当天
                        </button>
                        <button type="button" class="btn btn_navy" onclick="exportNullMobileCheckSummary()">
                            导出
                        </button>

                        <c:if test="${not empty summaryData}">
                            <p class="p_massage">${modal.startTime}日至${modal.endTime}日
                                调用成功总数为：<strong>${summaryData.success}</strong>次，
                                调用失败总数为：<strong>${summaryData.fail}</strong>次，
                                命中总数为：<strong>${summaryData.hitNum}</strong>次，
                                未命中总数为：<strong>${summaryData.unHitNum}</strong>次，
                                缓存命中总数为：<strong>${summaryData.cacheNum}</strong>次，
                                总成功率：<strong>
                                <c:choose>
                                    <c:when test="${summaryData.success + summaryData.fail != 0}">
                                        <fmt:formatNumber
                                            value="${summaryData.success/(summaryData.success + summaryData.fail)*100}"
                                            pattern="0.00" />%
                                    </c:when>
                                    <c:otherwise>
                                        <fmt:formatNumber value="0" pattern="0.00" />%
                                    </c:otherwise>
                                </c:choose>
                                </strong>，
                                命中率：<strong>
                                <c:choose>
                                    <c:when test="${summaryData.hitNum + summaryData.unHitNum != 0}">
                                        <fmt:formatNumber
                                            value="${summaryData.hitNum/(summaryData.hitNum + summaryData.unHitNum)*100}"
                                            pattern="0.00" />%
                                    </c:when>
                                    <c:otherwise>
                                        <fmt:formatNumber value="0" pattern="0.00" />%
                                    </c:otherwise>
                                </c:choose>
                                </strong>
                            </p>
                        </c:if>
                    </div>

                    <div class="table_wrap">
                        <table class="datatable">
                            <thead>
                            <tr>
                                <th>企业ID</th>
                                <th>企业名称</th>
                                <th>汇总类型</th>
                                <th>调用成功</th>
                                <th>调用失败</th>
                                <th>命中数量</th>
                                <th>未命中数量</th>
                                <th>缓存命中数</th>
                                <th>成功率</th>
                                <th>命中率</th>
                                <c:if test="${modal.byDay != null && modal.byDay == 1}">
                                    <th>统计时间</th>
                                </c:if>
                                <c:if test="${modal.bySupplier != null && modal.bySupplier == 1}">
                                    <th>供应商</th>
                                </c:if>
                                <c:if test="${modal.bySupplierAccount != null && modal.bySupplierAccount == 1}">
                                    <th>供应商账号</th>
                                </c:if>
                            </tr>
                            </thead>
                            <tbody>
                            <c:choose>
                                <c:when test="${not empty list}">
                                    <c:forEach items="${list}" var="item" varStatus="vs">
                                        <tr class="main_info">
                                            <td>${item.spId}</td>
                                            <td>${item.spName}</td>
                                            <td>
                                                <c:if test="${item.queryType == 0}">空号检测</c:if>
                                                <c:if test="${item.queryType == 1}">携号转网</c:if>
                                            </td>
                                            <td>${item.success}</td>
                                            <td>${item.fail}</td>
                                            <td>${item.hitNum == null ? 0 : item.hitNum}</td>
                                            <td>${item.unHitNum == null ? 0 : item.unHitNum}</td>
                                            <td>${item.cacheNum == null ? 0 : item.cacheNum}</td>
                                            <td>
                                                <c:choose>
                                                    <c:when test="${item.success + item.fail != 0}">
                                                        <fmt:formatNumber
                                                            value="${item.success/(item.success + item.fail)*100}"
                                                            pattern="0.00" />%
                                                    </c:when>
                                                    <c:otherwise>
                                                        <fmt:formatNumber value="0" pattern="0.00" />%
                                                    </c:otherwise>
                                                </c:choose>
                                            </td>
                                            <td>
                                                <c:choose>
                                                    <c:when test="${(item.hitNum == null ? 0 : item.hitNum) + (item.unHitNum == null ? 0 : item.unHitNum) != 0}">
                                                        <fmt:formatNumber
                                                            value="${(item.hitNum == null ? 0 : item.hitNum)/((item.hitNum == null ? 0 : item.hitNum) + (item.unHitNum == null ? 0 : item.unHitNum))*100}"
                                                            pattern="0.00" />%
                                                    </c:when>
                                                    <c:otherwise>
                                                        <fmt:formatNumber value="0" pattern="0.00" />%
                                                    </c:otherwise>
                                                </c:choose>
                                            </td>
                                            <c:if test="${modal.byDay != null && modal.byDay == 1}">
                                                <td>${item.createTime}</td>
                                            </c:if>
                                            <c:if test="${modal.bySupplier != null && modal.bySupplier == 1}">
                                                <td>
                                                    <c:choose>
                                                        <c:when test="${not empty item.supplierName}">
                                                            ${item.supplierName}
                                                        </c:when>
                                                        <c:otherwise>
                                                            <c:forEach items="${supplierMap}" var="supplier">
                                                                <c:if test="${supplier.value == item.supplier}">
                                                                    ${supplier.key}
                                                                </c:if>
                                                            </c:forEach>
                                                        </c:otherwise>
                                                    </c:choose>
                                                </td>
                                            </c:if>
                                            <c:if test="${modal.bySupplierAccount != null && modal.bySupplierAccount == 1}">
                                                <td>
                                                    <c:choose>
                                                        <c:when test="${not empty item.supplierAccountName}">
                                                            ${item.supplierAccountName}
                                                        </c:when>
                                                        <c:otherwise>
                                                            ${item.supplier}
                                                        </c:otherwise>
                                                    </c:choose>
                                                </td>
                                            </c:if>
                                        </tr>
                                    </c:forEach>
                                </c:when>
                                <c:otherwise>
                                    <tr class="main_info">
                                        <td colspan="20">没有相关数据</td>
                                    </tr>
                                </c:otherwise>
                            </c:choose>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td>${modal.page.pageStr}</td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script>
    $(function () {
        $('.singleSelect').select2();
    });
    function submitForm() {
        $("#form").submit();
    }
    
    function exportNullMobileCheckSummary() {
        if (confirm("确认导出供应商空号检测调用统计数据？")) {
            var spId = $("#spid").val() || '';
            var queryType = $('select[name="queryType"]').val() || '';
            var supplier = $('select[name="supplier"]').val() || '';
            var startTime = $("#d12").val() || '';
            var endTime = $("#d13").val() || '';
            var byDay = $('input[name="byDay"]:checked').length > 0 ? 1 : 0;
            var bySupplier = $('input[name="bySupplier"]:checked').length > 0 ? 1 : 0;
            var bySupplierAccount = $('input[name="bySupplierAccount"]:checked').length > 0 ? 1 : 0;
            
            var url = '<%=path%>/exportNullMobileCheckSummary.html' + 
                     '?spId=' + spId +
                     '&queryType=' + queryType +
                     '&supplier=' + supplier +
                     '&startTime=' + startTime +
                     '&endTime=' + endTime +
                     '&byDay=' + byDay +
                     '&bySupplier=' + bySupplier +
                     '&bySupplierAccount=' + bySupplierAccount;
            
            window.open(url, '_blank');
            alert('导出成功');
        }
    }
    
    function updateTableHeader() {
        var byDay = $('input[name="byDay"]:checked').length > 0;
        var bySupplier = $('input[name="bySupplier"]:checked').length > 0;
        var bySupplierAccount = $('input[name="bySupplierAccount"]:checked').length > 0;
        var theadHtml = '<tr>' +
            '<th>企业ID</th>' +
            '<th>企业名称</th>' +
            '<th>汇总类型</th>' +
            '<th>调用成功</th>' +
            '<th>调用失败</th>' +
            '<th>命中数量</th>' +
            '<th>未命中数量</th>' +
            '<th>缓存命中数</th>' +
            '<th>成功率</th>' +
            '<th>命中率</th>';
        if (byDay) {
            theadHtml += '<th>统计时间</th>';
        }
        if (bySupplier) {
            theadHtml += '<th>供应商</th>';
        }
        if (bySupplierAccount) {
            theadHtml += '<th>供应商账号</th>';
        }
        theadHtml += '</tr>';
        $('.datatable thead').html(theadHtml);
    }

    function setTodayDate() {
        var today = new Date();
        var yyyy = today.getFullYear();
        var mm = (today.getMonth() + 1).toString().padStart(2, '0');
        var dd = today.getDate().toString().padStart(2, '0');
        var todayStr = yyyy + '-' + mm + '-' + dd;
        $("#d12").val(todayStr);
        $("#d13").val(todayStr);
    }

    function queryTodayData() {
        setTodayDate();
        var loadingHtml = '<tr class="main_info"><td colspan="20">正在查询当天数据，请稍候...</td></tr>';
        $('.datatable tbody').html(loadingHtml);

        var queryParams = {
            spId: $('#spid').val() || '',
            queryType: $('select[name="queryType"]').val() || '',
            supplier: $('select[name="supplier"]').val() || '',
            bySupplier: $('input[name="bySupplier"]:checked').length > 0 ? 1 : 0,
            bySupplierAccount: $('input[name="bySupplierAccount"]:checked').length > 0 ? 1 : 0,
            byDay: $('input[name="byDay"]:checked').length > 0 ? 1 : 0
        };

        Object.keys(queryParams).forEach(key => {
            if (queryParams[key] === '' || queryParams[key] === null) {
                delete queryParams[key];
            }
        });

        $.ajax({
            url: '<%=path%>/queryTodayNullMobileCheckSummary.html',
            type: 'POST',
            data: queryParams,
            dataType: 'json',
            success: res => {
                if (res.code === 0) {
                    var data = res.data;
                    if (data) {
                        var list = data.list || [];
                        var summaryData = data.summaryData || {};
                        var today = data.today || '';
                        var queryCondition = data.queryCondition || {};

                        updateSummaryInfo(summaryData, today, queryCondition);
                        updateTableData(list);

                    } else {
                        $('.datatable tbody').html('<tr class="main_info"><td colspan="20">当天暂无符合条件的数据</td></tr>');
                        $('.p_massage').html('');
                    }
                } else {
                    $('.datatable tbody').html('<tr class="main_info"><td colspan="20">查询失败：' + (res.msg || '未知错误') + '</td></tr>');
                    $('.p_massage').html('');
                }
            },
            error: function (xhr, status, error) {
                $('.datatable tbody').html('<tr class="main_info"><td colspan="20">查询出错：网络异常</td></tr>');
                $('.p_massage').html('');
            }
        });
    }

    function updateSummaryInfo(summaryData, today, queryCondition) {
        var successRate = 0;
        var hitRate = 0;

        if ((summaryData.success || 0) + (summaryData.fail || 0) > 0) {
            successRate = ((summaryData.success || 0) / ((summaryData.success || 0) + (summaryData.fail || 0)) * 100).toFixed(2);
        }

        if ((summaryData.hitNum || 0) + (summaryData.unHitNum || 0) > 0) {
            hitRate = ((summaryData.hitNum || 0) / ((summaryData.hitNum || 0) + (summaryData.unHitNum || 0)) * 100).toFixed(2);
        }

        var conditionText = '';
        if (queryCondition && (queryCondition.spId || queryCondition.queryType || queryCondition.supplier || queryCondition.bySupplier)) {
            conditionText = '（筛选条件：';
            if (queryCondition.spId) {
                conditionText += '企业ID=' + queryCondition.spId + ' ';
            }
            // if (queryCondition.queryType) {
            //     conditionText += '类型=' + (queryCondition.queryType === '1' ? '携号转网' : '空号检测') + ' ';
            // }
            if (queryCondition.supplier) {
                conditionText += '供应商=' + queryCondition.supplier + ' ';
            }
            if (queryCondition.bySupplier && queryCondition.bySupplier == 1) {
                conditionText += '按供应商汇总 ';
            }
            if (queryCondition.byDay && queryCondition.byDay == 1) {
                conditionText += '按天汇总 ';
            }
            conditionText += '）';
        }

        var summaryHtml = today + '日当天数据' + conditionText + '：' +
            '调用成功总数为：<strong>' + (summaryData.success || 0) + '</strong>次，' +
            '调用失败总数为：<strong>' + (summaryData.fail || 0) + '</strong>次，' +
            '命中总数为：<strong>' + (summaryData.hitNum || 0) + '</strong>次，' +
            '未命中总数为：<strong>' + (summaryData.unHitNum || 0) + '</strong>次，' +
            '缓存命中总数为：<strong>' + (summaryData.cacheNum || 0) + '</strong>次，' +
            '总成功率：<strong>' + successRate + '%</strong>，' +
            '命中率：<strong>' + hitRate + '%</strong>';

        $('.p_massage').html(summaryHtml);
        $('.p_massage').show();
    }

    function updateTableData(list) {
        updateTableHeader();
        var tbody = $('.datatable tbody');
        tbody.empty();

        if (list && list.length > 0) {
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                var successRate = 0;
                var hitRate = 0;

                if ((item.success || 0) + (item.fail || 0) > 0) {
                    successRate = ((item.success || 0) / ((item.success || 0) + (item.fail || 0)) * 100).toFixed(2);
                }

                if ((item.hitNum || 0) + (item.unHitNum || 0) > 0) {
                    hitRate = ((item.hitNum || 0) / ((item.hitNum || 0) + (item.unHitNum || 0)) * 100).toFixed(2);
                }

                var queryTypeText = (item.queryType == '1') ? '携号转网' : '空号检测';

                var bySupplier = $('input[name="bySupplier"]:checked').length > 0;
                var bySupplierAccount = $('input[name="bySupplierAccount"]:checked').length > 0;
                var supplierCell = '';
                var supplierAccountCell = '';
                
                if (bySupplier) {
                    if (item.supplierName) {
                        supplierCell = '<td>' + (item.supplierName) + '</td>';
                    } else if (item.supplier) {
                        supplierCell = '<td>' + (item.supplier) + '</td>';
                    } else {
                        supplierCell = '<td>-</td>';
                    }
                }
                
                if (bySupplierAccount) {
                    if (item.supplierAccountName) {
                        supplierAccountCell = '<td>' + (item.supplierAccountName) + '</td>';
                    } else if (item.supplier) {
                        supplierAccountCell = '<td>' + (item.supplier) + '</td>';
                    } else {
                        supplierAccountCell = '<td>-</td>';
                    }
                }

                var spIdDisplay = item.spId || '';
                var spNameDisplay = item.spName || '';

                var row = '<tr class="main_info">' +
                    '<td>' + spIdDisplay + '</td>' +
                    '<td>' + spNameDisplay + '</td>' +
                    '<td>' + queryTypeText + '</td>' +
                    '<td>' + (item.success || 0) + '</td>' +
                    '<td>' + (item.fail || 0) + '</td>' +
                    '<td>' + (item.hitNum || 0) + '</td>' +
                    '<td>' + (item.unHitNum || 0) + '</td>' +
                    '<td>' + (item.cacheNum || 0) + '</td>' +
                    '<td>' + successRate + '%</td>' +
                    '<td>' + hitRate + '%</td>';

                var byDay = $('input[name="byDay"]:checked').length > 0;
                if (byDay) {
                    row += '<td>' + (item.createTime || '') + '</td>';
                }

                row += supplierCell;
                row += supplierAccountCell;

                row += '</tr>';

                tbody.append(row);
            }
        } else {
            tbody.html('<tr class="main_info"><td colspan="20">当天暂无数据</td></tr>');
        }
    }
</script>
</html>
