ProductSpecService.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using ZR.Mall.Model;
  2. using ZR.Mall.Model.Dto;
  3. using ZR.Mall.Service.IService;
  4. namespace ZR.Mall.Service
  5. {
  6. /// <summary>
  7. /// 商品规格Service业务层处理
  8. /// </summary>
  9. [AppService(ServiceType = typeof(IProductSpecService))]
  10. public class ProductSpecService : BaseService<ProductSpec>, IProductSpecService
  11. {
  12. /// <summary>
  13. /// 查询商品规格列表
  14. /// </summary>
  15. /// <param name="parm"></param>
  16. /// <returns></returns>
  17. public PagedInfo<ProductSpecDto> GetList(ShoppingProductSpecQueryDto parm)
  18. {
  19. var predicate = QueryExp(parm);
  20. var response = Queryable()
  21. .Where(predicate.ToExpression())
  22. .ToPage<ProductSpec, ProductSpecDto>(parm);
  23. return response;
  24. }
  25. /// <summary>
  26. /// 添加商品规格
  27. /// </summary>
  28. /// <param name="model"></param>
  29. /// <returns></returns>
  30. public ProductSpec AddShoppingProductspec(ProductSpec model)
  31. {
  32. return Insertable(model).ExecuteReturnEntity();
  33. }
  34. /// <summary>
  35. /// 修改商品规格
  36. /// </summary>
  37. /// <param name="productId"></param>
  38. /// <param name="newSpecs"></param>
  39. /// <returns></returns>
  40. public long UpdateProductSpec(long productId, List<ProductSpec> newSpecs)
  41. {
  42. var result = 0;
  43. // 获取数据库中旧数据
  44. var oldSpecs = GetList(f => f.ProductId == productId);
  45. // 提取 ID 列表
  46. var newIds = newSpecs.Where(x => x.Id > 0).Select(x => x.Id).ToList();
  47. var oldIds = oldSpecs.Select(x => x.Id).ToList();
  48. // ➤ 更新:有 ID 且在数据库中
  49. var updateSpecs = newSpecs.Where(x => x.Id > 0 && oldIds.Contains(x.Id)).ToList();
  50. foreach (var spec in updateSpecs)
  51. {
  52. result = Update(spec, true, "数据修改"); // 可带日志记录
  53. }
  54. // ➤ 插入:没有 ID 的新增项
  55. var insertSpecs = newSpecs.Where(x => x.Id <= 0).ToList();
  56. if (insertSpecs.Count != 0)
  57. InsertRange(insertSpecs);
  58. // ➤ 删除:数据库有但前端没传的
  59. var deleteIds = oldSpecs
  60. .Where(x => !newIds.Contains(x.Id)) // 不在新 ID 列表中
  61. .Select(x => x.Id)
  62. .ToList();
  63. if (deleteIds.Count != 0)
  64. Delete(deleteIds);
  65. return result;
  66. }
  67. /// <summary>
  68. /// 查询导出表达式
  69. /// </summary>
  70. /// <param name="parm"></param>
  71. /// <returns></returns>
  72. private static Expressionable<ProductSpec> QueryExp(ShoppingProductSpecQueryDto parm)
  73. {
  74. var predicate = Expressionable.Create<ProductSpec>();
  75. return predicate;
  76. }
  77. public long DeleteSpecByProductId(long productId)
  78. {
  79. return Deleteable()
  80. .Where(f => f.ProductId == productId)
  81. .EnableDiffLogEvent("删除商品规格")
  82. .ExecuteCommand();
  83. }
  84. }
  85. }