OrderService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Services\Order;
  3. use App\Consts\BaseConst;
  4. use App\Consts\ErrorConst;
  5. use App\Dao\Order\OrderDao;
  6. use App\Facade\Site;
  7. use App\Models\Order\Order;
  8. use App\Libs\Utils;
  9. use App\Services\OpenApi\OpenService;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Redis;
  14. use Illuminate\Support\Facades\Response;
  15. use Vinkla\Hashids\Facades\Hashids;
  16. use App\Exceptions\ApiException;
  17. class OrderService
  18. {
  19. protected $orderDao;
  20. protected $openService;
  21. public function __construct(
  22. OrderDao $orderDao,
  23. OpenService $openService
  24. )
  25. {
  26. $this->orderDao = $orderDao;
  27. $this->openService = $openService;
  28. }
  29. /**
  30. * @param $data
  31. * @return mixed
  32. * @throws ApiException
  33. */
  34. public function channelList($data)
  35. {
  36. $channel_id = Site::getChannelId();
  37. $result = DB::table('distribution_channels as dc')->leftJoin('channel_users as cu', 'cu.id', 'dc.channel_user_id')->whereIn('dc.id', $channel_id)
  38. ->select('dc.id as channel_id', 'dc.channel_type', 'cu.account', 'cu.nickname as account_nickname')
  39. ->orderBy('dc.id')->get()->map(function ($value) {
  40. return (array)$value;
  41. })->toArray();
  42. foreach ($result as &$item) {
  43. $item['channel_type'] = $item['channel_type'] == 'PERIOD' ? '会员制' : '充送制';
  44. }
  45. return $result;
  46. }
  47. /**
  48. * @param $data
  49. * @return mixed
  50. */
  51. public function userList($data) {
  52. $channel_id = Site::getChannelId();
  53. $uid = getProp($data, 'uid');
  54. $param_channel_id = getProp($data, 'channel_id');
  55. $send_order_id = getProp($data, 'send_order_id');
  56. $date_range = getProp($data, 'register_time');
  57. $query = DB::table('users as u')
  58. ->whereIn('u.distribution_channel_id', $channel_id)->where('u.is_lock', 0)
  59. ->select('u.*', DB::raw("(select created_at from channel_copy_user_mappings where uid = u.id order by created_at desc limit 1) as bind_send_order_time"));
  60. //用户id筛选
  61. if ($uid) {
  62. $query->where('u.id', $uid);
  63. }
  64. //站点id筛选
  65. if ($param_channel_id) {
  66. $query->where('u.distribution_channel_id', $param_channel_id);
  67. }
  68. //派单id筛选
  69. if ($send_order_id) {
  70. $query->where('u.send_order_id', $send_order_id);
  71. }
  72. //时间周期筛选
  73. if ($date_range) {
  74. $date_range = explode(',', $date_range);
  75. if (count($date_range) == 2) {
  76. if ($date_range[0]) {
  77. $start = date('Y-m-d 00:00:00', strtotime($date_range[0]));
  78. $query->where('u.created_at', '>=', $start);
  79. }
  80. if ($date_range[1]) {
  81. $end = date('Y-m-d 23:59:59', strtotime($date_range[1]));
  82. $query->where('u.created_at', '<=', $end);
  83. }
  84. }
  85. }
  86. return $query->paginate(1000);
  87. }
  88. /**
  89. * @param $data
  90. * @return mixed
  91. */
  92. public function sendOrderList($data) {
  93. $channel_id = Site::getChannelId();
  94. $param_channel_id = getProp($data, 'channel_id');
  95. $send_order_id = getProp($data, 'send_order_id');
  96. $query = DB::table('send_orders')->whereIn('distribution_channel_id', $channel_id)->where('is_enable', 1);
  97. //站点筛选
  98. if ($param_channel_id) {
  99. $query->where('distribution_channel_id', $param_channel_id);
  100. }
  101. //派单id筛选
  102. if ($send_order_id) {
  103. $query->where('id', $send_order_id);
  104. }
  105. return $query->paginate(1000);
  106. }
  107. // 订单列表
  108. public function orderList($data)
  109. {
  110. $channel_id = Site::getChannelId(); // 绑定的权限站点id集
  111. $uid = getProp($data, 'uid'); // 用户id
  112. $send_order_id = getProp($data, 'send_order_id'); // 派单id
  113. $promotionid = getProp($data, 'promotion_id'); // 广告id
  114. $param_channel_id = getProp($data, 'channel_id'); // 站点id
  115. $register_date_range = getProp($data, 'register_time'); // 注册时间
  116. $pay_date_range = getProp($data, 'pay_time'); // 支付时间
  117. // 列表
  118. $query = DB::table('orders')->leftJoin('users', 'users.id', 'orders.uid')
  119. ->leftJoin('dy_report_logs', 'dy_report_logs.trade_no', 'orders.trade_no')
  120. ->leftJoin('send_orders', 'send_orders.id', 'orders.send_order_id')
  121. ->leftJoin('channel_copy_user_mappings', 'channel_copy_user_mappings.trade_no', 'orders.trade_no')
  122. ->leftJoin('book_configs', 'book_configs.bid', 'orders.from_bid')
  123. ->whereIn('send_orders.distribution_channel_id', $channel_id)
  124. ->where('orders.status', '<>', 'UNPAID')
  125. ->select('orders.id', 'orders.trade_no', 'orders.uid', 'users.created_at as register_time', 'channel_copy_user_mappings.created_at as bind_send_order_time',
  126. 'orders.price', 'orders.status', 'orders.created_at', 'orders.pay_end_at as pay_time', 'send_orders.distribution_channel_id', 'dy_report_logs.id as report_id',
  127. 'orders.send_order_id', 'dy_report_logs.advertiserid', 'dy_report_logs.promotionid', 'dy_report_logs.clickid', 'dy_report_logs.callback_result', 'orders.pay_num',
  128. 'dy_report_logs.remark', 'dy_report_logs.updated_at as report_time', 'book_configs.book_name', 'orders.order_type', 'orders.transaction_id');
  129. if ($uid) {
  130. $query->where('orders.uid', $uid);
  131. }
  132. if ($send_order_id) {
  133. $query->where('orders.send_order_id', $send_order_id);
  134. }
  135. if ($promotionid) {
  136. $query->where('dy_report_logs.promotionid', $promotionid);
  137. }
  138. if ($param_channel_id) {
  139. $query->where('send_orders.distribution_channel_id', $param_channel_id);
  140. }
  141. // 注册时间周期筛选
  142. if ($register_date_range) {
  143. $date_range = explode(',', $register_date_range);
  144. if (count($date_range) == 2) {
  145. if ($date_range[0]) {
  146. $start = date('Y-m-d 00:00:00', strtotime($date_range[0]));
  147. $query->where('users.created_at', '>=', $start);
  148. }
  149. if ($date_range[1]) {
  150. $end = date('Y-m-d 23:59:59', strtotime($date_range[1]));
  151. $query->where('users.created_at', '<=', $end);
  152. }
  153. }
  154. }
  155. // 支付时间周期筛选
  156. if ($pay_date_range) {
  157. $date_range = explode(',', $pay_date_range);
  158. if (count($date_range) == 2) {
  159. if ($date_range[0]) {
  160. $start = date('Y-m-d 00:00:00', strtotime($date_range[0]));
  161. $query->where('orders.pay_end_at', '>=', $start);
  162. }
  163. if ($date_range[1]) {
  164. $end = date('Y-m-d 23:59:59', strtotime($date_range[1]));
  165. $query->where('orders.pay_end_at', '<=', $end);
  166. }
  167. }
  168. }
  169. // 获取分页数据
  170. return $query->orderBy('orders.pay_end_at', 'desc')->orderBy('users.created_at', 'desc')->paginate();
  171. }
  172. }