OrderService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace App\Modules\Order\Services;
  3. use App\Modules\Order\Models\ChannelOrderStatistic;
  4. use App\Modules\Order\Models\ChannelOrderStatisticDate;
  5. use App\Modules\Order\Models\ChannelSendOrderStatistic;
  6. use App\Modules\Order\Models\ChannelSendOrderStatisticDate;
  7. use App\Modules\Trade\Models\Order;
  8. use App\Modules\User\Models\User;
  9. use DB;
  10. use Illuminate\Support\Collection;
  11. class OrderService
  12. {
  13. /**
  14. * 增加派单统计金额
  15. */
  16. public function increaseSendOrderAmount(string $date, int $channel_id, int $send_order_id, float $amount, string $pay_time)
  17. {
  18. $date_statistic = ChannelSendOrderStatisticDate::where([
  19. 'date' => $date,
  20. 'channel_id' => $channel_id,
  21. 'send_order_id' => $send_order_id,
  22. ])->first();
  23. if ($date_statistic) {
  24. $date_statistic->amount += $amount;
  25. $date_statistic->save();
  26. } else {
  27. ChannelSendOrderStatisticDate::create([
  28. 'date' => $date,
  29. 'channel_id' => $channel_id,
  30. 'send_order_id' => $send_order_id,
  31. 'amount' => $amount,
  32. ]);
  33. }
  34. $statistic = ChannelSendOrderStatistic::where([
  35. 'channel_id' => $channel_id,
  36. 'send_order_id' => $send_order_id,
  37. ])->first();
  38. if ($statistic) {
  39. $statistic->amount += $amount;
  40. $statistic->save();
  41. } else {
  42. ChannelSendOrderStatistic::create([
  43. 'channel_id' => $channel_id,
  44. 'send_order_id' => $send_order_id,
  45. 'first_pay_time' => $pay_time,
  46. 'amount' => $amount,
  47. ]);
  48. }
  49. }
  50. /**
  51. * 增加派单用户统计金额
  52. */
  53. public function increaseUserSendOrderAmount(string $date, int $channel_id, int $send_order_id, float $amount, string $pay_time)
  54. {
  55. $date_statistic = ChannelSendOrderStatisticDate::where([
  56. 'date' => $date,
  57. 'channel_id' => $channel_id,
  58. 'send_order_id' => $send_order_id,
  59. ])->first();
  60. if ($date_statistic) {
  61. $date_statistic->user_amount += $amount;
  62. $date_statistic->save();
  63. } else {
  64. ChannelSendOrderStatisticDate::create([
  65. 'date' => $date,
  66. 'channel_id' => $channel_id,
  67. 'send_order_id' => $send_order_id,
  68. 'user_amount' => $amount,
  69. ]);
  70. }
  71. $statistic = ChannelSendOrderStatistic::where([
  72. 'channel_id' => $channel_id,
  73. 'send_order_id' => $send_order_id,
  74. ])->first();
  75. if ($statistic) {
  76. $statistic->user_amount += $amount;
  77. $statistic->save();
  78. } else {
  79. ChannelSendOrderStatistic::create([
  80. 'channel_id' => $channel_id,
  81. 'send_order_id' => $send_order_id,
  82. 'first_pay_time' => $pay_time,
  83. 'user_amount' => $amount,
  84. ]);
  85. }
  86. }
  87. /**
  88. * 增加站点统计金额
  89. */
  90. public function increaseChannelAmount(string $date, int $channel_id, float $amount)
  91. {
  92. $date_statistic = ChannelOrderStatisticDate::where([
  93. 'date' => $date,
  94. 'channel_id' => $channel_id,
  95. ])->first();
  96. if ($date_statistic) {
  97. $date_statistic->amount += $amount;
  98. $date_statistic->save();
  99. } else {
  100. ChannelOrderStatisticDate::create([
  101. 'date' => $date,
  102. 'channel_id' => $channel_id,
  103. 'amount' => $amount,
  104. ]);
  105. }
  106. $statistic = ChannelOrderStatistic::where('channel_id', $channel_id)->first();
  107. if ($statistic) {
  108. $statistic->amount += $amount;
  109. $statistic->save();
  110. } else {
  111. ChannelOrderStatistic::create([
  112. 'channel_id' => $channel_id,
  113. 'amount' => $amount,
  114. ]);
  115. }
  116. }
  117. /**
  118. * 查找当日订单
  119. */
  120. private function findOrders(string $date)
  121. {
  122. return Order::where([
  123. ['status', '=', 'PAID'],
  124. ['created_at', '>=', $date],
  125. ['created_at', '<=', $date . ' 23:59:59'],
  126. ])
  127. ->select('send_order_id', 'distribution_channel_id', 'created_at', 'uid', 'price')
  128. ->get();
  129. }
  130. /**
  131. * @param string $startTime
  132. * @param string $endTime
  133. * @return array
  134. */
  135. public function getSendOrdersByDate(string $startTime, string $endTime): array
  136. {
  137. $result = Order::where([
  138. ['created_at', '>=', $startTime],
  139. ['created_at', '<=', $endTime],
  140. ['send_order_id', '>', 0],
  141. // ['status', '=', 'PAID'],
  142. ])->get();
  143. return $result ? $result->toArray() : [];
  144. }
  145. /**
  146. * 查找用户
  147. */
  148. private function findUsers(array $uids)
  149. {
  150. return User::whereIn('id', $uids)->select('id', 'send_order_id')->get();
  151. }
  152. /**
  153. * 重算站点充值金额
  154. */
  155. private function reCalcChannelOrderAmount(string $date, int $channel_id, float $amount)
  156. {
  157. ChannelOrderStatisticDate::updateOrCreate([
  158. 'date' => $date,
  159. 'channel_id' => $channel_id,
  160. ], [
  161. 'amount' => $amount,
  162. ]);
  163. $amount = ChannelOrderStatisticDate::where('channel_id', $channel_id)->sum('amount');
  164. ChannelOrderStatistic::updateOrCreate([
  165. 'channel_id' => $channel_id,
  166. ], [
  167. 'amount' => $amount,
  168. ]);
  169. }
  170. /**
  171. * 重算派单用户充值金额
  172. */
  173. private function reCalcUserSendOrderAmount(string $date, int $channel_id, Collection $orders)
  174. {
  175. $uids = $orders->pluck('uid')->all();
  176. $users = $this->findUsers($uids);
  177. $send_order_users = $users->groupBy('send_order_id')->toArray();
  178. foreach ($send_order_users as $send_order_id => $item) {
  179. $uids = collect($item)->pluck('id')->all();
  180. $amount = $orders->whereIn('uid', $uids)->sum('price');
  181. ChannelSendOrderStatisticDate::updateOrCreate([
  182. 'date' => $date,
  183. 'channel_id' => $channel_id,
  184. 'send_order_id' => $send_order_id,
  185. ], [
  186. 'user_amount' => $amount,
  187. ]);
  188. $amount = ChannelSendOrderStatisticDate::where('send_order_id', $send_order_id)->sum('user_amount');
  189. ChannelSendOrderStatistic::updateOrCreate([
  190. 'channel_id' => $channel_id,
  191. 'send_order_id' => $send_order_id,
  192. ], [
  193. 'user_amount' => $amount,
  194. ]);
  195. }
  196. }
  197. /**
  198. * 重算派单充值金额
  199. */
  200. private function reCalcSendOrderAmount(string $date, int $channel_id, Collection $orders)
  201. {
  202. $send_order_users = $orders->groupBy('send_order_id')->toArray();
  203. foreach ($send_order_users as $send_order_id => $item) {
  204. $amount = collect($item)->sum('price');
  205. ChannelSendOrderStatisticDate::updateOrCreate([
  206. 'date' => $date,
  207. 'channel_id' => $channel_id,
  208. 'send_order_id' => $send_order_id,
  209. ], [
  210. 'amount' => $amount,
  211. ]);
  212. $amount = ChannelSendOrderStatisticDate::where('send_order_id', $send_order_id)->sum('amount');
  213. $first_pay_time = Order::where(['send_order_id' => $send_order_id, 'status' => 'PAID'])->min('created_at');
  214. ChannelSendOrderStatistic::updateOrCreate([
  215. 'channel_id' => $channel_id,
  216. 'send_order_id' => $send_order_id,
  217. ], [
  218. 'amount' => $amount,
  219. 'first_pay_time' => $first_pay_time,
  220. ]);
  221. }
  222. }
  223. public function reCalcOrderStatisticAmount(string $date)
  224. {
  225. $orders = $this->findOrders($date);
  226. $channel_orders = $orders->groupBy('distribution_channel_id')->toArray();
  227. foreach ($channel_orders as $channel_id => $channel_order) {
  228. $colllection = collect($channel_order);
  229. $amount = $colllection->sum('price');
  230. $this->reCalcChannelOrderAmount($date, $channel_id, $amount);
  231. $this->reCalcSendOrderAmount($date, $channel_id, $colllection);
  232. $this->reCalcUserSendOrderAmount($date, $channel_id, $colllection);
  233. }
  234. }
  235. #region 派单按用户注册统计
  236. /**
  237. * 获取时间段内的派单号
  238. * @param $start
  239. * @param $end
  240. * @return array
  241. */
  242. public function getSendOrderIdByTime($start, $end): array
  243. {
  244. $order = new Order();
  245. $result = $order->getSendOrderIdByTime($start, $end);
  246. return $result;
  247. }
  248. /**
  249. * 获取时间段内派单首充数,和首充金额
  250. * @param $start
  251. * @param $end
  252. * @return array
  253. */
  254. public function getSendOrderFirstPayCountAndPriceByTime($start, $end): array
  255. {
  256. $order = new Order();
  257. $result = $order->getSendOrderFirstPayCountAndPriceByTime($start, $end);
  258. return $result;
  259. }
  260. /**
  261. * 根据派单ID获取截止时间点前派单首充数,和首充金额
  262. * @param $send_order_id
  263. * @param $end
  264. * @return array
  265. */
  266. public function getSendOrderFirstPayCountAndPriceByID($send_order_id, $end): array
  267. {
  268. $order = new Order();
  269. $result = $order->getSendOrderFirstPayCountAndPriceByID($send_order_id, $end);
  270. return $result;
  271. }
  272. /**
  273. * 获取时间段内派单充值数和总金额
  274. * @param $start
  275. * @param $end
  276. * @return array
  277. */
  278. public function getSendOrderSuccessPayCountByTime($start, $end): array
  279. {
  280. $order = new Order();
  281. $result = $order->getSendOrderSuccessPayCountByTime($start, $end);
  282. return $result;
  283. }
  284. /**
  285. * 根据派单ID获取时间段内派单充值数和总金额
  286. * @param $send_order_id
  287. * @param $end
  288. * @return array
  289. */
  290. public function getSendOrderSuccessPayCountByID($send_order_id, $end): array
  291. {
  292. $order = new Order();
  293. $result = $order->getSendOrderSuccessPayCountByID($send_order_id, $end);
  294. return $result;
  295. }
  296. /**
  297. * 获取时间段内派单付费人数
  298. * @param $start
  299. * @param $end
  300. * @return array
  301. */
  302. public function getSendOrderSuccessPayUserCountByTime($start, $end): array
  303. {
  304. $order = new Order();
  305. $result = $order->getSendOrderSuccessPayUserCountByTime($start, $end);
  306. return $result;
  307. }
  308. /**
  309. * 获取时间段内派单N小时充值金额
  310. * @param $start
  311. * @param $end
  312. * @param $hour
  313. * @return array
  314. */
  315. public function getSendOrderPayPriceByHour($start, $end, $hour): array
  316. {
  317. $order = new Order();
  318. $result = $order->getSendOrderPayPriceByHour($start, $end, $hour);
  319. return $result;
  320. }
  321. /**
  322. * 获取时间段内派单N小时充值金额
  323. * @param $send_order_id
  324. * @param $end
  325. * @param $hour
  326. * @return array
  327. */
  328. public function getSendOrderPayPriceByIdAndHour($send_order_id, $end, $hour): array
  329. {
  330. $order = new Order();
  331. $result = $order->getSendOrderPayPriceByIdAndHour($send_order_id, $end, $hour);
  332. return $result;
  333. }
  334. /**
  335. * 获取时间段内派单N小时(首充或非首充33)用户数
  336. * @param $start
  337. * @param $end
  338. * @param $hour
  339. * @param int $first_pay_type 首充类型 -1全部 0非首充 1首充
  340. * @return array
  341. */
  342. public function getSendOrderPayUserCountByHour($start, $end, $hour, $first_pay_type =-1): array
  343. {
  344. $order = new Order();
  345. $result = $order->getSendOrderPayUserCountByHour($start, $end, $hour, $first_pay_type);
  346. return $result;
  347. }
  348. /**
  349. * 获取时间段内派单N小时(首充或非首充33)用户数
  350. * @param $send_order_id
  351. * @param $end
  352. * @param $hour
  353. * @param int $first_pay_type 首充类型 -1全部 0非首充 1首充
  354. * @return array
  355. */
  356. public function getSendOrderPayUserCountByIdAndHour($send_order_id, $end, $hour, $first_pay_type =-1): array
  357. {
  358. $order = new Order();
  359. $result = $order->getSendOrderPayUserCountByIdAndHour($send_order_id, $end, $hour, $first_pay_type);
  360. return $result;
  361. }
  362. #endregion
  363. }