OrderService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Modules\Subscribe\Services;
  3. use App\Modules\Subscribe\Models\Order;
  4. use DB;
  5. class OrderService
  6. {
  7. static function getByTradeNo($trade_no)
  8. {
  9. return Order::where('trade_no',$trade_no)->first();
  10. }
  11. public static function getOrderList($uid,$page_size){
  12. return Order::select('id','price','created_at','status','trade_no')->where('uid',$uid)->where('from_type','!=','order_add')->orderBy('created_at','desc')->paginate($page_size);
  13. }
  14. public static function getSuccessOrderList($uid,$page_size){
  15. return Order::select('id','price','created_at','status','trade_no','order_type')->where('uid',$uid)->where('status','PAID')->where('from_type','!=','order_add')->orderBy('created_at','desc')->paginate($page_size);
  16. }
  17. public static function isUserHasOrder($uid){
  18. return Order::where('uid',$uid)->where('from_type','!=','order_add')->count();
  19. }
  20. /**
  21. * 获取充值订单
  22. * @param $uid
  23. * @return mixed
  24. */
  25. public static function totalChargeList($uid){
  26. return Order::join('products','orders.product_id','=','products.id')
  27. ->select('products.price','products.given')
  28. ->where('orders.order_type','RECHARGE')
  29. ->where('orders.status','PAID')
  30. ->where('orders.uid',$uid)
  31. ->get();
  32. }
  33. /**
  34. * 订单生成
  35. */
  36. static function generate($data)
  37. {
  38. //订单号生成
  39. $data['trade_no'] = $data['product_id'].date("YmdHis").hexdec(uniqid());
  40. return Order::create($data);
  41. }
  42. static function save_order($data){
  43. $insert_data = array();
  44. $insert_data['uid'] = $data['uid'];
  45. $insert_data['price'] = $data['price'];
  46. $insert_data['status'] = $data['status'];
  47. $insert_data['pay_type'] = $data['pay_type'];
  48. $insert_data['product_id'] = $data['product_id'];
  49. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  50. $insert_data['trade_no'] = $data['trade_no'];
  51. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  52. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  53. $insert_data['order_type'] = $data['order_type'];
  54. $insert_data['transaction_id'] = $data['transaction_id'];
  55. $insert_data['from_bid'] = $data['from_bid'];
  56. $insert_data['pay_end_at'] = $data['pay_end_at'];
  57. $insert_data['create_ip'] = $data['create_ip'];
  58. $insert_data['send_order_id'] = $data['send_order_id'];
  59. $insert_data['send_order_name'] = isset($data['send_order_name'])?$data['send_order_name']:'';
  60. $insert_data['from_type'] = $data['from_type'];
  61. $insert_data['activity_id'] = $data['activity_id'];
  62. $insert_data['inner_send_order_id'] = isset($data['inner_send_order_id'])?$data['inner_send_order_id']:'';
  63. return $result = Order::save_order($insert_data);
  64. }
  65. /**
  66. * 获取所有分销渠道
  67. * @return mixed
  68. */
  69. static function getAllChannelId(){
  70. return Order::select('distribution_channel_id')->distinct()->get()->toArray();
  71. }
  72. /**
  73. * 获取渠道下最近20条订单记录
  74. * @param $distribution_channel_id
  75. * @param int $num
  76. */
  77. static function getLatestOrderByChannleId($source,$num=20){
  78. return Order::select('orders.id','orders.status','orders.created_at')
  79. ->orderBy('orders.created_at','desc')
  80. ->where('orders.pay_merchant_source',$source)
  81. ->where('orders.created_at','<=',date('Y-m-d H:i:s',time()-10))
  82. ->limit($num)
  83. ->get();
  84. }
  85. /**
  86. * 获取最后一条订单,用于订单预警
  87. * @return mixed
  88. */
  89. public static function getLastOrder(){
  90. return Order::orderBy('created_at','desc')->first();
  91. }
  92. public static function userIsParticipateActivity($uid,$activity_id){
  93. return Order::where('uid',$uid)->where('activity_id',$activity_id)->where('status','PAID')->count();
  94. }
  95. /**
  96. * 渠道公众号重充值统计
  97. */
  98. public static function OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,$page_size=15){
  99. return Order::OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,$page_size);
  100. }
  101. /**
  102. * 渠道公众号重充值统计
  103. */
  104. public static function OfficialAccountOrderSum($distribution_channel_id,$start_time,$end_time,$appid){
  105. return Order::OfficialAccountOrderSum($distribution_channel_id,$start_time,$end_time,$appid);
  106. }
  107. /**
  108. * 根据派单获取充值总量
  109. * @param int $send_order_id
  110. * @param int $distribution_channel_id
  111. * @return float
  112. */
  113. public static function getSumBySendOrderId(int $send_order_id,int $distribution_channel_id):float {
  114. return (float)Order::where('send_order_id',$send_order_id)
  115. //->where('distribution_channel_id',$distribution_channel_id)
  116. ->whereIn('order_type',['RECHARGE','YEAR'])
  117. ->where('status','PAID')->sum('price');
  118. }
  119. /**
  120. * @param int $send_order_id
  121. * @return array
  122. */
  123. public static function getSuccessRate(int $send_order_id):array {
  124. $sql_format = "select
  125. count(*) as total,count(case when `status`='PAID' then `status` else null end) as success
  126. from orders
  127. WHERE send_order_id = %s and order_type= 'RECHARGE' ";
  128. $sql = sprintf($sql_format,$send_order_id);
  129. $res = DB::select($sql);
  130. $total = $res[0]->total;
  131. $success = $res[0]->success;
  132. return compact('total','success');
  133. }
  134. /**
  135. * 判断用户首充
  136. */
  137. public static function judgeUserFirstRecharge($uid) {
  138. $user_recharge = Order::where([
  139. ['status','=','PAID'],
  140. ['uid','=',$uid]
  141. ])->first();
  142. if(!$user_recharge){
  143. return true;
  144. }
  145. return false;
  146. }
  147. /**
  148. * @param $inner_send_order_id
  149. */
  150. public static function getInnerSendOrderStats($inner_send_order_id){
  151. return Order::where('inner_send_order_id',$inner_send_order_id)
  152. ->select(
  153. DB::raw('count(distinct uid) as pay_user_num'),
  154. DB::raw('sum(price) as charge_amount')
  155. )
  156. ->where('status','PAID')
  157. ->first();
  158. }
  159. public static function getUserOrderByProductId($uid,$product_id){
  160. return Order::where('uid',$uid)->where('product_id',$product_id)->where('status','PAID')->first();
  161. }
  162. /**
  163. * 是否是付费用户
  164. * @param $uid
  165. * @return bool
  166. */
  167. public static function isPaidUser($uid){
  168. if(empty($uid)){
  169. return false;
  170. }
  171. $result = Order::select('id')->where('uid',$uid)->where('status','PAID')->first();
  172. if($result && $result->id){
  173. return true;
  174. }
  175. return false;
  176. }
  177. }