UserRepository.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using IMCS.CCS.Entitys;
  2. using IMCS.CCS.Models.vo;
  3. using Microsoft.EntityFrameworkCore;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace IMCS.CCS.Repository
  9. {
  10. public class UserRepository:IUserRepository
  11. {
  12. private readonly CcsContext _context;
  13. public UserRepository(CcsContext context)
  14. {
  15. _context = context;
  16. }
  17. //查询用户分页列表
  18. public async Task<ResponseData<List<User>>> GetUsers(UserListInput input)
  19. {
  20. IQueryable<User> users = _context.User;
  21. if (!string.IsNullOrEmpty(input.LoginName))
  22. {
  23. users = users.Where(x => x.LoginName.Contains(input.LoginName));
  24. }
  25. if (input.Params != null && input.Params.Count == 2)
  26. {
  27. users = users.Where(x => x.CreateTime >= input.Params["beginCreateTime"]
  28. && x.CreateTime <= input.Params["endCreateTime"]);
  29. }
  30. // 分页
  31. int total = await users.CountAsync();
  32. if (input.PageSize != 0 && input.PageNum != 0)
  33. {
  34. users = users.Skip((input.PageNum - 1) * input.PageSize).Take(input.PageSize);
  35. }
  36. ResponseData<List<User>> output = new ResponseData<List<User>>
  37. {
  38. Data = await users.ToListAsync(),
  39. Total = total,
  40. };
  41. return output;
  42. }
  43. //查询用户详情
  44. public async Task<ResponseData<User>> GetUserById(string id)
  45. {
  46. return new ResponseData<User>() { Data = await _context.User.FindAsync(id) };
  47. }
  48. //添加用户
  49. public async Task<ResponseData<bool>> CreateUser(User user)
  50. {
  51. user.CreateTime = DateTime.Now;
  52. _context.User.Add(user);
  53. await _context.SaveChangesAsync();
  54. return new ResponseData<bool>() { Data = true };
  55. }
  56. //更新用户
  57. public async Task<ResponseData<bool>> UpdateUser(User user)
  58. {
  59. _context.Entry(user).State = EntityState.Modified;
  60. try
  61. {
  62. await _context.SaveChangesAsync();
  63. }
  64. catch (DbUpdateConcurrencyException)
  65. {
  66. if (!UserExists(user.Id))
  67. {
  68. return new ResponseData<bool>() { Data = false,Msg="can not find entity by id" };
  69. }
  70. else
  71. {
  72. throw;
  73. }
  74. }
  75. return new ResponseData<bool>() { Data = true };
  76. }
  77. //删除用户
  78. public async Task<ResponseData<bool>> DeleteUserByIds(string ids)
  79. {
  80. if (ids.Contains(","))
  81. {
  82. string[] idList = ids.Split(',');
  83. foreach (var id in idList)
  84. {
  85. await DeleteUser(id);
  86. }
  87. }
  88. else
  89. {
  90. await DeleteUser(ids);
  91. }
  92. return new ResponseData<bool>() { Data = true };
  93. }
  94. private async Task DeleteUser(string id)
  95. {
  96. var user = await _context.User.FindAsync(id);
  97. _context.User.Remove(user);
  98. await _context.SaveChangesAsync();
  99. }
  100. private bool UserExists(String id)
  101. {
  102. return _context.User.Any(e => e.Id == id);
  103. }
  104. }
  105. }