StoreCart.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\order;
  12. use app\model\product\branch\StoreBranchProductAttrValue;
  13. use app\model\product\product\StoreProduct;
  14. use app\model\product\sku\StoreProductAttrValue;
  15. use app\model\user\User;
  16. use crmeb\basic\BaseModel;
  17. use crmeb\traits\ModelTrait;
  18. use think\Model;
  19. /**
  20. * 购物车Model
  21. * Class StoreCart
  22. * @package app\model\order
  23. */
  24. class StoreCart extends BaseModel
  25. {
  26. use ModelTrait;
  27. /**
  28. * 数据表主键
  29. * @var string
  30. */
  31. protected $pk = 'id';
  32. /**
  33. * 模型名称
  34. * @var string
  35. */
  36. protected $name = 'store_cart';
  37. /**
  38. * 自动添加字段
  39. * @var string[]
  40. */
  41. protected $insert = ['add_time'];
  42. /**
  43. * @return \think\model\relation\HasOne
  44. */
  45. public function user()
  46. {
  47. return $this->hasOne(User::class, 'uid', 'uid')->bind([
  48. 'nickname' => 'nickname',
  49. 'avatar' => 'avatar'
  50. ]);
  51. }
  52. /**
  53. * 添加时间修改器
  54. * @return int
  55. */
  56. protected function setAddTimeAttr()
  57. {
  58. return time();
  59. }
  60. /**
  61. * 一对一关联
  62. * 购物车关联商品商品详情
  63. * @return \think\model\relation\HasOne
  64. */
  65. public function productInfo()
  66. {
  67. return $this->hasOne(StoreProduct::class, 'id', 'product_id');
  68. }
  69. /**
  70. * 一对一关联
  71. * 购物车关联商品商品规格
  72. * @return \think\model\relation\HasOne
  73. */
  74. public function attrInfo()
  75. {
  76. return $this->hasOne(StoreProductAttrValue::class, 'unique', 'product_attr_unique');
  77. }
  78. /**
  79. * 门店规格表
  80. * @return \think\model\relation\HasOne
  81. */
  82. public function storeBranchProductAttr()
  83. {
  84. return $this->hasOne(StoreBranchProductAttrValue::class, 'unique', 'product_attr_unique');
  85. }
  86. /**
  87. * 类型搜索器
  88. * @param Model $query
  89. * @param $value
  90. * @param $data
  91. */
  92. public function searchTypeAttr($query, $value, $data)
  93. {
  94. $query->where('type', $value);
  95. }
  96. /**
  97. * 购物车ID搜索器
  98. * @param $query
  99. * @param $value
  100. */
  101. public function searchCartIdsAttr($query, $value)
  102. {
  103. if ($value) {
  104. if (is_array($value)) {
  105. $query->whereIn('id', $value);
  106. } else {
  107. $query->where('id', $value);
  108. }
  109. }
  110. }
  111. /**
  112. * 是否支付
  113. * @param Model $query
  114. * @param $value
  115. * @param $data
  116. */
  117. public function searchIsPayAttr($query, $value, $data)
  118. {
  119. $query->where('is_pay', $value);
  120. }
  121. /**
  122. * 是否删除
  123. * @param Model $query
  124. * @param $value
  125. * @param $data
  126. */
  127. public function searchIsDelAttr($query, $value, $data)
  128. {
  129. $query->where('is_del', $value);
  130. }
  131. /**
  132. * 是否立即支付
  133. * @param Model $query
  134. * @param $value
  135. * @param $data
  136. */
  137. public function searchIsNewAttr($query, $value, $data)
  138. {
  139. $query->where('is_new', $value);
  140. }
  141. /**
  142. * 查询用户购物车
  143. * @param Model $query
  144. * @param $value
  145. * @param $data
  146. */
  147. public function searchUidAttr($query, $value, $data)
  148. {
  149. $query->where('uid', $value);
  150. }
  151. /**
  152. * @param $query
  153. * @param $value
  154. */
  155. public function searchTouristUidAttr($query, $value)
  156. {
  157. if ($value) {
  158. $query->where('tourist_uid', $value);
  159. }
  160. }
  161. /**
  162. * 商品ID搜索器
  163. * @param Model $query
  164. * @param $value
  165. * @param $data
  166. */
  167. public function searchProductIdAttr($query, $value, $data)
  168. {
  169. if (is_array($value)) {
  170. $query->whereIn('product_id', $value);
  171. } else {
  172. $query->where('product_id', $value);
  173. }
  174. }
  175. /**
  176. * 商品类型搜索器
  177. * @param Model $query
  178. * @param $value
  179. * @param $data
  180. */
  181. public function searchProductTypeAttr($query, $value, $data)
  182. {
  183. if (is_array($value)) {
  184. $query->whereIn('product_type', $value);
  185. } else {
  186. $query->where('product_type', $value);
  187. }
  188. }
  189. /**
  190. * 商品规格唯一值搜索器
  191. * @param Model $query
  192. * @param $value
  193. * @param $data
  194. */
  195. public function searchProductAttrUniqueAttr($query, $value, $data)
  196. {
  197. $query->where('product_attr_unique', $value);
  198. }
  199. /**
  200. * store_id搜索器
  201. * @param $query
  202. * @param $value
  203. */
  204. public function searchStoreIdAttr($query, $value)
  205. {
  206. if ($value !== '') $query->where('store_id', $value);
  207. }
  208. /**
  209. * 店员搜索器
  210. * @param $query
  211. * @param $value
  212. */
  213. public function searchStaffIdAttr($query, $value)
  214. {
  215. if ($value !== '') {
  216. $query->where('staff_id', $value);
  217. }
  218. }
  219. /**
  220. * 一对多关联
  221. * 商品关联优惠卷模板id
  222. * @return \think\model\relation\HasMany
  223. */
  224. public function product()
  225. {
  226. return $this->hasMany(StoreProduct::class, 'id', 'product_id');
  227. }
  228. }