Sin descripción

StarLiveRankStat.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\JuxingAdAccount;
  4. use App\Models\JxQtaskLiveOrderList;
  5. use App\Models\JxStarLiveOrderList;
  6. use App\Models\JxStarLiveInfoList;
  7. use App\Models\JxStarUsers;
  8. use App\Support\Log;
  9. use App\Support\RedisModel;
  10. use Illuminate\Console\Command;
  11. class StarLiveRankStat extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'StarLiveRankStat {day?}';
  19. protected $rkey = 'StarLiveRankDatas-';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '达人排行榜-直播更新缓存';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $this->info(date('H:i') . ' 开始执行');
  43. $d = $this->argument('day') ? $this->argument('day') : 30;
  44. $this->start($d);
  45. $this->info(date('H:i') . ' 结束执行');
  46. }
  47. public function start($d)
  48. {
  49. $start = date('Y-m-d', strtotime('-'.$d.' day')); //统计近d天
  50. $orderList = array();
  51. $orderGenerator = $this->getQtaskDbData(0, $start);
  52. foreach($orderGenerator as $list){
  53. $orderList = array_merge($orderList, $list);
  54. }
  55. $orderGenerator = $this->getStarDbData(0, $start);
  56. foreach($orderGenerator as $list){
  57. $orderList = array_merge($orderList, $list);
  58. }
  59. #字段指标
  60. $columns = [
  61. 'amount', //消耗
  62. 'play_cnt',
  63. 'play_uv',
  64. 'like_cnt',
  65. 'comment_cnt',
  66. 'share_cnt',
  67. 'item_show_cnt',
  68. 'item_click_cnt',
  69. 'item_click_rate',
  70. //'ctr', // 视频曝光到视频播放的点击率
  71. 'cpm', // 千次播放成本
  72. //'item_cpc', // 点击成本
  73. 'cpa', // 激活成本/注册成本
  74. ];
  75. #达人头像,粉丝数
  76. $userInfos = JxStarUsers::select('user_id', 'name', 'head_url', 'fans_number', 'gender')->get()->keyBy('user_id')->toArray();
  77. $data_max = array();
  78. $data_min = array();
  79. foreach($orderList as $k=>$v){
  80. #计算值补充
  81. $v['cpm'] = $v['play_cnt']>0 ? round($v['amount']/$v['play_cnt']*1000, 2) : null;
  82. //$v['item_cpc'] = $v['item_click_cnt']>0 ? round($v['amount']/$v['item_click_cnt'], 2) : null;
  83. $v['cpa'] = $v['item_click_cnt']>0 ? round($v['amount']/$v['item_click_cnt'], 2) : null; //激活成本
  84. $uid = $v['user_id'];
  85. $uid = $v['user_id'];
  86. $v['head_url'] = $userInfos[$uid]['head_url'] ?? null;
  87. $v['fans_number'] = $userInfos[$uid]['fans_number'] ?? null;
  88. $v['gender'] = $userInfos[$uid]['gender'] ?? null;
  89. foreach($columns as $column){
  90. if( !isset($data_max[$column][$uid]) ){
  91. $data_max[$column][$uid] = $v;
  92. } else {
  93. if($v[$column]>$data_max[$column][$uid][$column]){
  94. $data_max[$column][$uid] = $v;
  95. }
  96. }
  97. if( !isset($data_min[$column][$uid]) ){
  98. $data_min[$column][$uid] = $v;
  99. } else {
  100. if($v[$column]<$data_min[$column][$uid][$column]){
  101. $data_min[$column][$uid] = $v;
  102. }
  103. }
  104. }
  105. }
  106. if(!empty($data_max)){
  107. foreach($data_max as $column=>$list){
  108. $rkey = $this->rkey . 'max-'. $column.'-'.$d;
  109. $list = array_values($list);
  110. array_multisort(array_column($list, $column), SORT_DESC, $list);
  111. RedisModel::set($rkey, json_encode($list));
  112. }
  113. }
  114. if(!empty($data_min)){
  115. foreach($data_min as $column=>$list){
  116. $rkey = $this->rkey . 'min-'. $column.'-'.$d;
  117. $list = array_values($list);
  118. array_multisort(array_column($list, $column), SORT_ASC, $list);
  119. RedisModel::set($rkey, json_encode($list));
  120. }
  121. }
  122. }
  123. private function getQtaskDbData($min_id=0, $start){
  124. while(true){
  125. $orderList = JxQtaskLiveOrderList::query()
  126. ->where('star_release_time', '>=', $start)
  127. ->where('enable', 1)
  128. ->where('amount', '>', 0)
  129. ->where('id', '>', $min_id)
  130. ->select([
  131. 'id',
  132. 'advertiser_id',
  133. 'task_id',
  134. 'order_id',
  135. 'amount',
  136. 'star_name',
  137. 'kwai_id',
  138. 'user_id',
  139. 'play_cnt',
  140. 'play_uv',
  141. 'like_cnt',
  142. 'comment_cnt',
  143. 'share_cnt',
  144. 'item_show_cnt',
  145. 'item_click_cnt',
  146. 'item_click_rate',
  147. 'star_release_time'
  148. ])
  149. ->orderBy('id')
  150. ->limit(500)
  151. ->get()
  152. ->toArray();
  153. if(empty($orderList)){
  154. break;
  155. }
  156. $min_id = end($orderList)['id'];
  157. yield $orderList;
  158. }
  159. }
  160. private function getStarDbData($min_id=0, $start){
  161. while(true){
  162. $orderList = JxStarLiveOrderList::query()
  163. ->where('live_start_time', '>=', $start)
  164. ->where('enable', 1)
  165. ->where('real_amount', '>', 0)
  166. ->where('id', '>', $min_id)
  167. ->select([
  168. 'id',
  169. 'advertiser_id',
  170. 'task_id',
  171. 'order_id',
  172. 'real_amount', //
  173. 'star_name',
  174. 'kwai_id',
  175. 'user_id',
  176. 'play_cnt',
  177. 'play_uv',
  178. 'like_cnt',
  179. 'comment_cnt',
  180. 'share_cnt',
  181. 'live_start_time' //
  182. ])
  183. ->orderBy('id')
  184. ->limit(500)
  185. ->get()
  186. ->toArray();
  187. if(empty($orderList)){
  188. break;
  189. }
  190. foreach($orderList as &$val){
  191. $val['amount'] = $val['real_amount'];
  192. $val['star_release_time'] = $val['live_start_time'];
  193. #查组件数据
  194. $liveInfo = JxStarLiveInfoList::where('order_id', $val['order_id'])
  195. ->where('enable', 1)
  196. ->selectRaw('sum(item_show_cnt) as item_show_cnt, sum(item_click_cnt) as item_click_cnt')
  197. ->first();
  198. if(!empty($liveInfo)){
  199. $val['item_show_cnt'] = $liveInfo->item_show_cnt;
  200. $val['item_click_cnt'] = $liveInfo->item_click_cnt;
  201. $val['item_click_rate'] = $val['item_show_cnt']>0 ? round($val['item_click_cnt']/$val['item_show_cnt'], 4) : null;
  202. } else {
  203. $val['item_show_cnt'] = null;
  204. $val['item_click_cnt'] = null;
  205. $val['item_click_rate'] = null;
  206. }
  207. }
  208. $min_id = end($orderList)['id'];
  209. yield $orderList;
  210. }
  211. }
  212. }