123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Http;
- using Serilog;
- using XdCxRhDW.Dto;
- using XdCxRhDW.Repostory;
- using System.IO;
- using XdCxRhDW.Entity;
- using XdCxRhDW.Api;
- using XdCxRhDW.WebApi;
- using DPP_YH_Core.Extensions;
- using XdCxRhDW.App.Model;
- using System.Diagnostics;
- using System.Configuration;
- namespace XdCxRhDW.App.Controllers
- {
- /// <summary>
- /// 日志相关接口
- /// </summary>
- public class LogController : BaseController
- {
- /// <summary>
- /// 将日志写入到平台
- /// </summary>
- /// <param name="dto">日志信息</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult> Add(LogInfoDto dto)
- {
- try
- {
- using (RHDWContext db = new RHDWContext())
- {
- LogRes res = new LogRes()
- {
- LogTime = dto.LogTime,
- Module = dto.Module,
- LogType = (Entity.EnumLogType)(int)dto.LogType,
- Msg = dto.Msg,
- };
- db.LogRes.Add(res);
- await db.SaveChangesAsync();
- }
- return Success();
- }
- catch (TaskCanceledException)
- {
- Serilog.Log.Warning("日志信息写入超时");
- return Error("日志信息写入超时");
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "日志信息写入异常");
- return Error("日志信息写入异常");
- }
- }
- /// <summary>
- /// 查询日志信息
- /// </summary>
- /// <param name="dto">日志查询参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<List<LogInfoDto>>> Query(LogQueryDto dto)
- {
- try
- {
- using (RHDWContext db = new RHDWContext())
- {
- var query = db.LogRes.AsQueryable();
- if (!string.IsNullOrWhiteSpace(dto.Module) && dto.Module != "全部")
- query = query.Where(p => p.Module == dto.Module);
- if (dto.LogTimeBegin != null && dto.LogTimeBegin != DateTime.MinValue)
- query = query.Where(p => p.LogTime >= dto.LogTimeBegin);
- if (dto.LogTimeEnd != null && dto.LogTimeEnd != DateTime.MinValue)
- query = query.Where(p => p.LogTime <= dto.LogTimeEnd);
- var res = await query.OrderBy(p => p.Module).ThenByDescending(t => t.LogTime).ToListAsync();
- var dtoRes = res.Select(t => new LogInfoDto
- {
- ID = t.ID,
- LogTime = t.LogTime,
- LogType = (EnumLogTypeDto)(int)t.LogType,
- Module = t.Module,
- Msg = t.Msg,
- }).ToList();
- return Success(dtoRes);
- }
- }
- catch (TaskCanceledException)
- {
- Serilog.Log.Warning($"日志信息查询超时.Mudule={dto.Module},TimeStart={dto.LogTimeBegin},TimeEnd={dto.LogTimeEnd}");
- return Error<List<LogInfoDto>>("日志信息查询超时");
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "日志信息查询异常.Mudule={dto.Module},TimeStart={dto.LogTimeBegin},TimeEnd={dto.LogTimeEnd}");
- return Error<List<LogInfoDto>>("日志信息查询异常");
- }
- }
- /// <summary>
- /// 获取所有日志模块
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult<List<LogModulesResDto>>> GetModules()
- {
- try
- {
- using (RHDWContext db = new RHDWContext())
- {
- var res = await db.LogRes.Select(p => p.Module).Distinct().ToListAsync();
- var dtoRes = res.Select(p => new LogModulesResDto()
- {
- Module = p
- }).OrderBy(p => p.Module).ToList();
- return Success(dtoRes);
- }
- }
- catch (TaskCanceledException)
- {
- Serilog.Log.Warning($"日志模块查询超时");
- return Error<List<LogModulesResDto>>("日志模块查询超时");
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "日志模块查询异常");
- return Error<List<LogModulesResDto>>("日志模块查询异常");
- }
- }
- /// <summary>
- /// 删除日志
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult> Delete(IEnumerable<LogDeleteDto> dto)
- {
- if (dto == null || !dto.Any()) return Success();
- try
- {
- var ids = dto.Select(p => p.ID);
- using (RHDWContext db = new RHDWContext())
- {
- var delItems = await db.LogRes.Where(p => ids.Contains(p.ID)).ToListAsync();
- var delItemsReal = db.LogRes.RemoveRange(delItems);
- db.SaveChanges();
- await Add(new LogInfoDto()
- {
- LogTime = DateTime.Now,
- LogType = EnumLogTypeDto.Info,
- Module = "平台",
- Msg = $"用户删除了{delItemsReal.LongCount()}行日志"
- });
- return Success();
- }
- }
- catch (TaskCanceledException)
- {
- Serilog.Log.Warning($"日志信息删除超时.共{dto.Count()}条记录");
- return Error("日志信息删除超时");
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "日志信息删除异常.共{dto.Count()}条记录");
- return Error("日志信息删除异常");
- }
- }
- }
- }
|