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

286 lines
12 KiB

  1. using BPA.SAAS.Manage.Application.DataBase.Interface;
  2. using BPA.SAAS.Manage.Core.Base;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Core.DataBase;
  5. using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsAttribute;
  6. using StackExchange.Profiling.Internal;
  7. using BPA.SAAS.Manage.Comm.Enum;
  8. namespace BPA.Franchisee.Application.FranchiseeCenter.GoodsServices
  9. {
  10. public class GoodsAttributeService: IGoodsAttributeService, ITransient
  11. {
  12. private readonly ISqlSugarClient _db;
  13. public GoodsAttributeService(ISqlSugarClient db)
  14. {
  15. _db = db; // 推荐操作
  16. }
  17. /// <summary>
  18. /// 分页查询
  19. /// </summary>
  20. /// <param name="dto"></param>m
  21. /// <returns></returns>
  22. public PageUtil GetGoodsAttributePageAsync(GoodsAttributeQueryDto dto)
  23. {
  24. List<IConditionalModel> conModels = new List<IConditionalModel>();
  25. string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  26. int total = new RefAsync<int>();
  27. var res = _db.Queryable<BPA_GoodsAttribute>()
  28. .Where(a=> a.IsDeleted == 0)
  29. .WhereIF(!dto.AttributeName.IsNullOrWhiteSpace(),x=>x.AttributeName.Contains(dto.AttributeName))
  30. .WhereIF(!dto.GoodsTypeId.IsNullOrWhiteSpace(), x => x.GoodsTypeId.Contains(dto.GoodsTypeId))
  31. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  32. .Select(a => new GoodsAttributeView
  33. {
  34. Id = a.Id,
  35. AttributeName=a.AttributeName,
  36. GoodsTypeId= a.GoodsTypeId,
  37. Sort =a.Sort,
  38. })
  39. .Mapper(x =>
  40. {
  41. var list=_db.Queryable<BPA_GoodsAttributeValue>().Where(p=> p.GoodsAttributeId==x.Id).ToList();
  42. x.AttributeValueList = list;
  43. })
  44. .ToPageList(dto.Current, dto.PageSize, ref 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> AddGoodsAttribute(GoodsAttributeDto dto)
  58. {
  59. try
  60. {
  61. if (!string.IsNullOrWhiteSpace(dto.Id))
  62. {
  63. _db.Ado.BeginTran();
  64. var resEntity = _db.Queryable<BPA_GoodsAttribute>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  65. if (null == resEntity)
  66. {
  67. return false;
  68. }
  69. resEntity.AttributeName = dto.AttributeName;
  70. resEntity.GoodsTypeId = dto.GoodsTypeId;
  71. resEntity.Sort = dto.Sort;
  72. await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  73. var dellist = _db.Queryable<BPA_GoodsAttributeValue>().Where(it => it.GoodsAttributeId == dto.Id).ToList();
  74. await _db.Deleteable(dellist).ExecuteCommandAsync(); ;
  75. List<BPA_GoodsAttributeValue> list = new();
  76. for (int i = 0; i < dto.GoodsAttributeValueList.Count; i++)
  77. {
  78. BPA_GoodsAttributeValue GoodsAttributeValue = new();
  79. GoodsAttributeValue.AttributeValue = dto.GoodsAttributeValueList[i].AttributeValue;
  80. GoodsAttributeValue.GoodsAttributeId = dto.Id;
  81. GoodsAttributeValue.Sort= dto.GoodsAttributeValueList[i].Sort;
  82. list.Add(GoodsAttributeValue);
  83. }
  84. var res = await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  85. _db.Ado.CommitTran();
  86. return res > 0;
  87. }
  88. else
  89. {
  90. _db.Ado.BeginTran();
  91. var newGoodsAttribute = new BPA_GoodsAttribute
  92. {
  93. AttributeName = dto.AttributeName,
  94. GoodsTypeId = dto.GoodsTypeId,
  95. Sort = dto.Sort,
  96. };
  97. var GoodsAttribute = await _db.Insertable(newGoodsAttribute).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  98. List<BPA_GoodsAttributeValue> list = new();
  99. for (int i = 0; i < dto.GoodsAttributeValueList.Count; i++)
  100. {
  101. BPA_GoodsAttributeValue GoodsAttributeValue = new();
  102. GoodsAttributeValue.AttributeValue = dto.GoodsAttributeValueList[i].AttributeValue;
  103. GoodsAttributeValue.GoodsAttributeId = GoodsAttribute.Id;
  104. GoodsAttributeValue.Sort = dto.GoodsAttributeValueList[i].Sort;
  105. list.Add(GoodsAttributeValue);
  106. }
  107. var res = await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  108. _db.Ado.CommitTran();
  109. return res > 0;
  110. }
  111. }
  112. catch (Exception)
  113. {
  114. _db.Ado.RollbackTran();
  115. throw Oops.Oh("添加失败");
  116. }
  117. }
  118. /// <summary>
  119. /// 删除
  120. /// </summary>
  121. /// <param name="Ids"></param>
  122. /// <returns></returns>
  123. public async Task<bool> DeleteGoodsAttribute(string[] Ids)
  124. {
  125. // 查询数据库中是否存在未删除的商品
  126. var resEntitites = _db.Queryable<BPA_GoodsAttribute>().In(Ids).ToList();
  127. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  128. if (res > 0)
  129. {
  130. var resEntititesattr = _db.Queryable<BPA_GoodsAttributeValue>().In("GoodsAttributeId", Ids).ToList();
  131. if (resEntititesattr != null)
  132. {
  133. await _db.Deleteable(resEntititesattr).ExecuteCommandAsync();
  134. }
  135. }
  136. return res > 0;
  137. }
  138. /// <summary>
  139. /// 查询商品属性值列表
  140. /// </summary>
  141. /// <param name="goodsAttributeId"></param>
  142. /// <returns></returns>
  143. public async Task<List<BPA_GoodsAttributeValue>> GetGoodsAttributeValueAsync(string goodsAttributeId)
  144. {
  145. string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  146. var res =await _db.Queryable<BPA_GoodsAttributeValue>().Where(x=>x.GoodsAttributeId== goodsAttributeId && x.IsDeleted==0)
  147. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  148. .Select(a => new BPA_GoodsAttributeValue
  149. {
  150. Id = a.Id.SelectAll(),
  151. })
  152. .ToListAsync();
  153. return res;
  154. }
  155. /// <summary>
  156. /// 添加/修改
  157. /// </summary>
  158. /// <param name="dto"></param>
  159. /// <returns></returns>
  160. public async Task<bool> AddGoodsAttributeValue(List<GoodsAttributeValueDto> dto)
  161. {
  162. List<BPA_GoodsAttributeValue> editlist = new();
  163. List<BPA_GoodsAttributeValue> addlist = new();
  164. var res = 0;
  165. for (int i = 0; i < dto.Count; i++)
  166. {
  167. Guid newGuid = Guid.Empty;
  168. if (Guid.TryParse(dto[i].Id, out newGuid))
  169. {
  170. var resEntity = _db.Queryable<BPA_GoodsAttributeValue>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto[i].Id);
  171. resEntity.AttributeValue = dto[i].AttributeValue;
  172. resEntity.Sort = dto[i].Sort;
  173. resEntity.WaiKey= dto[i].WaiKey;
  174. editlist.Add(resEntity);
  175. }
  176. else
  177. {
  178. var newGoods = new BPA_GoodsAttributeValue
  179. {
  180. GoodsAttributeId = dto[i].GoodsAttributeId,
  181. AttributeValue = dto[i].AttributeValue,
  182. Sort = dto[i].Sort,
  183. WaiKey = dto[i].WaiKey
  184. };
  185. addlist.Add(newGoods);
  186. }
  187. }
  188. if (editlist.Count > 0)
  189. {
  190. res = await _db.Updateable(editlist).ExecuteCommandAsync();
  191. }
  192. if (addlist.Count > 0)
  193. {
  194. res = await _db.Insertable(addlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  195. }
  196. return res > 0;
  197. }
  198. /// <summary>
  199. /// 删除
  200. /// </summary>
  201. /// <param name="id"></param>
  202. /// <returns></returns>
  203. public async Task<bool> DeleteGoodsAttributeValue(string id)
  204. {
  205. // 查询数据库中是否存在未删除的商品
  206. var resEntitites = _db.Queryable<BPA_GoodsAttributeValue>().In(id).ToList();
  207. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  208. if (res > 0)
  209. {
  210. //1.查询上架商品
  211. //2.删除上架商品
  212. }
  213. return res > 0;
  214. }
  215. /// <summary>
  216. /// 根据商品id查询商品属性
  217. /// </summary>
  218. /// <param name="id"></param>
  219. /// <returns></returns>
  220. public async Task<List<GoodsAttributeList>> GetByGoodsIdAttribute(string id)
  221. {
  222. var goods=await _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == id).FirstAsync();
  223. if (goods == null) throw Oops.Oh("商品不存在");
  224. var goodsAttributeList = await _db.Queryable<BPA_GoodsAttribute>().Where(x => x.GoodsTypeId.Contains( goods.GoodsTypeId) && x.IsDeleted == 0)
  225. .Select(x=>new GoodsAttributeList()
  226. {
  227. GoodsAttributeId=x.Id,
  228. AttributeName=x.AttributeName,
  229. GoodsTypeId=goods.GoodsTypeId,
  230. Sort=x.Sort,
  231. }).Mapper(p =>
  232. {
  233. p.GoodsAttributeValueList = _db.Queryable<BPA_GoodsAttributeValue>().Where(x => x.GoodsAttributeId == p.GoodsAttributeId && x.IsDeleted==0).Select(x => new GoodsAttributeValueList()
  234. {
  235. GoodsAttributeValuId = x.Id,
  236. GoodsAttributeId = p.GoodsAttributeId,
  237. AttributeValue = x.AttributeValue,
  238. Sort = x.Sort
  239. }).ToList();
  240. })
  241. .OrderBy(x=>x.Sort,OrderByType.Asc)
  242. .ToListAsync();
  243. return goodsAttributeList;
  244. }
  245. public async Task<List<GoodsAttributeList>> GetByNameAttribute(string name)
  246. {
  247. //var goods = await _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == id).FirstAsync();
  248. //if (goods == null) throw Oops.Oh("商品不存在");
  249. var goodsAttributeList = await _db.Queryable<BPA_GoodsAttribute>().Where(x => x.AttributeName== name && x.IsDeleted == 0)
  250. .Select(x => new GoodsAttributeList()
  251. {
  252. GoodsAttributeId = x.Id,
  253. AttributeName = x.AttributeName,
  254. //GoodsTypeId = goods.Goods_TypeID,
  255. Sort = x.Sort,
  256. }).Mapper(p =>
  257. {
  258. p.GoodsAttributeValueList = _db.Queryable<BPA_GoodsAttributeValue>().Where(x => x.GoodsAttributeId == p.GoodsAttributeId && x.IsDeleted == 0).Select(x => new GoodsAttributeValueList()
  259. {
  260. GoodsAttributeValuId = x.Id,
  261. GoodsAttributeId = p.GoodsAttributeId,
  262. AttributeValue = x.AttributeValue,
  263. Sort = x.Sort
  264. }).ToList();
  265. })
  266. .OrderBy(x => x.Sort, OrderByType.Asc)
  267. .ToListAsync();
  268. return goodsAttributeList;
  269. }
  270. }
  271. }