OrderService.php 13 KB

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