OrderService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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', '<>', 'RECHARGE')
  22. ->where('pay_category', '<>', 'CLEAR')
  23. ->sum('pay_amount');
  24. // 今日充值额
  25. $data['todayRecharge'] = Order::where('order_status_payment', 'SUCCESS')
  26. ->where('order_addtimes', '>', $todayTimeUnix)
  27. ->where('order_category', 'RECHARGE')
  28. ->where('order_status_payment','SUCCESS')
  29. ->sum('order_amount_pay');
  30. // 今日退单
  31. $data['todayRefund'] = OrderReturn::where('order_return_addtimes', '>', $todayTimeUnix)
  32. ->count();
  33. // 柱状图
  34. // 产品订单
  35. $newAddition = Db::select("SELECT DATE_FORMAT(FROM_UNIXTIME(order_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order group by month");
  36. // $newAddition = Db::select("
  37. // SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  38. // left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  39. // where g.goods_classify = 'GOODS'
  40. // group by month
  41. // ");
  42. $category = [];
  43. $newAdditionData = [];
  44. foreach ($newAddition as $item) {
  45. if (!in_array($item->month, $category)) {
  46. $category[] = $item->month;
  47. }
  48. // $newAdditionData['category'][] = $item->month;
  49. // $newAdditionData['data'][] = $item->num;
  50. }
  51. // 服务订单
  52. $newAdditionService = Db::select("
  53. SELECT DATE_FORMAT(FROM_UNIXTIME(order_sheet_addtimes), '%Y/%m') AS `month`,count(*) as `num` FROM app_order_sheet as s
  54. left join app_goods as g on s.join_sheet_goods_id = g.goods_id
  55. where g.goods_classify = 'SERVICE'
  56. group by month
  57. ");
  58. $newAdditionServiceData = [];
  59. foreach ($newAdditionService as $item) {
  60. if (!in_array($item->month, $category)) {
  61. $category[] = $item->month;
  62. }
  63. // $newAdditionServiceData['category'][] = $item->month;
  64. // $newAdditionServiceData['data'][] = $item->num;
  65. }
  66. sort($category);
  67. foreach ($category as $item2){
  68. $isHaveService = false;
  69. $isHaveGoods = false;
  70. foreach ($newAdditionService as $item){
  71. if ($item2 == $item->month){
  72. $isHaveService = true;
  73. $newAdditionServiceData['category'][] = $item->month;
  74. $newAdditionServiceData['data'][] = $item->num;
  75. }
  76. }
  77. foreach ($newAddition as $item3){
  78. if ($item2 == $item3->month){
  79. $isHaveGoods = true;
  80. $newAdditionData['category'][] = $item3->month;
  81. $newAdditionData['data'][] = $item3->num;
  82. }
  83. }
  84. if (!$isHaveService){
  85. $newAdditionServiceData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  86. $newAdditionServiceData['data'][] = 0;
  87. }
  88. if (!$isHaveGoods){
  89. $newAdditionData['category'][] = $item2;//['month'=>$item2,'num'=>0];
  90. $newAdditionData['data'][] = 0;
  91. }
  92. }
  93. $data['addition']['goods'] = $newAdditionData;
  94. $data['addition']['service'] = $newAdditionServiceData;
  95. return $data;
  96. }
  97. }