Category.php 1.7 KB

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