CustomChapterOrder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Modules\Subscribe\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class CustomChapterOrder extends Model
  6. {
  7. protected $table = 'custom_chapter_orders';
  8. protected $fillable = ['distribution_channel_id','bid','cid','chapter_name','book_name','uid','send_order_id',
  9. 'fee','custom_id','flag','charge_balance','reward_balance','send_time'];
  10. public static function getStats($custom_id=0,$distribution_channel_id=0,$bid=0)
  11. {
  12. $where = [];
  13. $custom_id && $where[] = ['custom_chapter_orders.custom_id','=',$custom_id];
  14. $distribution_channel_id && $where[] = ['custom_chapter_orders.distribution_channel_id','=',$distribution_channel_id];
  15. $bid && $where[] = ['custom_chapter_orders.bid','=',$bid];
  16. return self::join('custom_send_msgs',function ($join){
  17. $join->on('custom_send_msgs.id','=','custom_chapter_orders.custom_id')
  18. ->where('custom_chapter_orders.flag',1);
  19. })->select(
  20. 'custom_chapter_orders.custom_id',//id
  21. 'custom_chapter_orders.distribution_channel_id',//站点
  22. 'custom_chapter_orders.send_time',//推送时间
  23. 'custom_chapter_orders.bid',
  24. 'custom_chapter_orders.book_name',
  25. 'custom_send_msgs.user_num',//送达人数
  26. DB::raw('sum(case when custom_chapter_orders.send_time
  27. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 1 day")
  28. then custom_chapter_orders.fee else null end) as one_day_amount'),//24小时订阅总额
  29. DB::raw('count(distinct case when custom_chapter_orders.send_time
  30. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 1 day")
  31. then custom_chapter_orders.uid else null end) as one_day_num'),//24小时订阅人数
  32. DB::raw('sum(case when custom_chapter_orders.send_time
  33. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 3 day")
  34. then custom_chapter_orders.fee else null end) as three_day_amount'),//72小时订阅总额
  35. DB::raw('count(distinct case when custom_chapter_orders.send_time
  36. <= ADDDATE(custom_chapter_orders.uid,"INTERVAL 3 day")
  37. then custom_chapter_orders.uid else null end) as three_day_num'),//72小时订阅人数
  38. DB::raw('count(distinct uid) as total_num'),//订阅用户
  39. //DB::raw('one_day_amount/one_day_num as one_day_rate'),//24小时人均订阅
  40. //DB::raw('three_day_amount/three_day_num as three_day_rate'),//72h人均订阅
  41. DB::raw('sum(fee) as total_amount')//订阅总额
  42. )->where($where)
  43. ->groupBy('custom_chapter_orders.custom_id')
  44. ->paginate();
  45. }
  46. }