StoreCategory.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/12
  6. */
  7. namespace app\models\store;
  8. use crmeb\basic\BaseModel;
  9. use think\facade\Cache;
  10. /**
  11. * TODO 产品分类Model
  12. * Class StoreCategory
  13. * @package app\models\store
  14. */
  15. class StoreCategory extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'store_category';
  27. public static function pidByCategory($pid, $field = '*', $limit = 0)
  28. {
  29. $model = self::where('pid', $pid)->where('is_show', 1)->order('sort desc,id desc')->field($field);
  30. if ($limit) $model->limit($limit);
  31. return $model->select();
  32. }
  33. public static function pidBySidList($pid)
  34. {
  35. return self::where('pid', $pid)->field('id,cate_name,pid')->select();
  36. }
  37. public static function cateIdByPid($cateId)
  38. {
  39. return self::where('id', $cateId)->value('pid');
  40. }
  41. /*
  42. * 获取一级和二级分类
  43. * @return array
  44. * */
  45. public static function getProductCategory($expire = 800)
  46. {
  47. if (Cache::has('parent_category')) {
  48. return Cache::get('parent_category');
  49. } else {
  50. $parentCategory = self::pidByCategory(0, 'id,cate_name')->toArray();
  51. foreach ($parentCategory as $k => $category) {
  52. $category['child'] = self::pidByCategory($category['id'], 'id,cate_name,pic')->toArray();
  53. $parentCategory[$k] = $category;
  54. }
  55. Cache::set('parent_category', $parentCategory, $expire);
  56. return $parentCategory;
  57. }
  58. }
  59. /**
  60. * TODO 获取首页展示的二级分类 排序默认降序
  61. * @param string $field
  62. * @param int $limit
  63. * @return false|\PDOStatement|string|\think\Collection
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public static function byIndexList($limit = 4,bool $bool = true, $field = 'id,cate_name,pid,pic')
  69. {
  70. if(!$limit && !$bool) return [];
  71. return self::where('pid', '>', 0)->where('is_show', 1)->field($field)->order('sort DESC')->limit($limit)->select();
  72. }
  73. /**
  74. * 获取子集分类查询条件
  75. * @return \think\model\relation\HasMany
  76. */
  77. public function children()
  78. {
  79. return $this->hasMany(self::class, 'pid','id')->where('is_show',1)->order('sort DESC,id DESC');
  80. }
  81. }