SubstituteOrderService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2018/10/17
  6. * Time: 16:08
  7. */
  8. namespace App\Modules\Subscribe\Services;
  9. use App\Modules\Subscribe\Models\SubstituteOrder;
  10. use App\Modules\User\Services\UserService;
  11. use DB;
  12. use Redis;
  13. class SubstituteOrderService
  14. {
  15. public static function createOrder(int $order_id, int $uid, int $pay_uid)
  16. {
  17. if (!self::getByOrderId($order_id)) {
  18. $date = date('Y-m-d');
  19. return SubstituteOrder::create(compact('order_id', 'uid', 'pay_uid', 'date'));
  20. }
  21. return null;
  22. }
  23. public static function getByOrderId(int $order_id)
  24. {
  25. return SubstituteOrder::where('order_id', $order_id)
  26. ->select('id', 'order_id', 'uid', 'pay_uid', 'created_at', 'updated_at', 'is_prize', 'prize_amount')
  27. ->first();
  28. }
  29. public static function SubstituteOrderPrize(int $order_id, int $amount)
  30. {
  31. $res = self::getByOrderId($order_id);
  32. if ($res && $res->is_prize == 0) {
  33. SubstituteOrder::where('id', $res->id)->update([
  34. 'is_prize' => 1,
  35. 'prize_amount' => $amount
  36. ]);
  37. UserService::addBalance($res->pay_uid, $amount, 0, $amount);
  38. return true;
  39. }
  40. return false;
  41. }
  42. public static function substituteButtonUvPv(int $uid, int $distribution_channel_id)
  43. {
  44. $day = date('Y-m-d');
  45. try {
  46. Redis::HINCRBY('substitutebuttonpvuv:distribution_channel_id:' . $distribution_channel_id, $day, 1);
  47. Redis::sadd(sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day), $uid);
  48. Redis::sadd('substitutebuttonpvuv' . $day, $distribution_channel_id);
  49. } catch (\Exception $e) {
  50. }
  51. }
  52. public static function getSubstituteButtonUvPv($distribution_channel_id, $day)
  53. {
  54. return Redis::scard(sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day));
  55. }
  56. public static function getSubstitutePageUvPv($distribution_channel_id, $day)
  57. {
  58. return Redis::scard(sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day));
  59. }
  60. public static function substitutePageUvPv(int $uid, int $distribution_channel_id)
  61. {
  62. $day = date('Y-m-d');
  63. try {
  64. Redis::HINCRBY('substitutepagepvuv:distribution_channel_id:' . $distribution_channel_id, $day, 1);
  65. Redis::sadd(sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s', $distribution_channel_id, $day), $uid);
  66. Redis::sadd('substitutepagepvuv' . $day, $distribution_channel_id);
  67. } catch (\Exception $e) {
  68. }
  69. }
  70. public static function getSubstituteOrders(int $distribution_channel_id, $start_time = '', $end_time = '')
  71. {
  72. $result = SubstituteOrder::join('orders', 'orders.id', '=', 'substitute_orders.order_id')
  73. ->select('orders.price', 'orders.status', 'orders.pay_end_at')
  74. ->where('orders.distribution_channel_id', $distribution_channel_id);
  75. if ($start_time) {
  76. $result->where('substitute_orders.created_at', '>=', $start_time);
  77. }
  78. if ($end_time) {
  79. $result->where('substitute_orders.created_at', '<', $start_time);
  80. }
  81. return $result->orderBy('substitute_orders.id', 'desc')->paginate(20);
  82. }
  83. public static function substituteStats($isAll, string $start_date, string $end_date, $distribution_channel_id = '')
  84. {
  85. $search_obj = SubstituteOrder::join('orders', 'orders.id', '=', 'substitute_orders.order_id');
  86. if ($distribution_channel_id) {
  87. $search_obj->where('orders.distribution_channel_id', $distribution_channel_id);
  88. };
  89. $search_obj = $search_obj->whereBetween('substitute_orders.date', [$start_date, $end_date])
  90. ->select(
  91. 'substitute_orders.date',
  92. 'orders.distribution_channel_id',
  93. DB::raw('count(*) as num'),//代付订单
  94. DB::raw('count(distinct case when orders.status="PAID" then substitute_orders.pay_uid else null end) as users_num'),//代付人数
  95. DB::raw('count(case when orders.status="PAID" then substitute_orders.id else null end) as success_num'),//成功订单数
  96. DB::raw('sum(case when orders.status="PAID" then orders.price else null end) as amount')//代付金额
  97. )
  98. ->groupBy('substitute_orders.date')
  99. ->groupBy('orders.distribution_channel_id')
  100. ->orderBy('substitute_orders.date', 'desc');
  101. if ($isAll) {
  102. return $search_obj->get();
  103. } else {
  104. return $search_obj->paginate();
  105. }
  106. }
  107. }