Category.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\Category as CategoryModel;
  14. /**
  15. * 商品分类模型
  16. * Class Category
  17. * @package app\store\model
  18. */
  19. class Category extends CategoryModel
  20. {
  21. /**
  22. * 添加新记录
  23. * @param $data
  24. * @return bool
  25. */
  26. public function add($data): bool
  27. {
  28. $data['store_id'] = self::$storeId;
  29. return $this->save($data);
  30. }
  31. /**
  32. * 编辑记录
  33. * @param $data
  34. * @return bool
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function edit($data): bool
  40. {
  41. // 判断上级分类是否为当前子级
  42. if ($data['parent_id'] > 0) {
  43. // 获取所有上级id集
  44. $parentIds = $this->getTopCategoryIds($data['parent_id']);
  45. if (in_array($this['category_id'], $parentIds)) {
  46. $this->error = '上级分类不允许设置为当前子分类';
  47. return false;
  48. }
  49. }
  50. // 是否删除图片
  51. !isset($data['image_id']) && $data['image_id'] = 0;
  52. return $this->save($data) !== false;
  53. }
  54. /**
  55. * 获取所有上级id集
  56. * @param int $categoryId
  57. * @param iterable|null $list
  58. * @return array
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. private function getTopCategoryIds(int $categoryId, iterable $list = null): array
  64. {
  65. static $parentIds = [];
  66. is_null($list) && $list = $this->getAll();
  67. foreach ($list as $item) {
  68. if ($item['category_id'] == $categoryId && $item['parent_id'] > 0) {
  69. $parentIds[] = $item['parent_id'];
  70. $this->getTopCategoryIds($item['parent_id'], $list);
  71. }
  72. }
  73. return $parentIds;
  74. }
  75. /**
  76. * 删除记录
  77. * @return bool
  78. */
  79. public function remove(): bool
  80. {
  81. // 判断是否存在下级分类
  82. if (static::detail(['parent_id' => $this['category_id']])) {
  83. $this->error = '当前分类下存在子分类,不允许删除';
  84. return false;
  85. }
  86. // 判断该分类是否被商品引用
  87. $goodsCount = GoodsCategoryRel::getCountByCategoryId($this['category_id']);
  88. if ($goodsCount > 0) {
  89. $this->error = "该分类被{$goodsCount}个商品引用,不允许删除";
  90. return false;
  91. }
  92. // 删除分类记录
  93. return $this->delete();
  94. }
  95. }