GoodsCategoryRel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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)->where('category_id', '=', $categoryId)->count();
  29. }
  30. /**
  31. * 获取商品分类ID集
  32. * @param int $goodsId
  33. * @return array
  34. */
  35. public static function getCategoryIds(int $goodsId)
  36. {
  37. return (new static)->where('goods_id', '=', $goodsId)->column('category_id');
  38. }
  39. /**
  40. * 批量写入商品分类记录
  41. * @param int $goodsId
  42. * @param array $categoryIds
  43. * @return array|false
  44. */
  45. public static function increased(int $goodsId, array $categoryIds)
  46. {
  47. $dataset = [];
  48. foreach ($categoryIds as $categoryId) {
  49. $dataset[] = [
  50. 'category_id' => $categoryId,
  51. 'goods_id' => $goodsId,
  52. 'store_id' => self::$storeId
  53. ];
  54. }
  55. return (new static)->addAll($dataset);
  56. }
  57. /**
  58. * 更新关系记录
  59. * @param $goodsId
  60. * @param array $categoryIds 新的分类集
  61. * @return array|false
  62. * @throws \Exception
  63. */
  64. public static function updates(int $goodsId, $categoryIds)
  65. {
  66. // 已分配的分类集
  67. $assignCategoryIds = self::getCategoryIdsByGoodsId($goodsId);
  68. // 找出删除的分类
  69. $deleteCategoryIds = array_diff($assignCategoryIds, $categoryIds);
  70. if (!empty($deleteCategoryIds)) {
  71. static::deleteAll([
  72. ['goods_id', '=', $goodsId],
  73. ['category_id', 'in', $deleteCategoryIds]
  74. ]);
  75. }
  76. // 找出添加的分类
  77. $newCategoryIds = array_diff($categoryIds, $assignCategoryIds);
  78. $dataset = [];
  79. foreach ($newCategoryIds as $categoryId) {
  80. $dataset[] = [
  81. 'goods_id' => $goodsId,
  82. 'category_id' => $categoryId,
  83. 'store_id' => self::$storeId,
  84. ];
  85. }
  86. return (new static)->addAll($dataset);
  87. }
  88. /**
  89. * 获取指定商品的所有分类id
  90. * @param int $goodsId
  91. * @return array
  92. */
  93. public static function getCategoryIdsByGoodsId(int $goodsId)
  94. {
  95. return (new static)->where('goods_id', '=', $goodsId)->column('category_id');
  96. }
  97. }