No Description

StarVideoRankStat.php 7.3KB

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