OrderDao.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Dao\Order;
  3. use App\Models\Order\Order;
  4. class OrderDao
  5. {
  6. /**
  7. * @param $param
  8. * @return mixed
  9. */
  10. public function orderList($param)
  11. {
  12. // 查询字段
  13. $fields = [
  14. 'orders.id', 'orders.distribution_channel_id', 'orders.uid', 'orders.price', 'orders.status', 'orders.trade_no',
  15. 'orders.from_bid', 'orders.order_type', 'orders.send_order_id', 'send_orders.name as send_order_name',
  16. 'books.name as book_name', 'orders.created_at', 'orders.updated_at', 'users.created_at as register_time',
  17. 'users.register_ip', 'channel_users.nickname', 'channel_copy_user_mappings.created_at as bind_send_order_time'
  18. ];
  19. $query = Order::select($fields)
  20. ->leftJoin('users', 'users.id', 'orders.uid')
  21. ->leftJoin('books', 'books.id', 'orders.from_bid')
  22. ->leftJoin('send_orders', 'send_orders.id', 'orders.send_order_id')
  23. ->leftJoin('distribution_channels', 'distribution_channels.id', 'orders.distribution_channel_id')
  24. ->leftJoin('channel_users', 'channel_users.id', 'distribution_channels.channel_user_id')
  25. ->leftJoin('channel_copy_user_mappings', 'channel_copy_user_mappings.trade_no', 'orders.trade_no');
  26. // 站点
  27. $channelId = getProp($param, 'channel_id');
  28. if ($channelId) {
  29. $query->whereIn('orders.distribution_channel_id', $channelId);
  30. }
  31. // 订单编号
  32. $tradeNo = trim(getProp($param, 'trade_no'));
  33. if ($tradeNo) {
  34. $query->where('orders.trade_no', $tradeNo);
  35. }
  36. // 用户uid
  37. $uid = (int)getProp($param, 'uid');
  38. if ($uid) {
  39. $query->where('orders.uid', $uid);
  40. }
  41. // 派单id
  42. $sendOrderId = (int)getProp($param, 'send_order_id');
  43. if ($sendOrderId) {
  44. $query->where('orders.send_order_id', $sendOrderId);
  45. }
  46. // 充值类型
  47. $orderType = trim(getProp($param, 'order_type'));
  48. if (in_array($orderType, ['RECHARGE', 'YEAR', 'QUARTER', 'MONTH', 'WEEK'])) {
  49. $query->where('orders.order_type', $orderType);
  50. }
  51. // 提现状态,状态 PAID:已支付 UNPAID未支付; REFUND 退款; FAIL失败
  52. $status = trim(getProp($param, 'status'));
  53. if ($status) {
  54. $query->where('orders.status', $status);
  55. }
  56. // 订单创建时间
  57. $dateRange = getProp($param, 'date_range');
  58. if ($dateRange) {
  59. [$startDate, $endDate] = explode(',', $dateRange);
  60. if ($startDate && $endDate && $startDate <= $endDate) {
  61. $query->where('orders.created_at', '>=', date('Y-m-d 00:00:00', strtotime($startDate)));
  62. $query->where('orders.created_at', '<=', date('Y-m-d 23:59:59', strtotime($endDate)));
  63. }
  64. }
  65. // 导出用
  66. $all = (int)getProp($param, 'all');
  67. if ($all) {
  68. return $query->orderBy('orders.created_at', 'desc')->get();
  69. }
  70. return $query->orderBy('orders.created_at', 'desc')->paginate(10);
  71. }
  72. /**
  73. * 站点充值总额
  74. *
  75. * @param $channelId
  76. * @return mixed
  77. */
  78. public function getChannelRechargeAmount($channelId)
  79. {
  80. return Order::where('distribution_channel_id', $channelId)
  81. ->where('status', 'PAID')
  82. ->sum('price');
  83. }
  84. /**
  85. * 站点充值总额(不含今日)
  86. *
  87. * @param $channelId
  88. * @return mixed
  89. */
  90. public function getChannelRechargeAmountWithoutToday($channelId)
  91. {
  92. return Order::where('distribution_channel_id', $channelId)
  93. ->where('status', 'PAID')
  94. ->where('created_at', '<', date('Y-m-d 00:00:00'))
  95. ->sum('price');
  96. }
  97. /**
  98. * 站点月充值总额(不含今日)
  99. *
  100. * @param $channelId
  101. * @return mixed
  102. */
  103. public function getChannelMonthRechargeAmountWithoutToday($channelId)
  104. {
  105. return Order::where('distribution_channel_id', $channelId)
  106. ->where('status', 'PAID')
  107. ->where('created_at', '>=', date('Y-m-01 00:00:00'))
  108. ->where('created_at', '<', date('Y-m-d 00:00:00'))
  109. ->sum('price');
  110. }
  111. }