基础服务api
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.
 
 

232 lines
9.6 KiB

  1. using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsEnergyConfig;
  2. using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsType;
  3. using BPA.SAAS.Manage.Application.DataBase.Interface;
  4. using BPA.SAAS.Manage.Comm.Const;
  5. using BPA.SAAS.Manage.Comm.Enum;
  6. using BPA.SAAS.Manage.Core.Base;
  7. using BPA.SAAS.Manage.Core.DataBase;
  8. using NPOI.POIFS.Storage;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace BPA.SAAS.Manage.Application.DataBase.Services
  15. {
  16. public class GoodsEnergyConfigService: IGoodsEnergyConfigService, ITransient
  17. {
  18. private readonly ISqlSugarClient _db;
  19. public GoodsEnergyConfigService(ISqlSugarClient db)
  20. {
  21. _db = db;
  22. }
  23. /// <summary>
  24. /// 分页查询
  25. /// </summary>
  26. /// <param name="dto"></param>
  27. /// <returns></returns>
  28. public async Task<PageUtil> GetGoodsEnergyConfigPage(GoodsEnergyConfigQueryDto dto)
  29. {
  30. List<IConditionalModel> conModels = new List<IConditionalModel>();
  31. //string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  32. if (!string.IsNullOrEmpty(dto.Name))
  33. {
  34. conModels.Add(new ConditionalModel() { FieldName = "Name", ConditionalType = ConditionalType.Like, FieldValue = dto.Name });
  35. }
  36. if (!string.IsNullOrEmpty(dto.GoodsId))
  37. {
  38. conModels.Add(new ConditionalModel() { FieldName = "GoodsId", ConditionalType = ConditionalType.Like, FieldValue = dto.GoodsId });
  39. }
  40. int total = new RefAsync<int>();
  41. var res = await _db.Queryable<BPA_GoodsEnergyConfig>()
  42. .Where(conModels)
  43. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  44. .ToPageListAsync(dto.Current, dto.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. /// <param name="dto"></param>
  56. /// <returns></returns>
  57. public async Task<bool> AddGoodsEnergyConfig(GoodsEnergyConfigDto dto)
  58. {
  59. try
  60. {
  61. _db.Ado.BeginTran();
  62. var resEntity = _db.Queryable<BPA_GoodsEnergyConfig>().First(it => it.Name == dto.Name && it.GoodsId == dto.GoodsId);
  63. if (resEntity != null) throw Oops.Oh("配置名称已存在");
  64. var newType = new BPA_GoodsEnergyConfig
  65. {
  66. GoodsId = dto.GoodsId,
  67. Name = dto.Name,
  68. Uint = dto.Uint,
  69. };
  70. var model = await _db.Insertable(newType).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  71. List<BPA_GoodsEnergyConfigDetails> list = new();
  72. for (int i = 0; i < dto.Details.Count; i++)
  73. {
  74. var model1 = new BPA_GoodsEnergyConfigDetails
  75. {
  76. GoodsenergyconfigId = model.Id,
  77. Key = dto.Details[i].Key,
  78. Value = dto.Details[i].Value,
  79. };
  80. list.Add(model1);
  81. }
  82. var res = await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  83. _db.Ado.CommitTran();
  84. return res > 0;
  85. }
  86. catch (Exception e)
  87. {
  88. _db.Ado.RollbackTran();
  89. throw Oops.Oh("系统异常:"+ e.Message);
  90. }
  91. }
  92. /// <summary>
  93. /// 更新
  94. /// </summary>
  95. /// <param name="dto"></param>
  96. /// <returns></returns>
  97. public async Task<bool> UpdateGoodsEnergyConfig(GoodsEnergyConfigDto dto)
  98. {
  99. try
  100. {
  101. _db.Ado.BeginTran();
  102. // 查询数据库中是否存在未删除的商品类型
  103. var resEntity = _db.Queryable<BPA_GoodsEnergyConfig>().First(it => it.Id == dto.Id);
  104. if (null == resEntity) throw Oops.Oh("配置不存在");
  105. var resEntity1 = _db.Queryable<BPA_GoodsEnergyConfig>().First(it => it.Name == dto.Name && it.Id != dto.Id && it.GoodsId!= dto.GoodsId);
  106. if (resEntity1 != null) throw Oops.Oh("配置名称已存在");
  107. resEntity.Name = dto.Name;
  108. resEntity.Uint = dto.Uint;
  109. await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  110. var model = _db.Queryable<BPA_GoodsEnergyConfigDetails>().Where(it => it.GoodsenergyconfigId == dto.Id).ToList();
  111. if (model.Count > 0) await _db.Deleteable(model).ExecuteCommandAsync();
  112. List<BPA_GoodsEnergyConfigDetails> list = new();
  113. for (int i = 0; i < dto.Details.Count; i++)
  114. {
  115. var model1 = new BPA_GoodsEnergyConfigDetails
  116. {
  117. GoodsenergyconfigId = dto.Id,
  118. Key = dto.Details[i].Key,
  119. Value = dto.Details[i].Value,
  120. };
  121. list.Add(model1);
  122. }
  123. var res = await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  124. _db.Ado.CommitTran();
  125. return res > 0;
  126. }
  127. catch (Exception e)
  128. {
  129. _db.Ado.RollbackTran();
  130. throw Oops.Oh("系统异常,异常信息:" + e.Message);
  131. }
  132. }
  133. /// <summary>
  134. /// 删除
  135. /// </summary>
  136. /// <param name="Id"></param>
  137. /// <returns></returns>
  138. public async Task<bool> DelGoodsEnergyConfig(string Id)
  139. {
  140. try
  141. {
  142. _db.Ado.BeginTran();
  143. // 查询数据库中是否存在未删除的商品类型
  144. var resEntity = _db.Queryable<BPA_GoodsEnergyConfig>().First(it => it.Id == Id);
  145. if (resEntity == null)
  146. {
  147. throw Oops.Oh("配置不存在");
  148. }
  149. var model = _db.Queryable<BPA_GoodsEnergyConfigDetails>().Where(it => it.GoodsenergyconfigId == Id).ToList();
  150. if (model.Count > 0) await _db.Deleteable(model).ExecuteCommandAsync();
  151. var res = await _db.Deleteable(resEntity).ExecuteCommandAsync();
  152. _db.Ado.CommitTran();
  153. return res > 0;
  154. }
  155. catch (Exception e)
  156. {
  157. _db.Ado.RollbackTran();
  158. throw Oops.Oh("系统异常,异常信息:" + e.Message);
  159. }
  160. }
  161. public async Task<List<GoodsEnergyConfigDetailsViewDto>> GetGoodsEnergyConfigDetailsList(string goodsenergyconfigId)
  162. {
  163. int total = new RefAsync<int>();
  164. var res = await _db.Queryable<BPA_GoodsEnergyConfigDetails>()
  165. .Where(x=>x.GoodsenergyconfigId== goodsenergyconfigId)
  166. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  167. .Select(x => new GoodsEnergyConfigDetailsViewDto() { Id=x.Id,Key=x.Key,Value=x.Value})
  168. .ToListAsync();
  169. return res;
  170. }
  171. /// <summary>
  172. /// 添加
  173. /// </summary>
  174. /// <param name="dto"></param>
  175. /// <returns></returns>
  176. public async Task<bool> AddGoodsEnergyConfigDetails(GoodsEnergyConfigDetailsDto dto)
  177. {
  178. var resEntity = _db.Queryable<BPA_GoodsEnergyConfigDetails>().First(it => it.Key == dto.Key);
  179. if(resEntity!=null) throw Oops.Oh("key已存在");
  180. var newType = new BPA_GoodsEnergyConfigDetails
  181. {
  182. GoodsenergyconfigId = dto.GoodsenergyconfigId,
  183. Key = dto.Key,
  184. Value = dto.Value,
  185. };
  186. var res = await _db.Insertable(newType).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  187. return res > 0;
  188. }
  189. /// <summary>
  190. /// 更新
  191. /// </summary>
  192. /// <param name="dto"></param>
  193. /// <returns></returns>
  194. public async Task<bool> UpdateGoodsEnergyConfigDetails(GoodsEnergyConfigDetailsDto dto)
  195. {
  196. // 查询数据库中是否存在未删除的商品类型
  197. var resEntity = _db.Queryable<BPA_GoodsEnergyConfigDetails>().First(it => it.Id == dto.Id);
  198. if (null == resEntity) throw Oops.Oh("配置不存在");
  199. var resEntity1 = _db.Queryable<BPA_GoodsEnergyConfigDetails>().First(it => it.Key == dto.Key && it.Id != dto.Id);
  200. if (resEntity1 != null) throw Oops.Oh("配置Key已存在");
  201. resEntity.Key = dto.Key;
  202. resEntity.Value = dto.Value;
  203. var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  204. return res > 0;
  205. }
  206. /// <summary>
  207. /// 删除
  208. /// </summary>
  209. /// <param name="Id"></param>
  210. /// <returns></returns>
  211. public async Task<bool> DelGoodsEnergyConfigDetails(string Id)
  212. {
  213. // 查询数据库中是否存在未删除的商品类型
  214. var resEntity = _db.Queryable<BPA_GoodsEnergyConfigDetails>().First(it => it.Id == Id);
  215. if (resEntity == null)
  216. {
  217. throw Oops.Oh("配置不存在");
  218. }
  219. var res = await _db.Deleteable(resEntity).ExecuteCommandAsync();
  220. return res > 0;
  221. }
  222. }
  223. }