Category.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\store\model;
  3. use think\Cache;
  4. use app\common\model\Category as CategoryModel;
  5. /**
  6. * 商品分类模型
  7. * Class Category
  8. * @package app\store\model
  9. */
  10. class Category extends CategoryModel
  11. {
  12. /**
  13. * 添加新记录
  14. * @param $data
  15. * @return false|int
  16. */
  17. public function add($data)
  18. {
  19. $data['wxapp_id'] = self::$wxapp_id;
  20. // if (!empty($data['image'])) {
  21. // $data['image_id'] = UploadFile::getFildIdByName($data['image']);
  22. // }
  23. $this->deleteCache();
  24. return $this->allowField(true)->save($data);
  25. }
  26. /**
  27. * 编辑记录
  28. * @param $data
  29. * @return bool|int
  30. */
  31. public function edit($data)
  32. {
  33. // 验证:一级分类如果存在子类,则不允许移动
  34. if ($data['parent_id'] > 0 && static::hasSubCategory($this['category_id'])) {
  35. $this->error = '该分类下存在子分类,不可以移动';
  36. return false;
  37. }
  38. $this->deleteCache();
  39. !array_key_exists('image_id', $data) && $data['image_id'] = 0;
  40. return $this->allowField(true)->save($data) !== false;
  41. }
  42. /**
  43. * 删除商品分类
  44. * @param $categoryId
  45. * @return bool|int
  46. */
  47. public function remove($categoryId)
  48. {
  49. // 判断是否存在商品
  50. if ($goodsCount = (new Goods)->getGoodsTotal(['category_id' => $categoryId])) {
  51. $this->error = '该分类下存在' . $goodsCount . '个商品,不允许删除';
  52. return false;
  53. }
  54. // 判断是否存在子分类
  55. if (static::hasSubCategory($categoryId)) {
  56. $this->error = '该分类下存在子分类,请先删除';
  57. return false;
  58. }
  59. $this->deleteCache();
  60. return $this->delete();
  61. }
  62. /**
  63. * 删除缓存
  64. * @return bool
  65. */
  66. private function deleteCache()
  67. {
  68. return Cache::rm('category_' . static::$wxapp_id);
  69. }
  70. }