You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthorizationService.cs 6.7 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using BPA.Kitchen.Core.Common.RequestAnalysis;
  2. using BPA.KitChen.GroupMeal.Application.BaseDto;
  3. using BPA.KitChen.GroupMeal.Application.Service.Authorization.Dtos;
  4. using BPA.KitChen.GroupMeal.Core.Common;
  5. using BPA.KitChen.GroupMeal.Core.Entity;
  6. using Furion.DependencyInjection;
  7. using Furion.FriendlyException;
  8. using SqlSugar;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace BPA.KitChen.GroupMeal.Application.Service.Authorization.Services
  16. {
  17. public class AuthorizationService: UserAnalysis,IAuthorizationService, ITransient
  18. {
  19. private readonly ISqlSugarClient _db;
  20. public AuthorizationService(ISqlSugarClient db)
  21. {
  22. _db = db;
  23. }
  24. /// <summary>
  25. /// 分页店铺授权码
  26. /// </summary>
  27. /// <param name="input"></param>
  28. /// <returns></returns>
  29. public async Task<PageUtil> PageStoreAuthorization(PageInputBase input)
  30. {
  31. RefAsync<int> total = 0;
  32. var res = await _db.Queryable<BPA_StoreAuthorization, ShopEntity>((a,b) => new JoinQueryInfos(
  33. JoinType.Left,a.StoreId==b.Id))
  34. .Select((a, b) =>new StoreAuthorizationDto
  35. {
  36. Id = a.Id,
  37. Key = a.Key,
  38. StoreId = a.StoreId,
  39. StoreName = b.Name,
  40. UpdateAt = a.UpdateAt,
  41. PeriodValidity = a.PeriodValidity,
  42. GroupId = a.GroupId,
  43. })
  44. .ToPageListAsync(input.Current, input.PageSize, total);
  45. PageUtil util = new PageUtil()
  46. {
  47. Total = total,
  48. Data = res
  49. };
  50. return util;
  51. }
  52. /// <summary>
  53. /// 添加店铺授权码
  54. /// </summary>
  55. /// <returns></returns>
  56. public async Task<bool> AddStoreAuthorization(CreateOrUpDateStoreAuthorizationDto input)
  57. {
  58. var data = _db.Queryable<BPA_StoreAuthorization>().Where(x=>x.StoreId== input.StoreId).ToList();
  59. if (data.Count > 0)
  60. {
  61. throw Oops.Oh("店铺授权码已存在");
  62. }
  63. var res = await _db.Insertable(new BPA_StoreAuthorization()
  64. {
  65. StoreId = input.StoreId,
  66. Key = Guid.NewGuid().ToString(),
  67. PeriodValidity = input.PeriodValidity,
  68. UpdateAt=DateTime.Now,
  69. GroupId= CurrentUser.TenantId
  70. }).ExecuteCommandAsync();
  71. return res > 0;
  72. }
  73. /// <summary>
  74. /// 修改店铺授权码
  75. /// </summary>
  76. /// <param name="id"></param>
  77. /// <returns></returns>
  78. public async Task<bool> UpdateStoreAuthorization(string id)
  79. {
  80. var data = await _db.Queryable<BPA_StoreAuthorization>().FirstAsync(x => x.Id == id);
  81. if (data == null)
  82. {
  83. throw Oops.Oh("授权信息不存在");
  84. }
  85. data.Key = Guid.NewGuid().ToString();
  86. data.UpdateAt = DateTime.Now;
  87. return await _db.Updateable(data).ExecuteCommandHasChangeAsync();
  88. }
  89. /// <summary>
  90. /// 修改店铺授权码
  91. /// </summary>
  92. /// <param name="id"></param>
  93. /// <returns></returns>
  94. public async Task<bool> UpdateStoreAuthTime(CreateOrUpDateStoreAuthorizationDto input)
  95. {
  96. var data1 = await _db.Queryable<BPA_StoreAuthorization>().FirstAsync(x => x.StoreId == input.StoreId);
  97. if (data1!=null&&data1.Id!=input.Id)
  98. {
  99. throw Oops.Oh("店铺授权信息已存在");
  100. }
  101. var data = await _db.Queryable<BPA_StoreAuthorization>().FirstAsync(x => x.Id == input.Id);
  102. if (data == null)
  103. {
  104. throw Oops.Oh("授权信息不存在");
  105. }
  106. data.Key = Guid.NewGuid().ToString();
  107. data.PeriodValidity = input.PeriodValidity;
  108. data.UpdateAt = DateTime.Now;
  109. return await _db.Updateable(data).ExecuteCommandHasChangeAsync();
  110. }
  111. /// <summary>
  112. /// 删除店铺权限
  113. /// </summary>
  114. /// <param name="id"></param>
  115. /// <returns></returns>
  116. public async Task<bool> DelStoreAuthorization(string id)
  117. {
  118. var data = await _db.Queryable<BPA_StoreAuthorization>().FirstAsync(x => x.Id == id);
  119. if (data == null)
  120. {
  121. throw Oops.Oh("授权信息不存在");
  122. }
  123. return await _db.Deleteable(data).ExecuteCommandHasChangeAsync();
  124. }
  125. /// <summary>
  126. /// 查询店铺授权信息
  127. /// </summary>
  128. /// <param name="id"></param>
  129. /// <returns></returns>
  130. public async Task<BPA_StoreAuthorization> GetStoreAuthorizationById(string id)
  131. {
  132. var data = await _db.Queryable<BPA_StoreAuthorization>().FirstAsync(x => x.Id == id);
  133. if (data == null)
  134. {
  135. throw Oops.Oh("授权信息不存在");
  136. }
  137. return data;
  138. }
  139. /// <summary>
  140. /// 查询店铺授权信息 更具KEY
  141. /// </summary>
  142. /// <param name="key"></param>
  143. /// <returns></returns>
  144. public async Task<BPA_StoreAuthorization> GetStoreAuthorizationByKey(string key)
  145. {
  146. var data = await _db.Queryable<BPA_StoreAuthorization>()
  147. .ClearFilter()
  148. .FirstAsync(x => x.Key == key);
  149. if (data == null)
  150. {
  151. throw Oops.Oh("授权信息不存在");
  152. }
  153. return data;
  154. }
  155. /// <summary>
  156. /// CodeFirst
  157. /// </summary>
  158. /// <param name="tableNames"></param>
  159. public void CodeFirst()
  160. {
  161. try
  162. {
  163. var list = new List<string>()
  164. {
  165. "BPA_PlatformAuthorization",
  166. "BPA_StoreAuthorization"
  167. };
  168. var types = Assembly.Load("BPA.SAAS.KitChenManage.Core").GetTypes()
  169. .Where(x => x.Namespace != null
  170. && x.GetCustomAttribute<SugarTable>() != null
  171. && x.Namespace.Contains("BPA.SAAS.KitChenManage.Core.Model")
  172. && list.Contains(x.Name))
  173. .ToArray();
  174. _db.CodeFirst.InitTables(types);
  175. }
  176. catch (Exception e)
  177. {
  178. Console.WriteLine(e);
  179. throw;
  180. }
  181. }
  182. }
  183. }