Category.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\store\model\sharing;
  3. use think\Cache;
  4. use app\common\model\sharing\Category as CategoryModel;
  5. /**
  6. * 拼团商品分类模型
  7. * Class Category
  8. * @package app\store\model
  9. */
  10. class Category extends CategoryModel
  11. {
  12. /**
  13. * 分类详情
  14. * @param $category_id
  15. * @return Category|null
  16. * @throws \think\exception\DbException
  17. */
  18. public static function detail($category_id)
  19. {
  20. return static::get($category_id);
  21. }
  22. /**
  23. * 添加新记录
  24. * @param $data
  25. * @return false|int
  26. */
  27. public function add($data)
  28. {
  29. $data['wxapp_id'] = self::$wxapp_id;
  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. $this->deleteCache();
  41. return $this->allowField(true)->save($data);
  42. }
  43. /**
  44. * 删除商品分类
  45. * @param $category_id
  46. * @return bool|int
  47. */
  48. public function remove($category_id)
  49. {
  50. // 判断是否存在商品
  51. if ($goodsCount = (new Goods)->getGoodsTotal(['category_id' => $category_id])) {
  52. $this->error = '该分类下存在' . $goodsCount . '个商品,不允许删除';
  53. return false;
  54. }
  55. // 判断是否存在子分类
  56. if ((new self)->where(['parent_id' => $category_id])->count()) {
  57. $this->error = '该分类下存在子分类,请先删除';
  58. return false;
  59. }
  60. $this->deleteCache();
  61. return $this->delete();
  62. }
  63. /**
  64. * 删除缓存
  65. * @return bool
  66. */
  67. private function deleteCache()
  68. {
  69. return Cache::rm('sharing_category_' . self::$wxapp_id);
  70. }
  71. }