using COMMON;
using DB_Server;
using Microsoft.EntityFrameworkCore;
using Models;
using Models.Models.DEBUGGER_DB;
using Models.Models.LOGDB;
using SERVER.LIB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Models.TCPDATA;
using static Models.TCPDATA.ECHARTS;

namespace SERVER.DataServer
{
    public class CHARTSSERVER
    {
        ~CHARTSSERVER()
        {

        }
        static readonly int NOW_S = ConfigEntity.Instance.NOW_S;

        #region 统计包数---------------------------------------------------- 阿里化完毕
        /// <summary>
        /// 通讯包数统计   -------------          
        /// </summary>
        /// <param name="TX"></param>
        /// <param name="RX"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ECHARTS GetCHARTS(LOG_DBContext data, int[] TX, int[] RX, string startDate, string endDate)
        {
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();
            ECHARTS res = new ECHARTS();
            res.title.text = "UDP通讯 ";
            DateTime dateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;
            DateTime dateTime2 = DateTime.Now;
            //
            // endDate==null 表示实时数据
            // 
            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
                dateTime1 = DateTime.Now;
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }

            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);

            var RESudpdata = data.TBL_RUCS_UDP_LOGS.AsNoTracking();

            dateTime2 = dateTime;

            while (dateTime <= dateTime1)
            {
                string time = dateTime.ToString("yyyy-MM-dd HH");
                if (endDate != null)
                    dateTime = dateTime.AddHours(1);
                else
                {
                    time = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    dateTime = dateTime.AddSeconds(NOW_S);
                }
                res.xAxis.data.Add(time);
            }

            List<DataHelp> ds = new List<DataHelp>();
            ds.Add( new DataHelp());
            ds.Add(new DataHelp());
            ds.Add(new DataHelp());
            ds.Add(new DataHelp());
            ds.Add(new DataHelp());
            ds.Add(new DataHelp());
            ds.Add(new DataHelp());
            ds.Add(new DataHelp());


