Category.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\api\model\service;
  3. use app\api\model\BaseModel;
  4. use think\Model;
  5. class Category extends BaseModel
  6. {
  7. // 表名
  8. protected $name = 'service_category';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'integer';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'type_text',
  18. 'status_text'
  19. ];
  20. public function getTypeList()
  21. {
  22. return ['app' => __('App'), 'store' => __('Store')];
  23. }
  24. public function getStatusList()
  25. {
  26. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  27. }
  28. public function getTypeTextAttr($value, $data)
  29. {
  30. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  31. $list = $this->getTypeList();
  32. return isset($list[$value]) ? $list[$value] : '';
  33. }
  34. public function getStatusTextAttr($value, $data)
  35. {
  36. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  37. $list = $this->getStatusList();
  38. return isset($list[$value]) ? $list[$value] : '';
  39. }
  40. public function store()
  41. {
  42. return $this->belongsTo('app\admin\model\Store', 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
  43. }
  44. public function fetchServiceCategory($type, $store_id = null)
  45. {
  46. $where = [
  47. "status" => \E_BASE_STATUS::Normal,
  48. "type" => $type,
  49. ];
  50. if ($store_id)
  51. $where['store_id'] = $store_id;
  52. return $this->where($where)
  53. ->order("sort", "desc")
  54. ->limit(4)
  55. ->select();
  56. }
  57. }