OrderController.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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\AdminMerchant;
  15. use App\Models\AdminUser;
  16. use App\Models\AdminUserArea;
  17. use App\Models\Bike;
  18. use App\Models\CardRidingOrder;
  19. use App\Models\DepositCardOrder;
  20. use App\Models\DepositOrder;
  21. use App\Models\DepositRefund;
  22. use App\Models\LocationsLog;
  23. use App\Models\Order;
  24. use App\Models\OrderRent;
  25. use App\Models\RechargeOrder;
  26. use App\Models\User;
  27. use App\Models\WalletLog;
  28. use App\Utils\Admin;
  29. use Carbon\Carbon;
  30. use Illuminate\Http\Request;
  31. use App\Http\Controllers\Controller;
  32. use App\Models\Area;
  33. use App\Models\Statistic;
  34. use Illuminate\Support\Facades\Cache;
  35. use Illuminate\Support\Facades\DB;
  36. use Illuminate\Support\Facades\Log;
  37. class OrderController extends AppBaseController
  38. {
  39. /**
  40. * orderList 订单列表
  41. *
  42. * @param OrderFilter $filter
  43. * @return \Illuminate\Http\JsonResponse
  44. * @author Fx
  45. *
  46. */
  47. public function orderList(OrderFilter $filter)
  48. {
  49. $area_ids = self::$areaIds;
  50. $order = Order::query()
  51. ->whereIn('area_id', $area_ids)
  52. ->where(AdminMerchant::getMerchantWhere())
  53. ->filter($filter)
  54. ->with('users')
  55. ->where('status', '!=', Order::STATUS_CLOSE_ORDER)
  56. ->orderByDesc('id')
  57. ->paginate();
  58. return $this->ok(OrderResource::collection($order));
  59. }
  60. /**
  61. * orderStatistics 订单统计
  62. *
  63. * @param OrderFilter $filter
  64. * @return \Illuminate\Http\JsonResponse
  65. * @author Fx
  66. *
  67. */
  68. public function orderStatistics(OrderFilter $filter)
  69. {
  70. $area_ids = self::$areaIds;
  71. // 总订单数
  72. $order_total = Order::query()
  73. ->where(AdminMerchant::getMerchantWhere())
  74. ->whereIn('area_id', $area_ids)
  75. ->filter($filter)
  76. ->where('status', Order::STATUS_COMPLETE_ORDER)
  77. ->count('id');
  78. $order_rent_total = OrderRent::query()
  79. ->where(AdminMerchant::getMerchantWhere())
  80. ->whereIn('area_id', $area_ids)
  81. ->filter($filter)
  82. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  83. ->count('id');
  84. $order_total += $order_rent_total;
  85. // 今日新增订单数
  86. $today_add_order = Order::query()
  87. ->where(AdminMerchant::getMerchantWhere())
  88. ->whereIn('area_id', $area_ids)
  89. ->filter($filter)
  90. ->where('created_at', '>', Carbon::today())
  91. ->where('status', Order::STATUS_COMPLETE_ORDER)
  92. ->count('id');
  93. $today_add_order_rent = OrderRent::query()
  94. ->where(AdminMerchant::getMerchantWhere())
  95. ->whereIn('area_id', $area_ids)
  96. ->filter($filter)
  97. ->where('pay_time', '>', Carbon::today())
  98. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  99. ->count('id');
  100. $today_add_order += $today_add_order_rent;
  101. // 骑行中订单数
  102. $riding_status = [
  103. Order::STATUS_RIDE_BIKE,
  104. // Order::STATUS_CLOSE_BIKE, // 待支付
  105. Order::STATUS_PAUSE_BIKE,
  106. ];
  107. $riding_order = Order::query()
  108. ->where(AdminMerchant::getMerchantWhere())
  109. ->whereIn('area_id', $area_ids)
  110. ->filter($filter)
  111. ->whereIn('status', $riding_status)
  112. ->count('id');
  113. $riding_order_rent = OrderRent::query()
  114. ->where(AdminMerchant::getMerchantWhere())
  115. ->whereIn('area_id', $area_ids)
  116. ->filter($filter)
  117. ->where('status', OrderRent::STATUS_RENT_BIKE)
  118. ->count('id');
  119. $riding_order += $riding_order_rent;
  120. // 待支付
  121. $waiting_pay_order = Order::query()
  122. ->where(AdminMerchant::getMerchantWhere())
  123. ->whereIn('area_id', $area_ids)
  124. ->filter($filter)
  125. ->where('status', Order::STATUS_CLOSE_BIKE)
  126. ->count('id');
  127. $waiting_pay_rent = OrderRent::query()
  128. ->where(AdminMerchant::getMerchantWhere())
  129. ->whereIn('area_id', $area_ids)
  130. ->filter($filter)
  131. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  132. ->count('id');
  133. $waiting_pay_order += $waiting_pay_rent;
  134. // 今日新增收入
  135. $today_add_money = Order::query()
  136. ->where(AdminMerchant::getMerchantWhere())
  137. ->whereIn('area_id', $area_ids)
  138. ->filter($filter)
  139. ->where('pay_time', '>', Carbon::today())
  140. ->where('status', Order::STATUS_COMPLETE_ORDER)
  141. ->sum('pay_money');
  142. $today_add_money_rent = OrderRent::query()
  143. ->where(AdminMerchant::getMerchantWhere())
  144. ->whereIn('area_id', $area_ids)
  145. ->filter($filter)
  146. ->where('pay_time', '>', Carbon::today())
  147. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  148. ->sum('pay_money');
  149. $today_add_money += $today_add_money_rent;
  150. $order = [
  151. 'order_total' => $order_total ?? 0,
  152. 'today_add_order' => $today_add_order ?? 0,
  153. 'riding_order' => $riding_order ?? 0,
  154. 'today_add_money' => $today_add_money ?? 0,
  155. 'waiting_pay' => $waiting_pay_order ?? 0,
  156. ];
  157. return $this->ok($order);
  158. }
  159. /**
  160. * orderProfitStatistics 订单收益统计
  161. *
  162. * @param WalletLogFilter $walletLogFilter
  163. * @param DepositFilter $depositFilter
  164. * @param OrderFilter $orderFilter
  165. * @param OrderRentFilter $orderRentFilter
  166. * @return \Illuminate\Http\JsonResponse
  167. * @author Fx
  168. *
  169. */
  170. public function orderProfitStatistics(WalletLogFilter $walletLogFilter, DepositFilter $depositFilter, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter)
  171. {
  172. $area_ids = self::$areaIds;
  173. $admin_type = Admin::user()->type;
  174. if ($admin_type == AdminUser::TYPE_WORKER) {
  175. $data = [
  176. 'totalProfit' => $totalProfit ?? 0,
  177. 'depositTotal' => $depositTotal ?? 0,
  178. 'todayProfit' => $todayProfit ?? 0,
  179. 'depositToday' => $depositToday ?? 0,
  180. 'monthProfit' => $monthProfit ?? 0,
  181. ];
  182. return $this->ok($data);
  183. }
  184. // 总收益
  185. // $totalProfit = WalletLog::query()
  186. // ->whereIn('area_id', $area_ids)
  187. // ->filter($walletLogFilter)
  188. // ->whereIn('type', WalletLog::$subType)
  189. // ->sum('money');
  190. $totalOrderProfit = Order::query()
  191. ->where(AdminMerchant::getMerchantWhere())
  192. ->whereIn('area_id', $area_ids)
  193. ->filter($orderFilter)
  194. ->where('status', Order::STATUS_COMPLETE_ORDER)
  195. ->sum('pay_money');
  196. $totalOrderRentProfit = OrderRent::query()
  197. ->where(AdminMerchant::getMerchantWhere())
  198. ->whereIn('area_id', $area_ids)
  199. ->filter($orderRentFilter)
  200. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  201. ->sum('pay_money');
  202. $totalProfit = bcadd($totalOrderProfit, $totalOrderRentProfit, 0);
  203. // 总押金
  204. $depositTotal = DepositOrder::query()
  205. ->where(AdminMerchant::getMerchantWhere())
  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. ->sum('pay_money');
  211. // 今日收益
  212. // $todayProfit = WalletLog::query()
  213. // ->whereIn('area_id', $area_ids)
  214. // ->filter($walletLogFilter)
  215. // ->whereIn('type', WalletLog::$subType)
  216. // ->where('created_at', '>', Carbon::today())
  217. // ->sum('money');
  218. $todayOrderProfit = Order::query()
  219. ->where(AdminMerchant::getMerchantWhere())
  220. ->whereIn('area_id', $area_ids)
  221. ->filter($orderFilter)
  222. ->where('status', Order::STATUS_COMPLETE_ORDER)
  223. ->where('created_at', '>', Carbon::today())
  224. ->sum('pay_money');
  225. $todayOrderRentProfit = OrderRent::query()
  226. ->where(AdminMerchant::getMerchantWhere())
  227. ->whereIn('area_id', $area_ids)
  228. ->filter($orderRentFilter)
  229. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  230. ->where('created_at', '>', Carbon::today())
  231. ->sum('pay_money');
  232. $todayProfit = bcadd($todayOrderProfit, $todayOrderRentProfit, 0);
  233. // 今日新增押金
  234. $depositToday = DepositOrder::query()
  235. ->where(AdminMerchant::getMerchantWhere())
  236. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  237. ->where('is_refund', DepositOrder::REFUND_NO)
  238. ->whereIn('area_id', $area_ids)
  239. ->filter($depositFilter)
  240. ->where('created_at', '>', Carbon::today())
  241. ->sum('pay_money');
  242. // 本月收益
  243. // $monthProfit = WalletLog::query()
  244. // ->whereIn('area_id', $area_ids)
  245. // ->filter($walletLogFilter)
  246. // ->whereIn('type', WalletLog::$subType)
  247. // ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  248. // ->sum('money');
  249. $monthOrderProfit = Order::query()
  250. ->where(AdminMerchant::getMerchantWhere())
  251. ->whereIn('area_id', $area_ids)
  252. ->filter($orderFilter)
  253. ->where('status', Order::STATUS_COMPLETE_ORDER)
  254. ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  255. ->sum('pay_money');
  256. $monthOrderRentProfit = OrderRent::query()
  257. ->where(AdminMerchant::getMerchantWhere())
  258. ->whereIn('area_id', $area_ids)
  259. ->filter($orderRentFilter)
  260. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  261. ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
  262. ->sum('pay_money');
  263. $monthProfit = bcadd($monthOrderProfit, $monthOrderRentProfit, 0);
  264. $data = [
  265. 'totalProfit' => floor(abs($totalProfit)) ?? 0,
  266. 'depositTotal' => floor($depositTotal) ?? 0,
  267. 'todayProfit' => floor($todayProfit) ?? 0,
  268. 'depositToday' => floor($depositToday) ?? 0,
  269. 'monthProfit' => floor($monthProfit) ?? 0,
  270. ];
  271. return $this->ok($data);
  272. }
  273. /**
  274. * 商户列表
  275. * @return \Illuminate\Http\JsonResponse
  276. * @author ht
  277. */
  278. public function adminMerchantList()
  279. {
  280. $data = [];
  281. // 管理员可以获取商户列表
  282. if (Admin::isAdministrator()) {
  283. // status 状态(1正常0暂停)
  284. $data = AdminMerchant::where('status', 1)->select(['id', 'username', 'name'])->get()->toArray();
  285. }
  286. return $this->ok($data);
  287. }
  288. /**
  289. * 区域列表 - 依据所选商户获取对应区域
  290. *
  291. * @param Request $request
  292. * @return \Illuminate\Http\JsonResponse
  293. * @author ht
  294. */
  295. public function areaList(Request $request)
  296. {
  297. $admin_id = Admin::user()->id;
  298. if (Admin::isAdministrator()) {
  299. $merchant_id = $request->get('merchant_id') ?? 0;
  300. if ($merchant_id) {
  301. $data = Area::where('status', 1)->where('merchant_id', $merchant_id)->orderByDesc('id')->select(['id', 'name'])->get()->toArray();
  302. } else {
  303. $data = Area::where('status', 1)->orderByDesc('id')->select(['id', 'name'])->get()->toArray();
  304. }
  305. } else {
  306. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  307. $data = Area::whereIn('id', $area_ids)->where('status', 1)->orderByDesc('id')->select(['id', 'name'])->get()->toArray();
  308. }
  309. return $this->ok($data);
  310. }
  311. /**
  312. * profitDetail 收益详情
  313. *
  314. * @param Request $request
  315. * @param OrderFilter $orderFilter
  316. * @param OrderRentFilter $orderRentFilter
  317. * @param DepositFilter $depositFilter
  318. * @param WalletLogFilter $walletLogFilter
  319. * @param UserFilter $userFilter
  320. * @return \Illuminate\Http\JsonResponse
  321. * @author Fx
  322. *
  323. */
  324. public function profitDetail_back(Request $request, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter, DepositFilter $depositFilter, WalletLogFilter $walletLogFilter, UserFilter $userFilter,BikeFilter $bikeFilter,RechargeOrderFilter $rechargeOrderFilter,CardRidingOrderFilter $cardRidingOrderFilter,DepositCardOrderFilter $depositCardOrderFilter)
  325. {
  326. $area_ids = self::$areaIds;
  327. $time_between = $request->get('pay_time_between') ?? [];
  328. if (empty($time_between)) return $this->error('参数错误');
  329. $t1 = Carbon::make($time_between[0]);
  330. $t2 = Carbon::make($time_between[1]);
  331. $days = $t1->diffInDays($t2);
  332. // 总收益
  333. $totalProfit = WalletLog::query()
  334. ->where(AdminMerchant::getMerchantWhere())
  335. ->filter($walletLogFilter)
  336. ->whereIn('area_id', $area_ids)
  337. ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
  338. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
  339. ->whereIn('type', WalletLog::$subType)
  340. ->sum('money');
  341. // 总押金
  342. $depositTotal = DepositOrder::query()
  343. ->where(AdminMerchant::getMerchantWhere())
  344. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  345. ->where('is_refund', DepositOrder::REFUND_NO)
  346. ->whereIn('area_id', $area_ids)
  347. ->filter($depositFilter)
  348. ->sum('pay_money');
  349. // 普通订单
  350. $orderTotalProfit = Order::query()
  351. ->where(AdminMerchant::getMerchantWhere())
  352. ->where('pay_status', Order::PAY_STATUS_OK)
  353. ->where('status', Order::STATUS_COMPLETE_ORDER)
  354. ->whereIn('area_id', $area_ids)
  355. ->filter($orderFilter)
  356. ->sum('pay_money');
  357. // 骑行花费
  358. $orderTotalTimeProfit = Order::query()
  359. ->where(AdminMerchant::getMerchantWhere())
  360. ->where('pay_status', Order::PAY_STATUS_OK)
  361. ->where('status', Order::STATUS_COMPLETE_ORDER)
  362. ->whereIn('area_id', $area_ids)
  363. ->filter($orderFilter)
  364. ->sum('time_money');
  365. $orderTotal = Order::query()
  366. ->where(AdminMerchant::getMerchantWhere())
  367. ->where('pay_status', Order::PAY_STATUS_OK)
  368. ->where('status', Order::STATUS_COMPLETE_ORDER)
  369. ->whereIn('area_id', $area_ids)
  370. ->filter($orderFilter)
  371. ->count();
  372. $orderDispatchTotalProfit = Order::query()
  373. ->where(AdminMerchant::getMerchantWhere())
  374. ->where('pay_status', Order::PAY_STATUS_OK)
  375. ->where('status', Order::STATUS_COMPLETE_ORDER)
  376. ->whereIn('area_id', $area_ids)
  377. ->filter($orderFilter)
  378. ->sum('dispatch_money');
  379. // 日租订单
  380. $orderRentTotalProfit = OrderRent::query()
  381. ->where(AdminMerchant::getMerchantWhere())
  382. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  383. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  384. ->whereIn('area_id', $area_ids)
  385. ->filter($orderRentFilter)
  386. ->sum('pay_money');
  387. // 日租超时花费
  388. $orderRentTotalTimeProfit = OrderRent::query()
  389. ->where(AdminMerchant::getMerchantWhere())
  390. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  391. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  392. ->whereIn('area_id', $area_ids)
  393. ->filter($orderRentFilter)
  394. ->sum('time_money');
  395. // 日租租金
  396. $orderRentTotalRentProfit = OrderRent::query()
  397. ->where(AdminMerchant::getMerchantWhere())
  398. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  399. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  400. ->whereIn('area_id', $area_ids)
  401. ->filter($orderRentFilter)
  402. ->sum('rent_money');
  403. $orderRentTotal = OrderRent::query()
  404. ->where(AdminMerchant::getMerchantWhere())
  405. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  406. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  407. ->whereIn('area_id', $area_ids)
  408. ->filter($orderRentFilter)
  409. ->count();
  410. // 日租调度费
  411. $orderRentDispatchTotalProfit = OrderRent::query()
  412. ->where(AdminMerchant::getMerchantWhere())
  413. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  414. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  415. ->whereIn('area_id', $area_ids)
  416. ->filter($orderRentFilter)
  417. ->sum('dispatch_money');
  418. // 已实名用户数
  419. $userTotalCardOk = User::query()
  420. ->where(AdminMerchant::getMerchantWhere())
  421. ->filter($userFilter)
  422. ->whereIn('register_area_id', $area_ids)
  423. ->where('is_card_certified', User::CARD_OK)
  424. ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
  425. ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
  426. ->count();
  427. // 已缴纳押金用户数
  428. $userDepositTotal = User::query()
  429. ->where(AdminMerchant::getMerchantWhere())
  430. ->filter($userFilter)
  431. ->whereIn('register_area_id', $area_ids)
  432. ->where('is_card_certified', User::CARD_OK)
  433. ->where('is_card_certified', User::CARD_OK)
  434. ->where('is_deposit', User::DEPOSIT_OK)
  435. ->count();
  436. $orderWaitPay = Order::query()
  437. ->where(AdminMerchant::getMerchantWhere())
  438. ->filter($orderFilter)
  439. ->where('status', Order::STATUS_CLOSE_BIKE)
  440. ->whereIn('area_id', $area_ids)
  441. ->sum('pay_money');
  442. $orderRentWaitPay = OrderRent::query()
  443. ->where(AdminMerchant::getMerchantWhere())
  444. ->filter($orderRentFilter)
  445. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  446. ->whereIn('area_id', $area_ids)
  447. ->sum('pay_money');
  448. // 待支付
  449. $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
  450. $orderWaitPayNum = Order::query()
  451. ->where(AdminMerchant::getMerchantWhere())
  452. ->filter($orderFilter)
  453. ->where('status', Order::STATUS_CLOSE_BIKE)
  454. ->whereIn('area_id', $area_ids)
  455. ->count();
  456. $orderRentWaitPayNum = OrderRent::query()
  457. ->where(AdminMerchant::getMerchantWhere())
  458. ->filter($orderRentFilter)
  459. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
  460. ->whereIn('area_id', $area_ids)
  461. ->count();
  462. $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
  463. $bikeTotal = Bike::query()
  464. ->where(AdminMerchant::getMerchantWhere())
  465. ->filter($bikeFilter)
  466. ->where('put_status', Bike::PUT_STATUS_YES)
  467. ->whereIn('put_area_id', $area_ids)
  468. ->count();
  469. $totalUsers = User::query()
  470. ->where(AdminMerchant::getMerchantWhere())
  471. ->filter($userFilter)
  472. ->whereIn('register_area_id', $area_ids)
  473. ->count();
  474. $riddingCardOrderTotalProfit = CardRidingOrder::query()
  475. ->where(AdminMerchant::getMerchantWhere())
  476. ->whereIn('area_id', $area_ids)
  477. ->filter($cardRidingOrderFilter)
  478. ->where('pay_status', CardRidingOrder::PAY_STATUS_OK)
  479. ->sum('pay_money');
  480. $rechargeOrderTotalProfit = RechargeOrder::query()
  481. ->where(AdminMerchant::getMerchantWhere())
  482. ->whereIn('area_id', $area_ids)
  483. ->filter($rechargeOrderFilter)
  484. ->where('pay_status', RechargeOrder::PAY_STATUS_OK)
  485. ->sum('pay_money');
  486. $depositCardTotalProfit = DepositCardOrder::query()
  487. ->where(AdminMerchant::getMerchantWhere())
  488. ->whereIn('area_id', $area_ids)
  489. ->filter($depositCardOrderFilter)
  490. ->where('pay_status', DepositCardOrder::PAY_STATUS_OK)
  491. ->sum('pay_money');
  492. $data = [
  493. 'totalProfit' => bcadd($orderTotalProfit, $orderRentTotalProfit, 2) ?? 0, // 新增收益
  494. 'depositTotal' => $depositTotal ?? 0, // 新增押金
  495. 'orderTotalProfit' => $orderTotalProfit ?? 0, // 新增普通订单收入 // 骑行花费
  496. 'orderTotal' => $orderTotal ?? 0, // 新增普通订单数量
  497. 'orderDispatchTotalProfit' => $orderDispatchTotalProfit ?? 0, // 新增普通订单调度费收入 // 调度费
  498. 'orderRentTotalProfit' => $orderRentTotalProfit ?? 0, // 新增日租订单收入
  499. 'orderRentTotal' => $orderRentTotal ?? 0, // 新增日租订单数量
  500. 'orderRentDispatchTotalProfit' => $orderRentDispatchTotalProfit ?? 0, // 新增日租订单调度费收入
  501. 'userTotalCardOk' => $userTotalCardOk ?? 0, // 新增身份认证用户
  502. 'userTotal' => $totalUsers ?? 0, // 总用户数
  503. 'userDepositTotal' => $userDepositTotal ?? 0, // 总缴纳押金用户数
  504. 'wiatPayTotal' => $wiatPayTotal ?? 0, //总待支付
  505. 'wiatNum' => $wiatNum ?? 0, //总待支付数量
  506. 'bikeTotal' => $bikeTotal ?? 0, // 总投放车辆
  507. 'days' => abs($days), // 天数
  508. 'riddingCardOrderTotalProfit' => $riddingCardOrderTotalProfit ?? 0, // 骑行卡收益
  509. 'rechargeOrderTotalProfit' => $rechargeOrderTotalProfit ?? 0, // 充值订单收益
  510. 'depositCardTotalProfit' => $depositCardTotalProfit ?? 0, // 免押金卡收益
  511. 'activityTotalProfit' => bcadd(bcadd($riddingCardOrderTotalProfit,$rechargeOrderTotalProfit,2),$depositCardTotalProfit,2), // 活动总收益
  512. ];
  513. return $this->ok($data);
  514. }
  515. // 验证是否有该区域的权限
  516. private function validateAreaId($area_id)
  517. {
  518. $area_ids = AdminUser::getAreaIdsByAdminId(Admin::user()->id);
  519. if (count($area_ids) !== 0 && in_array($area_id, $area_ids)) {
  520. return false;
  521. } else {
  522. $id = AdminUserArea::query()->where('admin_id', Admin::user()->id)->value('area_id');
  523. $id = $id ?? 0;
  524. if ($area_id == $id) return false;
  525. }
  526. return true;
  527. }
  528. /**
  529. * 重构 profitDetail
  530. * 收益详情统计
  531. * 区分管理员和商户,依据不同角色查询
  532. * 添加商户列表和区域列表接口
  533. * 查询时间做个限制,最多查询三个月的记录
  534. * 重构总收益和总押金,累计净押金的时候,
  535. * 注意历史数据可以直接查询统计表statistics,当天数据的查询可以直接查询wallet_logs
  536. */
  537. public function profitDetail(Request $request, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter, DepositFilter $depositFilter, WalletLogFilter $walletLogFilter, UserFilter $userFilter,BikeFilter $bikeFilter,RechargeOrderFilter $rechargeOrderFilter,CardRidingOrderFilter $cardRidingOrderFilter,DepositCardOrderFilter $depositCardOrderFilter)
  538. {
  539. $time_between = $request->get('pay_time_between') ?? [];
  540. if (empty($time_between)) return $this->error('参数错误');
  541. $t1 = Carbon::make($time_between[0]);
  542. $t2 = Carbon::make($time_between[1]);
  543. $days = $t1->diffInDays($t2);
  544. // 如果选择的时间段超过了三个月,拒绝查询
  545. if($days > 92) return $this->error('选择的时间段超过了三个月');
  546. $area_id = $request->get('area_id') ?? '';
  547. if ($area_id) {
  548. $flag = $this->validateAreaId($area_id);
  549. if ($flag) return $this->error('无权限查看该区域');
  550. }
  551. $merchant_id = $request->get('merchant_id') ?? '';
  552. $startTime = date('Y-m-d', strtotime($time_between[0]));
  553. $endTime = date('Y-m-d', strtotime($time_between[1]));
  554. // 总收益
  555. $totalProfit = $this->getTotalProfit($area_id,$merchant_id,$startTime,$endTime,Statistic::SLUG_PROFIT_STATIC);
  556. $resDa = $this->getTotalProfit($area_id,$merchant_id,$startTime,$endTime,Statistic::SLUG_DEPOSIT_STATIC);
  557. $depositTotal = $resDa[0]; // 总押金
  558. $cumulativeDepositTotal = $resDa[1]; // 累计净押金
  559. // 普通订单
  560. $orderTotalProfit = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'pay_money');
  561. // 骑行花费
  562. $orderTotalTimeProfit = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'time_money');
  563. // 订单数量
  564. $orderTotal = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter);
  565. // 里程费用
  566. $orderDispatchTotalProfit = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'dispatch_money');
  567. // 日租订单
  568. $orderRentTotalProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'pay_money');
  569. // 日租超时花费
  570. $orderRentTotalTimeProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'time_money');
  571. // 日租租金
  572. $orderRentTotalRentProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'rent_money');
  573. // 日租订单数量
  574. $orderRentTotal = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter);
  575. // 日租调度费
  576. $orderRentDispatchTotalProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'dispatch_money');
  577. // 已实名用户数
  578. $userTotalCardOk = $this->getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,'is_card_certified');
  579. // 已缴纳押金用户数
  580. $userDepositTotal = $this->getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,'is_deposit');
  581. // 骑车结束,待支付总金额
  582. $orderWaitPay = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'order','pay_money');
  583. $orderRentWaitPay = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'orderRent','pay_money');
  584. $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
  585. // 骑车结束,待支付总订单数量
  586. $orderWaitPayNum = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'order');
  587. $orderRentWaitPayNum = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'orderRent');
  588. $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
  589. // 总投放车辆
  590. $bikeTotal = $this->getBikeTotal($area_id,$merchant_id,$startTime,$endTime,$bikeFilter);
  591. // 总用户数
  592. $totalUsers = $this->getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,'total_num');
  593. // 骑行卡收益
  594. $riddingCardOrderTotalProfit = $this->getRiddingCardOrderProfit($area_id,$merchant_id,$startTime,$endTime,$cardRidingOrderFilter);
  595. // 充值订单收益
  596. $rechargeOrderTotalProfit = $this->getRechargeOrderProfit($area_id,$merchant_id,$startTime,$endTime,$rechargeOrderFilter);
  597. // 免押金卡收益
  598. $depositCardTotalProfit = $this->getDepositCardTotalProfit($area_id,$merchant_id,$startTime,$endTime,$depositCardOrderFilter);
  599. $data = [
  600. 'totalProfit' => bcadd($orderTotalProfit, $orderRentTotalProfit, 2) ?? 0, // 新增收益
  601. 'depositTotal' => $depositTotal ?? 0, // 新增押金
  602. 'cumulativeDepositTotal' => $cumulativeDepositTotal ?? 0, // 累计净押金
  603. 'orderTotalProfit' => $orderTotalProfit ?? 0, // 新增普通订单收入 // 骑行花费
  604. 'orderTotal' => $orderTotal ?? 0, // 新增普通订单数量
  605. 'orderDispatchTotalProfit' => $orderDispatchTotalProfit ?? 0, // 新增普通订单调度费收入 // 调度费
  606. 'orderRentTotalProfit' => $orderRentTotalProfit ?? 0, // 新增日租订单收入
  607. 'orderRentTotal' => $orderRentTotal ?? 0, // 新增日租订单数量
  608. 'orderRentDispatchTotalProfit' => $orderRentDispatchTotalProfit ?? 0, // 新增日租订单调度费收入
  609. 'userTotalCardOk' => $userTotalCardOk ?? 0, // 新增身份认证用户
  610. 'userTotal' => $totalUsers ?? 0, // 总用户数
  611. 'userDepositTotal' => $userDepositTotal ?? 0, // 总缴纳押金用户数
  612. 'wiatPayTotal' => $wiatPayTotal ?? 0, //总待支付
  613. 'wiatNum' => $wiatNum ?? 0, //总待支付数量
  614. 'bikeTotal' => $bikeTotal ?? 0, // 总投放车辆
  615. 'days' => abs($days), // 天数
  616. 'riddingCardOrderTotalProfit' => $riddingCardOrderTotalProfit ?? 0, // 骑行卡收益
  617. 'rechargeOrderTotalProfit' => $rechargeOrderTotalProfit ?? 0, // 充值订单收益
  618. 'depositCardTotalProfit' => $depositCardTotalProfit ?? 0, // 免押金卡收益
  619. 'activityTotalProfit' => bcadd(bcadd($riddingCardOrderTotalProfit,$rechargeOrderTotalProfit,2),$depositCardTotalProfit,2), // 活动总收益
  620. ];
  621. return $this->ok($data);
  622. }
  623. // profitDetail 收益详情 - 总收益 - 总押金 - 累计净押金
  624. private function getTotalProfit($area_id,$merchant_id,$startTime,$endTime,$slug)
  625. {
  626. if (Admin::isAdministrator()) {
  627. if ($merchant_id) {
  628. $newOrders = Statistic::query()->where('merchant_id', $merchant_id)->where('slug', $slug);
  629. } else {
  630. $newOrders = Statistic::query()->where('merchant_id', 0)->where('slug', $slug);
  631. }
  632. if(empty($area_id)){
  633. $newOrders = $newOrders->where('area_id', config('statistic.all'));
  634. }
  635. } else {
  636. $newOrders = Statistic::query()->where(AdminMerchant::getMerchantWhere())->where('slug', $slug);
  637. if(empty($area_id)){
  638. $newOrders = $newOrders->where('area_id', 0);
  639. }
  640. }
  641. if ($area_id) {
  642. $newOrders = $newOrders->where('area_id', $area_id);
  643. }
  644. $data = $newOrders->whereBetween('date', [$startTime,$endTime])->select('body')->get()->toArray();
  645. if($slug == Statistic::SLUG_PROFIT_STATIC){
  646. $totalMoney = 0.00;
  647. }else{
  648. $totalMoney = [0.00,0.00];
  649. }
  650. if (!empty($data)) {
  651. switch($slug){
  652. case Statistic::SLUG_PROFIT_STATIC: // 总收益
  653. foreach ($data as $v) {
  654. $body = json_decode($v['body'], true);
  655. $totalMoney = bcadd($totalMoney,bcadd($body['profit'],$body['card_profit'],2),2);
  656. }
  657. break;
  658. case Statistic::SLUG_DEPOSIT_STATIC:
  659. // 总押金
  660. foreach ($data as $v) {
  661. $body = json_decode($v['body'], true);
  662. $money_all = isset($body['money_all']) ? $body['money_all'] : 0;
  663. $totalMoney[0] = bcadd($totalMoney[0],$money_all,2);
  664. }
  665. // 累计净押金
  666. foreach ($data as $v) {
  667. $body = json_decode($v['body'], true);
  668. $money_all = isset($body['money_all']) ? $body['money_all'] : 0;
  669. $money_refund_all = isset($body['money_refund_all']) ? $body['money_refund_all'] : 0.00;
  670. $totalMoney[1] = bcadd($totalMoney[1],bcsub($money_all,$money_refund_all,2),2);
  671. }
  672. break;
  673. default:
  674. }
  675. }
  676. if($slug == Statistic::SLUG_PROFIT_STATIC){
  677. // 时间区间包含今天的时候
  678. if($endTime == date('Y-m-d') || (strtotime($endTime) >= strtotime(date('Y-m-d 00:00:00')))){
  679. // 再加上今天的收益 = 纯收益 + 骑行卡收益
  680. $totayMoney = $this->getTotayTotalProfit($area_id, $merchant_id);
  681. $totalMoney = bcadd($totalMoney,$totayMoney,2);
  682. }
  683. }else{
  684. // 时间区间包含今天的时候
  685. if($endTime == date('Y-m-d') || (strtotime($endTime) >= strtotime(date('Y-m-d 00:00:00')))){
  686. // 再加上今天的押金 和 累计净押金
  687. $resDa = $this->getTotayDeposit($area_id, $merchant_id);
  688. $totalMoney[0] = bcadd($totalMoney[0],$resDa[0],2);
  689. $totalMoney[1] = bcadd($totalMoney[1],$resDa[0],2);
  690. }
  691. }
  692. return $totalMoney;
  693. }
  694. // profitDetail 收益详情 - 总收益 - 今天收益
  695. private function getTotayTotalProfit($area_id, $merchant_id)
  696. {
  697. $totalProfit = 0.00;
  698. // 管理员,并且选择了商户
  699. if (Admin::isAdministrator() && $merchant_id) {
  700. $walletLogProfit = WalletLog::query()->where('merchant_id', $merchant_id);
  701. $cardRidingOrdersProfit = CardRidingOrder::query()->where('merchant_id',$merchant_id);
  702. } else {
  703. $walletLogProfit = WalletLog::query()->where(AdminMerchant::getMerchantWhere());
  704. $cardRidingOrdersProfit = CardRidingOrder::query()->where(AdminMerchant::getMerchantWhere());
  705. }
  706. if (!empty($area_id)) {
  707. $walletLogProfit = $walletLogProfit->where('area_id', $area_id);
  708. $cardRidingOrdersProfit = $cardRidingOrdersProfit->where('area_id', $area_id);
  709. } else {
  710. $admin_id = Admin::user()->id;
  711. if (!Admin::isAdministrator()) {
  712. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  713. if (count($area_ids) !== 0) {
  714. $walletLogProfit = $walletLogProfit->whereIn('area_id', $area_ids);
  715. $cardRidingOrdersProfit = $cardRidingOrdersProfit->whereIn('area_id', $area_ids);
  716. } else {
  717. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  718. $area_id = $area_id ?? 0;
  719. $walletLogProfit = $walletLogProfit->where('area_id', $area_id);
  720. $cardRidingOrdersProfit = $cardRidingOrdersProfit->where('area_id', $area_id);
  721. }
  722. }
  723. }
  724. // 口袋钱包纯收益
  725. $walletLogProfit = $walletLogProfit
  726. ->whereIn('type', WalletLog::$subType) // 交易类型 - subType 存收益
  727. ->where('status', WalletLog::STATUS_OK) // 数据状态 - 有效
  728. ->where('created_at','>=',Carbon::today())
  729. ->sum('money');
  730. // 骑行卡总收益
  731. $cardRidingOrdersProfit = $cardRidingOrdersProfit
  732. ->where('pay_type', CardRidingOrder::PAY_TYPE_WECHAT) // 支付方式 - 微信支付
  733. ->where('pay_status', CardRidingOrder::PAY_STATUS_OK) // 支付状态 - 支付成功
  734. ->where('status', CardRidingOrder::STATUS_OK) // 订单状态 - 成功
  735. ->where('pay_time','>=',Carbon::today())
  736. ->sum('pay_money');
  737. $totalProfit = abs((float)$walletLogProfit);
  738. $totalProfit = bcadd($totalProfit,(float)$cardRidingOrdersProfit,2);
  739. return $totalProfit;
  740. }
  741. // profitDetail 收益详情 - 总押金 - 累计净押金 - 今天金额
  742. private function getTotayDeposit($area_id, $merchant_id)
  743. {
  744. $totalProfit = [];
  745. // 管理员,并且选择了商户
  746. if (Admin::isAdministrator() && $merchant_id) {
  747. $depositTotal = DepositOrder::query()->where('merchant_id', $merchant_id);
  748. $deposiRefund = DepositRefund::query()->where('merchant_id', $merchant_id);
  749. } else {
  750. $depositTotal = DepositOrder::query()->where(AdminMerchant::getMerchantWhere());
  751. $deposiRefund = DepositRefund::query()->where(AdminMerchant::getMerchantWhere());
  752. }
  753. if (!empty($area_id)) {
  754. $depositTotal = $depositTotal->where('area_id', $area_id);
  755. } else {
  756. $admin_id = Admin::user()->id;
  757. if (!Admin::isAdministrator()) {
  758. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  759. if (count($area_ids) !== 0) {
  760. $depositTotal = $depositTotal->whereIn('area_id', $area_ids);
  761. $deposiRefund = $deposiRefund->whereIn('area_id', $area_ids);
  762. } else {
  763. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  764. $area_id = $area_id ?? 0;
  765. $depositTotal = $depositTotal->where('area_id', $area_id);
  766. $deposiRefund = $deposiRefund->where('area_id', $area_id);
  767. }
  768. }
  769. }
  770. $totalProfit[0] = $depositTotal
  771. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  772. ->where('is_refund', DepositOrder::REFUND_NO)
  773. ->where('pay_time','>=',Carbon::today())
  774. ->sum('pay_money');
  775. $totalProfit[1] = $deposiRefund
  776. ->where('pay_status', DepositRefund::PAY_STATUS_OK)
  777. ->where('pay_time', '>=', Carbon::today())
  778. ->sum('pay_money');
  779. $totalProfit[1] = bcsub($totalProfit[0],$totalProfit[1],2);
  780. return $totalProfit;
  781. }
  782. // profitDetail 收益详情 - 普通订单 - 骑行花费 - 订单数量 - 里程费用
  783. private function getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,$money='')
  784. {
  785. // 管理员,并且选择了商户
  786. if (Admin::isAdministrator() && $merchant_id) {
  787. $orderTotalProfit = Order::query()->where('merchant_id',$merchant_id);
  788. }else{
  789. $orderTotalProfit = Order::query()->where(AdminMerchant::getMerchantWhere());
  790. }
  791. if (!empty($area_id)) {
  792. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  793. } else {
  794. $admin_id = Admin::user()->id;
  795. if (!Admin::isAdministrator()) {
  796. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  797. if (count($area_ids) !== 0) {
  798. $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
  799. } else {
  800. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  801. $area_id = $area_id ?? 0;
  802. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  803. }
  804. }
  805. }
  806. $orderTotalProfit = $orderTotalProfit
  807. ->where('pay_status', Order::PAY_STATUS_OK)
  808. ->where('status', Order::STATUS_COMPLETE_ORDER)
  809. ->whereBetween('pay_time', [$startTime,$endTime])
  810. ->filter($orderFilter);
  811. if($money){
  812. $orderTotalProfit = $orderTotalProfit->sum($money);
  813. }else{
  814. $orderTotalProfit = $orderTotalProfit->count();
  815. }
  816. return $orderTotalProfit;
  817. }
  818. // profitDetail 收益详情 - 日租订单 - 日租超时花费 - 日租租金 - 日租订单数量 - 日租调度费
  819. private function getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,$money='')
  820. {
  821. // 管理员,并且选择了商户
  822. if (Admin::isAdministrator() && $merchant_id) {
  823. $orderTotalProfit = OrderRent::query()->where('merchant_id',$merchant_id);
  824. }else{
  825. $orderTotalProfit = OrderRent::query()->where(AdminMerchant::getMerchantWhere());
  826. }
  827. if (!empty($area_id)) {
  828. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  829. } else {
  830. $admin_id = Admin::user()->id;
  831. if (!Admin::isAdministrator()) {
  832. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  833. if (count($area_ids) !== 0) {
  834. $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
  835. } else {
  836. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  837. $area_id = $area_id ?? 0;
  838. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  839. }
  840. }
  841. }
  842. $orderTotalProfit = $orderTotalProfit
  843. ->where('pay_status', OrderRent::PAY_STATUS_OK)
  844. ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
  845. ->whereBetween('pay_time', [$startTime,$endTime])
  846. ->filter($orderRentFilter);
  847. if($money){
  848. $orderTotalProfit = $orderTotalProfit->sum($money);
  849. }else{
  850. $orderTotalProfit = $orderTotalProfit->count();
  851. }
  852. return $orderTotalProfit;
  853. }
  854. // profitDetail 收益详情 - 已实名用户数 - 已缴纳押金用户数 - 总用户数
  855. private function getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,$money='')
  856. {
  857. // 管理员,并且选择了商户
  858. if (Admin::isAdministrator() && $merchant_id) {
  859. $orderTotalProfit = User::query()->where('merchant_id',$merchant_id);
  860. }else{
  861. $orderTotalProfit = User::query()->where(AdminMerchant::getMerchantWhere());
  862. }
  863. if (!empty($area_id)) {
  864. $orderTotalProfit = $orderTotalProfit->where('register_area_id', $area_id);
  865. } else {
  866. $admin_id = Admin::user()->id;
  867. if (!Admin::isAdministrator()) {
  868. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  869. if (count($area_ids) !== 0) {
  870. $orderTotalProfit = $orderTotalProfit->whereIn('register_area_id', $area_ids);
  871. } else {
  872. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  873. $area_id = $area_id ?? 0;
  874. $orderTotalProfit = $orderTotalProfit->where('register_area_id', $area_id);
  875. }
  876. }
  877. }
  878. $orderTotalProfit = $orderTotalProfit->filter($userFilter)
  879. ->whereBetween('created_at', [$startTime,$endTime]);
  880. switch($money){
  881. case 'is_card_certified':
  882. $orderTotalProfit = $orderTotalProfit
  883. ->where('is_card_certified', User::CARD_OK)
  884. ->count();
  885. break;
  886. case 'is_deposit':
  887. $orderTotalProfit = $orderTotalProfit
  888. ->where('is_deposit', User::DEPOSIT_OK)
  889. ->count();
  890. break;
  891. case 'total_num':
  892. $orderTotalProfit = $orderTotalProfit->count();
  893. break;
  894. default:
  895. }
  896. return $orderTotalProfit;
  897. }
  898. // profitDetail 收益详情 - (普通和日租) - 骑车结束,待支付总金额 - 待支付总订单数量
  899. private function getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderFilter,$modelsType,$money='')
  900. {
  901. $models = ($modelsType == 'order') ? Order::query() : OrderRent::query();
  902. // 管理员,并且选择了商户
  903. if (Admin::isAdministrator() && $merchant_id) {
  904. $orderTotalProfit = $models->where('merchant_id',$merchant_id);
  905. }else{
  906. $orderTotalProfit = $models->where(AdminMerchant::getMerchantWhere());
  907. }
  908. if (!empty($area_id)) {
  909. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  910. } else {
  911. $admin_id = Admin::user()->id;
  912. if (!Admin::isAdministrator()) {
  913. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  914. if (count($area_ids) !== 0) {
  915. $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
  916. } else {
  917. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  918. $area_id = $area_id ?? 0;
  919. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  920. }
  921. }
  922. }
  923. $orderTotalProfit = $orderTotalProfit->filter($orderFilter)->whereBetween('created_at', [$startTime,$endTime]);
  924. if($modelsType == 'order'){
  925. $orderTotalProfit = $orderTotalProfit
  926. ->where('status', Order::STATUS_CLOSE_BIKE);
  927. }else{
  928. $orderTotalProfit = $orderTotalProfit
  929. ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE);
  930. }
  931. if($money){
  932. $orderTotalProfit = $orderTotalProfit->sum($money);
  933. }else{
  934. $orderTotalProfit = $orderTotalProfit->count();
  935. }
  936. return $orderTotalProfit;
  937. }
  938. // profitDetail 收益详情 - 总投放车辆
  939. private function getBikeTotal($area_id,$merchant_id,$startTime,$endTime,$bikeFilter)
  940. {
  941. // 管理员,并且选择了商户
  942. if (Admin::isAdministrator() && $merchant_id) {
  943. $orderTotalProfit = Bike::query()->where('merchant_id',$merchant_id);
  944. }else{
  945. $orderTotalProfit = Bike::query()->where(AdminMerchant::getMerchantWhere());
  946. }
  947. if (!empty($area_id)) {
  948. $orderTotalProfit = $orderTotalProfit->where('put_area_id', $area_id);
  949. } else {
  950. $admin_id = Admin::user()->id;
  951. if (!Admin::isAdministrator()) {
  952. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  953. if (count($area_ids) !== 0) {
  954. $orderTotalProfit = $orderTotalProfit->whereIn('put_area_id', $area_ids);
  955. } else {
  956. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  957. $area_id = $area_id ?? 0;
  958. $orderTotalProfit = $orderTotalProfit->where('put_area_id', $area_id);
  959. }
  960. }
  961. }
  962. $orderTotalProfit = $orderTotalProfit
  963. ->filter($bikeFilter)
  964. ->where('put_status', Bike::PUT_STATUS_YES)
  965. ->whereBetween('put_time', [$startTime,$endTime])
  966. ->count();
  967. return $orderTotalProfit;
  968. }
  969. // profitDetail 收益详情 - 骑行卡收益
  970. private function getRiddingCardOrderProfit($area_id,$merchant_id,$startTime,$endTime,$cardRidingOrderFilter)
  971. {
  972. // 管理员,并且选择了商户
  973. if (Admin::isAdministrator() && $merchant_id) {
  974. $orderTotalProfit = CardRidingOrder::query()->where('merchant_id',$merchant_id);
  975. }else{
  976. $orderTotalProfit = CardRidingOrder::query()->where(AdminMerchant::getMerchantWhere());
  977. }
  978. if (!empty($area_id)) {
  979. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  980. } else {
  981. $admin_id = Admin::user()->id;
  982. if (!Admin::isAdministrator()) {
  983. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  984. if (count($area_ids) !== 0) {
  985. $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
  986. } else {
  987. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  988. $area_id = $area_id ?? 0;
  989. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  990. }
  991. }
  992. }
  993. $orderTotalProfit = $orderTotalProfit
  994. ->filter($cardRidingOrderFilter)
  995. ->where('pay_status', CardRidingOrder::PAY_STATUS_OK)
  996. ->whereBetween('pay_time', [$startTime,$endTime])
  997. ->sum('pay_money');
  998. return $orderTotalProfit;
  999. }
  1000. // profitDetail 收益详情 - 充值订单收益
  1001. private function getRechargeOrderProfit($area_id,$merchant_id,$startTime,$endTime,$rechargeOrderFilter)
  1002. {
  1003. // 管理员,并且选择了商户
  1004. if (Admin::isAdministrator() && $merchant_id) {
  1005. $orderTotalProfit = RechargeOrder::query()->where('merchant_id',$merchant_id);
  1006. }else{
  1007. $orderTotalProfit = RechargeOrder::query()->where(AdminMerchant::getMerchantWhere());
  1008. }
  1009. if (!empty($area_id)) {
  1010. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  1011. } else {
  1012. $admin_id = Admin::user()->id;
  1013. if (!Admin::isAdministrator()) {
  1014. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  1015. if (count($area_ids) !== 0) {
  1016. $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
  1017. } else {
  1018. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  1019. $area_id = $area_id ?? 0;
  1020. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  1021. }
  1022. }
  1023. }
  1024. $orderTotalProfit = $orderTotalProfit
  1025. ->filter($rechargeOrderFilter)
  1026. ->where('pay_status', RechargeOrder::PAY_STATUS_OK)
  1027. ->whereBetween('pay_time', [$startTime,$endTime])
  1028. ->sum('pay_money');
  1029. return $orderTotalProfit;
  1030. }
  1031. // profitDetail 收益详情 - 免押金卡收益
  1032. private function getDepositCardTotalProfit($area_id,$merchant_id,$startTime,$endTime,$depositCardOrderFilter)
  1033. {
  1034. // 管理员,并且选择了商户
  1035. if (Admin::isAdministrator() && $merchant_id) {
  1036. $orderTotalProfit = DepositCardOrder::query()->where('merchant_id',$merchant_id);
  1037. }else{
  1038. $orderTotalProfit = DepositCardOrder::query()->where(AdminMerchant::getMerchantWhere());
  1039. }
  1040. if (!empty($area_id)) {
  1041. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  1042. } else {
  1043. $admin_id = Admin::user()->id;
  1044. if (!Admin::isAdministrator()) {
  1045. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  1046. if (count($area_ids) !== 0) {
  1047. $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
  1048. } else {
  1049. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
  1050. $area_id = $area_id ?? 0;
  1051. $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
  1052. }
  1053. }
  1054. }
  1055. $orderTotalProfit = $orderTotalProfit
  1056. ->whereBetween('pay_time', [$startTime,$endTime])
  1057. ->filter($depositCardOrderFilter)
  1058. ->where('pay_status', DepositCardOrder::PAY_STATUS_OK)
  1059. ->sum('pay_money');
  1060. return $orderTotalProfit;
  1061. }
  1062. /**
  1063. * orderDetail 订单详情
  1064. *
  1065. * @param Request $request
  1066. * @return \Illuminate\Http\JsonResponse
  1067. * @author Fx
  1068. *
  1069. */
  1070. public function orderDetail(Request $request)
  1071. {
  1072. $order_id = $request->get('order_id') ?? '';
  1073. if (empty($order_id)) return $this->error('参数错误');
  1074. $order = Order::query()->where(AdminMerchant::getMerchantWhere())->where('id',$order_id)->first();
  1075. if (empty($order)) return $this->error('找不到该订单,参数错误');
  1076. $orderLocations = [];
  1077. $wx_orderLocations = [];
  1078. // 订单轨迹
  1079. $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();
  1080. if (!empty($locationsLog)) {
  1081. foreach ($locationsLog as $vv) {
  1082. $orderLocations[] = [$vv->longitude, $vv->latitude];
  1083. $wx_orderLocations[] = ['longitude' => $vv->longitude, 'latitude' => $vv->latitude];
  1084. }
  1085. }
  1086. // Log::info($orderLocations);
  1087. $data = [
  1088. 'order_status' => Order::$statusMaps[$order->status],
  1089. 'start_use_bike_time' => date('Y/m/d H:i:s', strtotime($order->start_use_bike_time)),
  1090. 'end_use_bike_time' => $order->end_use_bike_time ? date('Y/m/d H:i:s', strtotime($order->end_use_bike_time)) : '',
  1091. 'pay_money' => $order->pay_money,
  1092. 'dispatch_money' => $order->dispatch_money,
  1093. 'time_money' => $order->time_money,
  1094. 'bike_no' => $order->bike_no,
  1095. 'bike_id' => $order->bike_id,
  1096. 'user_id' => $order->user_id,
  1097. 'nickname' => $order->users->nickname ?? '',
  1098. 'mobile' => $order->users->mobile ?? '',
  1099. 'orderLocations' => $orderLocations,
  1100. 'start_location' => empty($orderLocations) ? [] : current($orderLocations),
  1101. 'end_location' => empty($orderLocations) ? [] : end($orderLocations),
  1102. 'wx_orderLocations' => $wx_orderLocations,
  1103. 'wx_start_location' => empty($wx_orderLocations) ? [] : current($wx_orderLocations),
  1104. 'wx_end_location' => empty($wx_orderLocations) ? [] : end($wx_orderLocations),
  1105. 'preferential_type_name' => $order->preferential_type_name, // 优惠方式
  1106. 'preferential_money' => $order->preferential_money,// 优惠金额
  1107. 'card_preferential_money' => $order->card_preferential_money,// 骑行卡优惠金额
  1108. 'coupon_preferential_money' => $order->coupon_preferential_money,// 优惠券优惠金额
  1109. 'is_coupon_name' => Order::$couponMaps[$order->is_coupon] ,// 优惠券优惠
  1110. 'walletLogs' => $order->walletLogs,
  1111. 'order_bike_operates' => $order->order_bike_operates,
  1112. 'remark' => $order->remark ?? '',
  1113. //'center_location' => GetCenterFromDegrees([['lat'=>$orderLocations[0][1],'lng'=>$orderLocations[0][0]],['lat'=>end($orderLocations)[1],'lng'=>end($orderLocations)[0]]])
  1114. ];
  1115. return $this->ok($data);
  1116. }
  1117. /**
  1118. * orderLocation 订单轨迹
  1119. *
  1120. * @param Request $request
  1121. * @return \Illuminate\Http\JsonResponse
  1122. * @author Fx
  1123. *
  1124. */
  1125. public function orderLocation(Request $request)
  1126. {
  1127. $order_id = $request->get('order_id') ?? '';
  1128. if (empty($order_id)) return $this->error('参数错误');
  1129. $orderLocations = [];
  1130. $locationsTimes = [];
  1131. try {
  1132. $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();
  1133. } catch (\Exception $exception) {
  1134. Log::info($exception->getMessage());
  1135. }
  1136. if (!empty($locationsLog)) {
  1137. foreach ($locationsLog as $vv) {
  1138. $orderLocations[] = [$vv->longitude, $vv->latitude];
  1139. $locationsTimes[] = Carbon::parse($vv->created_at)->format('Y-m-d H:i:s');
  1140. }
  1141. }
  1142. $data = [
  1143. 'orderLocations' => $orderLocations,
  1144. 'locationsTimes' => $locationsTimes,
  1145. ];
  1146. return $this->ok($data);
  1147. }
  1148. /**
  1149. * heatMap 热力图
  1150. *
  1151. * @param OrderFilter $orderFilter
  1152. * @return \Illuminate\Http\JsonResponse
  1153. * @author Fx
  1154. *
  1155. */
  1156. public function heatMap(OrderFilter $orderFilter)
  1157. {
  1158. $today = Carbon::today()->subMonth();
  1159. //whereIn('area_id',self::$areaIds)->
  1160. $order = Order::query()->filter($orderFilter)->where('created_at', '>', $today)->get();
  1161. $data1 = [];
  1162. $data2 = [];
  1163. if (!empty($order)) {
  1164. foreach ($order as $v) {
  1165. $location = json_decode($v->start_use_bike_location);
  1166. $location2 = json_decode($v->end_use_bike_location);
  1167. $data1[] = ['lat' => $location->latitude, 'lng' => $location->longitude, 'count' => 3];
  1168. if(!empty($location2)){
  1169. $data2[] = ['lat' => $location2->latitude, 'lng' => $location2->longitude, 'count' => 3];
  1170. }
  1171. }
  1172. }
  1173. $data = [
  1174. 'start' => $data1,
  1175. 'end' => $data2
  1176. ];
  1177. return $this->ok($data);
  1178. }
  1179. /**
  1180. * newOrderChart 新订单统计图
  1181. *
  1182. * @param Request $request
  1183. * @param OrderFilter $orderFilter
  1184. * @return array
  1185. * @author Fx
  1186. *
  1187. */
  1188. // public function newOrderChart(Request $request, OrderFilter $orderFilter)
  1189. // {
  1190. // $days = $request->get('days') ?? '';
  1191. // $areaId = $request->get('area_id') ?? '';
  1192. // if (empty($days) || empty($areaId)) return $this->error('缺少参数');
  1193. // if (empty($days)) return $this->error('缺少参数');
  1194. // $newOrders = Order::query()
  1195. // ->filter($orderFilter)
  1196. // ->where('status', Order::STATUS_COMPLETE_ORDER);
  1197. //
  1198. //// $admin_id = Admin::user()->id;
  1199. //// if (!Admin::isAdministrator()) {
  1200. //// $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  1201. //// if (count($area_ids) !== 0) {
  1202. //// $newOrders = $newOrders->whereIn('area_id', $area_ids);
  1203. //// } else {
  1204. //// $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  1205. //// $area_id = $area_id ?? 0;
  1206. //// $newOrders = $newOrders->where('area_id', $area_id);
  1207. //// }
  1208. //// }
  1209. //
  1210. // switch ($days) {
  1211. // case 'today':
  1212. // $today = Carbon::today();
  1213. // $newOrders = $newOrders->where('created_at', '>', $today)
  1214. // ->selectRaw('DATE_FORMAT(created_at,"%m-%d %H:00") as date,count(id) as value,sum(pay_money) as moneys')
  1215. // ->groupBy('date')->get()->toArray();
  1216. // // 有数据得数组
  1217. // $newOrdersKeyVal = [];
  1218. // if (!empty($newOrders)) {
  1219. // foreach ($newOrders as $v) {
  1220. // $newOrdersKeyVal[$v['date']] = $v['value'];
  1221. // }
  1222. // }
  1223. // // 为0得数组
  1224. // $i = Carbon::now()->format('H');
  1225. // $arr = [];
  1226. // for ($i; $i >= 0; $i--) {
  1227. // $str = Carbon::now()->subHours($i)->format('m-d H:00');
  1228. // $arr[$str] = 0;
  1229. // }
  1230. // //合并
  1231. // $merge = array_merge($arr, $newOrdersKeyVal);
  1232. // $data = [];
  1233. // foreach ($merge as $key=>$value){
  1234. // $data[]=['date'=>$key,'value'=>$value];
  1235. // }
  1236. //
  1237. // break;
  1238. // case 'threeDays':
  1239. // $i = 2;
  1240. // break;
  1241. // case 'sevenDays':
  1242. // $i = 6;
  1243. //
  1244. // break;
  1245. // case 'fifteenDays':
  1246. // $i = 14;
  1247. // break;
  1248. // case 'thirtyDays':
  1249. // $i = 29;
  1250. // break;
  1251. // default:
  1252. // return $this->error('参数错误');
  1253. //
  1254. // }
  1255. // if($days !== 'today'){
  1256. // $paramDays = Carbon::today()->subDays($i);
  1257. //
  1258. // // 赋值一个值为0得数组
  1259. // $arr = [];
  1260. // for ($i ; $i >= 0; $i--){
  1261. // $str = Carbon::today()->subDays($i)->format('Y/m/d');
  1262. // $arr[$str] = 0;
  1263. // }
  1264. //
  1265. // // 有数据得数组
  1266. // $newOrders = $newOrders->where('created_at', '>', $paramDays)
  1267. // ->selectRaw('DATE_FORMAT(created_at,"%Y/%m/%d") as date,count(id) as value,sum(pay_money) as moneys')
  1268. // ->groupBy('date')->get()->toArray();
  1269. // $newOrdersKeyVal = [];
  1270. // $newOrdersKeyValMoney = [];
  1271. // if(!empty($newOrders)){
  1272. // foreach ($newOrders as $v){
  1273. // $newOrdersKeyVal[$v['date']] = $v['value'];
  1274. // $newOrdersKeyValMoney[$v['date']] = $v['moneys'];
  1275. // }
  1276. // }
  1277. // // 合并覆盖0
  1278. // $merge = array_merge($arr,$newOrdersKeyVal);
  1279. // $mergemoney = array_merge($arr,$newOrdersKeyValMoney);
  1280. // // return $this->ok($merge);
  1281. //
  1282. // // 重组结构
  1283. // $data = [];
  1284. // foreach ($merge as $ks=>$vs){
  1285. // $data[]=['date'=>$ks,'value'=>$vs,'moneys'=>$mergemoney[$ks]];
  1286. // }
  1287. // }
  1288. //
  1289. // return $this->ok($data);
  1290. // }
  1291. public function orderChart($days, $orderFilter, $dispatch_money = false)
  1292. {
  1293. $newOrders = Order::query()
  1294. ->where(AdminMerchant::getMerchantWhere())
  1295. ->filter($orderFilter)
  1296. ->where('status', Order::STATUS_COMPLETE_ORDER);
  1297. if ($dispatch_money) {
  1298. $newOrders = $newOrders->where('dispatch_money', '>', 0);
  1299. }
  1300. $admin_id = Admin::user()->id;
  1301. if (!Admin::isAdministrator()) {
  1302. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  1303. if (count($area_ids) !== 0) {
  1304. $newOrders = $newOrders->whereIn('area_id', $area_ids);
  1305. } else {
  1306. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  1307. $area_id = $area_id ?? 0;
  1308. $newOrders = $newOrders->where('area_id', $area_id);
  1309. }
  1310. }
  1311. switch ($days) {
  1312. case 'today':
  1313. $today = Carbon::today();
  1314. $newOrders = $newOrders->where('created_at', '>', $today)
  1315. ->selectRaw('DATE_FORMAT(created_at,"%m-%d %H:00") as date,count(id) as value')
  1316. ->groupBy('date')->get()->toArray();
  1317. // 有数据得数组
  1318. $newOrdersKeyVal = [];
  1319. if (!empty($newOrders)) {
  1320. foreach ($newOrders as $v) {
  1321. $newOrdersKeyVal[$v['date']] = $v['value'];
  1322. }
  1323. }
  1324. // 为0得数组
  1325. $i = Carbon::now()->format('H');
  1326. $arr = [];
  1327. for ($i; $i >= 0; $i--) {
  1328. $str = Carbon::now()->subHours($i)->format('m-d H:00');
  1329. $arr[$str] = 0;
  1330. }
  1331. //合并
  1332. $merge = array_merge($arr, $newOrdersKeyVal);
  1333. $data = [];
  1334. foreach ($merge as $key => $value) {
  1335. $data[] = ['date' => $key, 'value' => $value];
  1336. }
  1337. break;
  1338. case 'threeDays':
  1339. $i = 2;
  1340. break;
  1341. case 'sevenDays':
  1342. $i = 6;
  1343. break;
  1344. case 'fifteenDays':
  1345. $i = 14;
  1346. break;
  1347. case 'thirtyDays':
  1348. $i = 29;
  1349. break;
  1350. default:
  1351. return [];
  1352. }
  1353. if ($days !== 'today') {
  1354. $paramDays = Carbon::today()->subDays($i);
  1355. // 赋值一个值为0得数组
  1356. $arr = [];
  1357. for ($i; $i >= 0; $i--) {
  1358. $str = Carbon::today()->subDays($i)->format('Y/m/d');
  1359. $arr[$str] = 0;
  1360. }
  1361. // 有数据得数组
  1362. $newOrders = $newOrders->where('created_at', '>', $paramDays)
  1363. ->selectRaw('DATE_FORMAT(created_at,"%Y/%m/%d") as date,count(id) as value')
  1364. ->groupBy('date')->get()->toArray();
  1365. $newOrdersKeyVal = [];
  1366. if (!empty($newOrders)) {
  1367. foreach ($newOrders as $v) {
  1368. $newOrdersKeyVal[$v['date']] = $v['value'];
  1369. }
  1370. }
  1371. // 合并覆盖0
  1372. $merge = array_merge($arr, $newOrdersKeyVal);
  1373. // 重组结构
  1374. $data = [];
  1375. foreach ($merge as $ks => $vs) {
  1376. $data[] = ['date' => $ks, 'value' => $vs];
  1377. }
  1378. }
  1379. return $data;
  1380. }
  1381. public function newOrderChart(Request $request, OrderFilter $orderFilter)
  1382. {
  1383. $days = $request->get('days') ?? '';
  1384. if (empty($days)) return $this->error('缺少参数');
  1385. $newOrderChart = $this->orderChart($days, $orderFilter);
  1386. $dispatchMoneyNewOrderChart = $this->orderChart($days, $orderFilter, true);
  1387. return $this->ok([
  1388. [
  1389. 'name' => '总订单数',
  1390. 'data' => $newOrderChart
  1391. ], [
  1392. 'name' => '有调度费的订单数',
  1393. 'data' => $dispatchMoneyNewOrderChart
  1394. ],
  1395. ]);
  1396. // return $this->ok([
  1397. // $newOrderChart,$dispatchMoneyNewOrderChart
  1398. // ]);
  1399. }
  1400. public function profitPolygonalChart($days, $filter, $model, $money = 'pay_money')
  1401. {
  1402. if (empty($days)) return $this->error('缺少参数');
  1403. $newOrders = $model
  1404. ->filter($filter)
  1405. ->where('pay_status', 1);
  1406. $admin_id = Admin::user()->id;
  1407. if (!Admin::isAdministrator()) {
  1408. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  1409. if (count($area_ids) !== 0) {
  1410. $newOrders = $newOrders->whereIn('area_id', $area_ids);
  1411. } else {
  1412. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  1413. $area_id = $area_id ?? 0;
  1414. $newOrders = $newOrders->where('area_id', $area_id);
  1415. }
  1416. }
  1417. switch ($days) {
  1418. case 'today':
  1419. $today = Carbon::today();
  1420. $newOrders = $newOrders->where('pay_time', '>', $today)
  1421. ->selectRaw("DATE_FORMAT(pay_time,'%m-%d %H:00') as date,sum($money) as value")
  1422. ->groupBy('date')->get()->toArray();
  1423. // 有数据得数组
  1424. $newOrdersKeyVal = [];
  1425. if (!empty($newOrders)) {
  1426. foreach ($newOrders as $v) {
  1427. $newOrdersKeyVal[$v['date']] = $v['value'];
  1428. }
  1429. }
  1430. // 为0得数组
  1431. $i = Carbon::now()->format('H');
  1432. $arr = [];
  1433. for ($i; $i >= 0; $i--) {
  1434. $str = Carbon::now()->subHours($i)->format('m-d H:00');
  1435. $arr[$str] = 0;
  1436. }
  1437. //合并
  1438. $merge = array_merge($arr, $newOrdersKeyVal);
  1439. $data = [];
  1440. foreach ($merge as $key => $value) {
  1441. $data[] = ['date' => $key, 'value' => $value];
  1442. }
  1443. break;
  1444. case 'threeDays':
  1445. $i = 2;
  1446. break;
  1447. case 'sevenDays':
  1448. $i = 6;
  1449. break;
  1450. case 'fifteenDays':
  1451. $i = 14;
  1452. break;
  1453. case 'thirtyDays':
  1454. $i = 29;
  1455. break;
  1456. default:
  1457. return $this->error('参数错误');
  1458. }
  1459. if ($days !== 'today') {
  1460. $paramDays = Carbon::today()->subDays($i);
  1461. // 赋值一个值为0得数组
  1462. $arr = [];
  1463. for ($i; $i >= 0; $i--) {
  1464. $str = Carbon::today()->subDays($i)->format('Y/m/d');
  1465. $arr[$str] = 0;
  1466. }
  1467. // 有数据得数组
  1468. $newOrders = $newOrders->where('pay_time', '>', $paramDays)
  1469. ->selectRaw("DATE_FORMAT(pay_time,'%Y/%m/%d') as date,sum($money) as value")
  1470. ->groupBy('date')->get()->toArray();
  1471. $newOrdersKeyVal = [];
  1472. if (!empty($newOrders)) {
  1473. foreach ($newOrders as $v) {
  1474. $newOrdersKeyVal[$v['date']] = $v['value'];
  1475. }
  1476. }
  1477. // 合并覆盖0
  1478. $merge = array_merge($arr, $newOrdersKeyVal);
  1479. // 重组结构
  1480. $data = [];
  1481. foreach ($merge as $ks => $vs) {
  1482. $data[] = ['date' => $ks, 'value' => $vs];
  1483. }
  1484. }
  1485. return $data;
  1486. }
  1487. public function profitChart(Request $request, OrderFilter $orderFilter, RechargeOrderFilter $rechargeOrderFilter, CardRidingOrderFilter $cardRidingOrderFilter, DepositCardOrderFilter $depositCardOrderFilter)
  1488. {
  1489. $days = $request->get('days') ?? '';
  1490. $ordersChart = $this->profitPolygonalChart($days, $orderFilter, Order::query()->where(AdminMerchant::getMerchantWhere()));
  1491. $ordersDispatchMoneyChart = $this->profitPolygonalChart($days, $orderFilter, Order::query()->where(AdminMerchant::getMerchantWhere()), 'dispatch_money');
  1492. $rechargeOrdersChart = $this->profitPolygonalChart($days, $rechargeOrderFilter, RechargeOrder::query()->where(AdminMerchant::getMerchantWhere()));
  1493. $cardRidingOrdersChart = $this->profitPolygonalChart($days, $cardRidingOrderFilter, CardRidingOrder::query()->where(AdminMerchant::getMerchantWhere()));
  1494. $depositCardOrdersChart = $this->profitPolygonalChart($days, $depositCardOrderFilter, DepositCardOrder::query()->where(AdminMerchant::getMerchantWhere()));
  1495. $total = [];
  1496. foreach ($ordersChart as $k => $v) {
  1497. $total[] = [
  1498. 'date' => $v['date'],
  1499. 'value' => bcadd(bcadd(bcadd($v['value'], $rechargeOrdersChart[$k]['value'], 2), $cardRidingOrdersChart[$k]['value'], 2), $depositCardOrdersChart[$k]['value'], 2)
  1500. ];
  1501. }
  1502. if (Admin::isNormalAdministrator() || Admin::isAdministrator()){
  1503. return $this->ok([
  1504. [
  1505. 'name' => '普通订单收益',
  1506. 'data' => $ordersChart
  1507. ],
  1508. [
  1509. 'name' => '订单调度费收益',
  1510. 'data' => $ordersDispatchMoneyChart
  1511. ],
  1512. [
  1513. 'name' => '充值收益',
  1514. 'data' => $rechargeOrdersChart
  1515. ],
  1516. [
  1517. 'name' => '骑行卡收益',
  1518. 'data' => $cardRidingOrdersChart
  1519. ],
  1520. [
  1521. 'name' => '免押金卡收益',
  1522. 'data' => $depositCardOrdersChart
  1523. ],
  1524. [
  1525. 'name' => '总收益',
  1526. 'data' => $total
  1527. ],
  1528. ]);
  1529. }else{
  1530. return $this->ok([]);
  1531. }
  1532. }
  1533. /**
  1534. * 前七天小时订单统计
  1535. * @return \Illuminate\Http\JsonResponse
  1536. * User: Mead
  1537. */
  1538. public function hourOrderNumber(Request $request,OrderFilter $filter)
  1539. {
  1540. $day = [
  1541. ["num" => 0, "hour" => 0],
  1542. ["num" => 0, "hour" => 1],
  1543. ["num" => 0, "hour" => 2],
  1544. ["num" => 0, "hour" => 3],
  1545. ["num" => 0, "hour" => 4],
  1546. ["num" => 0, "hour" => 5],
  1547. ["num" => 0, "hour" => 6],
  1548. ["num" => 0, "hour" => 7],
  1549. ["num" => 0, "hour" => 8],
  1550. ["num" => 0, "hour" => 9],
  1551. ["num" => 0, "hour" => 10],
  1552. ["num" => 0, "hour" => 11],
  1553. ["num" => 0, "hour" => 12],
  1554. ["num" => 0, "hour" => 13],
  1555. ["num" => 0, "hour" => 14],
  1556. ["num" => 0, "hour" => 15],
  1557. ["num" => 0, "hour" => 16],
  1558. ["num" => 0, "hour" => 17],
  1559. ["num" => 0, "hour" => 18],
  1560. ["num" => 0, "hour" => 19],
  1561. ["num" => 0, "hour" => 20],
  1562. ["num" => 0, "hour" => 21],
  1563. ["num" => 0, "hour" => 22],
  1564. ["num" => 0, "hour" => 23]
  1565. ];
  1566. $weeks = [];
  1567. $area_ids = self::$areaIds;
  1568. for ($i = 1, $c = 7; $i < 7; $i++, $c--) {
  1569. $now = Carbon::parse("-{$i}day");
  1570. $date = $now->toDateString();
  1571. $key = "{$date}-hour-order-num";
  1572. $area_id = $request->get('area_id') ?? '';
  1573. if(!empty($area_id)){
  1574. $key = $key .'-area_id:'. $area_id;
  1575. }
  1576. // Cache::forget($key);
  1577. $weeks["{$date}"] = Cache::remember($key, Carbon::parse("+{$c}day")->diffInMinutes(Carbon::now()), function () use ($date, $day,$filter,$area_ids) {
  1578. return array_replace($day, Order::query()->where(AdminMerchant::getMerchantWhere())->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) {
  1579. $arr = [
  1580. 'num' => (integer)$item->num,
  1581. 'hour' => (integer)$item->hour,
  1582. ];
  1583. return [(integer)$item->hour => $arr];
  1584. })->toArray());
  1585. });
  1586. }
  1587. $data = [
  1588. 'date' => [],
  1589. 'data' => []
  1590. ];
  1591. foreach ($weeks as $k => $v) {
  1592. $data['date'][] = $k;
  1593. $data['data'][$k] = [];
  1594. foreach ($v as $value) {
  1595. array_push($data['data'][$k], $value['num']);
  1596. }
  1597. }
  1598. // dd($data);
  1599. return $this->ok($data);
  1600. }
  1601. }