            //非实时
            if (endDate != null)
            {
                foreach (var item in res.xAxis.data)
                {
                    DateTime DT = DateTime.Parse(item + ":00:00");
                    string sql = $"* | SELECT direction,retry_count,result,data_valid, date_format(datetime, '%Y-%m-%d %H') day, count(datetime) count WHERE datetime >= '{DT.ToString("yyyy-MM-dd HH:00:00")}' AND datetime < '{DT.AddHours(1).ToString("yyyy-MM-dd HH:00:00")}' GROUP BY direction,retry_count,result,data_valid, date_format(datetime, '%Y-%m-%d %H') order by date_format(datetime, '%Y-%m-%d %H') ";

                    var DIC_DATA = XC_Redis.Redis.GET<IList<IDictionary<string, string>>>(item + "GetCHARTS", () =>
                    {
                        var data_ = new ASLS().GetLogs_(0,sql, DT.AddHours(-1), DT.AddHours(2), int.MaxValue, 0).Result;
                        return data_;
                    });
                    foreach (var TXitem in TX)
                    { 
                        int VAL = 0;
                        switch (TXitem)
                        {
                            case 1:
                                ds[0].name = "TX总数";
                                ds[0].lineStyle.normal.color = "#9b8bba";
                                VAL = DIC_DATA.Where(X => X["day"] == item && X["direction"] == "TX").Sum(x => int.Parse(x["count"]??"0"));
                                ds[0].data.Add(VAL);
                                break;
                            //TX 一次成功
                            case 2:
                                ds[1].name = "TX一次";
                                ds[1].lineStyle.normal.color = "#2b821d";
                                VAL = DIC_DATA.Where(X => X["data_valid"] == "1" && X["result"] == "1" && X["retry_count"] == "0" && X["day"] == item && X["direction"] == "TX").Sum(x => int.Parse(x["count"] ?? "0"));
                                ds[1].data.Add(VAL);
                                break;
                            case 3:
                                ds[2].name = "TX重发";
                                ds[2].lineStyle.normal.color = "#f7c5a0";
                                VAL = DIC_DATA.Where(X => X["data_valid"] == "1" && X["result"] == "1" && X["retry_count"] != "0" && X["day"] == item && X["direction"] == "TX").Sum(x => int.Parse(x["count"] ?? "0"));
                                ds[2].data.Add(VAL);
                                break;
                            case 4:
                                ds[3].name = "TX失败";
                                ds[3].lineStyle.normal.color = "#c1232b";
                                ds[3].lineStyle.normal.color = "#f7c5a0";
                                VAL = DIC_DATA.Where(X => X["result"] == "0" && X["direction"] == "TX").Sum(x => int.Parse(x["count"] ?? "0"));
                                ds[3].data.Add(VAL);
                                break;
                        }
                        

                    }

                    foreach (var RXitem in RX)
                    {

                        
                        int VAL = 0;
                        switch (RXitem)
                        {
                            case 1:
                                ds[4].lineStyle.normal.color = "#0098d9";
                                ds[4].name = "RX总数";
                                VAL = DIC_DATA.Where(X => X["day"] == item && X["direction"] == "RX").Sum(x => int.Parse(x["count"] ?? "0"));
                                if (item == "2022-05-24 10")
                                { }
                                ds[4].data.Add(VAL);

                                break;
                            //RX 一次成功
                            case 2:
                                ds[5].name = "RX一次";
                                ds[5].lineStyle.normal.color = "#7bd9a5";
                                VAL = DIC_DATA.Where(X => X["data_valid"] == "1" && X["result"] == "1" && X["retry_count"] == "0" && X["day"] == item && X["direction"] == "RX").Sum(x => int.Parse(x["count"] ?? "0"));
                                ds[5].data.Add(VAL);
                                break;
                            //RX重发
                            case 3:
                                ds[6].lineStyle.normal.color = "#f7c5a0";
                                ds[6].name = "RX重发";

                                VAL = DIC_DATA.Where(X => X["data_valid"] == "1" && X["result"] == "1" && X["retry_count"] != "0" && X["day"] == item && X["direction"] == "RX").Sum(x => int.Parse(x["count"] ?? "0"));
                                ds[6].data.Add(VAL);

                                break;
                            case 4:
                                ds[7].lineStyle.normal.color = "#d87a80";
                                ds[7].name = "RX无效";
                                VAL = DIC_DATA.Where(X => X["data_valid"] == "0" && X["day"] == item && X["direction"] == "RX").Sum(x => int.Parse(x["count"] ?? "0"));
                                ds[7].data.Add(VAL);
                                break;
                        }
                    }
                }

            }
            else
            {
                //var new_RESudpdata = RESudpdata.Where(x => x.DATETIME >= dateTime2 && x.DATETIME <= dateTime1).Select(x => new{x.RETRY_COUNT, x.RESULT,x.DIRECTION,x.DATETIME,x.DATA_VALID }).ToList();

                //实时 

                foreach (var TXitem in TX)
                {
                    DataHelp ds_ = new DataHelp();
                    switch (TXitem)
                    {
                        case 1:
                            ds_.name = "TX总数";
                            ds_.lineStyle.normal.color = "#9b8bba";

                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {
                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE  direction = 'TX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);

                                //var fifledata = new_RESudpdata.Where(x => x.DIRECTION == "TX" && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                        //TX 一次成功
                        case 2:
                            ds_.lineStyle.normal.color = "#2b821d";
                            ds_.name = "TX一次";
                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE retry_count = 0 and result = 1 and direction = 'TX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);
                                //var fifledata = new_RESudpdata.Where(x => x.DIRECTION == "TX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                        case 3:
                            ds_.lineStyle.normal.color = "#f7c5a0";
                            ds_.name = "TX重发";
                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE retry_count > 0 and result = 1 and direction = 'TX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);


                                //var fifledata = new_RESudpdata.Where(x => x.DIRECTION == "TX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                        case 4:
                            ds_.lineStyle.normal.color = "#c1232b";

                            ds_.name = "TX失败";
                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE  result = 0 and direction = 'TX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);
                                //var fifledata = new_RESudpdata.Where(x => x.DIRECTION == "TX" && x.RESULT == 0 && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                    }
                    res.color.Add(ds_.lineStyle.normal.color);
                    res.legend.data.Add(ds_.name);
                    res.series.Add(ds_);
                }

                foreach (var RXitem in RX)
                {
                    DataHelp ds_ = new DataHelp();
                    switch (RXitem)
                    {
                        case 1:
                            ds_.lineStyle.normal.color = "#0098d9";
                            ds_.name = "RX总数";
                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE  direction = 'RX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);

                                //var fifledata = new_RESudpdata.Where(x => x.DIRECTION == "RX" && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                        //TX 一次成功
                        case 2:
                            ds_.name = "RX一次";
                            ds_.lineStyle.normal.color = "#7bd9a5";

                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE retry_count = 0 and result = 1 and  direction = 'RX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);

                                //var fifledata = new_RESudpdata.Where(x => x.RETRY_COUNT == 0 && x.DIRECTION == "RX" && x.RESULT == 1 && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                        //TX 一次成功
                        case 3:
                            ds_.name = "RX重发";
                            ds_.lineStyle.normal.color = "#f7c5a0";

                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE retry_count > 0 and result = 1 and  direction = 'RX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);

                                //var fifledata = new_RESudpdata.Where(x => x.RETRY_COUNT > 0 && x.DIRECTION == "RX" && x.RESULT == 1 && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                        case 4:
                            ds_.lineStyle.normal.color = "#d87a80";
                            ds_.name = "RX无效";
                            for (int i = 0; i < res.xAxis.data.Count; i++)
                            {

                                var s = new ASLS().GetLogs_(0, $"* | SELECT count(*)  WHERE data_valid = 0 and  direction = 'RX' AND datetime >= '{DateTime.Parse(res.xAxis.data[i]).ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DateTime.Parse(res.xAxis.data[i]), DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S));
                                if (s.Result.Count > 0)
                                    ds_.data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                                else
                                    ds_.data.Add(0);
                                //var fifledata = new_RESudpdata.Where(x => x.DIRECTION == "RX" && x.DATA_VALID == 0 && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count();
                                //ds.data.Add(fifledata);
                            }
                            break;
                    }
                    res.color.Add(ds_.lineStyle.normal.color);
                    res.legend.data.Add(ds_.name);
                    res.series.Add(ds_);

                }
            }
            res.series.AddRange(ds.Where(x=>x.name.Length>0));
            res.color.AddRange(ds.Where(x => x.name.Length > 0).Select(x => x.lineStyle.normal.color));
            stopwatch.Stop();
            LogHelp.Error("  zong  用时：" + (stopwatch.ElapsedMilliseconds) + "。</p>");
            //res.labels = res.labels.Distinct().ToList(); ;
            return res;
        }
        #endregion 统计包数

        #region  通讯字节流量统计收起-------------------------------需要 阿里化完毕


        /// <summary>
        /// 通讯字节流量统计收起  -----------------已经修改查询  没有太多问题
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="data"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static ECHARTS GetErrorSucess(LOG_DBContext data, string startDate, string endDate)
        {

            ECHARTS res = new ECHARTS();
            res.title.text = "UDP通讯字节流量";
            //res.type = "bar";
            DateTime dateTime = DateTime.Now;
            DateTime startDatedateTime = DateTime.Now;
            DateTime dateTime2 = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;
            //
            // endDate==null 表示实时数据
            //
            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
                dateTime1 = DateTime.Now;
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }
            startDatedateTime = dateTime;

            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);

            var RESudpdata = data.TBL_RUCS_UDP_LOGS;

            dateTime2 = dateTime;


            while (dateTime <= dateTime1)
            {
                string time = dateTime.ToString("yyyy-MM-dd HH");
                if (endDate != null)
                    dateTime = dateTime.AddHours(1);
                else
                {
                    time = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    dateTime = dateTime.AddSeconds(NOW_S);

                }
                res.xAxis.data.Add(time);

            }

            res.series.Add(new DataHelp { name = "TX" });
            res.series[0].lineStyle.normal.color = "#2b821d";
            res.color.Add(res.series[0].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX" });
            res.series[1].lineStyle.normal.color = "#c1232b";
            res.color.Add(res.series[1].lineStyle.normal.color);


            res.legend.data.Add("TX");
            res.legend.data.Add("RX");
            //非实时
            if (endDate != null)
            {
                ///访问大量数据 卡 
                foreach (var item in res.xAxis.data)
                {
                    DateTime DT = DateTime.Parse(item + ":00:00");
                    res.series[0].data.Add(
                    XC_Redis.Redis.GET<int>(item + "_GetErrorSucess_TX_SUM", () =>
                    {
                        var s = new ASLS().GetLogs_(0, $"* | SELECT sum(length)  WHERE  direction = 'TX' AND datetime >= '{DT.ToString("yyyy-MM-dd HH:00:00")}' AND datetime < '{DT.AddHours(1).ToString("yyyy-MM-dd HH:00:00")}'", DT.AddHours(-0.5), DT.AddHours(0.5));
                       if (s.Result.Count > 0)
                            return int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]);
                      
                        return 0;
                        //return RESudpdata.Where(X => X.DATETIME >= DT && X.DATETIME < DT.AddHours(1) && X.DIRECTION == "TX").Sum(x => x.LENGTH);
                    }, DateTime.Now.ToString("yyyy-MM-dd HH") == item ? 5 : -1)
                    );
                    res.series[1].data.Add(
                    XC_Redis.Redis.GET<int>(item + "_GetErrorSucess_RX_SUM", () =>
                    {
                        var s = new ASLS().GetLogs_(0, $"* | SELECT sum(length)  WHERE  direction = 'RX' AND datetime >= '{DT.ToString("yyyy-MM-dd HH:00:00")}' AND datetime < '{DT.AddHours(1).ToString("yyyy-MM-dd HH:00:00")}'", DT.AddHours(-0.5), DT.AddHours(1.5));
                        if (s.Result.Count > 0)
                            return int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]);
                        return 0;

                        return RESudpdata.Where(X => X.DATETIME >= DT && X.DATETIME < DT.AddHours(1) && X.DIRECTION == "RX").Sum(x => x.LENGTH);
                    }, DateTime.Now.ToString("yyyy-MM-dd HH") == item ? 5 : -1)
                    );
                }
                if (res.series[0].data.Count <= 1)
                    res.series[0].type = "bar";
                if (res.series[1].data.Count <= 1)
                    res.series[1].type = "bar";
            }
            else
            {
                var RESudpdata_NEW = RESudpdata.Where(x => x.DATETIME >= dateTime2 && x.DATETIME <= dateTime1).Select(x => new { x.DATETIME, x.DIRECTION, x.LENGTH });
                //实时 
                for (int i = 0; i < res.xAxis.data.Count; i++)
                {
                    var asls = new ASLS();
                    DateTime DT = DateTime.Parse(res.xAxis.data[i]);
                    var s = asls.GetLogs_(0, $"* | SELECT sum(length)  WHERE  direction = 'TX' AND datetime >= '{DT.ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DT.AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DT.AddSeconds(NOW_S).AddHours(-0.5), DT.AddSeconds(NOW_S).AddHours(1.5));
                    if (s.Result.Count > 0)
                        res.series[0].data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                    else
                        res.series[0].data.Add(0);
                    s = asls.GetLogs_(0, $"* | SELECT sum(length)  WHERE  direction = 'RX' AND datetime >= '{DT.ToString("yyyy-MM-dd HH:mm:ss")}' AND datetime < '{DT.AddSeconds(NOW_S).ToString("yyyy-MM-dd HH:mm:ss")}'", DT.AddSeconds(NOW_S).AddHours(-0.5), DT.AddSeconds(NOW_S).AddHours(1.5));
                    if (s.Result.Count > 0)
                        res.series[1].data.Add(int.Parse(s.Result[0]["_col0"] == "null" ? "0" : s.Result[0]["_col0"].Split(".")[0]));
                    else
                        res.series[1].data.Add(0);
                    //res.series[0].data.Add(RESudpdata_NEW.Where(x => x.DIRECTION == "TX" && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Sum(x => x.LENGTH));
                    //res.series[1].data.Add(RESudpdata_NEW.Where(x => x.DIRECTION == "RX" && x.DATETIME >= DateTime.Parse(res.xAxis.data[i]) && x.DATETIME < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Sum(x => x.LENGTH));
                }

            }
            return res;
        }

        #endregion  通讯字节流量统计收起

        #region  端口使用                   已经优化 ----------------------- 阿里化完毕 (数据多时非常卡)
        /// <summary>
        /// 端口数据使用情况    ------------------------------------ 已经优化  
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="data"></param>
        /// <param name="endDate">没有结束时间时 统计前一秒的数据</param>
        /// <returns></returns>
        public static ECHARTS GetProt(LOG_DBContext data, string startDate, string endDate)
        {
            ECHARTS res = new ECHARTS();
            res.title.text = "UDP通讯端口";
            DateTime dateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;
            //
            // endDate==null 表示实时数据  只统计当前时间 的前一秒到现在的数据   
            // 
            // 已经更改   这里endDate前端不会传入NULL  实时为统计前一分钟 前30分钟等等 数据  
            // 
            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
            {
                dateTime1 = DateTime.Now;
                dateTime = dateTime1.AddSeconds(-NOW_S);
            }
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }

            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);



            // 0 - 255端口  
            for (int i = 0; i < 256; i++)
            {
                res.xAxis.data.Add(i.ToString());
            }

            res.series.Add(new DataHelp { name = "TX" });
            res.series[0].lineStyle.normal.color = "#2b821d";
            res.series[0].type = "bar";
            res.color.Add(res.series[0].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX" });
            res.series[1].lineStyle.normal.color = "#c1232b";
            res.series[1].type = "bar";
            res.color.Add(res.series[1].lineStyle.normal.color);

            res.legend.data.Add("TX");
            res.legend.data.Add("RX");

            #region  阿里的方式 
            var asls = new ASLS();


            var res_ = asls.GetLogs_(0,
                $@"* |   SELECT  direction,  
                CASE
                when direction = 'TX' then source_port
                when direction = 'RX' then destination_port
                END
                _port ,COUNT(*) _sum  WHERE  datetime >= '{dateTime.ToString("yyyy-MM-dd HH:mm:ss")}'  AND datetime < '{dateTime1.ToString("yyyy-MM-dd HH:mm:ss")}'  GROUP BY _port,direction HAVING _port >= 6000 ORDER BY _port", dateTime.AddHours(-0.5), dateTime1.AddHours(1.5));

            IList<IDictionary<string, string>> alsdata = new List<IDictionary<string, string>>();
            if (res_.Result != null && res_.Result.Count > 0)
                alsdata = res_.Result;
            for (int i = 0; i < 256; i++)
            {
                var s = alsdata.FirstOrDefault(x => x["direction"] == "TX" && x["_port"] == $"{6000+i}.0");

                   res.series[0].data.Add(int.Parse(s == null ? "0" : s["_sum"].Split(".")[0]));

                s = null;
                 
                s = alsdata.FirstOrDefault(x => x["direction"] == "RX" && x["_port"] == $"{6000 + i}.0");

                res.series[1].data.Add(int.Parse(s == null ? "0" : s["_sum"].Split(".")[0]));

            }
            return res;
            #endregion





            // 小于1H 的数据 还是查库
            if (DateTime.Now - dateTime < TimeSpan.FromHours(1))
            {
                var RESudpdata = data.TBL_RUCS_UDP_LOGS.Where(x =>
             x.DATETIME >= dateTime && x.DATETIME <= dateTime1
              ).Select(x => new { x.DIRECTION, x.SOURCE_PORT, x.DESTINATION_PORT }).AsNoTracking().ToList();
                // 数据库  通讯量   256 * 2   每次查询指定端口数量      一次性取出数据量过大  =>   导致查询耗时非常大
                // 先统计总数  如果总数为 0 或者总数减去所有查询数  之后 数量等于  0  则直接添加   0    
                for (int i = 0; i < 256; i++)
                {
                    int sum = RESudpdata.Where(x => x.DIRECTION == "TX").Where(x => x.SOURCE_PORT == (6000 + i).ToString()).Count();
                    res.series[0].data.Add(sum);

                    sum = RESudpdata.Where(x => x.DIRECTION == "RX").Where(x => x.DESTINATION_PORT == (6000 + i).ToString()).Count();
                    res.series[1].data.Add(sum);
                }
            }
            else
            {
                res.series[0].data.AddRange(GetProt(dateTime, dateTime1));
                res.series[1].data.AddRange(GetProt(dateTime, dateTime1, "RX"));
            }
            return res;
        }
        #endregion

        #region 处理耗时 ------------------------------------ ----------------    阿里修改完毕
        /// <summary>
        /// 处理耗时    ------------------------------------ ----------------需要   阿里修改完毕
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static ECHARTS GetPROCESSINGTIME(LOG_DBContext data, string startDate, string endDate)
        {
            ECHARTS res = new ECHARTS();
            res.title.text = "UDP通讯处理耗时";
            DateTime dateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;

            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
            {
                dateTime1 = DateTime.Now;
                dateTime = dateTime1.AddSeconds(-NOW_S);
            }
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }
            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);


            // 0 - 30  30 - 60  60 - 100  >100

            res.xAxis.data.Add("0-5");
            res.xAxis.data.Add("5-10");
            res.xAxis.data.Add("10-15");
            res.xAxis.data.Add("15-20");
            res.xAxis.data.Add("20-30");
            res.xAxis.data.Add("30-50");
            res.xAxis.data.Add("50-100");
            res.xAxis.data.Add(">100");

            res.series.Add(new DataHelp { name = "TX一次" });
            res.series[0].lineStyle.normal.color = "#2b821d";
            res.series[0].type = "bar";
            res.series[0].stack = "TX";
            res.color.Add(res.series[0].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "TX重发" });
            res.series[1].lineStyle.normal.color = "#f7c5a0";
            res.series[1].stack = "TX";
            res.series[1].type = "bar";
            res.color.Add(res.series[1].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "TX失败" });
            res.series[2].lineStyle.normal.color = "#c1232b";
            res.series[2].stack = "TX";
            res.series[2].type = "bar";
            res.color.Add(res.series[2].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX一次" });
            res.series[3].lineStyle.normal.color = "#7bd9a5";
            res.series[3].type = "bar";
            res.series[3].stack = "RX";
            res.color.Add(res.series[3].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX重发" });
            res.series[4].lineStyle.normal.color = "#f7c5a0";
            res.series[4].type = "bar";
            res.series[4].stack = "RX";
            res.color.Add(res.series[4].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX无效" });
            res.series[5].lineStyle.normal.color = "#d87a80";
            res.series[5].type = "bar";
            res.series[5].stack = "RX";
            res.color.Add(res.series[5].lineStyle.normal.color);

            res.legend.data.Add("TX一次");
            res.legend.data.Add("TX重发");
            res.legend.data.Add("TX失败");
            res.legend.data.Add("RX一次");
            res.legend.data.Add("RX重发");
            res.legend.data.Add("RX无效");


            #region  阿里的方式    
            var asls = new ASLS();
            int tump = 5;

            String SQL = $@"  * | 
select 
case
when (direction = 'RX' and result = 1 and retry_count = 0  and data_valid = 1) then 'rx一次' 
when direction = 'RX' and  result = 1 and retry_count > 0  and  data_valid = 1 then 'rx重发'  
when direction = 'RX' and  data_valid = 0  then 'rx无效'
when (direction = 'TX' and result = 1 and retry_count = 0  and data_valid = 1) then 'tx一次' 
when direction = 'TX' and  result = 1 and retry_count > 0    and  data_valid = 1 then 'tx重发'  
when direction = 'TX' and result = 0 and data_valid = 1  then 'tx失败'
end as cf,
case  when processing_timespent >=0 and  processing_timespent <5 then '0-5' 
when processing_timespent>=5   and processing_timespent <10  then '5-10'
when processing_timespent>=10   and processing_timespent <15  then '10-15' 
when processing_timespent>=15   and processing_timespent <20  then '15-20' 
when processing_timespent>=20   and processing_timespent <30  then '20-30' 
when processing_timespent>=30   and processing_timespent <50  then '30-50' 
when processing_timespent>=50   and processing_timespent <100  then '50-100'
when processing_timespent>=100     then '>=100' 
else '其他' end as qj,
count(*) as num 
where udp_cmd = '7 0' and   datetime >= '{dateTime.ToString("yyyy-MM-dd HH:mm:ss")}' and datetime < '{dateTime1.ToString("yyyy-MM-dd HH:mm:ss")}'  
group by 
case  when processing_timespent >=0 and  processing_timespent <5 then '0-5' 
when processing_timespent>=5   and processing_timespent <10  then '5-10'
when processing_timespent>=10   and processing_timespent <15  then '10-15' 
when processing_timespent>=15   and processing_timespent <20  then '15-20' 
when processing_timespent>=20   and processing_timespent <30  then '20-30' 
when processing_timespent>=30   and processing_timespent <50  then '30-50' 
when processing_timespent>=50   and processing_timespent <100  then '50-100'
when processing_timespent>=100     then '>=100' 
else '其他' end,
case
when (direction = 'RX' and result = 1 and retry_count = 0  and data_valid = 1) then 'rx一次' 
when direction = 'RX' and  result = 1 and retry_count > 0    and  data_valid = 1 then 'rx重发'  
when direction = 'RX' and  data_valid = 0  then 'rx无效'
when (direction = 'TX' and result = 1 and retry_count = 0  and data_valid = 1) then 'tx一次' 
when direction = 'TX' and  result = 1 and retry_count > 0    and  data_valid = 1 then 'tx重发'  
when direction = 'TX' and result = 0 and data_valid = 1  then 'tx失败'
end
order by qj";
            var RES_DATA = asls.GetLogs_(0, SQL, dateTime.AddHours(-0.5), dateTime1.AddHours(0.5)).Result;

            string str = string.Empty;

            for (int i = 0; i < 100; i += tump)
            {
                if (i > 100)
                {
                    i = 100;
                }

                if (i == 20)
                {
                    tump = 10;
                }
                if (i == 30)
                {
                    tump = 20;
                }
                if (i == 50)
                {
                    tump = 50;
                }

                str = $"{i}-{i+tump}";

                int sum = 0;
                var s = RES_DATA.FirstOrDefault(X => X["cf"] == "tx一次"&& X["qj"]==str);
                sum = s != null ?int.Parse(s["num"].Split(".")[0]):0;
                res.series[0].data.Add(sum);

                sum = 0;
                s = RES_DATA.FirstOrDefault(X => X["cf"] == "tx重发" && X["qj"] == str);
                sum = s != null ? int.Parse(s["num"].Split(".")[0]) : 0;
                res.series[1].data.Add(sum);

                sum = 0;
                s = RES_DATA.FirstOrDefault(X => X["cf"] == "tx失败" && X["qj"] == str);
                sum = s != null ? int.Parse(s["num"].Split(".")[0]) : 0;
                res.series[2].data.Add(sum);

                sum = 0;
                s = RES_DATA.FirstOrDefault(X => X["cf"] == "rx一次" && X["qj"] == str);
                sum = s != null ? int.Parse(s["num"].Split(".")[0]) : 0;
                res.series[3].data.Add(sum);

                sum = 0;
                s = RES_DATA.FirstOrDefault(X => X["cf"] == "rx重发" && X["qj"] == str);
                sum = s != null ? int.Parse(s["num"].Split(".")[0]) : 0;
                res.series[4].data.Add(sum);


                sum = 0;
                s = RES_DATA.FirstOrDefault(X => X["cf"] == "rx无效" && X["qj"] == str);
                sum = s != null ? int.Parse(s["num"].Split(".")[0]) : 0;
                res.series[5].data.Add(sum);
            }

            return res;
            #endregion


            // 小于 1小时 还是采取 实时数据
            if (DateTime.Now - dateTime <= TimeSpan.FromHours(1))
            {
                var RESudpdata = data.TBL_RUCS_UDP_LOGS.Where(x =>
            x.DATETIME >= dateTime && x.DATETIME < dateTime1
             ).Select(x => new { x.DIRECTION, x.RETRY_COUNT, x.RESULT, x.PROCESSING_TIMESPENT, x.DATA_VALID }).AsNoTracking().ToList();
                // 查询数据库数量   6 * 4    24次  非正确注释
                tump = 5;

                for (int i = 0; i < 100; i += tump)
                {
                    if (i > 100)
                    {
                        i = 100;
                    }

                    if (i == 20)
                    {
                        tump = 10;
                    }
                    if (i == 30)
                    {
                        tump = 20;
                    }
                    if (i == 50)
                    {
                        tump = 50;
                    }


                    int sum = 0;
                    sum = RESudpdata.Where(x => x.DIRECTION == "TX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                    res.series[0].data.Add(sum);
                    sum = RESudpdata.Where(x => x.DIRECTION == "TX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                    res.series[1].data.Add(sum);

                    sum = RESudpdata.Where(x => x.DIRECTION == "TX" && x.RESULT == 0 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                    res.series[2].data.Add(sum);

                    sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                    res.series[3].data.Add(sum);

                    sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                    res.series[4].data.Add(sum);

                    sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.DATA_VALID == 0 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                    res.series[5].data.Add(sum);


                }
            }

            else
            {
                var XC_DATA = GetPROCESSINGTIME(dateTime, dateTime1);
                for (int i = 0; i < res.legend.data.Count; i++)
                {
                    res.series[i].data.AddRange(XC_DATA[i]);
                }
            }
            return res;
        }

        #endregion

        #region UDP通讯接收延迟  07 命令   ------------------------------------------------ 阿里化 处理完毕   需要
        /// <summary>
        /// 处理耗时    ------------------------------------ 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static ECHARTS GetRevDelayDateTime(LOG_DBContext data, string startDate, string endDate)
        {
            ECHARTS res = new ECHARTS();
            res.title.text = "UDP通讯接收延迟";
            DateTime dateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;

            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
            {
                dateTime1 = DateTime.Now;
                dateTime = dateTime1.AddSeconds(-NOW_S);
            }
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }
            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);


            // 0 - 30  30 - 60  60 - 100  >100
            res.xAxis.data.Add("<-10");
            res.xAxis.data.Add("-10-0");
            res.xAxis.data.Add("0-10");
            res.xAxis.data.Add("10-20");
            res.xAxis.data.Add("20-30");
            res.xAxis.data.Add("30-40");
            res.xAxis.data.Add("40-50");
            res.xAxis.data.Add("50-60");
            res.xAxis.data.Add("60-80");
            res.xAxis.data.Add("80-110");
            res.xAxis.data.Add("110-150");
            res.xAxis.data.Add("150-200");
            res.xAxis.data.Add("200-300");
            res.xAxis.data.Add("300-500");
            res.xAxis.data.Add(">500");



            res.series.Add(new DataHelp { name = "RX一次" });
            res.series[0].lineStyle.normal.color = "#7bd9a5";
            res.series[0].type = "bar";
            res.series[0].stack = "RX";
            res.color.Add(res.series[0].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX重发" });
            res.series[1].lineStyle.normal.color = "#f7c5a0";
            res.series[1].type = "bar";
            res.series[1].stack = "RX";
            res.color.Add(res.series[1].lineStyle.normal.color);

            res.series.Add(new DataHelp { name = "RX无效" });
            res.series[2].lineStyle.normal.color = "#d87a80";
            res.series[2].type = "bar";
            res.series[2].stack = "RX";
            res.color.Add(res.series[2].lineStyle.normal.color);

            res.legend.data.Add("RX一次");
            res.legend.data.Add("RX重发");
            res.legend.data.Add("RX无效");



            #region  阿里的方式 
            var asls = new ASLS();

            var RES_data = asls.GetLogs_(0, @$"* | select 
case
when (result = 1 and retry_count = 0  and data_valid = 1) then 'rx一次' 
when result = 1 and retry_count > 0    and  data_valid = 1 then 'rx重发'  
when   data_valid = 0  then 'rx无效' else '其他' 
end as cf,
case  when revdelaydatetime < -10  then '<-10' 
when revdelaydatetime>=-10   and revdelaydatetime <0  then '-10-0'
when revdelaydatetime>=0   and revdelaydatetime <10  then '0-10' 
when revdelaydatetime>=10   and revdelaydatetime <20  then '10-20' 
when revdelaydatetime>=20   and revdelaydatetime <30  then '20-30' 
when revdelaydatetime>=30   and revdelaydatetime <40  then '30-40' 
when revdelaydatetime>=40   and revdelaydatetime <50  then '40-50' 
when revdelaydatetime>=50   and revdelaydatetime <60  then '50-60' 
when revdelaydatetime>=60   and revdelaydatetime <80  then '60-80' 
when revdelaydatetime>=80   and revdelaydatetime <110  then '80-110' 
when revdelaydatetime>=110   and revdelaydatetime <150  then '110-150' 
when revdelaydatetime>=150   and revdelaydatetime <200  then '150-200'
when revdelaydatetime>=200   and revdelaydatetime <300  then '200-300'  
when revdelaydatetime>=300   and revdelaydatetime <500  then '300-500' 
when revdelaydatetime>=500     then '>=500' 
else '其他' end as qj,
count(revdelaydatetime) as num 
where udp_cmd = '7 0' and direction = 'RX' AND datetime >= '{dateTime.ToString("yyyy-MM-dd HH: mm:ss")}' AND datetime < '{dateTime1.ToString("yyyy-MM-dd HH: mm: ss")}' 
group by 
case  when revdelaydatetime < -10  then '<-10' 
when revdelaydatetime>=-10   and revdelaydatetime <0  then '-10-0' 
when revdelaydatetime>=0   and revdelaydatetime <10  then '0-10' 
when revdelaydatetime>=10   and revdelaydatetime <20  then '10-20' 
when revdelaydatetime>=20   and revdelaydatetime <30  then '20-30' 
when revdelaydatetime>=30   and revdelaydatetime <40  then '30-40' 
when revdelaydatetime>=40   and revdelaydatetime <50  then '40-50' 
when revdelaydatetime>=50   and revdelaydatetime <60  then '50-60' 
when revdelaydatetime>=60   and revdelaydatetime <80  then '60-80' 
when revdelaydatetime>=80   and revdelaydatetime <110  then '80-110' 
when revdelaydatetime>=110   and revdelaydatetime <150  then '110-150' 
when revdelaydatetime>=150   and revdelaydatetime <200  then '150-200'
when revdelaydatetime>=200   and revdelaydatetime <300  then '200-300'  
when revdelaydatetime>=300   and revdelaydatetime <500  then '300-500' 
when revdelaydatetime>=500     then '>=500' 
else '其他' 
end,
case
when (result = 1 and retry_count = 0  and data_valid = 1) then  'rx一次' 
when result = 1 and retry_count > 0    and  data_valid = 1 then 'rx重发'   
when   data_valid = 0  then 'rx无效' else '其他' 
end ORDER BY qj", dateTime.AddHours(-0.5), dateTime1.AddHours(0.5)).Result;


            int tump = 10;

            for (int i = -20; i < 500; i += tump)
            {
                if (i == 60)
                {
                    tump = 20;
                }
                if (i == 80)
                {
                    tump = 30;
                }
                if (i == 110)
                {
                    tump = 40;
                }
                if (i == 150)
                {
                    tump = 50;
                }
                if (i == 200)
                {
                    tump = 100;
                }
                if (i == 300)
                {
                    tump = 200;
                }
                if (i > 500)
                {
                    i = 500;
                }
                int sum = 0;

                var STR = $"{i}-{i + tump}";
                if (i >= 500)
                    STR = ">=500";

                if (i < -10)
                    STR = "<-10";

                    var tump_ = RES_data.FirstOrDefault(x => x["cf"] == "rx一次" && x["qj"] == STR);
                    sum = tump_ == null?0:int.Parse(tump_["num"].Split(".")[0]);
                    res.series[0].data.Add(sum);

                    tump_ = RES_data.FirstOrDefault(x => x["cf"] == "rx重发" && x["qj"] == STR);
                    sum = tump_ == null ? 0 : int.Parse(tump_["num"].Split(".")[0]);
                    res.series[1].data.Add(sum);


                    tump_ = RES_data.FirstOrDefault(x => x["cf"] == "rx无效" && x["qj"] == STR);
                    sum = tump_ == null ? 0 : int.Parse(tump_["num"].Split(".")[0]);
                    res.series[2].data.Add(sum);
            }

            return res;
            #endregion





            // 小于 1小时 还是采取 实时数据
            if (DateTime.Now - dateTime <= TimeSpan.FromHours(1))
            {
                var RESudpdata = data.TBL_RUCS_UDP_LOGS.Where(x =>
            x.DATETIME >= dateTime && x.DATETIME < dateTime1
             ).Select(x => new { x.DIRECTION, x.RETRY_COUNT, x.RESULT, x.RevDelayDateTime, x.DATA_VALID }).AsNoTracking().ToList();
                // 查询数据库数量   6 * 4    24次  非正确注释
                tump = 10;

                for (int i = -20; i < 500; i += tump)
                {
                    if (i == 60)
                    {
                        tump = 20;
                    }
                    if (i == 80)
                    {
                        tump = 30;
                    }
                    if (i == 110)
                    {
                        tump = 40;
                    }
                    if (i == 150)
                    {
                        tump = 50;
                    }
                    if (i == 200)
                    {
                        tump = 100;
                    }
                    if (i == 300)
                    {
                        tump = 200;
                    }


                    if (i > 500)
                    {
                        i = 500;
                    }
                    int sum = 0;
                    if (i == -10)
                    {
                        sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.RevDelayDateTime < i).Count();
                        res.series[0].data.Add(sum);
                        sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.RevDelayDateTime < i).Count();
                        res.series[1].data.Add(sum);
                        sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.DATA_VALID == 0 && x.RevDelayDateTime < i).Count();
                        res.series[2].data.Add(sum);
                    }
                    else
                    {

                        sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.RevDelayDateTime >= i && (x.RevDelayDateTime < (i + tump) || i == 500)).Count();
                        res.series[0].data.Add(sum);
                        sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.RevDelayDateTime >= i && (x.RevDelayDateTime < (i + tump) || i == 500)).Count();
                        res.series[1].data.Add(sum);

                        sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.DATA_VALID == 0 && x.RevDelayDateTime >= i && (x.RevDelayDateTime < (i + tump) || i == 500)).Count();
                        res.series[2].data.Add(sum);
                    }


                }
            }
            else
            {
                var XC_DATA = GetRevDelayDateTime(dateTime, dateTime1);
                for (int i = 0; i < res.legend.data.Count; i++)
                {
                    res.series[i].data.AddRange(XC_DATA[i]);
                }
            }
            return res;
        }

        #endregion



        #region  BLV_UdpDebug    包数 统计图                     暂时未使用
        public static ECHARTS GetBLV_UdpDebugImg(DEBUGGER_DB data, string startDate, string endDate)
        {
            ECHARTS res = new ECHARTS();
            res.title.text = "包数统计图";
            //res.type = "bar";
            DateTime dateTime = DateTime.Now;
            DateTime startDatedateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;
            //
            // endDate==null 表示实时数据
            //
            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
                dateTime1 = DateTime.Now;
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }
            startDatedateTime = dateTime;

            var RESudpdata = data.UDPDEBUGGERS;

            while (dateTime <= dateTime1)
            {
                string time = dateTime.ToString("yyyy-MM-dd HH");
                if (endDate != null)
                    dateTime = dateTime.AddHours(1);
                else
                {
                    time = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    dateTime = dateTime.AddSeconds(NOW_S);

                }
                res.xAxis.data.Add(time);

            }

            res.series.Add(new DataHelp { name = "调试通讯数" });
            res.series[0].lineStyle.normal.color = "#2b821d";
            res.color.Add(res.series[0].lineStyle.normal.color);

            res.legend.data.Add("调试通讯数");

            //非实时
            if (endDate != null)
            {
                foreach (var item in res.xAxis.data)
                {
                    DateTime DT = DateTime.Parse(item + ":00:00");
                    res.series[0].data.Add(
                    XC_Redis.Redis.GET<int>(item + "_GetBLV_UdpDebugImg_sum", () =>
                    {    
                        return RESudpdata.Where(X => X.CreateDateTime >= DT && X.CreateDateTime < DT.AddHours(1)).Count();
                    }, DateTime.Now.ToString("yyyy-MM-dd HH") == item ? 5 : -1)
                    );
                }
            }
            else
            {
                //实时 
                for (int i = 0; i < res.xAxis.data.Count; i++)
                {
                    res.series[0].data.Add(RESudpdata.Where(x => x.CreateDateTime >= DateTime.Parse(res.xAxis.data[i]) && x.CreateDateTime < DateTime.Parse(res.xAxis.data[i]).AddSeconds(NOW_S)).Count());
                }

            }
            return res;
        }
        #endregion





        #region  BLV_UdpDebug    数据统计接收耗时条形统计图
        public static ECHARTS GetBLV_UdpDebugImg_RevDelayDateTime(DEBUGGER_DB data, string startDate, string endDate)
        {
            ECHARTS res = new ECHARTS();
            res.title.text = "接收耗时";
            DateTime dateTime = DateTime.Now;
            DateTime startDatedateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;

            //
            // endDate==null 表示实时数据
            // 

            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
                dateTime1 = DateTime.Now;
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }

            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);

            var RESudpdata = data.UDPDEBUGGERS.Where(x => x.CreateDateTime >= dateTime && x.CreateDateTime < dateTime1);
            res.xAxis.data.Add("＜0");
            res.xAxis.data.Add("0-30");
            res.xAxis.data.Add("30-60");
            res.xAxis.data.Add("60-100");
            res.xAxis.data.Add("100-130");
            res.xAxis.data.Add("≥130");

            res.series.Add(new DataHelp { });
            res.series[0].lineStyle.normal.color = "#2b821d";
            res.series[0].type = "bar";

            res.color.Add(res.series[0].lineStyle.normal.color);

            for (var i = 0; i < 130; i += 30)
            {
                if (i == 0)
                {
                    res.series[0].data.Add(RESudpdata.Where(X => X.RevDelayDateTime < 0).Count());
                    continue;
                }
                if (i == 130)
                {
                    i = 130;
                    res.series[0].data.Add(RESudpdata.Where(X => X.RevDelayDateTime >= i).Count());
                    break;
                }
                res.series[0].data.Add(RESudpdata.Where(X => X.RevDelayDateTime >= i && X.RevDelayDateTime < i + 30).Count());
            }


            return res;
        }
        #endregion




        #region 统计指定时间段的处理耗时 有定时器 每小时统计一次

        /// <summary>
        /// 统计端口每小时的处理耗时                
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>

        public static List<List<dynamic>> GetPROCESSINGTIME(DateTime start, DateTime end)
        {

            List<List<dynamic>> res = null;


            if (end < start)
                return res;

            using (var db = XC_Data.GetLogDataBase())
            {
                while (start.ToString("yyyyMMddHH") != end.AddHours(1).ToString("yyyyMMddHH"))
                {
                    var dateTime = start;
                    var dateTime1 = start.AddHours(1);

                    var xc_data = XC_Redis.Redis.GET(start.ToString("yyyyMMddHH") + "GetPROCESSINGTIME", () =>
                   {

                       // TX 一次
                       List<dynamic> res1 = new List<dynamic>();

                       // TX 重发
                       List<dynamic> res2 = new List<dynamic>();

                       // TX 失败
                       List<dynamic> res3 = new List<dynamic>();

                       // rx 一次
                       List<dynamic> res4 = new List<dynamic>();

                       // rx 重发
                       List<dynamic> res5 = new List<dynamic>();

                       // rx 无效
                       List<dynamic> res6 = new List<dynamic>();


                       var RESudpdata = db.TBL_RUCS_UDP_LOGS.Where(x =>
               x.DATETIME >= dateTime && x.DATETIME < dateTime1
                ).Select(x => new { x.DIRECTION, x.RETRY_COUNT, x.RESULT, x.PROCESSING_TIMESPENT, x.DATA_VALID }).AsNoTracking();

                       int tump = 5;

                       for (int i = 0; i < 100; i += tump)
                       {
                           if (i > 100)
                           {
                               i = 100;
                           }

                           if (i == 20)
                           {
                               tump = 10;
                           }
                           if (i == 30)
                           {
                               tump = 20;
                           }
                           if (i == 50)
                           {
                               tump = 50;
                           }

                           int sum = 0;
                           sum = RESudpdata.Where(x => x.DIRECTION == "TX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                           res1.Add(sum);
                           sum = RESudpdata.Where(x => x.DIRECTION == "TX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                           res2.Add(sum);

                           sum = RESudpdata.Where(x => x.DIRECTION == "TX" && x.RESULT == 0 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                           res3.Add(sum);

                           sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT == 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                           res4.Add(sum);

                           sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.RETRY_COUNT > 0 && x.RESULT == 1 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                           res5.Add(sum);

                           sum = RESudpdata.Where(x => x.DIRECTION == "RX" && x.DATA_VALID == 0 && x.PROCESSING_TIMESPENT >= i && (x.PROCESSING_TIMESPENT < (i + tump) || i == 100)).Count();
                           res6.Add(sum);

                           if (i == 20)
                           {
                               tump += 5;
                           }
                           if (i == 30)
                           {
                               tump += 10;
                           }
                           if (i == 50)
                           {
                               tump = 50;
                           }
                       }
                       return new List<List<dynamic>> { res1, res2, res3, res4, res5, res6 };
                   }, start.ToString("yyyyMMddHH") == DateTime.Now.ToString("yyyyMMddHH") ? 5 : -1);

                    if (res == null)
                        res = xc_data;
                    else
                    {
                        for (int i = 0; i < xc_data.Count; i++)
                        {
                            for (int j = 0; j < xc_data[i].Count; j++)
                            {
                                res[i][j] += xc_data[i][j];
                            }
                        }
                    }
                    start = start.AddHours(1);
                }

                return res;
            }

        }
        #endregion



        #region 命令饼图   ---------------------------------------------------------需要 阿里化完成

        public static ECHARTS GetCMDpie_(LOG_DBContext data, string startDate, string endDate, string type = "TX")
        {
            ECHARTS res = new ECHARTS();
            res.yAxis = null;
            res.xAxis = null;
            res.title.text = $"UDP通讯{type}命令饼图";
            DateTime dateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;
            res.series.Add(new DataHelp() { type = "pie" });
            res.color.Add("#2b821d");
            res.color.Add("#f7c5a0");
            res.color.Add("#c1232b");
            res.color.Add("#9b8bba");
            res.color.Add("#0098d9");
            res.color.Add("#7bd9a5");
            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
            {
                dateTime1 = DateTime.Now;
                dateTime = dateTime1.AddSeconds(-NOW_S);
            }
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }
            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);



            #region  阿里的方式 
            var asls = new ASLS();
            int cmdcount = 0;
            int other = 0;

            var s = asls.GetLogs_(0, $"* | SELECT udp_cmd,count(*) sum  WHERE direction = '{type}'  AND datetime >= '{dateTime.ToString("yyyy-MM-dd HH:mm:dd")}' and udp_cmd like '%0' AND datetime < '{dateTime1.ToString("yyyy-MM-dd HH:mm:dd")}' group by udp_cmd  order by count(*) desc", dateTime.AddHours(-0.5), dateTime1.AddHours(0.5));
            if (s.Result.Count <= 0)
                return res;
            foreach (var item in s.Result)
            {
                cmdcount++;
                if (cmdcount <= 5)
                    res.series[0].data.Add(new { name = item["udp_cmd"], value = item["sum"] });
                else
                    other += int.Parse(item["sum"]);
            }
            if (other > 0)
                res.series[0].data.Add(new { name = "其他命令", value = other });
            return res;
            #endregion



            // 小于 1小时 还是采取 实时数据
            if (DateTime.Now - dateTime <= TimeSpan.FromHours(1))
            {
                var RESudpdata = data.TBL_RUCS_UDP_LOGS.AsNoTracking().Where(x =>
                x.DATETIME >= dateTime && x.DATETIME < dateTime1 && x.DIRECTION == type && x.UDP_CMD.Contains("0")
                 ).Select(x => x.UDP_CMD).AsEnumerable().GroupBy(x => x).Select(x => new { x.Key, sum = x.Count() }).OrderByDescending(x => x.sum).ToList();




                foreach (var item in RESudpdata)
                {
                    cmdcount++;
                    if (cmdcount <= 5)
                        res.series[0].data.Add(new { name = item.Key, value = item.sum });
                    else
                        other += item.sum;
                }
                if (other > 0)
                    res.series[0].data.Add(new { name = "其他命令", value = other });
            }
            else
            {
                var DATA = GetCMDpie(dateTime, dateTime1, type);
                foreach (var item in DATA.Keys)
                {
                    res.series[0].data.Add(new { name = item, value = DATA[item] });
                }
            }
            return res;
        }
        #endregion


        #region 重发饼图   ---------------------------------------------------需要    阿里化完成

        public static ECHARTS GetRETRY_COUNT(LOG_DBContext data, string startDate, string endDate, string type = "TX")
        {
            ECHARTS res = new ECHARTS();
            res.yAxis = null;
            res.xAxis = null;
            res.title.text = $"UDP通讯{type}重发饼图";
            DateTime dateTime = DateTime.Now;
            DateTime dateTime1 = DateTime.Now;
            res.series.Add(new DataHelp() { type = "pie" });
            res.color.Add("#2b821d");
            res.color.Add("#f7c5a0");
            res.color.Add("#c1232b");
            res.color.Add("#9b8bba");
            res.color.Add("#0098d9");
            res.color.Add("#7bd9a5");
            if (!DateTime.TryParse(startDate, out dateTime))
            {
                return null;
            }
            if (endDate == null)
            {
                dateTime1 = DateTime.Now;
                dateTime = dateTime1.AddSeconds(-NOW_S);
            }
            else
            {
                if (!DateTime.TryParse(endDate, out dateTime1))
                {
                    return null;
                };
            }
            dateTime = STARTTIME_HELP.GetSTARTTIME(dateTime);



            #region  阿里的方式 
            var asls = new ASLS();
            int cmdcount = 0;
            int other = 0;

            var s = asls.GetLogs_(0, $"* | SELECT retry_count, count(*) sum WHERE direction = '{type}' and retry_count > 0  AND datetime >= '{dateTime.ToString("yyyy-MM-dd HH:mm:dd")}' AND datetime < '{dateTime1.ToString("yyyy-MM-dd HH:mm:dd")}' group by retry_count order by count(*) desc", dateTime.AddHours(-0.5), dateTime1.AddHours(0.5));

            if (s.Result.Count <= 0)
                return res;
            foreach (var item in s.Result)
            {
                cmdcount++;
                if (cmdcount <= 5)
                    res.series[0].data.Add(new { name = "重发" + item["retry_count"] + "次", value = item["sum"] });
                else
                    other += int.Parse(item["sum"]);
            }
            if (other > 0)
                res.series[0].data.Add(new { name = "其他", value = other });
            return res;
            #endregion

            // 小于 1小时 还是采取 实时数据
            if (DateTime.Now - dateTime <= TimeSpan.FromHours(1))
            {
                var RESudpdata = data.TBL_RUCS_UDP_LOGS.AsNoTracking().Where(x =>
                x.DATETIME >= dateTime && x.DATETIME < dateTime1 && x.DIRECTION == type
                 ).Select(x => x.RETRY_COUNT).AsEnumerable().GroupBy(x => x).Select(x => new { x.Key, sum = x.Count() }).OrderByDescending(x => x.sum).ToList();

                foreach (var item in RESudpdata)
                {
                    cmdcount++;
                    if (cmdcount <= 5)
                        res.series[0].data.Add(new { name = "重发" + item.Key + "次", value = item.sum });
                    else
                        other += item.sum;
                }
                if (other > 0)
                    res.series[0].data.Add(new { name = "其他", value = other });
            }
            else
            {
                var DATA = GetRETRY_COUNT_(dateTime, dateTime1, type);
                foreach (var item in DATA.Keys)
                {
                    res.series[0].data.Add(new { name = item, value = DATA[item] });
                }
            }
            return res;
        }
        #endregion




        #region  接受延迟  07  定时器
        public static List<List<dynamic>> GetRevDelayDateTime(DateTime start, DateTime end)
        {

            List<List<dynamic>> res = null;


            if (end < start)
                return res;

            using (var db = XC_Data.GetLogDataBase())
            {
                while (start.ToString("yyyyMMddHH") != end.AddHours(1).ToString("yyyyMMddHH"))
                {
                    var dateTime = start;
                    var dateTime1 = start.AddHours(1);

                    var xc_data = XC_Redis.Redis.GET(start.ToString("yyyyMMddHH") + "GetRevDelayDateTime", () =>
                    {

                        // rx 一次
                        List<dynamic> res1 = new List<dynamic>();

                        // rx 重发
                        List<dynamic> res2 = new List<dynamic>();

                        // rx 无效
                        List<dynamic> res3 = new List<dynamic>();


                        var RESudpdata = db.TBL_RUCS_UDP_LOGS.Where(x =>
                x.DATETIME >= dateTime && x.DATETIME < dateTime1 && x.UDP_CMD == "7 0" && x.DIRECTION == "RX"
                 ).Select(x => new { x.RETRY_COUNT, x.RESULT, x.RevDelayDateTime, x.DATA_VALID }).AsNoTracking();

                        int tump = 10;

                        for (int i = -20; i < 500; i += tump)
                        {
                            if (i == 60)
                            {
                                tump = 20;
                            }
                            if (i == 80)
                            {
                                tump = 30;
                            }
                            if (i == 110)
                            {
                                tump = 40;
                            }
                            if (i == 150)
                            {
                                tump = 50;
                            }
                            if (i == 200)
                            {
                                tump = 100;
                            }
                            if (i == 300)
                            {
                                tump = 200;
                            }
                            if (i > 500)
                            {
                                i = 500;
                            }
                            int sum = 0;
                            if (i < -10)
                            {
                                sum = RESudpdata.Where(x => x.RETRY_COUNT == 0 && x.RESULT == 1 && x.RevDelayDateTime < -10).Count();
                                res1.Add(sum);

                                sum = RESudpdata.Where(x => x.RETRY_COUNT > 0 && x.RESULT == 1 && x.RevDelayDateTime < -10).Count();
                                res2.Add(sum);

                                sum = RESudpdata.Where(x => x.DATA_VALID == 0 && x.RevDelayDateTime < -10).Count();
                                res3.Add(sum);
                            }
                            else
                            {

                                sum = RESudpdata.Where(x => x.RETRY_COUNT == 0 && x.RESULT == 1 && x.RevDelayDateTime >= i && (x.RevDelayDateTime < (i + tump) || i == 500)).Count();
                                res1.Add(sum);
                                sum = RESudpdata.Where(x => x.RETRY_COUNT > 0 && x.RESULT == 1 && x.RevDelayDateTime >= i && (x.RevDelayDateTime < (i + tump) || i == 500)).Count();
                                res2.Add(sum);

                                sum = RESudpdata.Where(x => x.DATA_VALID == 0 && x.RevDelayDateTime >= i && (x.RevDelayDateTime < (i + tump) || i == 500)).Count();
                                res3.Add(sum);
                            }



                        }
                        return new List<List<dynamic>> { res1, res2, res3 };
                    }, start.ToString("yyyyMMddHH") == DateTime.Now.ToString("yyyyMMddHH") ? 5 : -1);

                    if (res == null)
                        res = xc_data;
                    else
                    {
                        for (int i = 0; i < xc_data.Count; i++)
                        {
                            for (int j = 0; j < xc_data[i].Count; j++)
                            {
                                res[i][j] += xc_data[i][j];
                            }
                        }
                    }
                    start = start.AddHours(1);
                }

                return res;
            }

        }
        #endregion

        #region 命令饼图    定时器
        public static Dictionary<string, int> GetCMDpie(DateTime start, DateTime end, string type = "TX")
        {

            Dictionary<string, int> res = null;

            if (end < start)
                return res;

            using (var db = XC_Data.GetLogDataBase())
            {
                while (start.ToString("yyyyMMddHH") != end.AddHours(1).ToString("yyyyMMddHH"))
                {
                    var dateTime = start;
                    var dateTime1 = start.AddHours(1);

                    var xc_data = XC_Redis.Redis.GET(start.ToString("yyyyMMddHH") + "GetLogDataBase",
                        () =>
                    {

                        var RESudpdata = db.TBL_RUCS_UDP_LOGS.AsNoTracking().Where(x =>
                x.DATETIME >= dateTime && x.DATETIME < dateTime1 && x.DIRECTION == type && x.UDP_CMD.Contains("0")
                 ).Select(x => x.UDP_CMD).AsEnumerable().GroupBy(x => x).Select(x => new { x.Key, sum = x.Count() }).OrderByDescending(x => x.sum).ToList();
                        Dictionary<string, int> resdata = new Dictionary<string, int>();

                        int cmdcount = 0;
                        int other = 0;

                        foreach (var item in RESudpdata)
                        {
                            cmdcount++;
                            if (cmdcount <= 5)
                                resdata.Add(item.Key, item.sum);
                            else
                                other += item.sum;
                        }
                        if (other > 0)
                            resdata.Add("其他命令", other);
                        return resdata;
                    }, start.ToString("yyyyMMddHH") == DateTime.Now.ToString("yyyyMMddHH") ? 5 : -1);

                    if (res == null)
                        res = xc_data;
                    else
                    {
                        foreach (var item in xc_data.Keys)
                        {
                            if (res.Keys.Contains(item))
                                res[item] += xc_data[item];
                            else
                                res.Add(item, xc_data[item]);
                        }
                    }
                    start = start.AddHours(1);
                }

                return res;
            }

        }
        #endregion

        #region 统计端口每小时的端口使用情况  有定时器

        /// <summary>
        /// 统计端口每小时的端口使用情况
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="TYPE"></param>
        /// <returns></returns>

        public static List<dynamic> GetProt(DateTime start, DateTime end, string TYPE = "TX")
        {

            List<dynamic> res = new List<dynamic>();
            if (end < start)
                return res;

            using (var db = XC_Data.GetLogDataBase())
            {
                while (start.ToString("yyyyMMddHH") != end.AddHours(1).ToString("yyyyMMddHH"))
                {
                    string STRKEY = TYPE + "_" + start.ToString("yyyyMMddHH");


                    int[] rd = XC_Redis.Redis.GetKey<int[]>(STRKEY);

                    if (rd == null || rd.Length == 0)
                    {
                        var NEWDATA = db.TBL_RUCS_UDP_LOGS.AsNoTracking().Where(x => x.DIRECTION == TYPE && x.DATETIME >= start && x.DATETIME < start.AddHours(1)).Select(X => X.SOURCE_PORT).ToList();
                        rd = new int[255];
                        for (int Prot = 0; Prot < 255; Prot++)
                        {
                            rd[Prot] = TYPE == "TX" ?
                                NEWDATA.Where(x => x == (6000 + Prot).ToString()).Count()
                                :
                                NEWDATA.Where(x => x == (6000 + Prot).ToString()).Count();
                            if (res.Count == Prot)
                                res.Add(rd[Prot]);
                            else
                                res[Prot] += rd[Prot];
                        }

                    }
                    else
                    {
                        for (int Prot = 0; Prot < 255; Prot++)
                        {
                            if (res.Count == Prot)
                                res.Add(rd[Prot]);
                            else
                                res[Prot] += rd[Prot];
                        }
                    }
                    XC_Redis.Redis.SetKey(STRKEY, rd, start.ToString("yyyyMMddHH") == DateTime.Now.ToString("yyyyMMddHH") ? 5 : -1);
                    start = start.AddHours(1);
                }
            }
            return res;
        }

        #endregion


        #region 重发饼图    定时器
        public static Dictionary<string, int> GetRETRY_COUNT_(DateTime start, DateTime end, string type = "TX")
        {

            Dictionary<string, int> res = null;

            if (end < start)
                return res;

            using (var db = XC_Data.GetLogDataBase())
            {
                while (start.ToString("yyyyMMddHH") != end.AddHours(1).ToString("yyyyMMddHH"))
                {
                    var dateTime = start;
                    var dateTime1 = start.AddHours(1);

                    var xc_data = XC_Redis.Redis.GET(start.ToString("yyyyMMddHH") + "GetRETRY_COUNT",
                        () =>
                        {

                            var RESudpdata = db.TBL_RUCS_UDP_LOGS.AsNoTracking().Where(x =>
                    x.DATETIME >= dateTime && x.DATETIME < dateTime1 && x.DIRECTION == type
                     ).Select(x => x.RETRY_COUNT).AsEnumerable().GroupBy(x => x).Select(x => new { x.Key, sum = x.Count() }).OrderByDescending(x => x.sum).ToList();
                            Dictionary<string, int> resdata = new Dictionary<string, int>();

                            int cmdcount = 0;
                            int other = 0;

                            foreach (var item in RESudpdata)
                            {
                                cmdcount++;
                                if (cmdcount <= 5)
                                    resdata.Add("重发" + item.Key + "次", item.sum);
                                else
                                    other += item.sum;
                            }
                            if (other > 0)
                                resdata.Add("其他", other);
                            return resdata;
                        }, start.ToString("yyyyMMddHH") == DateTime.Now.ToString("yyyyMMddHH") ? 5 : -1);

                    if (res == null)
                        res = xc_data;
                    else
                    {
                        foreach (var item in xc_data.Keys)
                        {
                            if (res.Keys.Contains(item))
                                res[item] += xc_data[item];
                            else
                                res.Add(item, xc_data[item]);
                        }
                    }
                    start = start.AddHours(1);
                }

                return res;
            }

        }
        #endregion

    }
}
