ActionsRepository.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Dapper;
  7. using GCAS.Model;
  8. namespace GCAS.Code
  9. {
  10. public class ActionsRepository
  11. {
  12. public async Task<int> Insert(ActionsModel entity)
  13. {
  14. string insertSql = "INSERT INTO actions (operator,time,info,rolename)VALUES(@Operator,@Time,@Info,@RoleName)";
  15. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  16. {
  17. return await conn.ExecuteAsync(insertSql, entity);
  18. }
  19. }
  20. public async Task<List<ActionsModel>> GetList(string keyWord, DateTime? startTime, DateTime? endTime)
  21. {
  22. string selectSql = "SELECT * FROM actions where 1=1 ";
  23. if (!string.IsNullOrEmpty(keyWord))
  24. {
  25. selectSql += " and (actions.info like @KeyWord or operator like @KeyWord ) ";
  26. }
  27. if (startTime != null && endTime != null)
  28. {
  29. selectSql += " and time>@StartTime and time<@EndTime ";
  30. }
  31. selectSql += " order by id desc";
  32. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  33. {
  34. return (await conn.QueryAsync<ActionsModel>(selectSql, new { KeyWord = "%" + keyWord + "%", StartTime = startTime, EndTime = endTime })).ToList();
  35. }
  36. }
  37. public async Task<int> Recover()
  38. {
  39. string sql = @"TRUNCATE TABLE actions;TRUNCATE TABLE record;";
  40. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  41. {
  42. try
  43. {
  44. await Task.Run(() => conn.Execute(sql));
  45. return 1;
  46. }
  47. catch { return 0; }
  48. }
  49. }
  50. }
  51. }