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

309 lines
13 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. public async Task<List<GoodsAttributeView>> GetGoodsAttributeList_alm()
  53. {
  54. List<IConditionalModel> conModels = new List<IConditionalModel>();
  55. string groupId = App.HttpContext.Request.Headers["groupId"].ToString();
  56. if (string.IsNullOrWhiteSpace(groupId)) throw Oops.Oh("加盟商id不能为空");
  57. var res =await _db.Queryable<BPA_GoodsAttribute>()
  58. .Where(a => a.GroupId == groupId)
  59. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  60. .Select(a => new GoodsAttributeView
  61. {
  62. Id = a.Id,
  63. AttributeName = a.AttributeName,
  64. GoodsTypeId = a.GoodsTypeId,
  65. Sort = a.Sort,
  66. })
  67. .Mapper(x =>
  68. {
  69. var list = _db.Queryable<BPA_GoodsAttributeValue>().Where(p => p.GoodsAttributeId == x.Id).ToList();
  70. x.AttributeValueList = list;
  71. })
  72. .ToListAsync();
  73. return res;
  74. }
  75. /// <summary>
  76. /// 添加/修改
  77. /// </summary>
  78. /// <param name="dto"></param>
  79. /// <returns></returns>
  80. public async Task<bool> AddGoodsAttribute(GoodsAttributeDto dto)
  81. {
  82. try
  83. {
  84. if (!string.IsNullOrWhiteSpace(dto.Id))
  85. {
  86. _db.Ado.BeginTran();
  87. var resEntity = _db.Queryable<BPA_GoodsAttribute>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
  88. if (null == resEntity)
  89. {
  90. return false;
  91. }
  92. resEntity.AttributeName = dto.AttributeName;
  93. resEntity.GoodsTypeId = dto.GoodsTypeId;
  94. resEntity.Sort = dto.Sort;
  95. await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
  96. var dellist = _db.Queryable<BPA_GoodsAttributeValue>().Where(it => it.GoodsAttributeId == dto.Id).ToList();
  97. await _db.Deleteable(dellist).ExecuteCommandAsync(); ;
  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 = dto.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. else
  112. {
  113. _db.Ado.BeginTran();
  114. var newGoodsAttribute = new BPA_GoodsAttribute
  115. {
  116. AttributeName = dto.AttributeName,
  117. GoodsTypeId = dto.GoodsTypeId,
  118. Sort = dto.Sort,
  119. };
  120. var GoodsAttribute = await _db.Insertable(newGoodsAttribute).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
  121. List<BPA_GoodsAttributeValue> list = new();
  122. for (int i = 0; i < dto.GoodsAttributeValueList.Count; i++)
  123. {
  124. BPA_GoodsAttributeValue GoodsAttributeValue = new();
  125. GoodsAttributeValue.AttributeValue = dto.GoodsAttributeValueList[i].AttributeValue;
  126. GoodsAttributeValue.GoodsAttributeId = GoodsAttribute.Id;
  127. GoodsAttributeValue.Sort = dto.GoodsAttributeValueList[i].Sort;
  128. list.Add(GoodsAttributeValue);
  129. }
  130. var res = await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  131. _db.Ado.CommitTran();
  132. return res > 0;
  133. }
  134. }
  135. catch (Exception)
  136. {
  137. _db.Ado.RollbackTran();
  138. throw Oops.Oh("添加失败");
  139. }
  140. }
  141. /// <summary>
  142. /// 删除
  143. /// </summary>
  144. /// <param name="Ids"></param>
  145. /// <returns></returns>
  146. public async Task<bool> DeleteGoodsAttribute(string[] Ids)
  147. {
  148. // 查询数据库中是否存在未删除的商品
  149. var resEntitites = _db.Queryable<BPA_GoodsAttribute>().In(Ids).ToList();
  150. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  151. if (res > 0)
  152. {
  153. var resEntititesattr = _db.Queryable<BPA_GoodsAttributeValue>().In("GoodsAttributeId", Ids).ToList();
  154. if (resEntititesattr != null)
  155. {
  156. await _db.Deleteable(resEntititesattr).ExecuteCommandAsync();
  157. }
  158. }
  159. return res > 0;
  160. }
  161. /// <summary>
  162. /// 查询商品属性值列表
  163. /// </summary>
  164. /// <param name="goodsAttributeId"></param>
  165. /// <returns></returns>
  166. public async Task<List<BPA_GoodsAttributeValue>> GetGoodsAttributeValueAsync(string goodsAttributeId)
  167. {
  168. string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  169. var res =await _db.Queryable<BPA_GoodsAttributeValue>().Where(x=>x.GoodsAttributeId== goodsAttributeId && x.IsDeleted==0)
  170. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  171. .Select(a => new BPA_GoodsAttributeValue
  172. {
  173. Id = a.Id.SelectAll(),
  174. })
  175. .ToListAsync();
  176. return res;
  177. }
  178. /// <summary>
  179. /// 添加/修改
  180. /// </summary>
  181. /// <param name="dto"></param>
  182. /// <returns></returns>
  183. public async Task<bool> AddGoodsAttributeValue(List<GoodsAttributeValueDto> dto)
  184. {
  185. List<BPA_GoodsAttributeValue> editlist = new();
  186. List<BPA_GoodsAttributeValue> addlist = new();
  187. var res = 0;
  188. for (int i = 0; i < dto.Count; i++)
  189. {
  190. Guid newGuid = Guid.Empty;
  191. if (Guid.TryParse(dto[i].Id, out newGuid))
  192. {
  193. var resEntity = _db.Queryable<BPA_GoodsAttributeValue>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto[i].Id);
  194. resEntity.AttributeValue = dto[i].AttributeValue;
  195. resEntity.Sort = dto[i].Sort;
  196. resEntity.WaiKey= dto[i].WaiKey;
  197. editlist.Add(resEntity);
  198. }
  199. else
  200. {
  201. var newGoods = new BPA_GoodsAttributeValue
  202. {
  203. GoodsAttributeId = dto[i].GoodsAttributeId,
  204. AttributeValue = dto[i].AttributeValue,
  205. Sort = dto[i].Sort,
  206. WaiKey = dto[i].WaiKey
  207. };
  208. addlist.Add(newGoods);
  209. }
  210. }
  211. if (editlist.Count > 0)
  212. {
  213. res = await _db.Updateable(editlist).ExecuteCommandAsync();
  214. }
  215. if (addlist.Count > 0)
  216. {
  217. res = await _db.Insertable(addlist).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
  218. }
  219. return res > 0;
  220. }
  221. /// <summary>
  222. /// 删除
  223. /// </summary>
  224. /// <param name="id"></param>
  225. /// <returns></returns>
  226. public async Task<bool> DeleteGoodsAttributeValue(string id)
  227. {
  228. // 查询数据库中是否存在未删除的商品
  229. var resEntitites = _db.Queryable<BPA_GoodsAttributeValue>().In(id).ToList();
  230. var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  231. if (res > 0)
  232. {
  233. //1.查询上架商品
  234. //2.删除上架商品
  235. }
  236. return res > 0;
  237. }
  238. /// <summary>
  239. /// 根据商品id查询商品属性
  240. /// </summary>
  241. /// <param name="id"></param>
  242. /// <returns></returns>
  243. public async Task<List<GoodsAttributeList>> GetByGoodsIdAttribute(string id)
  244. {
  245. var goods=await _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == id).FirstAsync();
  246. if (goods == null) throw Oops.Oh("商品不存在");
  247. var goodsAttributeList = await _db.Queryable<BPA_GoodsAttribute>().Where(x => x.GoodsTypeId.Contains( goods.GoodsTypeId) && x.IsDeleted == 0)
  248. .Select(x=>new GoodsAttributeList()
  249. {
  250. GoodsAttributeId=x.Id,
  251. AttributeName=x.AttributeName,
  252. GoodsTypeId=goods.GoodsTypeId,
  253. Sort=x.Sort,
  254. }).Mapper(p =>
  255. {
  256. p.GoodsAttributeValueList = _db.Queryable<BPA_GoodsAttributeValue>().Where(x => x.GoodsAttributeId == p.GoodsAttributeId && x.IsDeleted==0).Select(x => new GoodsAttributeValueList()
  257. {
  258. GoodsAttributeValuId = x.Id,
  259. GoodsAttributeId = p.GoodsAttributeId,
  260. AttributeValue = x.AttributeValue,
  261. Sort = x.Sort
  262. }).ToList();
  263. })
  264. .OrderBy(x=>x.Sort,OrderByType.Asc)
  265. .ToListAsync();
  266. return goodsAttributeList;
  267. }
  268. public async Task<List<GoodsAttributeList>> GetByNameAttribute(string name)
  269. {
  270. //var goods = await _db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == id).FirstAsync();
  271. //if (goods == null) throw Oops.Oh("商品不存在");
  272. var goodsAttributeList = await _db.Queryable<BPA_GoodsAttribute>().Where(x => x.AttributeName== name && x.IsDeleted == 0)
  273. .Select(x => new GoodsAttributeList()
  274. {
  275. GoodsAttributeId = x.Id,
  276. AttributeName = x.AttributeName,
  277. //GoodsTypeId = goods.Goods_TypeID,
  278. Sort = x.Sort,
  279. }).Mapper(p =>
  280. {
  281. p.GoodsAttributeValueList = _db.Queryable<BPA_GoodsAttributeValue>().Where(x => x.GoodsAttributeId == p.GoodsAttributeId && x.IsDeleted == 0).Select(x => new GoodsAttributeValueList()
  282. {
  283. GoodsAttributeValuId = x.Id,
  284. GoodsAttributeId = p.GoodsAttributeId,
  285. AttributeValue = x.AttributeValue,
  286. Sort = x.Sort
  287. }).ToList();
  288. })
  289. .OrderBy(x => x.Sort, OrderByType.Asc)
  290. .ToListAsync();
  291. return goodsAttributeList;
  292. }
  293. }
  294. }