Category.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\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 false|int
  25. */
  26. public function add($data)
  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)
  40. {
  41. // 判断上级分类是否为当前子级
  42. if ($data['parent_id'] > 0) {
  43. // 上级不能是自己
  44. if ($this['category_id'] == $data['parent_id']) {
  45. $this->error = '上级分类不允许设置为当前分类';
  46. return false;
  47. }
  48. // 获取所有上级id集
  49. $parentIds = $this->getTopCategoryIds($data['parent_id']);
  50. if (in_array($this['category_id'], $parentIds)) {
  51. $this->error = '上级分类不允许设置为当前子分类';
  52. return false;
  53. }
  54. }
  55. // 是否删除图片
  56. !isset($data['image_id']) && $data['image_id'] = 0;
  57. return $this->save($data) !== false;
  58. }
  59. /**
  60. * 获取所有上级id集
  61. * @param int $categoryId
  62. * @param null|array $list
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. private function getTopCategoryIds(int $categoryId, $list = null)
  69. {
  70. static $parentIds = [];
  71. is_null($list) && $list = $this->getAll();
  72. foreach ($list as $item) {
  73. if ($item['category_id'] == $categoryId && $item['parent_id'] > 0) {
  74. $parentIds[] = $item['parent_id'];
  75. $this->getTopCategoryIds($item['parent_id'], $list);
  76. }
  77. }
  78. return $parentIds;
  79. }
  80. /**
  81. * 删除记录
  82. * @return bool
  83. */
  84. public function remove()
  85. {
  86. // 判断是否存在下级分类
  87. if (static::detail(['parent_id' => $this['category_id']])) {
  88. $this->error = '当前分类下存在子分类,不允许删除';
  89. return false;
  90. }
  91. // 判断该分类是否被商品引用
  92. $goodsCount = GoodsCategoryRel::getCountByCategoryId($this['category_id']);
  93. if ($goodsCount > 0) {
  94. $this->error = "该分类被{$goodsCount}个商品引用,不允许删除";
  95. return false;
  96. }
  97. // 删除分类记录
  98. return $this->delete();
  99. }
  100. }