GoodsCategoryRel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model;
  13. use app\common\model\GoodsCategoryRel as GoodsCategoryRelModel;
  14. /**
  15. * 商品与分类关系模型
  16. * Class GoodsCategoryRel
  17. * @package app\store\model
  18. */
  19. class GoodsCategoryRel extends GoodsCategoryRelModel
  20. {
  21. /**
  22. * 根据分类ID获取记录总数量
  23. * @param int $categoryId
  24. * @return int
  25. */
  26. public static function getCountByCategoryId(int $categoryId)
  27. {
  28. return (new static)->alias('m')
  29. ->join('goods', 'goods.goods_id = m.goods_id')
  30. ->where('m.category_id', '=', $categoryId)
  31. ->where('goods.is_delete', '=', 0)
  32. ->count();
  33. }
  34. /**
  35. * 获取商品分类ID集
  36. * @param int $goodsId
  37. * @return array
  38. */
  39. public static function getCategoryIds(int $goodsId)
  40. {
  41. return (new static)->where('goods_id', '=', $goodsId)->column('category_id');
  42. }
  43. /**
  44. * 批量写入商品分类记录
  45. * @param int $goodsId
  46. * @param array $categoryIds
  47. * @return array|false
  48. */
  49. public static function increased(int $goodsId, array $categoryIds)
  50. {
  51. $dataset = [];
  52. foreach ($categoryIds as $categoryId) {
  53. $dataset[] = [
  54. 'category_id' => $categoryId,
  55. 'goods_id' => $goodsId,
  56. 'store_id' => self::$storeId
  57. ];
  58. }
  59. return (new static)->addAll($dataset);
  60. }
  61. /**
  62. * 更新关系记录
  63. * @param $goodsId
  64. * @param array $categoryIds 新的分类集
  65. * @return array|false
  66. * @throws \Exception
  67. */
  68. public static function updates(int $goodsId, $categoryIds)
  69. {
  70. // 已分配的分类集
  71. $assignCategoryIds = self::getCategoryIdsByGoodsId($goodsId);
  72. // 找出删除的分类
  73. $deleteCategoryIds = array_diff($assignCategoryIds, $categoryIds);
  74. if (!empty($deleteCategoryIds)) {
  75. static::deleteAll([
  76. ['goods_id', '=', $goodsId],
  77. ['category_id', 'in', $deleteCategoryIds]
  78. ]);
  79. }
  80. // 找出添加的分类
  81. $newCategoryIds = array_diff($categoryIds, $assignCategoryIds);
  82. $dataset = [];
  83. foreach ($newCategoryIds as $categoryId) {
  84. $dataset[] = [
  85. 'goods_id' => $goodsId,
  86. 'category_id' => $categoryId,
  87. 'store_id' => self::$storeId,
  88. ];
  89. }
  90. return (new static)->addAll($dataset);
  91. }
  92. /**
  93. * 获取指定商品的所有分类id
  94. * @param int $goodsId
  95. * @return array
  96. */
  97. public static function getCategoryIdsByGoodsId(int $goodsId)
  98. {
  99. return (new static)->where('goods_id', '=', $goodsId)->column('category_id');
  100. }
  101. }