OrderService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\service\statistics;
  3. use app\model\Order;
  4. use app\model\OrderReturn;
  5. use app\model\PayDetail;
  6. use support\Db;
  7. class OrderService
  8. {
  9. public static function getOrder()
  10. {
  11. // 今日订单
  12. $todayTimeUnix = strtotime(date('Y-m-d'));
  13. // 今日订单
  14. $data['todayOrder'] = Order::where('order_addtimes', '>', $todayTimeUnix)->where('order_category', '<>', 'RECHARGE')->count();
  15. // 今日收入
  16. // $couponDetails = Db::select("select * from app_coupon_detail where coupon_detail_deadline_datetime != '' AND CAST(UNIX_TIMESTAMP(coupon_detail_deadline_datetime) as SIGNED) < " . time());
  17. $data['todayRevenue'] = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ' , [$todayTimeUnix])
  18. ->where('join_pay_order_id','<>','')
  19. ->where('pay_status', 'SUCCESS')
  20. ->where('pay_category', '<>', 'RETURN')
  21. ->where('pay_category', '<>', 'REFUND')
  22. ->where('pay_category', '<>', 'RECHARGE')
  23. ->where('pay_category', '<>', 'CLEAR')
  24. ->sum('pay_amount');
  25. $refundAmount = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) > ? ' , [$todayTimeUnix])
  26. ->where('pay_status', 'SUCCESS')
  27. ->where('pay_category', 'REFUND')
  28. ->sum('pay_amount');
  29. // $data['todayRevenue'] = $data['todayRevenue'] - $refundAmount;
  30. $data['todayRevenue'] = round($data['todayRevenue'],2);
  31. // 今日充值额
  32. $data['todayRecharge'] = Order::where('order_status_payment', 'SUCCESS')
  33. ->where('order_addtimes', '>', $todayTimeUnix)
  34. ->where('order_category', 'RECHARGE')
  35. ->where('order_status_payment','SUCCESS')
  36. ->sum('order_amount_pay');
  37. // 今日退单
  38. $data['todayRefund'] = $refundAmount;
  39. // 柱状图
  40. // 产品订单
  41. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
  42. // $newAddition = Db::select("
  43. // SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  44. // left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  45. // where g.goods_classify = 'GOODS'
  46. // group by month
  47. // ");
  48. $category = [];
  49. $newAdditionData = [];
  50. foreach ($newAddition as $item) {
  51. if (!in_array($item->month, $category)) {
  52. $category[] = $item->month;
  53. }
  54. // $newAdditionData['category'][] = $item->month;
  55. // $newAdditionData['data'][] = $item->num;
  56. }
  57. // 服务订单
  58. $newAdditionService = Db::select("
  59. SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  60. left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  61. where g.goods_classify = 'SERVICE'
  62. group by month
  63. ");
  64. $newAdditionServiceData = [];
  65. foreach ($newAdditionService as $item) {
  66. if (!in_array($item->month, $category)) {
  67. $category[] = $item->month;
  68. }
  69. // $newAdditionServiceData['category'][] = $item->month;
  70. // $newAdditionServiceData['data'][] = $item->num;
  71. }
  72. sort($category);
  73. foreach ($category as $item2){
  74. $isHaveService = false;
  75. $isHaveGoods = false;
  76. foreach ($newAdditionService as $item){
  77. if ($item2 == $item->month){
  78. $isHaveService = true;
  79. $newAdditionServiceData['category'][] = $item->month;
  80. $newAdditionServiceData['data'][] = $item->num;
  81. }
  82. }
  83. foreach ($newAddition as $item3){
  84. if ($item2 == $item3->month){
  85. $isHaveGoods = true;
  86. $newAdditionData['category'][] = $item3->month;
  87. $newAdditionData['data'][] = $item3->num;
  88. }
  89. }
  90. if (!$isHaveService){
  91. $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  92. $newAdditionServiceData['data'][] = 0;
  93. }
  94. if (!$isHaveGoods){
  95. $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  96. $newAdditionData['data'][] = 0;
  97. }
  98. }
  99. $data['addition']['goods'] = $newAdditionData;
  100. $data['addition']['service'] = $newAdditionServiceData;
  101. return $data;
  102. }
  103. }