CustomChapterOrder.php 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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,$book_name='',$is_all=false)
  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. $bids = [];
  17. if($book_name){
  18. $bids = DB::table('book_configs')->where('book_name','like','%'.$book_name.'%')->select('bid')->pluck('bid')->all();
  19. \Log::info($bids);
  20. }
  21. $result = self::join('custom_send_msgs',function ($join){
  22. $join->on('custom_send_msgs.id','=','custom_chapter_orders.custom_id')
  23. ->where('custom_chapter_orders.flag',1);
  24. })->select(
  25. 'custom_chapter_orders.custom_id',//id
  26. 'custom_chapter_orders.distribution_channel_id',//站点
  27. 'custom_chapter_orders.send_time',//推送时间
  28. 'custom_chapter_orders.bid',
  29. 'custom_chapter_orders.book_name',
  30. 'custom_send_msgs.user_num',//送达人数
  31. DB::raw('sum(case when custom_chapter_orders.send_time
  32. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 1 day")
  33. then custom_chapter_orders.fee else null end) as one_day_amount'),//24小时订阅总额
  34. DB::raw('count(distinct case when custom_chapter_orders.send_time
  35. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 1 day")
  36. then custom_chapter_orders.uid else null end) as one_day_num'),//24小时订阅人数
  37. DB::raw('sum(case when custom_chapter_orders.send_time
  38. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 3 day")
  39. then custom_chapter_orders.fee else null end) as three_day_amount'),//72小时订阅总额
  40. DB::raw('count(distinct case when custom_chapter_orders.send_time
  41. <= ADDDATE(custom_chapter_orders.send_time,"INTERVAL 3 day")
  42. then custom_chapter_orders.uid else null end) as three_day_num'),//72小时订阅人数
  43. DB::raw('count(distinct uid) as total_num'),//订阅用户
  44. //DB::raw('one_day_amount/one_day_num as one_day_rate'),//24小时人均订阅
  45. //DB::raw('three_day_amount/three_day_num as three_day_rate'),//72h人均订阅
  46. DB::raw('sum(fee) as total_amount')//订阅总额
  47. )->where($where);
  48. if($bids){
  49. $result->whereIn('custom_chapter_orders.bid',$bids);
  50. }
  51. if($is_all){
  52. return $result->groupBy('custom_chapter_orders.custom_id')
  53. ->get();
  54. }else{
  55. return $result->groupBy('custom_chapter_orders.custom_id')
  56. ->paginate();
  57. }
  58. }
  59. public static function export(\Illuminate\Http\Request $request)
  60. {
  61. $custom_id = $request->get('custom_id');
  62. $distribution_channel_id = $request->get('distribution_channel_id');
  63. $bid = $request->get('bid');
  64. $book_name = $request->get('book_name');
  65. $result = self::getStats($custom_id,$distribution_channel_id,$bid,$book_name,true);
  66. $filename = 'custom_chapter_order'.date('YmdHis').'.csv';
  67. \Storage::append($filename,mb_convert_encoding("id,站点,推送时间,书籍,送达人数,24小时订阅总额,24小时订阅人数,24小时人均订阅,72小时订阅总额,72小时订阅人数,72h人均订阅,订阅用户,订阅总额",'gbk'));
  68. $str = '';
  69. foreach ($result as $val){
  70. if(!$val->one_day_num){
  71. $one_day_rate = 0;
  72. }else{
  73. $one_day_rate = round($val->one_day_amount/$val->one_day_num,2);
  74. }
  75. if(!$val->three_day_num){
  76. $three_day_rate =0;
  77. }else{
  78. $three_day_rate = round($val->three_day_amount/$val->three_day_num,2);
  79. }
  80. $str .= "{$val->custom_id},{$val->distribution_channel_id},{$val->send_time},{$val->book_name},{$val->user_num},{$val->one_day_amount},{$val->one_day_num},{$one_day_rate},{$val->three_day_amount},{$val->three_day_num},{$three_day_rate},{$val->total_num},{$val->total_amount}\r\n";
  81. }
  82. \Storage::append($filename,mb_convert_encoding($str,'gbk'));
  83. return response()->download(storage_path('app/'.$filename))->deleteFileAfterSend(true);
  84. }
  85. }