SysCompanyService.cs 5.81 KB
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using Hh.Mes.Common;
using System.Linq;

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 公司设置
    /// </summary>
    public class SysCompanyService : RepositorySqlSugar<dynamic>
    {
        
        /// <summary>
        /// 主页面错误信息提示
        /// </summary>
        public dynamic LogTips()
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                #region cpu,硬盘,内存
                var tempServerLog = string.Empty;//"cpu 使用率:80%,内存使用率:80%,C盘使用率:80%,"
                var cpu = ComputerHelp.GetCpuUsage();
                if (int.TryParse(cpu.Replace("%", ""), out int cpuUsage) && cpuUsage >= 80)
                {
                    tempServerLog += $"cpu 使用率:{cpu};";
                }
                var memory = ComputerHelp.GetMemery();
                if (int.TryParse(memory.Item4.Replace("%", ""), out int memoryUsage) && memoryUsage >= 80)
                {
                    tempServerLog += $"内存 使用率:{memory.Item4};";
                }
                var disk = ComputerHelp.GetDriveInfos();
                var driveInfos = disk.Where(x => ((x.TotalSize - x.TotalFreeSpace) * 100 / x.TotalSize) >= 80).Select(x => new
                {
                    dirName = x.Name,
                    usage = ((x.TotalSize - x.TotalFreeSpace) * 100 / x.TotalSize).ToString("N1") + "%"
                }).ToList();
                foreach (var item in driveInfos)
                {
                    tempServerLog += $"{item.dirName}盘使用率:{item.usage};";
                }
                #endregion

                //接口日志
                var log = @" select count(1) as total from sys_interface_log t (nolock) 
                             where t.flag is null 
                             and t.response NOT LIKE '%200%'
                             --and t.createTime>=convert(varchar(15),getdate(),111)
                             --and t.createTime<convert(varchar(15),DATEADD(day,1,getdate()),111)
                             --and (t.method='POST' or t.method='post') 
                             --and (t.response like '%500%' or t.response like '%由于连接%' )

                             select count(1) as total from sys_job_log t (nolock) 
                             where t.flag is null 
                             --and t.createTime>=convert(varchar(15),getdate(),111)
                             --and t.createTime<convert(varchar(15),DATEADD(day,1,getdate()),111)

                              select count(1) as total from sys_interface_log t (nolock) 
                             where t.flag is null and [system] ='app Android'
                             and t.response NOT LIKE '%200%'

                              select count(1) as total from sys_interface_log t (nolock) 
                             where t.flag is null and [system] ='api' and [type]  like 'API%'
                             and t.response NOT LIKE '%200%'";

                var ds = Context.Ado.GetDataSetAll(log);
                var interLog = ds.Tables[0].Rows[0]["total"].ToString();
                if (interLog == "0") interLog = "";
                var jobLog = ds.Tables[1].Rows[0]["total"].ToString();
                if (jobLog == "0") jobLog = "";
                var pdaLog = ds.Tables[2].Rows[0]["total"].ToString();
                if (pdaLog == "0") pdaLog = "";
                var upstreamLog = ds.Tables[3].Rows[0]["total"].ToString();
                if (upstreamLog == "0") upstreamLog = "";
                response.Result = new
                {
                    interLog,

                    jobLog,

                    pdaLog,

                    upstreamLog,

                    serverLog = string.IsNullOrWhiteSpace(tempServerLog) ? "" : tempServerLog
                };
                return response;
            });
        }

        public dynamic UpdateLogTips(string flag)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                if (flag == "inter")
                {
                    string sql = "update sys_interface_log set flag=1 where flag is null";
                    var result = base.Context.Ado.ExecuteCommand(sql);
                    return result > 0 ? response.ResponseSuccess() : response.ResponseError();
                }
                else if (flag == "job")
                {
                    string sql = "update sys_job_log set flag=1 where flag is null";
                    var result = base.Context.Ado.ExecuteCommand(sql);
                    return result > 0 ? response.ResponseSuccess() : response.ResponseError();
                }
                else if (flag == "upstream")
                {
                    string sql = "update sys_interface_log set flag=1 where flag is null and [system] ='api' and [type]  like 'API%'";
                    var result = base.Context.Ado.ExecuteCommand(sql);
                    return result > 0 ? response.ResponseSuccess() : response.ResponseError();
                }
                else if (flag == "pda")
                {
                    string sql = "update sys_interface_log set flag=1 where flag is null and [system] ='app Android'";
                    var result = base.Context.Ado.ExecuteCommand(sql);
                    return result > 0 ? response.ResponseSuccess() : response.ResponseError();
                }
                return response;
            });
        }

    }
}