Category.php 2.0 KB

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