StoreOrder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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\activity\combination\StorePink;
  13. use app\model\product\sku\StoreProductVirtual;
  14. use app\model\store\DeliveryService;
  15. use app\model\store\SystemStore;
  16. use app\model\store\SystemStoreStaff;
  17. use app\model\supplier\SystemSupplier;
  18. use app\model\user\User;
  19. use app\model\user\UserBrokerage;
  20. use crmeb\basic\BaseModel;
  21. use crmeb\traits\ModelTrait;
  22. use think\Model;
  23. /**
  24. * 订单Model
  25. * Class StoreOrder
  26. * @package app\model\order
  27. */
  28. class StoreOrder extends BaseModel
  29. {
  30. use ModelTrait;
  31. /**
  32. * 支付类型
  33. * @var string[]
  34. */
  35. protected $pay_type = [
  36. 1 => 'weixin',
  37. 2 => 'yue',
  38. 3 => 'offline',
  39. 4 => 'alipay'
  40. ];
  41. /**
  42. * 数据表主键
  43. * @var string
  44. */
  45. protected $pk = 'id';
  46. /**
  47. * 模型名称
  48. * @var string
  49. */
  50. protected $name = 'store_order';
  51. protected $insert = ['add_time'];
  52. /**
  53. * 更新时间
  54. * @var bool | string | int
  55. */
  56. protected $updateTime = false;
  57. /**
  58. * 创建时间修改器
  59. * @return int
  60. */
  61. protected function setAddTimeAttr($time = 0)
  62. {
  63. if ($time) return $time;
  64. return time();
  65. }
  66. /**
  67. * 自定义表单信息
  68. * @param $value
  69. * @param $data
  70. * @return mixed
  71. */
  72. protected function setCustomFormAttr($value)
  73. {
  74. if ($value) {
  75. return is_array($value) ? json_encode($value) : $value;
  76. }
  77. return '';
  78. }
  79. /**
  80. * 自定义表单信息
  81. * @param $value
  82. * @param $data
  83. * @return mixed
  84. */
  85. protected function getCustomFormAttr($value)
  86. {
  87. if ($value) {
  88. return is_string($value) ? json_decode($value, true) : $value;
  89. }
  90. return [];
  91. }
  92. /**
  93. * 优惠活动赠送信息
  94. * @param $value
  95. * @param $data
  96. * @return mixed
  97. */
  98. protected function getPromotionsGiveAttr($value)
  99. {
  100. if ($value) {
  101. return is_string($value) ? json_decode($value, true) : $value;
  102. }
  103. return [];
  104. }
  105. /**
  106. * 优惠活动赠送优惠券
  107. * @param $value
  108. * @param $data
  109. * @return mixed
  110. */
  111. protected function getGiveCouponAttr($value)
  112. {
  113. if ($value) {
  114. return is_string($value) ? explode(',', $value) : $value;
  115. }
  116. return [];
  117. }
  118. /**
  119. * 一对多关联查询子订单
  120. * @return \think\model\relation\HasMany
  121. */
  122. public function split()
  123. {
  124. return $this->hasMany(StoreOrder::class, 'pid', 'id');
  125. }
  126. /**
  127. * 一对一关联用户表
  128. * @return \think\model\relation\HasOne
  129. */
  130. public function user()
  131. {
  132. return $this->hasOne(User::class, 'uid', 'uid', false)->field(['uid', 'avatar', 'nickname', 'phone', 'now_money', 'integral', 'delete_time'])->bind([
  133. 'avatar' => 'avatar',
  134. 'nickname' => 'nickname',
  135. 'phone' => 'phone',
  136. 'now_money' => 'now_money',
  137. 'integral' => 'integral',
  138. 'delete_time' => 'delete_time'
  139. ]);
  140. }
  141. /**
  142. * 一对多关联退款订单
  143. * @return \think\model\relation\hasMany
  144. */
  145. public function refund()
  146. {
  147. return $this->hasMany(StoreOrderRefund::class, 'store_order_id', 'id')->where('refund_type', '<>', 3)->where('is_cancel', 0);
  148. }
  149. /**
  150. * 一对多关联订单优惠详情
  151. * @return \think\model\relation\hasMany
  152. */
  153. public function promotions()
  154. {
  155. return $this->hasMany(StoreOrderPromotions::class, 'oid', 'id');
  156. }
  157. /**
  158. * 一对一关联上级用户信息
  159. * @return \think\model\relation\HasOne
  160. */
  161. public function spread()
  162. {
  163. return $this->hasOne(User::class, 'uid', 'spread_uid')->field(['uid', 'nickname'])->bind([
  164. 'spread_nickname' => 'nickname'
  165. ]);
  166. }
  167. /**
  168. * 一对一拼团获取状态
  169. * @return \think\model\relation\HasOne
  170. */
  171. public function pink()
  172. {
  173. return $this->hasOne(StorePink::class, 'id', 'pink_id')->field(['id', 'order_id_key', 'status'])->bind([
  174. 'pinkStatus' => 'status'
  175. ]);
  176. }
  177. /**
  178. * 门店一对一关联
  179. * @return \think\model\relation\HasOne
  180. */
  181. public function store()
  182. {
  183. return $this->hasOne(SystemStore::class, 'id', 'store_id')->hidden(['bank_code,bank_address', 'alipay_account', 'alipay_qrcode_url', 'wechat', 'wechat_qrcode_url']);
  184. }
  185. /**
  186. * 订单关联门店店员
  187. * @return \think\model\relation\HasOne
  188. */
  189. public function storeStaff()
  190. {
  191. return $this->hasOne(SystemStoreStaff::class, 'id', 'staff_id')->field(['id', 'uid', 'store_id', 'staff_name'])->bind([
  192. 'staff_uid' => 'uid',
  193. 'staff_store_id' => 'store_id',
  194. 'clerk_name' => 'staff_name'
  195. ]);
  196. }
  197. /**
  198. * 订单关联店员
  199. * @return \think\model\relation\HasOne
  200. */
  201. public function staff()
  202. {
  203. return $this->hasOne(SystemStoreStaff::class, 'uid', 'clerk_id')->field(['id', 'uid', 'store_id', 'staff_name'])->bind([
  204. 'staff_uid' => 'uid',
  205. 'staff_store_id' => 'store_id',
  206. 'clerk_name' => 'staff_name'
  207. ]);
  208. }
  209. /**
  210. * 店员关联用户
  211. * @return \think\model\relation\HasOne
  212. */
  213. public function staffUser()
  214. {
  215. return $this->hasOne(User::class, 'uid', 'staff_uid')->field(['uid', 'nickname'])->bind([
  216. 'clerk_name' => 'nickname'
  217. ]);
  218. }
  219. /**
  220. * 关联配送员
  221. * @return \think\model\relation\HasOne
  222. */
  223. public function deliveryService()
  224. {
  225. return $this->hasOne(DeliveryService::class, 'uid', 'delivery_uid')->field(['uid', 'nickname'])->bind([
  226. 'delivery_name' => 'nickname'
  227. ]);
  228. }
  229. /**
  230. * 关联订单发票
  231. * @return \think\model\relation\HasOne
  232. */
  233. public function invoice()
  234. {
  235. return $this->hasOne(StoreOrderInvoice::class, 'order_id', 'id');
  236. }
  237. /**
  238. * 关联分佣表
  239. * @return \think\model\relation\HasOne
  240. */
  241. public function brokerage()
  242. {
  243. return $this->hasOne(UserBrokerage::class, 'link_id', 'id');
  244. }
  245. /**
  246. * 关联卡密
  247. * @return \think\model\relation\HasMany
  248. */
  249. public function virtual()
  250. {
  251. return $this->hasMany(StoreProductVirtual::class, 'order_id', 'order_id')->where('order_type', 1);
  252. }
  253. /**
  254. * 关联订单记录
  255. * @return \think\model\relation\HasMany
  256. */
  257. public function orderStatus()
  258. {
  259. return $this->hasMany(StoreOrderStatus::class, 'oid', 'id');
  260. }
  261. /**
  262. * 关联配送订单记录
  263. * @return \think\model\relation\hasOne
  264. */
  265. public function deliveryOrder()
  266. {
  267. return $this->hasOne(StoreDeliveryOrder::class, 'oid', 'id');
  268. }
  269. /**
  270. * 购物车ID修改器
  271. * @param $value
  272. * @return false|string
  273. */
  274. protected function setCartIdAttr($value)
  275. {
  276. return is_array($value) ? json_encode($value) : $value;
  277. }
  278. /**
  279. * 购物车获取器
  280. * @param $value
  281. * @param $data
  282. * @return mixed
  283. */
  284. protected function getCartIdAttr($value, $data)
  285. {
  286. return $value ? json_decode($value, true) : [];
  287. }
  288. /**
  289. * 订单ID搜索器
  290. * @param Model $query
  291. * @param $value
  292. */
  293. public function searchOrderIdAttr($query, $value)
  294. {
  295. $query->where('order_id', $value);
  296. }
  297. /**
  298. * 活动ID搜索器
  299. * @param Model $query
  300. * @param $value
  301. */
  302. public function searchActivityIdAttr($query, $value)
  303. {
  304. if ($value !== '') $query->where('activity_id', $value);
  305. }
  306. /**
  307. * 父类ID搜索器
  308. * @param Model $query
  309. * @param $value
  310. */
  311. public function searchPidAttr($query, $value)
  312. {
  313. if ($value === 0) {
  314. $query->where('pid', '>=', 0);
  315. } else {
  316. if (is_array($value)) {
  317. $query->whereIn('pid', $value);
  318. } else {
  319. $query->where('pid', $value);
  320. }
  321. }
  322. }
  323. /**
  324. * 没拆分订单 与子订单(0:为拆分订单-1:已拆分主订单 >0 :拆分后子订单)
  325. * @param Model $query
  326. * @param $value
  327. */
  328. public function searchNotPidAttr($query, $value)
  329. {
  330. $query->where('pid', '<>', -1);
  331. }
  332. /**
  333. * @param Model $query
  334. * @param $value
  335. */
  336. public function searchIdAttr($query, $value)
  337. {
  338. if (is_array($value)) {
  339. $query->whereIn('id', $value);
  340. } else {
  341. $query->where('id', $value);
  342. }
  343. }
  344. /**
  345. * 支付方式搜索器
  346. * @param $query
  347. * @param $value
  348. */
  349. public function searchPayTypeAttr($query, $value)
  350. {
  351. if (is_array($value)) {
  352. $query->whereIn('pay_type', $value);
  353. } else {
  354. if ($value !== '') {
  355. $pay_type = $this->pay_type;
  356. if (in_array($value, array_keys($pay_type)) && $type = $pay_type[$value] ?? '') {
  357. $query->where('pay_type', $type);
  358. } else {
  359. $query->where('pay_type', $value);
  360. }
  361. }
  362. }
  363. }
  364. /**
  365. * 不等于余额支付
  366. * @param $query
  367. * @param $value
  368. */
  369. public function searchPayTypeNoAttr($query, $value)
  370. {
  371. $query->where('pay_type', "<>", $value);
  372. }
  373. /**
  374. * 订单id或者用户名搜索器
  375. * @param $query
  376. * @param $value
  377. */
  378. public function searchOrderIdRealNameAttr($query, $value)
  379. {
  380. $query->where('order_id|real_name', $value);
  381. }
  382. /**
  383. * 用户ID搜索器
  384. * @param Model $query
  385. * @param $value
  386. */
  387. public function searchUidAttr($query, $value)
  388. {
  389. if (is_array($value))
  390. $query->whereIn('uid', $value);
  391. else
  392. $query->where('uid', $value);
  393. }
  394. /**
  395. * 支付状态搜索器
  396. * @param Model $query
  397. * @param $value
  398. */
  399. public function searchPaidAttr($query, $value)
  400. {
  401. $query->where('paid', $value);
  402. }
  403. /**
  404. * 退款状态搜索器
  405. * @param Model $query
  406. * @param $value
  407. * @param $data
  408. */
  409. public function searchRefundStatusAttr($query, $value, $data)
  410. {
  411. if ($value !== '') {
  412. if (is_array($value)) {
  413. $query->whereIn('refund_status', $value);
  414. } else {
  415. $query->where('refund_status', $value);
  416. }
  417. }
  418. }
  419. /**
  420. * 退款状态搜索器
  421. * @param Model $query
  422. * @param $value
  423. * @param $data
  424. */
  425. public function searchRefundStatusInAttr($query, $value)
  426. {
  427. $query->whereIn('refund_status', $value);
  428. }
  429. /**
  430. * 是否是拼团订单
  431. * @param Model $query
  432. * @param $value
  433. */
  434. public function searchPinkIdAttr($query, $value)
  435. {
  436. $query->where('pink_id', $value);
  437. }
  438. /**
  439. * 核销码搜索器
  440. * @param Model $query
  441. * @param $value
  442. */
  443. public function searchVerifyCodeAttr($query, $value)
  444. {
  445. $query->where('verify_code', $value);
  446. }
  447. /**
  448. * 支付状态搜索器
  449. * @param Model $query
  450. * @param $value
  451. */
  452. public function searchIsDelAttr($query, $value)
  453. {
  454. if ($value != '') $query->where('is_del', $value);
  455. }
  456. /**
  457. * 是否删除搜索器
  458. * @param Model $query
  459. * @param $value
  460. */
  461. public function searchIsSystemDelAttr($query, $value)
  462. {
  463. if ($value != '') $query->where('is_system_del', $value);
  464. }
  465. /**
  466. * 退款状态搜索器
  467. * @param $query
  468. * @param $value
  469. */
  470. public function searchRefundTypeAttr($query, $value)
  471. {
  472. if (is_array($value)) {
  473. $query->whereIn('refund_type', $value);
  474. } else {
  475. if ($value == -1) {
  476. $query->where('refund_type', 'in', '0,3');
  477. } else {
  478. if ($value == 0 || $value == '') {
  479. $query->where('refund_type', '<>', 0);
  480. } else {
  481. $query->where('refund_type', $value);
  482. }
  483. }
  484. }
  485. }
  486. /**
  487. * 用户来源
  488. * @param Model $query
  489. * @param $value
  490. */
  491. public function searchChannelTypeAttr($query, $value)
  492. {
  493. if ($value != '') $query->where('channel_type', $value);
  494. }
  495. /**
  496. * 退款id搜索器
  497. * @param Model $query
  498. * @param $value
  499. */
  500. public function searchRefundIdAttr($query, $value)
  501. {
  502. if ($value) {
  503. $query->where('id', 'in', $value);
  504. }
  505. }
  506. /**
  507. * 上级|上上级推广人
  508. * @param $query
  509. * @param $value
  510. */
  511. public function searchSpreadOrUidAttr($query, $value)
  512. {
  513. if ($value) $query->where('spread_uid|spread_two_uid', $value);
  514. }
  515. /**
  516. * 上级推广人
  517. * @param $query
  518. * @param $value
  519. */
  520. public function searchSpreadUidAttr($query, $value)
  521. {
  522. if ($value) $query->where('spread_uid', $value);
  523. }
  524. /**
  525. * 上上级推广人
  526. * @param $query
  527. * @param $value
  528. */
  529. public function searchSpreadTwoUidAttr($query, $value)
  530. {
  531. if ($value) $query->where('spread_two_uid', $value);
  532. }
  533. /**
  534. * 门店ID
  535. * @param $query
  536. * @param $value
  537. */
  538. public function searchStoreIdAttr($query, $value)
  539. {
  540. if ($value !== '') {
  541. if ($value == -1) {//所有门店
  542. $query->where('store_id', '>', 0);
  543. } else {
  544. $query->where('store_id', $value);
  545. }
  546. }
  547. }
  548. /**
  549. * 门店店员ID
  550. * @param $query
  551. * @param $value
  552. */
  553. public function searchStaffIdAttr($query, $value)
  554. {
  555. if ($value) $query->where('staff_id', $value);
  556. }
  557. /**
  558. * 配送方式
  559. * @param $query
  560. * @param $value
  561. */
  562. public function searchShippingTypeAttr($query, $value)
  563. {
  564. if ($value) {
  565. if (is_array($value)) {
  566. $query->where('shipping_type', $value);
  567. } else {
  568. $query->where('shipping_type', $value);
  569. }
  570. }
  571. }
  572. /**
  573. * 配送员UID
  574. * @param $query
  575. * @param $value
  576. */
  577. public function searchDeliveryUidAttr($query, $value)
  578. {
  579. if ($value) $query->where('delivery_uid', $value);
  580. }
  581. /**
  582. * 配送类型
  583. * @param $query
  584. * @param $value
  585. */
  586. public function searchDeliveryTypeAttr($query, $value)
  587. {
  588. if ($value) $query->where('delivery_type', $value);
  589. }
  590. /**
  591. * 供应商ID
  592. * @param $query
  593. * @param $value
  594. */
  595. public function searchSupplierIdAttr($query, $value)
  596. {
  597. if ($value !== '') {
  598. if ($value == -1) {
  599. $query->where('supplier_id', '>', 0);
  600. } else {
  601. $query->where('supplier_id', $value);
  602. }
  603. }
  604. }
  605. /**
  606. * 支付渠道
  607. * @param $query
  608. * @param $value
  609. */
  610. public function searchIsChannelAttr($query, $value)
  611. {
  612. if ($value !== '') $query->where('is_channel', $value);
  613. }
  614. /**
  615. * 一对一关联供应商
  616. * @return \think\model\relation\HasOne
  617. */
  618. public function supplier()
  619. {
  620. return $this->hasOne(SystemSupplier::class, 'id', 'supplier_id')->field(['id', 'supplier_name'])->bind([
  621. 'supplier_name'
  622. ]);
  623. }
  624. /**
  625. * 一对一关联供应商
  626. * @return \think\model\relation\HasOne
  627. */
  628. public function supplierInfo()
  629. {
  630. return $this->hasOne(SystemSupplier::class, 'id', 'supplier_id')->field(['id', 'supplier_name', 'name', 'phone', 'email']);
  631. }
  632. /**
  633. * 供应商统计
  634. * @param $query
  635. * @param $value
  636. */
  637. public function searchSupplierAttr($query, $value)
  638. {
  639. if ($value !== '') $query->where('supplier_id', '>', 0);
  640. }
  641. }