Order.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 $connection = 'api_mysql';
  8. protected $table = 'orders';
  9. protected $fillable = [
  10. 'uid', 'product_id', 'price', 'status', 'pay_type', 'trade_no', 'send_order_id', 'send_order_name',
  11. 'transaction_id', 'distribution_channel_id', 'pay_end_at', 'create_ip', 'u', 'pay_merchant_source', 'pay_merchant_id', 'from_bid', 'order_type',
  12. 'activity_id','from_type','inner_send_order_id'
  13. ];
  14. static function getByTradeNo($trade_no)
  15. {
  16. return self::where('trade_no',$trade_no)->first();
  17. }
  18. public static function getOrderList($uid,$page_size){
  19. return self::select('id','price','created_at','status','trade_no')->where('uid',$uid)->orderBy('id','desc')->paginate($page_size);
  20. }
  21. /**
  22. * 订单生成
  23. */
  24. static function generate($data)
  25. {
  26. //订单号生成
  27. $data['trade_no'] = $data['product_id'].date("YmdHis").hexdec(uniqid());
  28. return self::create($data);
  29. }
  30. static function save_order($data){
  31. return self::firstOrCreate($data);
  32. }
  33. /**
  34. * 渠道公众号重充值统计
  35. */
  36. static function OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,$page_size = 15){
  37. return self::where('orders.distribution_channel_id',$distribution_channel_id)
  38. ->where('orders.status','=','PAID')
  39. ->where('orders.created_at','>=',$start_time)
  40. ->where('orders.created_at','<',$end_time)
  41. ->join('force_subscribe_users','force_subscribe_users.uid','=','orders.uid')
  42. ->groupBy(DB::raw('date(orders.created_at)'))
  43. ->groupBy('force_subscribe_users.appid')
  44. ->select(DB::raw('sum(price) as amount'),DB::raw('date(orders.created_at) as `date`'),'force_subscribe_users.appid')
  45. ->orderBy(DB::raw('date(orders.created_at)'),'desc')
  46. ->paginate($page_size);
  47. }
  48. /**
  49. * 渠道公众号重充值统计
  50. */
  51. static function OfficialAccountOrderSum($distribution_channel_id,$start_time,$end_time,$appid){
  52. $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}");
  53. return $data ? $data[0]->price : 0;/*
  54. return self::where('orders.distribution_channel_id',$distribution_channel_id)
  55. ->join('force_subscribe_users','force_subscribe_users.uid','=','orders.uid')
  56. ->where('orders.created_at','>=',$start_time)
  57. ->where('orders.created_at','<',$end_time)
  58. ->where('orders.status','=','PAID')
  59. ->where('force_subscribe_users.appid',$appid)
  60. ->sum('orders.price');*/
  61. }
  62. }