StoreOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\activity\combination\StorePink;
  13. use app\model\system\store\SystemStore;
  14. use app\model\system\store\SystemStoreStaff;
  15. use app\model\user\User;
  16. use crmeb\basic\BaseModel;
  17. use crmeb\traits\ModelTrait;
  18. use think\Model;
  19. /**
  20. * TODO 订单Model
  21. * Class StoreOrder
  22. * @package app\model\order
  23. */
  24. class StoreOrder extends BaseModel
  25. {
  26. use ModelTrait;
  27. /**
  28. * 支付类型
  29. * @var string[]
  30. */
  31. protected $pay_type = [
  32. 1 => 'weixin',
  33. 2 => 'yue',
  34. 3 => 'offline',
  35. 4 => 'alipay'
  36. ];
  37. /**
  38. * 数据表主键
  39. * @var string
  40. */
  41. protected $pk = 'id';
  42. /**
  43. * 模型名称
  44. * @var string
  45. */
  46. protected $name = 'store_order';
  47. protected $insert = ['add_time'];
  48. /**
  49. * 更新时间
  50. * @var bool | string | int
  51. */
  52. protected $updateTime = false;
  53. /**
  54. * 创建时间修改器
  55. * @return int
  56. */
  57. protected function setAddTimeAttr($time = 0)
  58. {
  59. if ($time) return $time;
  60. return time();
  61. }
  62. /**
  63. * 自定义表单修改器
  64. * @param $value
  65. * @return array|mixed
  66. */
  67. public function setCustomFormAttr($value)
  68. {
  69. return is_array($value) ? json_encode($value) : $value;
  70. }
  71. /**
  72. * 自定义表单获取器
  73. * @param $value
  74. * @return array|mixed
  75. */
  76. public function getCustomFormAttr($value)
  77. {
  78. return is_string($value) ? json_decode($value, true) ?? [] : [];
  79. }
  80. /**
  81. * 一对多关联查询子订单
  82. * @return \think\model\relation\HasMany
  83. */
  84. public function split()
  85. {
  86. return $this->hasMany(StoreOrder::class, 'pid', 'id');
  87. }
  88. /**
  89. * 一对一关联用户表
  90. * @return \think\model\relation\HasOne
  91. */
  92. public function user()
  93. {
  94. return $this->hasOne(User::class, 'uid', 'uid')->field(['uid', 'nickname', 'phone', 'spread_uid'])->bind([
  95. 'nickname' => 'nickname',
  96. 'phone' => 'phone'
  97. ]);
  98. }
  99. public function division()
  100. {
  101. return $this->hasOne(User::class, 'uid', 'division_id')->field(['uid', 'nickname'])->bind([
  102. 'division_name' => 'nickname'
  103. ]);
  104. }
  105. /**
  106. * 一对一关联上级用户信息
  107. * @return \think\model\relation\HasOne
  108. */
  109. public function spread()
  110. {
  111. return $this->hasOne(User::class, 'uid', 'spread_uid')->field(['uid', 'nickname'])->bind([
  112. 'spread_nickname' => 'nickname'
  113. ]);
  114. }
  115. /**
  116. * 一对一拼团获取状态
  117. * @return \think\model\relation\HasOne
  118. */
  119. public function pink()
  120. {
  121. return $this->hasOne(StorePink::class, 'id', 'pink_id')->field(['id', 'order_id_key', 'status'])->bind([
  122. 'pinkStatus' => 'status'
  123. ]);
  124. }
  125. /**
  126. * 门店一对一关联
  127. * @return \think\model\relation\HasOne
  128. */
  129. public function store()
  130. {
  131. return $this->hasOne(SystemStore::class, 'id', 'store_id')->field(['id', 'name'])->bind([
  132. 'store_name' => 'name'
  133. ]);
  134. }
  135. /**
  136. * 订单关联店员
  137. * @return \think\model\relation\HasOne
  138. */
  139. public function staff()
  140. {
  141. return $this->hasOne(SystemStoreStaff::class, 'uid', 'clerk_id')->field(['id', 'uid', 'store_id', 'staff_name'])->bind([
  142. 'staff_uid' => 'uid',
  143. 'staff_store_id' => 'store_id',
  144. 'clerk_name' => 'staff_name'
  145. ]);
  146. }
  147. /**
  148. * 店员关联用户
  149. * @return \think\model\relation\HasOne
  150. */
  151. public function staffUser()
  152. {
  153. return $this->hasOne(User::class, 'uid', 'staff_uid')->field(['uid', 'nickname'])->bind([
  154. 'clerk_name' => 'nickname'
  155. ]);
  156. }
  157. /**
  158. * 关联订单发票
  159. * @return \think\model\relation\HasOne
  160. */
  161. public function invoice()
  162. {
  163. return $this->hasOne(StoreOrderInvoice::class, 'order_id', 'id');
  164. }
  165. /**
  166. * 一对多关联退款订单
  167. * @return \think\model\relation\hasMany
  168. */
  169. public function refund()
  170. {
  171. return $this->hasMany(StoreOrderRefund::class, 'store_order_id', 'id')->where('refund_type', '<>', 3)->where('is_cancel', 0);
  172. }
  173. /**
  174. * 购物车ID修改器
  175. * @param $value
  176. * @return false|string
  177. */
  178. protected function setCartIdAttr($value)
  179. {
  180. return is_array($value) ? json_encode($value) : $value;
  181. }
  182. /**
  183. * 购物车获取器
  184. * @param $value
  185. * @param $data
  186. * @return mixed
  187. */
  188. protected function getCartIdAttr($value, $data)
  189. {
  190. return $value ? json_decode($value, true) : [];
  191. }
  192. /**
  193. * 订单ID搜索器
  194. * @param Model $query
  195. * @param $value
  196. */
  197. public function searchOrderIdAttr($query, $value)
  198. {
  199. $query->where('order_id', $value);
  200. }
  201. /**
  202. * 父类ID搜索器
  203. * @param Model $query
  204. * @param $value
  205. */
  206. public function searchPidAttr($query, $value)
  207. {
  208. if ($value === 0) {
  209. $query->where('pid', '>=', 0);
  210. } else {
  211. $query->where('pid', $value);
  212. }
  213. }
  214. /**
  215. * 没拆分订单 与子订单(0:为拆分订单-1:已拆分主订单 >0 :拆分后子订单)
  216. * @param Model $query
  217. * @param $value
  218. */
  219. public function searchNotPidAttr($query, $value)
  220. {
  221. $query->where('pid', '<>', -1);
  222. }
  223. /**
  224. * @param Model $query
  225. * @param $value
  226. */
  227. public function searchIdAttr($query, $value)
  228. {
  229. if (is_array($value)) {
  230. $query->whereIn('id', $value);
  231. } else {
  232. $query->where('id', $value);
  233. }
  234. }
  235. /**
  236. * 支付方式搜索器
  237. * @param $query
  238. * @param $value
  239. */
  240. public function searchPayTypeAttr($query, $value)
  241. {
  242. if (is_array($value)) {
  243. $query->whereIn('pay_type', $value);
  244. } else {
  245. if ($value !== '') {
  246. $pay_type = $this->pay_type;
  247. if (in_array($value, array_keys($pay_type)) && $type = $pay_type[$value] ?? '') {
  248. $query->where('pay_type', $type);
  249. } else {
  250. $query->where('pay_type', $value);
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * 不等于余额支付
  257. * @param $query
  258. * @param $value
  259. */
  260. public function searchPayTypeNoAttr($query, $value)
  261. {
  262. $query->where('pay_type', "<>", $value);
  263. }
  264. /**
  265. * 订单id或者用户名搜索器
  266. * @param $query
  267. * @param $value
  268. */
  269. public function searchOrderIdRealNameAttr($query, $value)
  270. {
  271. $query->where('order_id|real_name', $value);
  272. }
  273. /**
  274. * 用户ID搜索器
  275. * @param Model $query
  276. * @param $value
  277. */
  278. public function searchUidAttr($query, $value)
  279. {
  280. if (is_array($value))
  281. $query->whereIn('uid', $value);
  282. else
  283. $query->where('uid', $value);
  284. }
  285. /**
  286. * 支付状态搜索器
  287. * @param Model $query
  288. * @param $value
  289. */
  290. public function searchPaidAttr($query, $value)
  291. {
  292. if (in_array($value, [0, 1])) {
  293. $query->where('paid', $value);
  294. }
  295. }
  296. /**
  297. * 退款状态搜索器
  298. * @param Model $query
  299. * @param $value
  300. * @param $data
  301. */
  302. public function searchRefundStatusAttr($query, $value, $data)
  303. {
  304. if ($value !== '') {
  305. if (is_array($value)) {
  306. $query->whereIn('refund_status', $value);
  307. } else {
  308. $query->where('refund_status', $value);
  309. }
  310. }
  311. }
  312. /**
  313. * 退款状态搜索器
  314. * @param Model $query
  315. * @param $value
  316. * @param $data
  317. */
  318. public function searchRefundStatusInAttr($query, $value)
  319. {
  320. $query->whereIn('refund_status', $value);
  321. }
  322. /**
  323. * 是否是拼团订单
  324. * @param Model $query
  325. * @param $value
  326. */
  327. public function searchPinkIdAttr($query, $value)
  328. {
  329. $query->where('pink_id', $value);
  330. }
  331. /**
  332. * 拼团id搜索器
  333. * @param Model $query
  334. * @param $value
  335. */
  336. public function searchCombinationIdAttr($query, $value)
  337. {
  338. $query->where('combination_id', $value);
  339. }
  340. /**
  341. * 没有拼团订单或拼团商品
  342. * @param Model $query
  343. * @param $value
  344. */
  345. public function searchCpIdGtAttr($query, $value)
  346. {
  347. $query->where('combination_id|pink_id', '>', $value);
  348. }
  349. /**
  350. * 不是秒杀搜索器
  351. * @param Model $query
  352. * @param $value
  353. */
  354. public function searchSeckillIdGtAttr($query, $value)
  355. {
  356. $query->where('seckill_id', '>', $value);
  357. }
  358. /**
  359. * 秒杀id商品搜索器
  360. * @param Model $query
  361. * @param $value
  362. */
  363. public function searchSeckillIdAttr($query, $value)
  364. {
  365. $query->where('seckill_id', $value);
  366. }
  367. /**
  368. * 砍价商品id搜索器
  369. * @param Model $query
  370. * @param $value
  371. */
  372. public function searchBargainIdAttr($query, $value)
  373. {
  374. $query->where('bargain_id', $value);
  375. }
  376. /**
  377. * 属于砍价搜索器
  378. * @param Model $query
  379. * @param $value
  380. * @param $data
  381. */
  382. public function searchBargainIdGtAttr($query, $value)
  383. {
  384. $query->where('bargain_id', '>', $value);
  385. }
  386. /**
  387. * 核销码搜索器
  388. * @param Model $query
  389. * @param $value
  390. */
  391. public function searchVerifyCodeAttr($query, $value)
  392. {
  393. $query->where('verify_code', $value);
  394. }
  395. /**
  396. * 支付状态搜索器
  397. * @param Model $query
  398. * @param $value
  399. */
  400. public function searchIsDelAttr($query, $value)
  401. {
  402. if ($value != '') $query->where('is_del', $value);
  403. }
  404. /**
  405. * 是否删除搜索器
  406. * @param Model $query
  407. * @param $value
  408. */
  409. public function searchIsSystemDelAttr($query, $value)
  410. {
  411. if ($value != '') $query->where('is_system_del', $value);
  412. }
  413. /**
  414. * 退款状态搜索器
  415. * @param $query
  416. * @param $value
  417. */
  418. public function searchRefundTypeAttr($query, $value)
  419. {
  420. if (is_array($value)) {
  421. $query->whereIn('refund_type', $value);
  422. } else {
  423. if ($value == -1) {
  424. $query->where('refund_type', 'in', '0,3');
  425. } else {
  426. if ($value == 0 || $value == '') {
  427. $query->where('refund_type', '<>', 0);
  428. } else {
  429. $query->where('refund_type', $value);
  430. }
  431. }
  432. }
  433. }
  434. /**
  435. * 用户来源
  436. * @param Model $query
  437. * @param $value
  438. */
  439. public function searchChannelTypeAttr($query, $value)
  440. {
  441. if ($value != '') $query->where('channel_type', $value);
  442. }
  443. /**
  444. * 退款id搜索器
  445. * @param Model $query
  446. * @param $value
  447. */
  448. public function searchRefundIdAttr($query, $value)
  449. {
  450. if ($value) {
  451. $query->where('id', 'in', $value);
  452. }
  453. }
  454. /**
  455. * 上级|上上级推广人
  456. * @param $query
  457. * @param $value
  458. */
  459. public function searchSpreadOrUidAttr($query, $value)
  460. {
  461. if ($value) $query->where('spread_uid|spread_two_uid', $value);
  462. }
  463. /**
  464. * 上级推广人
  465. * @param $query
  466. * @param $value
  467. */
  468. public function searchSpreadUidAttr($query, $value)
  469. {
  470. if ($value) $query->where('spread_uid', $value);
  471. }
  472. /**
  473. * 上上级推广人
  474. * @param $query
  475. * @param $value
  476. */
  477. public function searchSpreadTwoUidAttr($query, $value)
  478. {
  479. if ($value) $query->where('spread_two_uid', $value);
  480. }
  481. /**
  482. * 支付渠道
  483. * @param $query
  484. * @param $value
  485. */
  486. public function searchIsChannelAttr($query, $value)
  487. {
  488. if ($value !== '') $query->where('is_channel', $value);
  489. }
  490. /**
  491. * 活动查询0普通,1秒杀,2砍价,3拼团,4预售
  492. * @param $query
  493. * @param $value
  494. * @param $data
  495. */
  496. public function searchActivityTypeAttr($query, $value, $data)
  497. {
  498. if ($value !== '') {
  499. switch ($value) {
  500. case 0:
  501. $query->where('combination_id', 0)->where('seckill_id', 0)->where('bargain_id', 0)->where('advance_id', 0);
  502. break;
  503. case 1:
  504. $query->where('seckill_id', '>', 0);
  505. break;
  506. case 2:
  507. $query->where('bargain_id', '>', 0);
  508. break;
  509. case 3:
  510. $query->where('combination_id', '>', 0);
  511. break;
  512. case 4:
  513. $query->where('advance_id', '>', 0);
  514. break;
  515. default:
  516. break;
  517. }
  518. }
  519. }
  520. /**
  521. * 事业部推广订单
  522. * @param $query
  523. * @param $value
  524. */
  525. public function searchDivisionIdAttr($query, $value)
  526. {
  527. if ($value !== '') $query->where('division_id', $value);
  528. }
  529. /**
  530. * 代理商推广订单
  531. * @param $query
  532. * @param $value
  533. */
  534. public function searchAgentIdAttr($query, $value)
  535. {
  536. if ($value !== '') $query->where('agent_id', $value);
  537. }
  538. /**
  539. * @param $query
  540. * @param $value
  541. */
  542. public function searchIdsAttr($query, $value)
  543. {
  544. if (is_string($value)) $value = explode(',', $value);
  545. if (count($value)) $query->whereIn('id', $value);
  546. }
  547. }