OrderService.php 6.9 KB

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