LogController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using DllEapDal;
  4. using DllEapEntity;
  5. using DllEapEntity.Dtos;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Configuration;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace DllEapBll.Controllers
  15. {
  16. /// <summary>
  17. /// 日志
  18. /// </summary>
  19. [Route("eap/api/[controller]/[action]")]
  20. [ApiController]
  21. [Authorize]
  22. public class LogController : ControllerBase
  23. {
  24. private readonly string conn = "eapslave";
  25. private IConfiguration configuration;
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. /// <param name="configuration"></param>
  30. public LogController(IConfiguration configuration)
  31. {
  32. this.configuration = configuration;
  33. }
  34. /// <summary>
  35. /// 获取日志列表
  36. /// </summary>
  37. /// <param name="sort"></param>
  38. /// <param name="platform"></param>
  39. /// <param name="actionstr"></param>
  40. /// <param name="message"></param>
  41. /// <param name="startTime"></param>
  42. /// <param name="endTime"></param>
  43. /// <param name="opeatorCode"></param>
  44. /// <param name="filter"></param>
  45. /// <param name="pageIndex"></param>
  46. /// <param name="pageSize"></param>
  47. /// <param name="sortFiled"></param>
  48. /// <param name="sortOrder"></param>
  49. /// <returns></returns>
  50. [HttpGet]
  51. public async Task<LayuiModel<EapLogDto>> GetLogList(string sort,
  52. int? platform, string actionstr, string message, DateTime? startTime, DateTime? endTime,
  53. string opeatorCode, string filter,
  54. int pageIndex = 1, int pageSize = 10, string sortFiled = "OperatingTime", string sortOrder = "ASC")
  55. {
  56. if (configuration["LogDbType"] == null || configuration["LogDbType"] == "Mysql")
  57. {
  58. sortFiled = "RecTime";
  59. if (!string.IsNullOrEmpty(sort))
  60. {
  61. var arr = sort.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  62. if (arr != null && arr.Length > 0)
  63. {
  64. sortFiled = arr[0];
  65. sortOrder = arr[1];
  66. }
  67. }
  68. if (string.IsNullOrEmpty(sortFiled) || sortFiled.ToString().ToLower() == "null")
  69. sort = "id";
  70. if (sortOrder == "ascend")
  71. sortOrder = "asc";
  72. else
  73. {
  74. sortOrder = "desc";
  75. }
  76. IEnumerable<Log> list = null;
  77. filter = filter ?? " ";
  78. using (IDatabase db = DbFactory.Base(conn))
  79. {
  80. var dal = new LogDal(db);
  81. int start = (pageIndex - 1) * pageSize + 1;
  82. string errorinfo = string.Empty;
  83. list = dal.GetLogList(start, pageSize, filter, sortFiled, sortOrder, ref errorinfo);
  84. var count = dal.GetLogCount(filter);
  85. var responseData = new LayuiModel<EapLogDto>()
  86. {
  87. code = 0,
  88. count = count,
  89. data = list.Select(c => new EapLogDto
  90. {
  91. Action = c.Action,
  92. Application = c.PlatformName,
  93. ApplicationId = c.Platform,
  94. CallSite = string.Empty,
  95. Category = c.Category,
  96. Controller = c.Module,
  97. Id = c.Id.ToString(),
  98. Levels = c.LogLevel,
  99. Message = c.Message,
  100. OperatingTime = c.RecTime,
  101. Reccode = c.RecCode,
  102. Requesturl = c.Action
  103. }),
  104. msg = ""
  105. };
  106. return responseData;
  107. }
  108. }
  109. else
  110. {
  111. if (!string.IsNullOrEmpty(sort))
  112. {
  113. var arr = sort.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  114. if (arr != null && arr.Length > 0)
  115. {
  116. sortFiled = arr[0];
  117. sortOrder = arr[1];
  118. }
  119. }
  120. if (string.IsNullOrEmpty(sortFiled) || sortFiled.ToString().ToLower() == "null")
  121. sort = "OperatingTime";
  122. if (sortOrder == "ascend")
  123. sortOrder = "asc";
  124. else
  125. {
  126. sortOrder = "desc";
  127. }
  128. var dal = new EapLogDal(configuration);
  129. int skip = (pageIndex - 1) * pageSize;
  130. return await dal.GetLogList(skip, pageSize, sortFiled, sortOrder, platform, actionstr,
  131. message, startTime, endTime, opeatorCode);
  132. }
  133. }
  134. /// <summary>
  135. /// 日志详情
  136. /// </summary>
  137. /// <param name="id"></param>
  138. /// <returns></returns>
  139. public async Task<EapLogDto> GetSingle(string id)
  140. {
  141. var log = new EapLogDto();
  142. if (configuration["LogDbType"] == null || configuration["LogDbType"] == "Mysql")
  143. {
  144. using (IDatabase db = DbFactory.Base(conn))
  145. {
  146. var dal = new LogDal(db);
  147. var data = dal.Get(Convert.ToInt32(id));
  148. log.Id = data.Id.ToString();
  149. log.Action = data.Action;
  150. log.Application = data.PlatformName;
  151. log.ApplicationId = data.Platform;
  152. log.Category = data.Category;
  153. log.Controller = data.Module;
  154. log.Levels = data.LogLevel;
  155. log.Message = data.Message;
  156. log.OperatingTime = data.RecTime;
  157. log.Reccode = data.RecCode;
  158. log.RecName = data.RecName;
  159. }
  160. }
  161. else
  162. {
  163. var logDal = new EapLogDal(configuration);
  164. log = await logDal.Get(id);
  165. }
  166. return log;
  167. }
  168. /// <summary>
  169. /// 机台日志
  170. /// </summary>
  171. /// <param name="maccode"></param>
  172. /// <param name="startTime"></param>
  173. /// <param name="endTime"></param>
  174. /// <returns></returns>
  175. [HttpGet]
  176. public LayuiModel<string> ReadMachineLog(string maccode, string startTime, string endTime)
  177. {
  178. var responseData = new LayuiModel<string>()
  179. {
  180. code = 0,
  181. msg = ""
  182. };
  183. string errorinfo = string.Empty;
  184. List<string> vs = null;
  185. using (IDatabase db = DbFactory.Base(conn))
  186. {
  187. var dal = new LogDal(db);
  188. vs = dal.ReadLogList(maccode, startTime, endTime, ref errorinfo);
  189. }
  190. responseData.data = vs;
  191. responseData.msg = errorinfo;
  192. return responseData;
  193. }
  194. }
  195. }