BillDao.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Dao\Settlement;
  3. use App\Consts\BaseConst;
  4. use App\Consts\BillConst;
  5. use App\Models\Order\Order;
  6. use App\Models\Settlement\Bills;
  7. class BillDao
  8. {
  9. /**
  10. * 获取渠道结算账单列表
  11. *
  12. * @param $param
  13. * @return mixed
  14. */
  15. public function getBills($param)
  16. {
  17. $query = Bills::select('id', 'date', 'recharge_amount', 'rate', 'service_amount', 'settlement_price')
  18. ->where('type', BillConst::SETTLEMENT_TYPE_CHANNEL);
  19. // 站点
  20. $channelId = (int)getProp($param, 'channel_id');
  21. if ($channelId) {
  22. $query->where('distribution_channel_id', $channelId);
  23. }
  24. $dateRange = getProp($param, 'date_range');
  25. if ($dateRange) {
  26. [$startDate, $endDate] = explode(',', $dateRange);
  27. if ($startDate && $endDate && $startDate <= $endDate) {
  28. $query->where('date', '>=', $startDate);
  29. $query->where('date', '<=', $endDate);
  30. }
  31. } else {
  32. // 只显示7日前数据
  33. $query->where('date', '<=', date('Y-m-d', strtotime('-' . BaseConst::DATE_RANGE_DAYS . ' days')));
  34. }
  35. // 导出用
  36. $all = (int)getProp($param, 'export');
  37. if ($all) {
  38. return $query->orderBy('id', 'desc')->get();
  39. }
  40. return $query->orderBy('id', 'desc')->paginate(10);
  41. }
  42. /**
  43. * @param $channelId
  44. * @param $date
  45. * @return mixed
  46. */
  47. public function billOrders($channelId, $date)
  48. {
  49. return Order::select('orders.id', 'price', 'send_order_id', 'send_orders.name as send_order_name', 'orders.created_at')
  50. ->leftJoin('send_orders', 'orders.send_order_id', '=', 'send_orders.id')
  51. ->where('orders.distribution_channel_id', $channelId)
  52. ->where('orders.status', 'PAID')
  53. ->where('orders.send_order_id', '>', 0)
  54. ->where('orders.created_at', '>=', date('Y-m-d 00:00:00', strtotime($date)))
  55. ->where('orders.created_at', '<=', date('Y-m-d 23:59:59', strtotime($date)))
  56. ->orderBy('orders.id', 'desc')
  57. ->paginate();
  58. }
  59. /**
  60. * @param $channelId
  61. * @param string $startDate
  62. * @param string $endDate
  63. * @return mixed
  64. */
  65. public function getBillsRechargeAmount($channelId, string $startDate = '', string $endDate = '')
  66. {
  67. $query = Bills::where('distribution_channel_id', $channelId)->where('type', 'channel');
  68. if ($startDate && $endDate) {
  69. $query->whereBetween('date', [$startDate, $endDate]);
  70. }
  71. if ($endDate && !$startDate) {
  72. $query->where('date', '<=', $endDate);
  73. }
  74. return $query->sum('recharge_amount');
  75. }
  76. }