截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. <?php
  2. namespace app\common\model\order;
  3. use app\common\basics\Models;
  4. use app\common\enum\OrderEnum;
  5. use app\common\enum\OrderGoodsEnum;
  6. use app\common\enum\PayEnum;
  7. use app\common\model\Client_;
  8. use app\common\model\Pay;
  9. use app\common\model\shop\Shop;
  10. use app\common\model\team\TeamJoin;
  11. use app\common\model\user\User;
  12. use app\common\server\ConfigServer;
  13. use think\facade\Db;
  14. /**
  15. * Class order
  16. * @package app\common\model\order
  17. */
  18. class Order extends Models
  19. {
  20. //订单状态
  21. const STATUS_WAIT_PAY = 0; //待付款
  22. const STATUS_WAIT_DELIVERY = 1; //待发货
  23. const STATUS_WAIT_RECEIVE = 2; //待收货
  24. const STATUS_FINISH = 3; //已完成
  25. const STATUS_CLOSE = 4; //已关闭
  26. /**
  27. * @notes 关联OrderGoods模型
  28. * @return \think\model\relation\HasMany
  29. * @author suny
  30. * @date 2021/7/13 6:47 下午
  31. */
  32. public function orderGoods()
  33. {
  34. return $this->hasMany('order_goods', 'order_id', 'id')
  35. ->field('id,order_id,goods_id,item_id,goods_name,goods_price,discount_price,spec_value,image,goods_num,is_comment,refund_status,commission_ratio,total_pay_price');
  36. }
  37. /**
  38. * @notes 订单用户
  39. * @return \think\model\relation\HasOne
  40. * @author suny
  41. * @date 2021/7/13 6:48 下午
  42. */
  43. public function user()
  44. {
  45. return $this->hasOne(User::class, 'id', 'user_id')
  46. ->field('id,sn,nickname,avatar,level,mobile,sex,create_time');
  47. }
  48. /**
  49. * @notes 关联Shop模型
  50. * @return \think\model\relation\HasOne
  51. * @author suny
  52. * @date 2021/7/13 6:49 下午
  53. */
  54. public function shop()
  55. {
  56. return $this->hasOne(Shop::class, 'id', 'shop_id')
  57. ->field('id,name,type,logo,mobile,open_invoice,spec_invoice,weekdays,run_start_time,run_end_time')
  58. ->append(['type_desc']);
  59. }
  60. /**
  61. * @notes 关联发票模型
  62. * @return \think\model\relation\HasOne
  63. * @author 段誉
  64. * @date 2022/4/11 18:39
  65. */
  66. public function invoice()
  67. {
  68. return $this->hasOne(OrderInvoice::class, 'order_id', 'id');
  69. }
  70. /**
  71. * @notes 订单状态
  72. * @param bool $status
  73. * @param int $shop_id
  74. * @return string|string[]
  75. * @author suny
  76. * @date 2021/7/13 6:46 下午
  77. */
  78. public static function getOrderStatus($status = true, $shop_id = 0)
  79. {
  80. $desc = [
  81. OrderEnum::ORDER_STATUS_NO_PAID => "待付款",
  82. OrderEnum::ORDER_STATUS_DELIVERY => "待发货",
  83. OrderEnum::ORDER_STATUS_GOODS => "待收货",
  84. OrderEnum::ORDER_STATUS_COMPLETE => "已完成",
  85. OrderEnum::ORDER_STATUS_DOWN => "已关闭",
  86. ];
  87. if ($status === true) {
  88. return $desc;
  89. }
  90. return $desc[$status] ?? '未知';
  91. }
  92. /**
  93. * @notes 订单类型
  94. * @param $type
  95. * @return string|string[]
  96. * @author suny
  97. * @date 2021/7/13 6:47 下午
  98. */
  99. public static function getOrderType($type)
  100. {
  101. $desc = [
  102. OrderEnum::NORMAL_ORDER => '普通订单',
  103. OrderEnum::SECKILL_ORDER => '秒杀订单',
  104. OrderEnum::TEAM_ORDER => '拼团订单',
  105. OrderEnum::BARGAIN_ORDER => '砍价订单',
  106. ];
  107. if ($type === true) {
  108. return $desc;
  109. }
  110. return $desc[$type] ?? '未知';
  111. }
  112. /**
  113. * @notes 配送方式
  114. * @param $type
  115. * @return string|string[]
  116. * @author suny
  117. * @date 2021/7/13 6:47 下午
  118. */
  119. public static function getDeliveryType($type)
  120. {
  121. $desc = [
  122. OrderEnum::DELIVERY_TYPE_EXPRESS => '快递发货',
  123. OrderEnum::DELIVERY_TYPE_VIRTUAL => '虚拟发货',
  124. OrderEnum::DELIVERY_TYPE_SELF => '线下自提',
  125. ];
  126. if ($type === true) {
  127. return $desc;
  128. }
  129. return $desc[$type] ?? '未知';
  130. }
  131. /**
  132. * @notes can_change_invoice_no 是否可修改物流单号
  133. * @param $value
  134. * @param $data
  135. * @return int
  136. * @author lbzy
  137. * @datetime 2024-04-02 15:31:40
  138. */
  139. function getCanChangeInvoiceNoAttr($value, $data)
  140. {
  141. return (int) in_array($data['order_status'], OrderEnum::CAN_CHANGE_INVOICE_NO_ARR);
  142. }
  143. /**
  144. * @notes 配送方式
  145. * @param $value
  146. * @param $data
  147. * @return string|string[]
  148. * @author suny
  149. * @date 2021/7/13 6:47 下午
  150. */
  151. public function getDeliveryTypeTextAttr($value, $data)
  152. {
  153. return self::getDeliveryType($data['delivery_type']);
  154. }
  155. /**
  156. * @notes 订单类型
  157. * @param $value
  158. * @param $data
  159. * @return string|string[]
  160. * @author suny
  161. * @date 2021/7/13 6:47 下午
  162. */
  163. public function getOrderTypeTextAttr($value, $data)
  164. {
  165. return self::getOrderType($data['order_type']);
  166. }
  167. /**
  168. * @notes 订单状态
  169. * @param $value
  170. * @param $data
  171. * @return string|string[]
  172. * @author suny
  173. * @date 2021/7/13 6:47 下午
  174. */
  175. public function getOrderStatusTextAttr($value, $data)
  176. {
  177. return self::getOrderStatus($data['order_status']);
  178. }
  179. /**
  180. * @notes 订单支付方式
  181. * @param $value
  182. * @param $data
  183. * @return array|mixed|string|string[]
  184. * @author suny
  185. * @date 2021/7/13 6:48 下午
  186. */
  187. public function getPayWayTextAttr($value, $data)
  188. {
  189. return PayEnum::getPayWay($data['pay_way']);
  190. }
  191. /**
  192. * @notes 订单支付状态
  193. * @param $value
  194. * @param $data
  195. * @return string|string[]
  196. * @author suny
  197. * @date 2021/7/13 6:48 下午
  198. */
  199. public function getPayStatusTextAttr($value, $data)
  200. {
  201. return PayEnum::getPayStatus($data['pay_status']);
  202. }
  203. /**
  204. * @notes 订单来源
  205. * @param $value
  206. * @param $data
  207. * @return string|string[]
  208. * @author suny
  209. * @date 2021/7/13 6:48 下午
  210. */
  211. public function getOrderSourceTextAttr($value, $data)
  212. {
  213. return Client_::getClient($data['order_source']);
  214. }
  215. /**
  216. * @notes 订单商品数量
  217. * @param $value
  218. * @param $data
  219. * @return int
  220. * @author suny
  221. * @date 2021/7/13 6:48 下午
  222. */
  223. public function getGoodsCountAttr($value, $data)
  224. {
  225. return count($this->order_goods);
  226. }
  227. /**
  228. * @notes 收货地址
  229. * @param $value
  230. * @param $data
  231. * @return string
  232. * @author suny
  233. * @date 2021/7/13 6:48 下午
  234. */
  235. public function getDeliveryAddressAttr($value, $data)
  236. {
  237. $region = Db::name('dev_region')
  238. ->where('id', 'IN', [$data['province'], $data['city'], $data['district']])
  239. ->order('level asc')
  240. ->column('name');
  241. $region_desc = implode('', $region);
  242. return $region_desc . $data['address'];
  243. }
  244. public function getPcAddressAttr($value, $data)
  245. {
  246. $region = Db::name('dev_region')
  247. ->where('id', 'IN', [$data['province'], $data['city'], $data['district']])
  248. ->order('level asc')
  249. ->column('name');
  250. $region_desc = implode(' ', $region);
  251. return $region_desc . $data['address'];
  252. }
  253. /**
  254. * @notes 返回是否显示支付按钮
  255. * @param $value
  256. * @param $data
  257. * @return int
  258. * @author suny
  259. * @date 2021/7/13 6:48 下午
  260. */
  261. public function getPayBtnAttr($value, $data)
  262. {
  263. $btn = 0;
  264. if ($data['order_status'] == OrderEnum::ORDER_STATUS_NO_PAID && $data['pay_status'] == PayEnum::UNPAID) {
  265. $btn = 1;
  266. }
  267. return $btn;
  268. }
  269. /**
  270. * @notes 返回是否显示取消订单按钮
  271. * @param $value
  272. * @param $data
  273. * @return int
  274. * @author suny
  275. * @date 2021/7/13 6:48 下午
  276. */
  277. public function getCancelBtnAttr($value, $data)
  278. {
  279. $btn = 0;
  280. if (is_string($data['create_time'])) {
  281. $data['create_time'] = strtotime($data['create_time']);
  282. }
  283. //多长时间内允许客户自动取消
  284. $cancel_limit = ConfigServer::get('transaction', 'paid_order_cancel_time', 60);
  285. $limit_time = $data['create_time'] + $cancel_limit * 60;
  286. if ($limit_time < time()) {
  287. return $btn;
  288. }
  289. if (($data['order_status'] == OrderEnum::ORDER_STATUS_NO_PAID && $data['pay_status'] == PayEnum::UNPAID)
  290. || ($data['pay_status'] == PayEnum::ISPAID && $data['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY)) {
  291. $btn = 1;
  292. }
  293. return $btn;
  294. }
  295. /**
  296. * @notes 返回是否显示物流按钮
  297. * @param $value
  298. * @param $data
  299. * @return int
  300. * @author suny
  301. * @date 2021/7/13 6:49 下午
  302. */
  303. public function getDeliveryBtnAttr($value, $data)
  304. {
  305. $btn = 0;
  306. // 虚拟发货类型不显示物流查询按钮
  307. if ($data['delivery_type'] != OrderEnum::DELIVERY_TYPE_EXPRESS) {
  308. return $btn;
  309. }
  310. if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS && $data['pay_status'] == PayEnum::ISPAID && $data['shipping_status'] == 1) {
  311. $btn = 1;
  312. }
  313. if ($data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $data['pay_status'] == PayEnum::ISPAID && $data['shipping_status'] == 1) {
  314. $btn = 1;
  315. }
  316. return $btn;
  317. }
  318. /**
  319. * @notes 返回是否显示确认收货按钮
  320. * @param $value
  321. * @param $data
  322. * @return int
  323. * @author suny
  324. * @date 2021/7/13 6:49 下午
  325. */
  326. public function getTakeBtnAttr($value, $data)
  327. {
  328. $btn = 0;
  329. if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS
  330. && $data['pay_status'] == PayEnum::ISPAID
  331. && $data['shipping_status'] == 1
  332. && $data['delivery_type'] != OrderEnum::DELIVERY_TYPE_SELF
  333. ) {
  334. $btn = 1;
  335. }
  336. return $btn;
  337. }
  338. /**
  339. * @notes 返回是否显示删除按钮
  340. * @param $value
  341. * @param $data
  342. * @return int
  343. * @author suny
  344. * @date 2021/7/13 6:49 下午
  345. */
  346. public function getDelBtnAttr($value, $data)
  347. {
  348. $btn = 0;
  349. if (
  350. ($data['order_status'] == OrderEnum::ORDER_STATUS_DOWN && $data['pay_status'] == PayEnum::UNPAID) ||
  351. ($data['order_status'] == OrderEnum::ORDER_STATUS_DOWN && $data['pay_status'] == PayEnum::REFUNDED)
  352. ) {
  353. $btn = 1;
  354. }
  355. return $btn;
  356. }
  357. /**
  358. * @notes 返回是否显示已完成按钮
  359. * @param $value
  360. * @param $data
  361. * @return int
  362. * @author suny
  363. * @date 2021/7/13 6:49 下午
  364. */
  365. public function getFinishBtnAttr($value, $data)
  366. {
  367. $btn = 0;
  368. if ($data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $data['pay_status'] == PayEnum::ISPAID && $data['delivery_type'] != OrderEnum::DELIVERY_TYPE_SELF) {
  369. $btn = 1;
  370. }
  371. return $btn;
  372. }
  373. /**
  374. * @notes 返回是否显示去评论按钮
  375. * @param $value
  376. * @param $data
  377. * @return int
  378. * @author suny
  379. * @date 2021/7/13 6:49 下午
  380. */
  381. public function getCommentBtnAttr($value, $data)
  382. {
  383. $btn = 0;
  384. $comment_count = 0;
  385. if ($data['pay_status'] == PayEnum::ISPAID && $data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE) {
  386. $btn = 1;
  387. foreach ($this->order_goods as $item) {
  388. if ($item['is_comment'] == 1) {
  389. $comment_count += 1;
  390. };
  391. }
  392. if (count($this->orderGoods) == $comment_count) {
  393. $btn = 0;
  394. }
  395. }
  396. return $btn;
  397. }
  398. /**
  399. * @notes 返回是否显示申请退款按钮
  400. * @param $value
  401. * @param $data
  402. * @return int
  403. * @author suny
  404. * @date 2021/7/13 6:49 下午
  405. */
  406. public function getRefundBtnAttr($value, $data)
  407. {
  408. $btn = 0;
  409. $data['confirm_take_time'] = strtotime($data['confirm_take_time']);
  410. $refund_days = $data['confirm_take_time'] + ConfigServer::get('after_sale', 'refund_days', '', 0) * 86400;
  411. $now = time();
  412. //订单已完成、在售后期内。未申请退款、
  413. if ($data['order_status'] == OrderEnum::ORDER_STATUS_COMPLETE && $refund_days > $now && $data['refund_status'] = OrderGoodsEnum::REFUND_STATUS_NO) {
  414. $btn = 1;
  415. }
  416. return $btn;
  417. }
  418. /**
  419. * @notes 发货内容(查看内容按钮)
  420. * @param $value
  421. * @param $data
  422. * @return int
  423. * @author 段誉
  424. * @date 2022/4/8 10:22
  425. */
  426. public function getContentBtnAttr($value, $data)
  427. {
  428. $btn = 0;
  429. if ($data['delivery_type'] == OrderEnum::DELIVERY_TYPE_VIRTUAL && $data['shipping_status'] == OrderEnum::SHIPPING_FINISH) {
  430. $btn = 1;
  431. }
  432. return $btn;
  433. }
  434. /**
  435. * @notes 申请开票按钮
  436. * @param $value
  437. * @param $data
  438. * @return int
  439. * @author 段誉
  440. * @date 2022/4/12 15:19
  441. */
  442. public function getSaveInvoiceBtnAttr($value, $data)
  443. {
  444. $btn = 0;
  445. $invoice = OrderInvoice::where(['order_id' => $data['id']])->findOrEmpty();
  446. if ($invoice->isEmpty()) {
  447. $btn = 1;
  448. }
  449. return $btn;
  450. }
  451. /**
  452. * @notes 查看开票按钮
  453. * @param $value
  454. * @param $data
  455. * @return int
  456. * @author 段誉
  457. * @date 2022/4/12 15:19
  458. */
  459. public function getViewInvoiceBtnAttr($value, $data)
  460. {
  461. $btn = 0;
  462. $invoice = OrderInvoice::where(['order_id' => $data['id']])->findOrEmpty();
  463. if (!$invoice->isEmpty()) {
  464. $btn = 1;
  465. }
  466. return $btn;
  467. }
  468. /**
  469. * @notes 取消订单时间
  470. * @param $value
  471. * @param $data
  472. * @return false|float|int|string
  473. * @author suny
  474. * @date 2021/7/13 6:49 下午
  475. */
  476. public function getOrderCancelTimeAttr($value, $data)
  477. {
  478. $end_time = '';
  479. if (is_string($data['create_time'])) {
  480. $data['create_time'] = strtotime($data['create_time']);
  481. }
  482. if ($data['order_status'] == 0 && $data['pay_status'] == 0) {
  483. $order_cancel_time = ConfigServer::get('transaction', 'unpaid_order_cancel_time', 60);
  484. $end_time = $data['create_time'] + $order_cancel_time * 60;
  485. }
  486. return $end_time;
  487. }
  488. /**
  489. * @notes 关联未评价子订单
  490. * @return \think\model\relation\HasMany
  491. * @author suny
  492. * @date 2021/7/13 6:50 下午
  493. */
  494. public function orderGoodsUnComment()
  495. {
  496. return $this->hasMany('order_goods', 'order_id', 'id')
  497. ->field('id,order_id,goods_id,item_id,goods_num,goods_name,goods_price,is_comment')
  498. ->where('is_comment', 0);
  499. }
  500. /**
  501. * @notes 返回是否显示修改地址按钮
  502. * @param $value
  503. * @param $data
  504. * @return int
  505. * @author ljj
  506. * @date 2021/11/10 11:37 上午
  507. */
  508. public function getEditAddressBtnAttr($value, $data)
  509. {
  510. $btn = 0;
  511. if ($data['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF || $data['delivery_type'] == OrderEnum::DELIVERY_TYPE_VIRTUAL) {
  512. return $btn;
  513. }
  514. if ($data['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY && $data['pay_status'] == PayEnum::ISPAID) {
  515. $btn = 1;
  516. }
  517. return $btn;
  518. }
  519. /**
  520. * @notes 返回是否显示去发货按钮
  521. * @param $value
  522. * @param $data
  523. * @return int
  524. * @author ljj
  525. * @date 2021/11/10 11:41 上午
  526. */
  527. public function getToShipBtnAttr($value, $data)
  528. {
  529. if ($data['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF) {
  530. return 0;
  531. }
  532. if ($data['order_status'] != OrderEnum::ORDER_STATUS_DELIVERY) {
  533. return 0;
  534. }
  535. if ($data['order_type'] == OrderEnum::TEAM_ORDER && $this->is_team_success) {
  536. return 1;
  537. }
  538. if ($data['order_type'] != OrderEnum::TEAM_ORDER) {
  539. return 1;
  540. }
  541. return 0;
  542. }
  543. /**
  544. * @notes 返回是否显示取消订单(商家端)按钮
  545. * @param $value
  546. * @param $data
  547. * @return int
  548. * @author ljj
  549. * @date 2021/11/10 11:41 上午
  550. */
  551. public function getShopCancelBtnAttr($value, $data)
  552. {
  553. $btn = 0;
  554. if ($data['order_status'] == OrderEnum::ORDER_STATUS_NO_PAID && $data['pay_status'] == PayEnum::UNPAID){
  555. $btn = 1;
  556. }
  557. if ($data['pay_status'] == PayEnum::ISPAID && $data['order_status'] == OrderEnum::ORDER_STATUS_DELIVERY) {
  558. $btn = 1;
  559. }
  560. // 自提
  561. if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS && $data['delivery_type'] == 2) {
  562. $btn = 1;
  563. }
  564. return $btn;
  565. }
  566. /**
  567. * @notes is_team_success 是否拼团成功
  568. * @param $value
  569. * @param $data
  570. * @return int
  571. * @author lbzy
  572. * @datetime 2024-06-13 10:03:39
  573. */
  574. function getIsTeamSuccessAttr($value, $data)
  575. {
  576. $success = 0;
  577. if ($data['order_type'] == OrderEnum::TEAM_ORDER) {
  578. $team = TeamJoin::field('TJ.status,TF.status as found_status')->alias('TJ')
  579. ->where(['TJ.order_id'=>$data['id']])
  580. ->field([ 'TJ.status' ])
  581. ->join('team_found TF', 'TF.id = TJ.team_id')
  582. ->findOrEmpty()->toArray();
  583. if ($team) {
  584. $success = $team['status'] == 1 ? 1 : 0;
  585. }
  586. }
  587. return $success;
  588. }
  589. /**
  590. * @notes 返回是否显示跳转核销(商家端)按钮
  591. * @param $value
  592. * @param $data
  593. * @return int
  594. * @author mjf
  595. * @date 2022/11/03 18:57
  596. */
  597. public function getToVerificationBtnAttr($value, $data)
  598. {
  599. if ($data['order_status'] == OrderEnum::ORDER_STATUS_GOODS && $data['delivery_type'] == OrderEnum::DELIVERY_TYPE_SELF) {
  600. return 1;
  601. }
  602. return 0;
  603. }
  604. /**
  605. * @notes 核销状态描述
  606. * @param $value
  607. * @param $data
  608. * @return string|string[]
  609. * @author 段誉
  610. * @date 2022/11/2 14:16
  611. */
  612. public function getVerificationStatusTextAttr($value, $data)
  613. {
  614. return OrderEnum::getVerificationStatusDesc($data['verification_status']);
  615. }
  616. /**
  617. * @notes 汇付斗拱参数
  618. * @param $fieldValue
  619. * @param $data
  620. * @return array
  621. * @author lbzy
  622. * @datetime 2023-10-23 17:28:25
  623. */
  624. function getHfdgParamsAttr($fieldValue, $data)
  625. {
  626. return $fieldValue ? ((array) json_decode($fieldValue, true)) : [];
  627. }
  628. function setHfdgParamsAttr($fieldValue, $data)
  629. {
  630. if (is_string($fieldValue)) {
  631. return $fieldValue;
  632. }
  633. return json_encode((array) $fieldValue, JSON_UNESCAPED_UNICODE);
  634. }
  635. }