OrderController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. namespace App\Http\Controllers\App;
  3. use App\Filters\DepositFilter;
  4. use App\Filters\DepositOrderFilter;
  5. use App\Filters\OrderFilter;
  6. use App\Filters\OrderRentFilter;
  7. use App\Filters\UserFilter;
  8. use App\Filters\WalletLogFilter;
  9. use App\Http\Resources\App\OrderResource;
  10. use App\Models\AdminUser;
  11. use App\Models\Bike;
  12. use App\Models\DepositOrder;
  13. use App\Models\DepositRefund;
  14. use App\Models\LocationsLog;
  15. use App\Models\Order;
  16. use App\Models\OrderRent;
  17. use App\Models\User;
  18. use App\Models\WalletLog;
  19. use App\Utils\Admin;
  20. use Carbon\Carbon;
  21. use Illuminate\Http\Request;
  22. use App\Http\Controllers\Controller;
  23. use Illuminate\Support\Facades\Log;
  24. class OrderController extends AppBaseController
  25. {
  26. /**
  27. * orderList 订单列表
  28. *
  29. * @param OrderFilter $filter
  30. * @return \Illuminate\Http\JsonResponse
  31. * @author Fx
  32. *
  33. */
  34. public function orderList(OrderFilter $filter)
  35. {
  36. $area_ids = self::$areaIds;
  37. $order = Order::query()
  38. ->whereIn('area_id', $area_ids)
  39. ->filter($filter)
  40. ->with('users')
  41. ->where('status', '!=', Order::STATUS_CLOSE_ORDER)
  42. ->orderByDesc('id')
  43. ->paginate();
  44. return $this->ok(OrderResource::collection($order));
  45. }
  46. /**
  47. * orderStatistics 订单统计
  48. *
  49. * @param OrderFilter $filter
  50. * @return \Illuminate\Http\JsonResponse
  51. * @author Fx
  52. *
  53. */
  54. public function orderStatistics(OrderFilter $filter)
  55. {
  56. $area_ids = self::$areaIds;
  57. // 总订单数
  58. $order_total = Order::query()
  59. ->whereIn('area_id', $area_ids)
  60. ->filter($filter)
  61. ->where('status', Order::STATUS_COMPLETE_ORDER)
  62. ->count('id');
  63. $order_rent_total = OrderRent::query()
  64. ->whereIn('area_id', $area_ids)
  65. ->filter($filter)
  66. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  67. ->count('id');
  68. $order_total += $order_rent_total;
  69. // 今日新增订单数
  70. $today_add_order = Order::query()
  71. ->whereIn('area_id', $area_ids)
  72. ->filter($filter)
  73. ->where('created_at', '>', Carbon::today())
  74. ->where('status', Order::STATUS_COMPLETE_ORDER)
  75. ->count('id');
  76. $today_add_order_rent = OrderRent::query()
  77. ->whereIn('area_id', $area_ids)
  78. ->filter($filter)
  79. ->where('pay_time', '>', Carbon::today())
  80. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  81. ->count('id');
  82. $today_add_order += $today_add_order_rent;
  83. // 骑行中订单数
  84. $riding_status = [
  85. Order::STATUS_RIDE_BIKE,
  86. // Order::STATUS_CLOSE_BIKE, // 待支付
  87. Order::STATUS_PAUSE_BIKE,
  88. ];
  89. $riding_order = Order::query()
  90. ->whereIn('area_id', $area_ids)
  91. ->filter($filter)
  92. ->whereIn('status', $riding_status)
  93. ->count('id');
  94. $riding_order_rent = OrderRent::query()
  95. ->whereIn('area_id', $area_ids)
  96. ->filter($filter)
  97. ->where('status', OrderRent::STATUS_RENT_BIKE)
  98. ->count('id');
  99. $riding_order += $riding_order_rent;
  100. // 待支付
  101. $waiting_pay_order = Order::query()
  102. ->whereIn('area_id', $area_ids)
  103. ->filter($filter)
  104. ->where('status', Order::STATUS_CLOSE_BIKE)
  105. ->count('id');
  106. $waiting_pay_rent = OrderRent::query()
  107. ->whereIn('area_id', $area_ids)
  108. ->filter($filter)
  109. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  110. ->count('id');
  111. $waiting_pay_order += $waiting_pay_rent;
  112. // 今日新增收入
  113. $today_add_money = Order::query()
  114. ->whereIn('area_id', $area_ids)
  115. ->filter($filter)
  116. ->where('pay_time', '>', Carbon::today())
  117. ->where('status', Order::STATUS_COMPLETE_ORDER)
  118. ->sum('pay_money');
  119. $today_add_money_rent = OrderRent::query()
  120. ->whereIn('area_id', $area_ids)
  121. ->filter($filter)
  122. ->where('pay_time', '>', Carbon::today())
  123. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  124. ->sum('pay_money');
  125. $today_add_money += $today_add_money_rent;
  126. $order = [
  127. 'order_total' => $order_total ?? 0,
  128. 'today_add_order' => $today_add_order ?? 0,
  129. 'riding_order' => $riding_order ?? 0,
  130. 'today_add_money' => $today_add_money ?? 0,
  131. 'waiting_pay' => $waiting_pay_order ?? 0,
  132. ];
  133. return $this->ok($order);
  134. }
  135. /**
  136. * orderProfitStatistics 订单收益统计
  137. *
  138. * @param WalletLogFilter $walletLogFilter
  139. * @param DepositFilter $depositFilter
  140. * @param OrderFilter $orderFilter
  141. * @param OrderRentFilter $orderRentFilter
  142. * @return \Illuminate\Http\JsonResponse
  143. * @author Fx
  144. *
  145. */
  146. public function orderProfitStatistics(WalletLogFilter $walletLogFilter, DepositFilter $depositFilter, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter)
  147. {
  148. $area_ids = self::$areaIds;
  149. $admin_type = Admin::user()->type;
  150. if ($admin_type == AdminUser::TYPE_WORKER) {
  151. $data = [
  152. 'totalProfit' => $totalProfit ?? 0,
  153. 'depositTotal' => $depositTotal ?? 0,
  154. 'todayProfit' => $todayProfit ?? 0,
  155. 'depositToday' => $depositToday ?? 0,
  156. 'monthProfit' => $monthProfit ?? 0,
  157. ];
  158. return $this->ok($data);
  159. }
  160. // 总收益
  161. // $totalProfit = WalletLog::query()
  162. // ->whereIn('area_id', $area_ids)
  163. // ->filter($walletLogFilter)
  164. // ->whereIn('type', WalletLog::$subType)
  165. // ->sum('money');
  166. $totalOrderProfit = Order::query()
  167. ->whereIn('area_id', $area_ids)
  168. ->filter($orderFilter)
  169. ->where('status', Order::STATUS_COMPLETE_ORDER)
  170. ->sum('pay_money');
  171. $totalOrderRentProfit = OrderRent::query()
  172. ->whereIn('area_id', $area_ids)
  173. ->filter($orderRentFilter)
  174. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  175. ->sum('pay_money');
  176. $totalProfit = bcadd($totalOrderProfit, $totalOrderRentProfit, 0);
  177. // 总押金
  178. $depositTotal = DepositOrder::query()
  179. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  180. ->where('is_refund', DepositOrder::REFUND_NO)
  181. ->whereIn('area_id', $area_ids)
  182. ->filter($depositFilter)
  183. ->sum('pay_money');
  184. // 今日收益
  185. // $todayProfit = WalletLog::query()
  186. // ->whereIn('area_id', $area_ids)
  187. // ->filter($walletLogFilter)
  188. // ->whereIn('type', WalletLog::$subType)
  189. // ->where('created_at', '>', Carbon::today())
  190. // ->sum('money');
  191. $todayOrderProfit = Order::query()
  192. ->whereIn('area_id', $area_ids)
  193. ->filter($orderFilter)
  194. ->where('status', Order::STATUS_COMPLETE_ORDER)
  195. ->where('created_at', '>', Carbon::today())
  196. ->sum('pay_money');
  197. $todayOrderRentProfit = OrderRent::query()
  198. ->whereIn('area_id', $area_ids)
  199. ->filter($orderRentFilter)
  200. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  201. ->where('created_at', '>', Carbon::today())
  202. ->sum('pay_money');
  203. $todayProfit = bcadd($todayOrderProfit, $todayOrderRentProfit, 0);
  204. // 今日新增押金
  205. $depositToday = DepositOrder::query()
  206. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  207. ->where('is_refund', DepositOrder::REFUND_NO)
  208. ->whereIn('area_id', $area_ids)
  209. ->filter($depositFilter)
  210. ->where('created_at', '>', Carbon::today())
  211. ->sum('pay_money');
  212. // 本月收益
  213. // $monthProfit = WalletLog::query()
  214. // ->whereIn('area_id', $area_ids)
  215. // ->filter($walletLogFilter)
  216. // ->whereIn('type', WalletLog::$subType)
  217. // ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  218. // ->sum('money');
  219. $monthOrderProfit = Order::query()
  220. ->whereIn('area_id', $area_ids)
  221. ->filter($orderFilter)
  222. ->where('status', Order::STATUS_COMPLETE_ORDER)
  223. ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  224. ->sum('pay_money');
  225. $monthOrderRentProfit = OrderRent::query()
  226. ->whereIn('area_id', $area_ids)
  227. ->filter($orderRentFilter)
  228. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  229. ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  230. ->sum('pay_money');
  231. $monthProfit = bcadd($monthOrderProfit, $monthOrderRentProfit, 0);
  232. $data = [
  233. 'totalProfit' => floor(abs($totalProfit)) ?? 0,
  234. 'depositTotal' => floor($depositTotal) ?? 0,
  235. 'todayProfit' => floor($todayProfit) ?? 0,
  236. 'depositToday' => floor($depositToday) ?? 0,
  237. 'monthProfit' => floor($monthProfit) ?? 0,
  238. ];
  239. return $this->ok($data);
  240. }
  241. /**
  242. * profitDetail 收益详情
  243. *
  244. * @param Request $request
  245. * @param OrderFilter $orderFilter
  246. * @param OrderRentFilter $orderRentFilter
  247. * @param DepositFilter $depositFilter
  248. * @param WalletLogFilter $walletLogFilter
  249. * @param UserFilter $userFilter
  250. * @return \Illuminate\Http\JsonResponse
  251. * @author Fx
  252. *
  253. */
  254. public function profitDetail(Request $request, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter, DepositFilter $depositFilter, WalletLogFilter $walletLogFilter, UserFilter $userFilter)
  255. {
  256. $area_ids = self::$areaIds;
  257. $time_between = $request->get('pay_time_between') ?? [];
  258. if (empty($time_between)) return $this->error('参数错误');
  259. $t1 = Carbon::make($time_between[0]);
  260. $t2 = Carbon::make($time_between[1]);
  261. $days = $t1->diffInDays($t2);
  262. // 总收益
  263. $totalProfit = WalletLog::query()
  264. ->whereIn('area_id', $area_ids)
  265. ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
  266. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
  267. ->whereIn('type', WalletLog::$subType)
  268. ->sum('money');
  269. // 总押金
  270. $depositTotal = DepositOrder::query()
  271. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  272. ->where('is_refund', DepositOrder::REFUND_NO)
  273. ->whereIn('area_id', $area_ids)
  274. ->filter($depositFilter)
  275. ->sum('pay_money');
  276. // 普通订单
  277. $orderTotalProfit = Order::query()
  278. ->where('pay_status', Order::PAY_STATUS_OK)
  279. ->where('status', Order::STATUS_COMPLETE_ORDER)
  280. ->whereIn('area_id', $area_ids)
  281. ->filter($orderFilter)
  282. ->sum('pay_money');
  283. // 骑行花费
  284. $orderTotalTimeProfit = Order::query()
  285. ->where('pay_status', Order::PAY_STATUS_OK)
  286. ->where('status', Order::STATUS_COMPLETE_ORDER)
  287. ->whereIn('area_id', $area_ids)
  288. ->filter($orderFilter)
  289. ->sum('time_money');
  290. $orderTotal = Order::query()
  291. ->where('pay_status', Order::PAY_STATUS_OK)
  292. ->where('status', Order::STATUS_COMPLETE_ORDER)
  293. ->whereIn('area_id', $area_ids)
  294. ->filter($orderFilter)
  295. ->count();
  296. $orderDispatchTotalProfit = Order::query()
  297. ->where('pay_status', Order::PAY_STATUS_OK)
  298. ->where('status', Order::STATUS_COMPLETE_ORDER)
  299. ->whereIn('area_id', $area_ids)
  300. ->filter($orderFilter)
  301. ->sum('dispatch_money');
  302. // 日租订单
  303. $orderRentTotalProfit = OrderRent::query()
  304. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  305. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  306. ->whereIn('area_id', $area_ids)
  307. ->filter($orderRentFilter)
  308. ->sum('pay_money');
  309. // 日租超时花费
  310. $orderRentTotalTimeProfit = OrderRent::query()
  311. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  312. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  313. ->whereIn('area_id', $area_ids)
  314. ->filter($orderRentFilter)
  315. ->sum('time_money');
  316. // 日租租金
  317. $orderRentTotalRentProfit = OrderRent::query()
  318. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  319. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  320. ->whereIn('area_id', $area_ids)
  321. ->filter($orderRentFilter)
  322. ->sum('rent_money');
  323. $orderRentTotal = OrderRent::query()
  324. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  325. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  326. ->whereIn('area_id', $area_ids)
  327. ->filter($orderRentFilter)
  328. ->count();
  329. // 日租调度费
  330. $orderRentDispatchTotalProfit = OrderRent::query()
  331. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  332. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  333. ->whereIn('area_id', $area_ids)
  334. ->filter($orderRentFilter)
  335. ->sum('dispatch_money');
  336. // 已实名用户数
  337. $userTotal = User::query()
  338. ->whereIn('register_area_id', $area_ids)
  339. ->where('is_card_certified', User::CARD_OK)
  340. ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
  341. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
  342. ->count();
  343. // 已缴纳押金用户数
  344. $userDepositTotal = User::query()
  345. ->whereIn('register_area_id', $area_ids)
  346. ->where('is_card_certified', User::CARD_OK)
  347. ->where('is_card_certified', User::CARD_OK)
  348. ->where('is_deposit', User::DEPOSIT_OK)
  349. ->count();
  350. $orderWaitPay = Order::query()
  351. ->where('status', Order::STATUS_CLOSE_BIKE)
  352. ->whereIn('area_id', $area_ids)
  353. ->sum('pay_money');
  354. $orderRentWaitPay = OrderRent::query()
  355. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  356. ->whereIn('area_id', $area_ids)
  357. ->sum('pay_money');
  358. // 待支付
  359. $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
  360. $orderWaitPayNum = Order::query()
  361. ->where('status', Order::STATUS_CLOSE_BIKE)
  362. ->whereIn('area_id', $area_ids)
  363. ->count();
  364. $orderRentWaitPayNum = OrderRent::query()
  365. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  366. ->whereIn('area_id', $area_ids)
  367. ->count();
  368. $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
  369. $bikeTotal = Bike::query()
  370. ->where('put_status', Bike::PUT_STATUS_YES)
  371. ->count();
  372. $data = [
  373. 'totalProfit' => bcadd($orderTotalProfit,$orderRentTotalProfit,2) ?? 0, // 新增收益
  374. 'depositTotal' => $depositTotal ?? 0, // 新增押金
  375. 'orderTotalProfit' => $orderTotalTimeProfit ?? 0, // 新增普通订单收入 // 骑行花费
  376. 'orderTotal' => $orderTotal ?? 0, // 新增普通订单数量
  377. 'orderDispatchTotalProfit' => $orderDispatchTotalProfit ?? 0, // 新增普通订单调度费收入 // 调度费
  378. 'orderRentTotalProfit' => bcadd($orderRentTotalTimeProfit,$orderRentTotalRentProfit,2) ?? 0, // 新增日租订单收入
  379. 'orderRentTotal' => $orderRentTotal ?? 0, // 新增日租订单数量
  380. 'orderRentDispatchTotalProfit' => $orderRentDispatchTotalProfit ?? 0, // 新增日租订单调度费收入
  381. 'userTotal' => $userTotal ?? 0, // 新增身份认证用户
  382. 'userDepositTotal' => $userDepositTotal ?? 0, // 总缴纳押金用户数
  383. 'wiatPayTotal' => $wiatPayTotal ?? 0, //总待支付
  384. 'wiatNum' => $wiatNum ?? 0, //总待支付数量
  385. 'bikeTotal' => $bikeTotal ?? 0, // 总投放车辆
  386. 'days' => abs($days), // 天数
  387. ];
  388. return $this->ok($data);
  389. }
  390. /**
  391. * orderDetail 订单详情
  392. *
  393. * @param Request $request
  394. * @return \Illuminate\Http\JsonResponse
  395. * @author Fx
  396. *
  397. */
  398. public function orderDetail(Request $request)
  399. {
  400. $order_id = $request->get('order_id') ?? '';
  401. if (empty($order_id)) return $this->error('参数错误');
  402. $order = Order::query()->find($order_id);
  403. if (empty($order)) return $this->error('找不到该订单,参数错误');
  404. $orderLocations = [];
  405. $wx_orderLocations = [];
  406. // 订单轨迹
  407. $locationsLog = LocationsLog::query()->where('order_id', (int)$order_id)->where('is_rent', LocationsLog::RENT_NO)->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderBy('created_at', 'asc')->get();
  408. if (!empty($locationsLog)) {
  409. foreach ($locationsLog as $vv) {
  410. $orderLocations[] = [$vv->longitude, $vv->latitude];
  411. $wx_orderLocations[] = ['longitude' => $vv->longitude, 'latitude' => $vv->latitude];
  412. }
  413. }
  414. // Log::info($orderLocations);
  415. $data = [
  416. 'order_status' => Order::$statusMaps[$order->status],
  417. 'start_use_bike_time' => date('Y/m/d H:i:s', strtotime($order->start_use_bike_time)),
  418. 'end_use_bike_time' => $order->end_use_bike_time ? date('Y/m/d H:i:s', strtotime($order->end_use_bike_time)) : '',
  419. 'pay_money' => $order->pay_money,
  420. 'dispatch_money' => $order->dispatch_money,
  421. 'time_money' => $order->time_money,
  422. 'bike_no' => $order->bike_no,
  423. 'bike_id' => $order->bike_id,
  424. 'user_id' => $order->user_id,
  425. 'nickname' => $order->users->nickname ?? '',
  426. 'mobile' => $order->users->mobile ?? '',
  427. 'orderLocations' => $orderLocations,
  428. 'start_location' => empty($orderLocations) ? [] : current($orderLocations),
  429. 'end_location' => empty($orderLocations) ? [] : end($orderLocations),
  430. 'wx_orderLocations' => $wx_orderLocations,
  431. 'wx_start_location' => empty($wx_orderLocations) ? [] : current($wx_orderLocations),
  432. 'wx_end_location' => empty($wx_orderLocations) ? [] : end($wx_orderLocations),
  433. //'center_location' => GetCenterFromDegrees([['lat'=>$orderLocations[0][1],'lng'=>$orderLocations[0][0]],['lat'=>end($orderLocations)[1],'lng'=>end($orderLocations)[0]]])
  434. ];
  435. return $this->ok($data);
  436. }
  437. }