EntranceRepository.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Dapper;
  2. using GCAS.Model;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace GCAS.Code
  8. {
  9. public class EntranceRepository
  10. {
  11. public async Task<int> Insert(EntranceModel entity)
  12. {
  13. string exitSql = "Select count(1) from entrance where name=@Name";
  14. string insertSql = "INSERT into entrance (name,time) VALUES (@Name,NOW())";
  15. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  16. {
  17. var count = await conn.QueryFirstAsync<int>(exitSql, new { entity.Name });
  18. if (count > 0)
  19. {
  20. return -1;
  21. }
  22. return await conn.ExecuteAsync(insertSql, entity);
  23. }
  24. }
  25. public async Task<int> Update(EntranceModel entity)
  26. {
  27. string exitSql = "Select count(1) from entrance where name=@Name and id<>@Id";
  28. string insertSql = "Update entrance set name=@Name where id=@Id";
  29. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  30. {
  31. var count = await conn.QueryFirstAsync<int>(exitSql, entity);
  32. if (count > 0)
  33. {
  34. return -1;
  35. }
  36. return await conn.ExecuteAsync(insertSql, entity);
  37. }
  38. }
  39. public async Task<int> Delete(int id)
  40. {
  41. string updateSql = "Delete from entrance WHERE id=@Id";
  42. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  43. {
  44. return await conn.ExecuteAsync(updateSql, new { Id = id });
  45. }
  46. }
  47. public List<EntranceModel> GetList()
  48. {
  49. string selectSql = "SELECT id,name,time from entrance ORDER BY id ASC";
  50. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  51. {
  52. return conn.Query<EntranceModel>(selectSql).ToList();
  53. }
  54. }
  55. public async Task<EntranceModel> Get(int id)
  56. {
  57. string selectSql = "SELECT id,name,time FROM entrance WHERE id=@Id";
  58. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  59. {
  60. return await Task.Run(() => conn.QueryFirstOrDefault<EntranceModel>(selectSql, new { Id = id }));
  61. }
  62. }
  63. }
  64. }