using Cksoft.Data; using Cksoft.Data.Repository; using DllEapDal; using DllEapEntity; using DllEapEntity.Dtos; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DllEapBll.Controllers { /// /// 日志 /// [Route("eap/api/[controller]/[action]")] [ApiController] [Authorize] public class LogController : ControllerBase { private readonly string conn = "eapslave"; private IConfiguration configuration; /// /// /// /// public LogController(IConfiguration configuration) { this.configuration = configuration; } /// /// 获取日志列表 /// /// /// /// /// /// /// /// /// /// /// /// /// /// [HttpGet] public async Task> GetLogList(string sort, int? platform, string actionstr, string message, DateTime? startTime, DateTime? endTime, string opeatorCode, string filter, int pageIndex = 1, int pageSize = 10, string sortFiled = "OperatingTime", string sortOrder = "ASC") { if (configuration["LogDbType"] == null || configuration["LogDbType"] == "Mysql") { sortFiled = "RecTime"; if (!string.IsNullOrEmpty(sort)) { var arr = sort.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (arr != null && arr.Length > 0) { sortFiled = arr[0]; sortOrder = arr[1]; } } if (string.IsNullOrEmpty(sortFiled) || sortFiled.ToString().ToLower() == "null") sort = "id"; if (sortOrder == "ascend") sortOrder = "asc"; else { sortOrder = "desc"; } IEnumerable list = null; filter = filter ?? " "; using (IDatabase db = DbFactory.Base(conn)) { var dal = new LogDal(db); int start = (pageIndex - 1) * pageSize + 1; string errorinfo = string.Empty; list = dal.GetLogList(start, pageSize, filter, sortFiled, sortOrder, ref errorinfo); var count = dal.GetLogCount(filter); var responseData = new LayuiModel() { code = 0, count = count, data = list.Select(c => new EapLogDto { Action = c.Action, Application = c.PlatformName, ApplicationId = c.Platform, CallSite = string.Empty, Category = c.Category, Controller = c.Module, Id = c.Id.ToString(), Levels = c.LogLevel, Message = c.Message, OperatingTime = c.RecTime, Reccode = c.RecCode, Requesturl = c.Action }), msg = "" }; return responseData; } } else { if (!string.IsNullOrEmpty(sort)) { var arr = sort.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (arr != null && arr.Length > 0) { sortFiled = arr[0]; sortOrder = arr[1]; } } if (string.IsNullOrEmpty(sortFiled) || sortFiled.ToString().ToLower() == "null") sort = "OperatingTime"; if (sortOrder == "ascend") sortOrder = "asc"; else { sortOrder = "desc"; } var dal = new EapLogDal(configuration); int skip = (pageIndex - 1) * pageSize; return await dal.GetLogList(skip, pageSize, sortFiled, sortOrder, platform, actionstr, message, startTime, endTime, opeatorCode); } } /// /// 日志详情 /// /// /// public async Task GetSingle(string id) { var log = new EapLogDto(); if (configuration["LogDbType"] == null || configuration["LogDbType"] == "Mysql") { using (IDatabase db = DbFactory.Base(conn)) { var dal = new LogDal(db); var data = dal.Get(Convert.ToInt32(id)); log.Id = data.Id.ToString(); log.Action = data.Action; log.Application = data.PlatformName; log.ApplicationId = data.Platform; log.Category = data.Category; log.Controller = data.Module; log.Levels = data.LogLevel; log.Message = data.Message; log.OperatingTime = data.RecTime; log.Reccode = data.RecCode; log.RecName = data.RecName; } } else { var logDal = new EapLogDal(configuration); log = await logDal.Get(id); } return log; } /// /// 机台日志 /// /// /// /// /// [HttpGet] public LayuiModel ReadMachineLog(string maccode, string startTime, string endTime) { var responseData = new LayuiModel() { code = 0, msg = "" }; string errorinfo = string.Empty; List vs = null; using (IDatabase db = DbFactory.Base(conn)) { var dal = new LogDal(db); vs = dal.ReadLogList(maccode, startTime, endTime, ref errorinfo); } responseData.data = vs; responseData.msg = errorinfo; return responseData; } } }