Category.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\other;
  12. use app\model\product\label\StoreProductLabel;
  13. use app\model\product\specs\StoreProductSpecs;
  14. use app\model\user\label\UserLabel;
  15. use app\model\activity\table\TableQrcode;
  16. use crmeb\basic\BaseModel;
  17. use crmeb\traits\ModelTrait;
  18. use think\Model;
  19. /**
  20. * 分类表
  21. * Class Category
  22. * @package app\model\other
  23. */
  24. class Category extends BaseModel
  25. {
  26. use ModelTrait;
  27. /**
  28. * 表名
  29. * @var string
  30. */
  31. protected $name = 'category';
  32. /**
  33. * 主键
  34. * @var string
  35. */
  36. protected $pk = 'id';
  37. /**
  38. * 用户标签
  39. * @return \think\model\relation\HasMany
  40. */
  41. public function label()
  42. {
  43. return $this->hasMany(UserLabel::class, 'label_cate', 'id');
  44. }
  45. /**
  46. * 商品标签
  47. * @return \think\model\relation\HasMany
  48. */
  49. public function productLabel()
  50. {
  51. return $this->hasMany(StoreProductLabel::class, 'label_cate', 'id');
  52. }
  53. /**
  54. * 商品参数
  55. * @return \think\model\relation\HasMany
  56. */
  57. public function specs()
  58. {
  59. return $this->hasMany(StoreProductSpecs::class, 'temp_id', 'id');
  60. }
  61. /**
  62. * 获取子集分类查询条件
  63. * @return \think\model\relation\HasMany
  64. */
  65. public function children()
  66. {
  67. return $this->hasMany(self::class, 'pid', 'id')->where('is_show', 1)->order('sort DESC,id DESC');
  68. }
  69. /**分类关联桌码 一对多
  70. * @return \think\model\relation\HasMany
  71. */
  72. public function tableQrcode()
  73. {
  74. return $this->hasMany(TableQrcode::class, 'cate_id', 'id')->where('is_using', 1)->where('is_del', 0)->order('table_number ASC,id ASC');
  75. }
  76. /**
  77. * ID搜索器
  78. * @param Model $query
  79. * @param $value
  80. */
  81. public function searchIdAttr($query, $value)
  82. {
  83. if (is_array($value)) {
  84. if ($value) $query->whereIn('id', $value);
  85. } else {
  86. if ($value) $query->where('id', $value);
  87. }
  88. }
  89. /**
  90. * PID搜索器
  91. * @param Model $query
  92. * @param $value
  93. */
  94. public function searchPidAttr($query, $value)
  95. {
  96. if (is_array($value)) {
  97. if ($value) $query->whereIn('pid', $value);
  98. } else {
  99. if ($value !== '') $query->where('pid', $value);
  100. }
  101. }
  102. /**
  103. * 归属人
  104. * @param Model $query
  105. * @param $value
  106. */
  107. public function searchOwnerIdAttr($query, $value)
  108. {
  109. $query->where('owner_id', $value);
  110. }
  111. /**
  112. * 商户搜索器
  113. * @param Model $query
  114. * @param $value
  115. */
  116. public function searchTypeAttr($query, $value)
  117. {
  118. if (is_array($value)) {
  119. if ($value) $query->whereIn('type', $value);
  120. } else {
  121. if ($value !== '') $query->where('type', $value);
  122. }
  123. }
  124. /**
  125. * 关联门店ID、供应商ID搜索器
  126. * @param Model $query
  127. * @param $value
  128. */
  129. public function searchRelationIdAttr($query, $value)
  130. {
  131. if (is_array($value)) {
  132. if ($value) $query->whereIn('relation_id', $value);
  133. } else {
  134. if ($value !== '') $query->where('relation_id', $value);
  135. }
  136. }
  137. /**
  138. * 供应商
  139. * @param Model $query
  140. * @param $value
  141. */
  142. public function searchSupplierIdAttr($query, $value)
  143. {
  144. if (is_array($value)) {
  145. if ($value) $query->whereIn('relation_id', $value)->where('type', 2);
  146. } else {
  147. if ($value !== '') $query->where('relation_id', $value)->where('type', 2);
  148. }
  149. }
  150. /**
  151. * 门店
  152. * @param Model $query
  153. * @param $value
  154. */
  155. public function searchStoreIdAttr($query, $value)
  156. {
  157. if (is_array($value)) {
  158. if ($value) $query->whereIn('relation_id', $value)->where('type', 1);
  159. } else {
  160. if ($value !== '') $query->where('relation_id', $value)->where('type', 1);
  161. }
  162. }
  163. /**
  164. * 标签类型0:用户1:客服话术
  165. * @param Model $query
  166. * @param $value
  167. */
  168. public function searchGroupAttr($query, $value)
  169. {
  170. if ($value !== '') $query->where('group', $value);
  171. }
  172. /**
  173. * @param $query
  174. * @param $value
  175. */
  176. public function searchNotIdAttr($query, $value)
  177. {
  178. $query->where('id', '<>', $value);
  179. }
  180. /**
  181. * 分类是否显示搜索器
  182. * @param Model $query
  183. * @param $value
  184. * @param $data
  185. */
  186. public function searchIsShowAttr($query, $value, $data)
  187. {
  188. if ($value !== '') $query->where('is_show', $value);
  189. }
  190. }