Category.php 1.9 KB

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