OrderController.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. <?php
  2. namespace App\Http\Controllers\App;
  3. use App\Filters\BikeFilter;
  4. use App\Filters\CardRidingOrderFilter;
  5. use App\Filters\DepositCardOrderFilter;
  6. use App\Filters\DepositFilter;
  7. use App\Filters\DepositOrderFilter;
  8. use App\Filters\OrderFilter;
  9. use App\Filters\OrderRentFilter;
  10. use App\Filters\RechargeOrderFilter;
  11. use App\Filters\UserFilter;
  12. use App\Filters\WalletLogFilter;
  13. use App\Http\Resources\App\OrderResource;
  14. use App\Models\AdminUser;
  15. use App\Models\AdminUserArea;
  16. use App\Models\Bike;
  17. use App\Models\CardRidingOrder;
  18. use App\Models\DepositCardOrder;
  19. use App\Models\DepositOrder;
  20. use App\Models\DepositRefund;
  21. use App\Models\LocationsLog;
  22. use App\Models\Order;
  23. use App\Models\OrderRent;
  24. use App\Models\RechargeOrder;
  25. use App\Models\User;
  26. use App\Models\WalletLog;
  27. use App\Utils\Admin;
  28. use Carbon\Carbon;
  29. use Illuminate\Http\Request;
  30. use App\Http\Controllers\Controller;
  31. use Illuminate\Support\Facades\Cache;
  32. use Illuminate\Support\Facades\DB;
  33. use Illuminate\Support\Facades\Log;
  34. class OrderController extends AppBaseController
  35. {
  36. /**
  37. * orderList 订单列表
  38. *
  39. * @param OrderFilter $filter
  40. * @return \Illuminate\Http\JsonResponse
  41. * @author Fx
  42. *
  43. */
  44. public function orderList(OrderFilter $filter)
  45. {
  46. $area_ids = self::$areaIds;
  47. $order = Order::query()
  48. ->whereIn('area_id', $area_ids)
  49. ->filter($filter)
  50. ->with('users')
  51. ->where('status', '!=', Order::STATUS_CLOSE_ORDER)
  52. ->orderByDesc('id')
  53. ->paginate();
  54. return $this->ok(OrderResource::collection($order));
  55. }
  56. /**
  57. * orderStatistics 订单统计
  58. *
  59. * @param OrderFilter $filter
  60. * @return \Illuminate\Http\JsonResponse
  61. * @author Fx
  62. *
  63. */
  64. public function orderStatistics(OrderFilter $filter)
  65. {
  66. $area_ids = self::$areaIds;
  67. // 总订单数
  68. $order_total = Order::query()
  69. ->whereIn('area_id', $area_ids)
  70. ->filter($filter)
  71. ->where('status', Order::STATUS_COMPLETE_ORDER)
  72. ->count('id');
  73. $order_rent_total = OrderRent::query()
  74. ->whereIn('area_id', $area_ids)
  75. ->filter($filter)
  76. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  77. ->count('id');
  78. $order_total += $order_rent_total;
  79. // 今日新增订单数
  80. $today_add_order = Order::query()
  81. ->whereIn('area_id', $area_ids)
  82. ->filter($filter)
  83. ->where('created_at', '>', Carbon::today())
  84. ->where('status', Order::STATUS_COMPLETE_ORDER)
  85. ->count('id');
  86. $today_add_order_rent = OrderRent::query()
  87. ->whereIn('area_id', $area_ids)
  88. ->filter($filter)
  89. ->where('pay_time', '>', Carbon::today())
  90. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  91. ->count('id');
  92. $today_add_order += $today_add_order_rent;
  93. // 骑行中订单数
  94. $riding_status = [
  95. Order::STATUS_RIDE_BIKE,
  96. // Order::STATUS_CLOSE_BIKE, // 待支付
  97. Order::STATUS_PAUSE_BIKE,
  98. ];
  99. $riding_order = Order::query()
  100. ->whereIn('area_id', $area_ids)
  101. ->filter($filter)
  102. ->whereIn('status', $riding_status)
  103. ->count('id');
  104. $riding_order_rent = OrderRent::query()
  105. ->whereIn('area_id', $area_ids)
  106. ->filter($filter)
  107. ->where('status', OrderRent::STATUS_RENT_BIKE)
  108. ->count('id');
  109. $riding_order += $riding_order_rent;
  110. // 待支付
  111. $waiting_pay_order = Order::query()
  112. ->whereIn('area_id', $area_ids)
  113. ->filter($filter)
  114. ->where('status', Order::STATUS_CLOSE_BIKE)
  115. ->count('id');
  116. $waiting_pay_rent = OrderRent::query()
  117. ->whereIn('area_id', $area_ids)
  118. ->filter($filter)
  119. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  120. ->count('id');
  121. $waiting_pay_order += $waiting_pay_rent;
  122. // 今日新增收入
  123. $today_add_money = Order::query()
  124. ->whereIn('area_id', $area_ids)
  125. ->filter($filter)
  126. ->where('pay_time', '>', Carbon::today())
  127. ->where('status', Order::STATUS_COMPLETE_ORDER)
  128. ->sum('pay_money');
  129. $today_add_money_rent = OrderRent::query()
  130. ->whereIn('area_id', $area_ids)
  131. ->filter($filter)
  132. ->where('pay_time', '>', Carbon::today())
  133. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  134. ->sum('pay_money');
  135. $today_add_money += $today_add_money_rent;
  136. $order = [
  137. 'order_total' => $order_total ?? 0,
  138. 'today_add_order' => $today_add_order ?? 0,
  139. 'riding_order' => $riding_order ?? 0,
  140. 'today_add_money' => $today_add_money ?? 0,
  141. 'waiting_pay' => $waiting_pay_order ?? 0,
  142. ];
  143. return $this->ok($order);
  144. }
  145. /**
  146. * orderProfitStatistics 订单收益统计
  147. *
  148. * @param WalletLogFilter $walletLogFilter
  149. * @param DepositFilter $depositFilter
  150. * @param OrderFilter $orderFilter
  151. * @param OrderRentFilter $orderRentFilter
  152. * @return \Illuminate\Http\JsonResponse
  153. * @author Fx
  154. *
  155. */
  156. public function orderProfitStatistics(WalletLogFilter $walletLogFilter, DepositFilter $depositFilter, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter)
  157. {
  158. $area_ids = self::$areaIds;
  159. $admin_type = Admin::user()->type;
  160. if ($admin_type == AdminUser::TYPE_WORKER) {
  161. $data = [
  162. 'totalProfit' => $totalProfit ?? 0,
  163. 'depositTotal' => $depositTotal ?? 0,
  164. 'todayProfit' => $todayProfit ?? 0,
  165. 'depositToday' => $depositToday ?? 0,
  166. 'monthProfit' => $monthProfit ?? 0,
  167. ];
  168. return $this->ok($data);
  169. }
  170. // 总收益
  171. // $totalProfit = WalletLog::query()
  172. // ->whereIn('area_id', $area_ids)
  173. // ->filter($walletLogFilter)
  174. // ->whereIn('type', WalletLog::$subType)
  175. // ->sum('money');
  176. $totalOrderProfit = Order::query()
  177. ->whereIn('area_id', $area_ids)
  178. ->filter($orderFilter)
  179. ->where('status', Order::STATUS_COMPLETE_ORDER)
  180. ->sum('pay_money');
  181. $totalOrderRentProfit = OrderRent::query()
  182. ->whereIn('area_id', $area_ids)
  183. ->filter($orderRentFilter)
  184. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  185. ->sum('pay_money');
  186. $totalProfit = bcadd($totalOrderProfit, $totalOrderRentProfit, 0);
  187. // 总押金
  188. $depositTotal = DepositOrder::query()
  189. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  190. ->where('is_refund', DepositOrder::REFUND_NO)
  191. ->whereIn('area_id', $area_ids)
  192. ->filter($depositFilter)
  193. ->sum('pay_money');
  194. // 今日收益
  195. // $todayProfit = WalletLog::query()
  196. // ->whereIn('area_id', $area_ids)
  197. // ->filter($walletLogFilter)
  198. // ->whereIn('type', WalletLog::$subType)
  199. // ->where('created_at', '>', Carbon::today())
  200. // ->sum('money');
  201. $todayOrderProfit = Order::query()
  202. ->whereIn('area_id', $area_ids)
  203. ->filter($orderFilter)
  204. ->where('status', Order::STATUS_COMPLETE_ORDER)
  205. ->where('created_at', '>', Carbon::today())
  206. ->sum('pay_money');
  207. $todayOrderRentProfit = OrderRent::query()
  208. ->whereIn('area_id', $area_ids)
  209. ->filter($orderRentFilter)
  210. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  211. ->where('created_at', '>', Carbon::today())
  212. ->sum('pay_money');
  213. $todayProfit = bcadd($todayOrderProfit, $todayOrderRentProfit, 0);
  214. // 今日新增押金
  215. $depositToday = DepositOrder::query()
  216. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  217. ->where('is_refund', DepositOrder::REFUND_NO)
  218. ->whereIn('area_id', $area_ids)
  219. ->filter($depositFilter)
  220. ->where('created_at', '>', Carbon::today())
  221. ->sum('pay_money');
  222. // 本月收益
  223. // $monthProfit = WalletLog::query()
  224. // ->whereIn('area_id', $area_ids)
  225. // ->filter($walletLogFilter)
  226. // ->whereIn('type', WalletLog::$subType)
  227. // ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  228. // ->sum('money');
  229. $monthOrderProfit = Order::query()
  230. ->whereIn('area_id', $area_ids)
  231. ->filter($orderFilter)
  232. ->where('status', Order::STATUS_COMPLETE_ORDER)
  233. ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  234. ->sum('pay_money');
  235. $monthOrderRentProfit = OrderRent::query()
  236. ->whereIn('area_id', $area_ids)
  237. ->filter($orderRentFilter)
  238. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  239. ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  240. ->sum('pay_money');
  241. $monthProfit = bcadd($monthOrderProfit, $monthOrderRentProfit, 0);
  242. $data = [
  243. 'totalProfit' => floor(abs($totalProfit)) ?? 0,
  244. 'depositTotal' => floor($depositTotal) ?? 0,
  245. 'todayProfit' => floor($todayProfit) ?? 0,
  246. 'depositToday' => floor($depositToday) ?? 0,
  247. 'monthProfit' => floor($monthProfit) ?? 0,
  248. ];
  249. return $this->ok($data);
  250. }
  251. /**
  252. * profitDetail 收益详情
  253. *
  254. * @param Request $request
  255. * @param OrderFilter $orderFilter
  256. * @param OrderRentFilter $orderRentFilter
  257. * @param DepositFilter $depositFilter
  258. * @param WalletLogFilter $walletLogFilter
  259. * @param UserFilter $userFilter
  260. * @return \Illuminate\Http\JsonResponse
  261. * @author Fx
  262. *
  263. */
  264. public function profitDetail(Request $request, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter, DepositFilter $depositFilter, WalletLogFilter $walletLogFilter, UserFilter $userFilter,BikeFilter $bikeFilter,RechargeOrderFilter $rechargeOrderFilter,CardRidingOrderFilter $cardRidingOrderFilter,DepositCardOrderFilter $depositCardOrderFilter)
  265. {
  266. $area_ids = self::$areaIds;
  267. $time_between = $request->get('pay_time_between') ?? [];
  268. if (empty($time_between)) return $this->error('参数错误');
  269. $t1 = Carbon::make($time_between[0]);
  270. $t2 = Carbon::make($time_between[1]);
  271. $days = $t1->diffInDays($t2);
  272. // 总收益
  273. $totalProfit = WalletLog::query()
  274. ->filter($walletLogFilter)
  275. ->whereIn('area_id', $area_ids)
  276. ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
  277. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
  278. ->whereIn('type', WalletLog::$subType)
  279. ->sum('money');
  280. // 总押金
  281. $depositTotal = DepositOrder::query()
  282. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  283. ->where('is_refund', DepositOrder::REFUND_NO)
  284. ->whereIn('area_id', $area_ids)
  285. ->filter($depositFilter)
  286. ->sum('pay_money');
  287. // 普通订单
  288. $orderTotalProfit = Order::query()
  289. ->where('pay_status', Order::PAY_STATUS_OK)
  290. ->where('status', Order::STATUS_COMPLETE_ORDER)
  291. ->whereIn('area_id', $area_ids)
  292. ->filter($orderFilter)
  293. ->sum('pay_money');
  294. // 骑行花费
  295. $orderTotalTimeProfit = Order::query()
  296. ->where('pay_status', Order::PAY_STATUS_OK)
  297. ->where('status', Order::STATUS_COMPLETE_ORDER)
  298. ->whereIn('area_id', $area_ids)
  299. ->filter($orderFilter)
  300. ->sum('time_money');
  301. $orderTotal = Order::query()
  302. ->where('pay_status', Order::PAY_STATUS_OK)
  303. ->where('status', Order::STATUS_COMPLETE_ORDER)
  304. ->whereIn('area_id', $area_ids)
  305. ->filter($orderFilter)
  306. ->count();
  307. $orderDispatchTotalProfit = Order::query()
  308. ->where('pay_status', Order::PAY_STATUS_OK)
  309. ->where('status', Order::STATUS_COMPLETE_ORDER)
  310. ->whereIn('area_id', $area_ids)
  311. ->filter($orderFilter)
  312. ->sum('dispatch_money');
  313. // 日租订单
  314. $orderRentTotalProfit = OrderRent::query()
  315. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  316. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  317. ->whereIn('area_id', $area_ids)
  318. ->filter($orderRentFilter)
  319. ->sum('pay_money');
  320. // 日租超时花费
  321. $orderRentTotalTimeProfit = OrderRent::query()
  322. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  323. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  324. ->whereIn('area_id', $area_ids)
  325. ->filter($orderRentFilter)
  326. ->sum('time_money');
  327. // 日租租金
  328. $orderRentTotalRentProfit = OrderRent::query()
  329. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  330. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  331. ->whereIn('area_id', $area_ids)
  332. ->filter($orderRentFilter)
  333. ->sum('rent_money');
  334. $orderRentTotal = OrderRent::query()
  335. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  336. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  337. ->whereIn('area_id', $area_ids)
  338. ->filter($orderRentFilter)
  339. ->count();
  340. // 日租调度费
  341. $orderRentDispatchTotalProfit = OrderRent::query()
  342. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  343. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  344. ->whereIn('area_id', $area_ids)
  345. ->filter($orderRentFilter)
  346. ->sum('dispatch_money');
  347. // 已实名用户数
  348. $userTotalCardOk = User::query()
  349. ->filter($userFilter)
  350. ->whereIn('register_area_id', $area_ids)
  351. ->where('is_card_certified', User::CARD_OK)
  352. ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
  353. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
  354. ->count();
  355. // 已缴纳押金用户数
  356. $userDepositTotal = User::query()
  357. ->filter($userFilter)
  358. ->whereIn('register_area_id', $area_ids)
  359. ->where('is_card_certified', User::CARD_OK)
  360. ->where('is_card_certified', User::CARD_OK)
  361. ->where('is_deposit', User::DEPOSIT_OK)
  362. ->count();
  363. $orderWaitPay = Order::query()
  364. ->filter($orderFilter)
  365. ->where('status', Order::STATUS_CLOSE_BIKE)
  366. ->whereIn('area_id', $area_ids)
  367. ->sum('pay_money');
  368. $orderRentWaitPay = OrderRent::query()
  369. ->filter($orderRentFilter)
  370. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  371. ->whereIn('area_id', $area_ids)
  372. ->sum('pay_money');
  373. // 待支付
  374. $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
  375. $orderWaitPayNum = Order::query()
  376. ->filter($orderFilter)
  377. ->where('status', Order::STATUS_CLOSE_BIKE)
  378. ->whereIn('area_id', $area_ids)
  379. ->count();
  380. $orderRentWaitPayNum = OrderRent::query()
  381. ->filter($orderRentFilter)
  382. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  383. ->whereIn('area_id', $area_ids)
  384. ->count();
  385. $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
  386. $bikeTotal = Bike::query()
  387. ->filter($bikeFilter)
  388. ->where('put_status', Bike::PUT_STATUS_YES)
  389. ->whereIn('put_area_id', $area_ids)
  390. ->count();
  391. $totalUsers = User::query()
  392. ->filter($userFilter)
  393. ->whereIn('register_area_id', $area_ids)
  394. ->count();
  395. $riddingCardOrderTotalProfit = CardRidingOrder::query()
  396. ->whereIn('area_id', $area_ids)
  397. ->filter($cardRidingOrderFilter)
  398. ->where('pay_status', CardRidingOrder::PAY_STATUS_OK)
  399. ->sum('pay_money');
  400. $rechargeOrderTotalProfit = RechargeOrder::query()
  401. ->whereIn('area_id', $area_ids)
  402. ->filter($rechargeOrderFilter)
  403. ->where('pay_status', RechargeOrder::PAY_STATUS_OK)
  404. ->sum('pay_money');
  405. $depositCardTotalProfit = DepositCardOrder::query()
  406. ->whereIn('area_id', $area_ids)
  407. ->filter($depositCardOrderFilter)
  408. ->where('pay_status', DepositCardOrder::PAY_STATUS_OK)
  409. ->sum('pay_money');
  410. $data = [
  411. 'totalProfit' => bcadd($orderTotalProfit, $orderRentTotalProfit, 2) ?? 0, // 新增收益
  412. 'depositTotal' => $depositTotal ?? 0, // 新增押金
  413. 'orderTotalProfit' => $orderTotalProfit ?? 0, // 新增普通订单收入 // 骑行花费
  414. 'orderTotal' => $orderTotal ?? 0, // 新增普通订单数量
  415. 'orderDispatchTotalProfit' => $orderDispatchTotalProfit ?? 0, // 新增普通订单调度费收入 // 调度费
  416. 'orderRentTotalProfit' => $orderRentTotalProfit ?? 0, // 新增日租订单收入
  417. 'orderRentTotal' => $orderRentTotal ?? 0, // 新增日租订单数量
  418. 'orderRentDispatchTotalProfit' => $orderRentDispatchTotalProfit ?? 0, // 新增日租订单调度费收入
  419. 'userTotalCardOk' => $userTotalCardOk ?? 0, // 新增身份认证用户
  420. 'userTotal' => $totalUsers ?? 0, // 总用户数
  421. 'userDepositTotal' => $userDepositTotal ?? 0, // 总缴纳押金用户数
  422. 'wiatPayTotal' => $wiatPayTotal ?? 0, //总待支付
  423. 'wiatNum' => $wiatNum ?? 0, //总待支付数量
  424. 'bikeTotal' => $bikeTotal ?? 0, // 总投放车辆
  425. 'days' => abs($days), // 天数
  426. 'riddingCardOrderTotalProfit' => $riddingCardOrderTotalProfit ?? 0, // 骑行卡收益
  427. 'rechargeOrderTotalProfit' => $rechargeOrderTotalProfit ?? 0, // 充值订单收益
  428. 'depositCardTotalProfit' => $depositCardTotalProfit ?? 0, // 免押金卡收益
  429. 'activityTotalProfit' => bcadd(bcadd($riddingCardOrderTotalProfit,$rechargeOrderTotalProfit,2),$depositCardTotalProfit,2), // 活动总收益
  430. ];
  431. return $this->ok($data);
  432. }
  433. /**
  434. * orderDetail 订单详情
  435. *
  436. * @param Request $request
  437. * @return \Illuminate\Http\JsonResponse
  438. * @author Fx
  439. *
  440. */
  441. public function orderDetail(Request $request)
  442. {
  443. $order_id = $request->get('order_id') ?? '';
  444. if (empty($order_id)) return $this->error('参数错误');
  445. $order = Order::query()->find($order_id);
  446. if (empty($order)) return $this->error('找不到该订单,参数错误');
  447. $orderLocations = [];
  448. $wx_orderLocations = [];
  449. // 订单轨迹
  450. $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();
  451. if (!empty($locationsLog)) {
  452. foreach ($locationsLog as $vv) {
  453. $orderLocations[] = [$vv->longitude, $vv->latitude];
  454. $wx_orderLocations[] = ['longitude' => $vv->longitude, 'latitude' => $vv->latitude];
  455. }
  456. }
  457. // Log::info($orderLocations);
  458. $data = [
  459. 'order_status' => Order::$statusMaps[$order->status],
  460. 'start_use_bike_time' => date('Y/m/d H:i:s', strtotime($order->start_use_bike_time)),
  461. 'end_use_bike_time' => $order->end_use_bike_time ? date('Y/m/d H:i:s', strtotime($order->end_use_bike_time)) : '',
  462. 'pay_money' => $order->pay_money,
  463. 'dispatch_money' => $order->dispatch_money,
  464. 'time_money' => $order->time_money,
  465. 'bike_no' => $order->bike_no,
  466. 'bike_id' => $order->bike_id,
  467. 'user_id' => $order->user_id,
  468. 'nickname' => $order->users->nickname ?? '',
  469. 'mobile' => $order->users->mobile ?? '',
  470. 'orderLocations' => $orderLocations,
  471. 'start_location' => empty($orderLocations) ? [] : current($orderLocations),
  472. 'end_location' => empty($orderLocations) ? [] : end($orderLocations),
  473. 'wx_orderLocations' => $wx_orderLocations,
  474. 'wx_start_location' => empty($wx_orderLocations) ? [] : current($wx_orderLocations),
  475. 'wx_end_location' => empty($wx_orderLocations) ? [] : end($wx_orderLocations),
  476. 'preferential_type_name' => $order->preferential_type_name, // 优惠方式
  477. 'preferential_money' => $order->preferential_money,// 优惠金额
  478. 'card_preferential_money' => $order->card_preferential_money,// 骑行卡优惠金额
  479. 'coupon_preferential_money' => $order->coupon_preferential_money,// 优惠券优惠金额
  480. 'is_coupon_name' => Order::$couponMaps[$order->is_coupon] ,// 优惠券优惠
  481. 'walletLogs' => $order->walletLogs,
  482. 'order_bike_operates' => $order->order_bike_operates,
  483. 'remark' => $order->remark ?? '',
  484. //'center_location' => GetCenterFromDegrees([['lat'=>$orderLocations[0][1],'lng'=>$orderLocations[0][0]],['lat'=>end($orderLocations)[1],'lng'=>end($orderLocations)[0]]])
  485. ];
  486. return $this->ok($data);
  487. }
  488. /**
  489. * orderLocation 订单轨迹
  490. *
  491. * @param Request $request
  492. * @return \Illuminate\Http\JsonResponse
  493. * @author Fx
  494. *
  495. */
  496. public function orderLocation(Request $request)
  497. {
  498. $order_id = $request->get('order_id') ?? '';
  499. if (empty($order_id)) return $this->error('参数错误');
  500. $orderLocations = [];
  501. $locationsTimes = [];
  502. try {
  503. $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();
  504. } catch (\Exception $exception) {
  505. Log::info($exception->getMessage());
  506. }
  507. if (!empty($locationsLog)) {
  508. foreach ($locationsLog as $vv) {
  509. $orderLocations[] = [$vv->longitude, $vv->latitude];
  510. $locationsTimes[] = Carbon::parse($vv->created_at)->format('Y-m-d H:i:s');
  511. }
  512. }
  513. $data = [
  514. 'orderLocations' => $orderLocations,
  515. 'locationsTimes' => $locationsTimes,
  516. ];
  517. return $this->ok($data);
  518. }
  519. /**
  520. * heatMap 热力图
  521. *
  522. * @param OrderFilter $orderFilter
  523. * @return \Illuminate\Http\JsonResponse
  524. * @author Fx
  525. *
  526. */
  527. public function heatMap(OrderFilter $orderFilter)
  528. {
  529. $today = Carbon::today()->subMonth();
  530. //whereIn('area_id',self::$areaIds)->
  531. $order = Order::query()->filter($orderFilter)->where('created_at', '>', $today)->get();
  532. $data1 = [];
  533. $data2 = [];
  534. if (!empty($order)) {
  535. foreach ($order as $v) {
  536. $location = json_decode($v->start_use_bike_location);
  537. $location2 = json_decode($v->end_use_bike_location);
  538. $data1[] = ['lat' => $location->latitude, 'lng' => $location->longitude, 'count' => 3];
  539. if(!empty($location2)){
  540. $data2[] = ['lat' => $location2->latitude, 'lng' => $location2->longitude, 'count' => 3];
  541. }
  542. }
  543. }
  544. $data = [
  545. 'start' => $data1,
  546. 'end' => $data2
  547. ];
  548. return $this->ok($data);
  549. }
  550. /**
  551. * newOrderChart 新订单统计图
  552. *
  553. * @param Request $request
  554. * @param OrderFilter $orderFilter
  555. * @return array
  556. * @author Fx
  557. *
  558. */
  559. // public function newOrderChart(Request $request, OrderFilter $orderFilter)
  560. // {
  561. // $days = $request->get('days') ?? '';
  562. // $areaId = $request->get('area_id') ?? '';
  563. // if (empty($days) || empty($areaId)) return $this->error('缺少参数');
  564. // if (empty($days)) return $this->error('缺少参数');
  565. // $newOrders = Order::query()
  566. // ->filter($orderFilter)
  567. // ->where('status', Order::STATUS_COMPLETE_ORDER);
  568. //
  569. //// $admin_id = Admin::user()->id;
  570. //// if (!Admin::isAdministrator()) {
  571. //// $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  572. //// if (count($area_ids) !== 0) {
  573. //// $newOrders = $newOrders->whereIn('area_id', $area_ids);
  574. //// } else {
  575. //// $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  576. //// $area_id = $area_id ?? 0;
  577. //// $newOrders = $newOrders->where('area_id', $area_id);
  578. //// }
  579. //// }
  580. //
  581. // switch ($days) {
  582. // case 'today':
  583. // $today = Carbon::today();
  584. // $newOrders = $newOrders->where('created_at', '>', $today)
  585. // ->selectRaw('DATE_FORMAT(created_at,"%m-%d %H:00") as date,count(id) as value,sum(pay_money) as moneys')
  586. // ->groupBy('date')->get()->toArray();
  587. // // 有数据得数组
  588. // $newOrdersKeyVal = [];
  589. // if (!empty($newOrders)) {
  590. // foreach ($newOrders as $v) {
  591. // $newOrdersKeyVal[$v['date']] = $v['value'];
  592. // }
  593. // }
  594. // // 为0得数组
  595. // $i = Carbon::now()->format('H');
  596. // $arr = [];
  597. // for ($i; $i >= 0; $i--) {
  598. // $str = Carbon::now()->subHours($i)->format('m-d H:00');
  599. // $arr[$str] = 0;
  600. // }
  601. // //合并
  602. // $merge = array_merge($arr, $newOrdersKeyVal);
  603. // $data = [];
  604. // foreach ($merge as $key=>$value){
  605. // $data[]=['date'=>$key,'value'=>$value];
  606. // }
  607. //
  608. // break;
  609. // case 'threeDays':
  610. // $i = 2;
  611. // break;
  612. // case 'sevenDays':
  613. // $i = 6;
  614. //
  615. // break;
  616. // case 'fifteenDays':
  617. // $i = 14;
  618. // break;
  619. // case 'thirtyDays':
  620. // $i = 29;
  621. // break;
  622. // default:
  623. // return $this->error('参数错误');
  624. //
  625. // }
  626. // if($days !== 'today'){
  627. // $paramDays = Carbon::today()->subDays($i);
  628. //
  629. // // 赋值一个值为0得数组
  630. // $arr = [];
  631. // for ($i ; $i >= 0; $i--){
  632. // $str = Carbon::today()->subDays($i)->format('Y/m/d');
  633. // $arr[$str] = 0;
  634. // }
  635. //
  636. // // 有数据得数组
  637. // $newOrders = $newOrders->where('created_at', '>', $paramDays)
  638. // ->selectRaw('DATE_FORMAT(created_at,"%Y/%m/%d") as date,count(id) as value,sum(pay_money) as moneys')
  639. // ->groupBy('date')->get()->toArray();
  640. // $newOrdersKeyVal = [];
  641. // $newOrdersKeyValMoney = [];
  642. // if(!empty($newOrders)){
  643. // foreach ($newOrders as $v){
  644. // $newOrdersKeyVal[$v['date']] = $v['value'];
  645. // $newOrdersKeyValMoney[$v['date']] = $v['moneys'];
  646. // }
  647. // }
  648. // // 合并覆盖0
  649. // $merge = array_merge($arr,$newOrdersKeyVal);
  650. // $mergemoney = array_merge($arr,$newOrdersKeyValMoney);
  651. // // return $this->ok($merge);
  652. //
  653. // // 重组结构
  654. // $data = [];
  655. // foreach ($merge as $ks=>$vs){
  656. // $data[]=['date'=>$ks,'value'=>$vs,'moneys'=>$mergemoney[$ks]];
  657. // }
  658. // }
  659. //
  660. // return $this->ok($data);
  661. // }
  662. public function orderChart($days, $orderFilter, $dispatch_money = false)
  663. {
  664. $newOrders = Order::query()
  665. ->filter($orderFilter)
  666. ->where('status', Order::STATUS_COMPLETE_ORDER);
  667. if ($dispatch_money) {
  668. $newOrders = $newOrders->where('dispatch_money', '>', 0);
  669. }
  670. $admin_id = Admin::user()->id;
  671. if (!Admin::isAdministrator()) {
  672. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  673. if (count($area_ids) !== 0) {
  674. $newOrders = $newOrders->whereIn('area_id', $area_ids);
  675. } else {
  676. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  677. $area_id = $area_id ?? 0;
  678. $newOrders = $newOrders->where('area_id', $area_id);
  679. }
  680. }
  681. switch ($days) {
  682. case 'today':
  683. $today = Carbon::today();
  684. $newOrders = $newOrders->where('created_at', '>', $today)
  685. ->selectRaw('DATE_FORMAT(created_at,"%m-%d %H:00") as date,count(id) as value')
  686. ->groupBy('date')->get()->toArray();
  687. // 有数据得数组
  688. $newOrdersKeyVal = [];
  689. if (!empty($newOrders)) {
  690. foreach ($newOrders as $v) {
  691. $newOrdersKeyVal[$v['date']] = $v['value'];
  692. }
  693. }
  694. // 为0得数组
  695. $i = Carbon::now()->format('H');
  696. $arr = [];
  697. for ($i; $i >= 0; $i--) {
  698. $str = Carbon::now()->subHours($i)->format('m-d H:00');
  699. $arr[$str] = 0;
  700. }
  701. //合并
  702. $merge = array_merge($arr, $newOrdersKeyVal);
  703. $data = [];
  704. foreach ($merge as $key => $value) {
  705. $data[] = ['date' => $key, 'value' => $value];
  706. }
  707. break;
  708. case 'threeDays':
  709. $i = 2;
  710. break;
  711. case 'sevenDays':
  712. $i = 6;
  713. break;
  714. case 'fifteenDays':
  715. $i = 14;
  716. break;
  717. case 'thirtyDays':
  718. $i = 29;
  719. break;
  720. default:
  721. return [];
  722. }
  723. if ($days !== 'today') {
  724. $paramDays = Carbon::today()->subDays($i);
  725. // 赋值一个值为0得数组
  726. $arr = [];
  727. for ($i; $i >= 0; $i--) {
  728. $str = Carbon::today()->subDays($i)->format('Y/m/d');
  729. $arr[$str] = 0;
  730. }
  731. // 有数据得数组
  732. $newOrders = $newOrders->where('created_at', '>', $paramDays)
  733. ->selectRaw('DATE_FORMAT(created_at,"%Y/%m/%d") as date,count(id) as value')
  734. ->groupBy('date')->get()->toArray();
  735. $newOrdersKeyVal = [];
  736. if (!empty($newOrders)) {
  737. foreach ($newOrders as $v) {
  738. $newOrdersKeyVal[$v['date']] = $v['value'];
  739. }
  740. }
  741. // 合并覆盖0
  742. $merge = array_merge($arr, $newOrdersKeyVal);
  743. // 重组结构
  744. $data = [];
  745. foreach ($merge as $ks => $vs) {
  746. $data[] = ['date' => $ks, 'value' => $vs];
  747. }
  748. }
  749. return $data;
  750. }
  751. public function newOrderChart(Request $request, OrderFilter $orderFilter)
  752. {
  753. $days = $request->get('days') ?? '';
  754. if (empty($days)) return $this->error('缺少参数');
  755. $newOrderChart = $this->orderChart($days, $orderFilter);
  756. $dispatchMoneyNewOrderChart = $this->orderChart($days, $orderFilter, true);
  757. return $this->ok([
  758. [
  759. 'name' => '总订单数',
  760. 'data' => $newOrderChart
  761. ], [
  762. 'name' => '有调度费的订单数',
  763. 'data' => $dispatchMoneyNewOrderChart
  764. ],
  765. ]);
  766. // return $this->ok([
  767. // $newOrderChart,$dispatchMoneyNewOrderChart
  768. // ]);
  769. }
  770. public function profitPolygonalChart($days, $filter, $model, $money = 'pay_money')
  771. {
  772. if (empty($days)) return $this->error('缺少参数');
  773. $newOrders = $model
  774. ->filter($filter)
  775. ->where('pay_status', 1);
  776. $admin_id = Admin::user()->id;
  777. if (!Admin::isAdministrator()) {
  778. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  779. if (count($area_ids) !== 0) {
  780. $newOrders = $newOrders->whereIn('area_id', $area_ids);
  781. } else {
  782. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  783. $area_id = $area_id ?? 0;
  784. $newOrders = $newOrders->where('area_id', $area_id);
  785. }
  786. }
  787. switch ($days) {
  788. case 'today':
  789. $today = Carbon::today();
  790. $newOrders = $newOrders->where('pay_time', '>', $today)
  791. ->selectRaw("DATE_FORMAT(pay_time,'%m-%d %H:00') as date,sum($money) as value")
  792. ->groupBy('date')->get()->toArray();
  793. // 有数据得数组
  794. $newOrdersKeyVal = [];
  795. if (!empty($newOrders)) {
  796. foreach ($newOrders as $v) {
  797. $newOrdersKeyVal[$v['date']] = $v['value'];
  798. }
  799. }
  800. // 为0得数组
  801. $i = Carbon::now()->format('H');
  802. $arr = [];
  803. for ($i; $i >= 0; $i--) {
  804. $str = Carbon::now()->subHours($i)->format('m-d H:00');
  805. $arr[$str] = 0;
  806. }
  807. //合并
  808. $merge = array_merge($arr, $newOrdersKeyVal);
  809. $data = [];
  810. foreach ($merge as $key => $value) {
  811. $data[] = ['date' => $key, 'value' => $value];
  812. }
  813. break;
  814. case 'threeDays':
  815. $i = 2;
  816. break;
  817. case 'sevenDays':
  818. $i = 6;
  819. break;
  820. case 'fifteenDays':
  821. $i = 14;
  822. break;
  823. case 'thirtyDays':
  824. $i = 29;
  825. break;
  826. default:
  827. return $this->error('参数错误');
  828. }
  829. if ($days !== 'today') {
  830. $paramDays = Carbon::today()->subDays($i);
  831. // 赋值一个值为0得数组
  832. $arr = [];
  833. for ($i; $i >= 0; $i--) {
  834. $str = Carbon::today()->subDays($i)->format('Y/m/d');
  835. $arr[$str] = 0;
  836. }
  837. // 有数据得数组
  838. $newOrders = $newOrders->where('pay_time', '>', $paramDays)
  839. ->selectRaw("DATE_FORMAT(pay_time,'%Y/%m/%d') as date,sum($money) as value")
  840. ->groupBy('date')->get()->toArray();
  841. $newOrdersKeyVal = [];
  842. if (!empty($newOrders)) {
  843. foreach ($newOrders as $v) {
  844. $newOrdersKeyVal[$v['date']] = $v['value'];
  845. }
  846. }
  847. // 合并覆盖0
  848. $merge = array_merge($arr, $newOrdersKeyVal);
  849. // 重组结构
  850. $data = [];
  851. foreach ($merge as $ks => $vs) {
  852. $data[] = ['date' => $ks, 'value' => $vs];
  853. }
  854. }
  855. return $data;
  856. }
  857. public function profitChart(Request $request, OrderFilter $orderFilter, RechargeOrderFilter $rechargeOrderFilter, CardRidingOrderFilter $cardRidingOrderFilter, DepositCardOrderFilter $depositCardOrderFilter)
  858. {
  859. $days = $request->get('days') ?? '';
  860. $ordersChart = $this->profitPolygonalChart($days, $orderFilter, Order::query());
  861. $ordersDispatchMoneyChart = $this->profitPolygonalChart($days, $orderFilter, Order::query(), 'dispatch_money');
  862. $rechargeOrdersChart = $this->profitPolygonalChart($days, $rechargeOrderFilter, RechargeOrder::query());
  863. $cardRidingOrdersChart = $this->profitPolygonalChart($days, $cardRidingOrderFilter, CardRidingOrder::query());
  864. $depositCardOrdersChart = $this->profitPolygonalChart($days, $depositCardOrderFilter, DepositCardOrder::query());
  865. $total = [];
  866. foreach ($ordersChart as $k => $v) {
  867. $total[] = [
  868. 'date' => $v['date'],
  869. 'value' => bcadd(bcadd(bcadd($v['value'], $rechargeOrdersChart[$k]['value'], 2), $cardRidingOrdersChart[$k]['value'], 2), $depositCardOrdersChart[$k]['value'], 2)
  870. ];
  871. }
  872. if (Admin::isNormalAdministrator() || Admin::isAdministrator()){
  873. return $this->ok([
  874. [
  875. 'name' => '普通订单收益',
  876. 'data' => $ordersChart
  877. ],
  878. [
  879. 'name' => '订单调度费收益',
  880. 'data' => $ordersDispatchMoneyChart
  881. ],
  882. [
  883. 'name' => '充值收益',
  884. 'data' => $rechargeOrdersChart
  885. ],
  886. [
  887. 'name' => '骑行卡收益',
  888. 'data' => $cardRidingOrdersChart
  889. ],
  890. [
  891. 'name' => '免押金卡收益',
  892. 'data' => $depositCardOrdersChart
  893. ],
  894. [
  895. 'name' => '总收益',
  896. 'data' => $total
  897. ],
  898. ]);
  899. }else{
  900. return $this->ok([]);
  901. }
  902. }
  903. /**
  904. * 前七天小时订单统计
  905. * @return \Illuminate\Http\JsonResponse
  906. * User: Mead
  907. */
  908. public function hourOrderNumber(Request $request,OrderFilter $filter)
  909. {
  910. $day = [
  911. ["num" => 0, "hour" => 0],
  912. ["num" => 0, "hour" => 1],
  913. ["num" => 0, "hour" => 2],
  914. ["num" => 0, "hour" => 3],
  915. ["num" => 0, "hour" => 4],
  916. ["num" => 0, "hour" => 5],
  917. ["num" => 0, "hour" => 6],
  918. ["num" => 0, "hour" => 7],
  919. ["num" => 0, "hour" => 8],
  920. ["num" => 0, "hour" => 9],
  921. ["num" => 0, "hour" => 10],
  922. ["num" => 0, "hour" => 11],
  923. ["num" => 0, "hour" => 12],
  924. ["num" => 0, "hour" => 13],
  925. ["num" => 0, "hour" => 14],
  926. ["num" => 0, "hour" => 15],
  927. ["num" => 0, "hour" => 16],
  928. ["num" => 0, "hour" => 17],
  929. ["num" => 0, "hour" => 18],
  930. ["num" => 0, "hour" => 19],
  931. ["num" => 0, "hour" => 20],
  932. ["num" => 0, "hour" => 21],
  933. ["num" => 0, "hour" => 22],
  934. ["num" => 0, "hour" => 23]
  935. ];
  936. $weeks = [];
  937. $area_ids = self::$areaIds;
  938. for ($i = 1, $c = 7; $i < 7; $i++, $c--) {
  939. $now = Carbon::parse("-{$i}day");
  940. $date = $now->toDateString();
  941. $key = "{$date}-hour-order-num";
  942. $area_id = $request->get('area_id') ?? '';
  943. if(!empty($area_id)){
  944. $key = $key .'-area_id:'. $area_id;
  945. }
  946. // Cache::forget($key);
  947. $weeks["{$date}"] = Cache::remember($key, Carbon::parse("+{$c}day")->diffInMinutes(Carbon::now()), function () use ($date, $day,$filter,$area_ids) {
  948. return array_replace($day, Order::query()->filter($filter)->whereDate("created_at", $date)->whereIn('area_id',$area_ids)->select(DB::raw("count(*) as num,DATE_FORMAT(created_at,'%H') as hour"))->groupBy("hour")->get()->mapWithKeys(function ($item) {
  949. $arr = [
  950. 'num' => (integer)$item->num,
  951. 'hour' => (integer)$item->hour,
  952. ];
  953. return [(integer)$item->hour => $arr];
  954. })->toArray());
  955. });
  956. }
  957. $data = [
  958. 'date' => [],
  959. 'data' => []
  960. ];
  961. foreach ($weeks as $k => $v) {
  962. $data['date'][] = $k;
  963. $data['data'][$k] = [];
  964. foreach ($v as $value) {
  965. array_push($data['data'][$k], $value['num']);
  966. }
  967. }
  968. // dd($data);
  969. return $this->ok($data);
  970. }
  971. }