StoreOrder.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use think\Model;
  5. use think\facade\Db;
  6. /**
  7. * @mixin \think\Model
  8. */
  9. class StoreOrder extends Model
  10. {
  11. protected $name = 'store_order';
  12. /**
  13. * 获取订单列表
  14. * @param array $where
  15. * @param int $page
  16. * @param int $pageSize
  17. * @return array
  18. */
  19. public function getList($where = [], $page = 1, $pageSize = 20)
  20. {
  21. $query = $this->where($where);
  22. $totalCount = $query->count();
  23. $list = $query
  24. ->order('id', 'desc')
  25. ->page($page, $pageSize)
  26. ->select()
  27. ->toArray();
  28. // 关联订单商品信息
  29. if (!empty($list)) {
  30. foreach ($list as &$order) {
  31. $cartInfo = Db::name('store_order_cart_info')
  32. ->where('oid', $order['id'])
  33. ->select()
  34. ->toArray();
  35. foreach ($cartInfo as &$item) {
  36. if (!empty($item['cart_info'])) {
  37. $innerCartInfo = json_decode($item['cart_info'], true);
  38. unset($item['cart_info']);
  39. $item = array_merge($item, $innerCartInfo);
  40. }
  41. }
  42. $order['cart_info'] = $cartInfo;
  43. }
  44. }
  45. return [
  46. 'list' => $list,
  47. 'totalCount' => $totalCount,
  48. 'pageSize' => $pageSize,
  49. 'page' => $page
  50. ];
  51. }
  52. /**
  53. * 获取订单详情
  54. * @param int $id
  55. * @return array|null
  56. */
  57. public function getOrderDetail($id)
  58. {
  59. $order = $this->where('id', $id)->find();
  60. if (!$order) {
  61. return null;
  62. }
  63. $data = $order->toArray();
  64. // 获取订单商品
  65. $cartInfo = (new StoreOrderCartInfo())->where('oid', $id)->select()->toArray();
  66. $data['cart_info'] = $cartInfo;
  67. return $data;
  68. }
  69. }