Order.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Modules\Subscribe\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class Order extends Model
  6. {
  7. protected $table = 'orders';
  8. protected $fillable = [
  9. 'uid', 'product_id', 'price', 'status', 'pay_type', 'trade_no', 'send_order_id', 'send_order_name',
  10. 'transaction_id', 'distribution_channel_id', 'pay_end_at', 'create_ip', 'u', 'pay_merchant_source', 'pay_merchant_id', 'from_bid', 'order_type',
  11. 'activity_id','from_type','inner_send_order_id'
  12. ];
  13. static function getByTradeNo($trade_no)
  14. {
  15. return self::where('trade_no',$trade_no)->first();
  16. }
  17. public static function getOrderList($uid,$page_size){
  18. return self::select('id','price','created_at','status','trade_no')->where('uid',$uid)->orderBy('id','desc')->paginate($page_size);
  19. }
  20. /**
  21. * 订单生成
  22. */
  23. static function generate($data)
  24. {
  25. //订单号生成
  26. $data['trade_no'] = $data['product_id'].date("YmdHis").hexdec(uniqid());
  27. return self::create($data);
  28. }
  29. static function save_order($data){
  30. return self::firstOrCreate($data);
  31. }
  32. /**
  33. * 渠道公众号重充值统计
  34. */
  35. static function OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,$page_size = 15){
  36. return self::where('orders.distribution_channel_id',$distribution_channel_id)
  37. ->where('orders.status','=','PAID')
  38. ->where('orders.created_at','>=',$start_time)
  39. ->where('orders.created_at','<',$end_time)
  40. ->join('force_subscribe_users','force_subscribe_users.uid','=','orders.uid')
  41. ->groupBy(DB::raw('date(orders.created_at)'))
  42. ->groupBy('force_subscribe_users.appid')
  43. ->select(DB::raw('sum(price) as amount'),DB::raw('date(orders.created_at) as `date`'),'force_subscribe_users.appid')
  44. ->orderBy(DB::raw('date(orders.created_at)'),'desc')
  45. ->paginate($page_size);
  46. }
  47. /**
  48. * 渠道公众号重充值统计
  49. */
  50. static function OfficialAccountOrderSum($distribution_channel_id,$start_time,$end_time,$appid){
  51. $data = DB::select("select sum(price) price from orders where `orders`.`created_at` >= '{$start_time}' and `orders`.`created_at` < '{$end_time}' and `orders`.`status` = 'PAID' and uid in (select uid from force_subscribe_users where `appid` = '{$appid}') and `orders`.`distribution_channel_id` = {$distribution_channel_id}");
  52. return $data ? $data[0]->price : 0;/*
  53. return self::where('orders.distribution_channel_id',$distribution_channel_id)
  54. ->join('force_subscribe_users','force_subscribe_users.uid','=','orders.uid')
  55. ->where('orders.created_at','>=',$start_time)
  56. ->where('orders.created_at','<',$end_time)
  57. ->where('orders.status','=','PAID')
  58. ->where('force_subscribe_users.appid',$appid)
  59. ->sum('orders.price');*/
  60. }
  61. /**
  62. * 个人充值统计
  63. */
  64. static function getRechargeAmountByUidAndTime($uid,$start_time,$end_time){
  65. return self::where('uid',$uid)
  66. ->where('orders.status','=','PAID')
  67. ->where('orders.created_at','>=',$start_time)
  68. ->where('orders.created_at','<',$end_time)
  69. ->sum('price');
  70. }
  71. }