小说推广数据系统

ExportTask.php 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/6/7
  6. * Time: 17:13
  7. */
  8. namespace App\Console\Commands;
  9. use App\Services\NovelService;
  10. use App\Services\OmDataService;
  11. use App\Services\OrderService;
  12. use App\Services\PlayletService;
  13. use App\Services\PromoterService;
  14. use App\Services\WxAccountService;
  15. use Illuminate\Console\Command;
  16. use App\Models\ExportTask as Task;
  17. use Maatwebsite\Excel\Facades\Excel;
  18. class ExportTask extends Command
  19. {
  20. protected $signature = 'ExportTask';
  21. protected $description = '导出任务';
  22. protected $taskId = null;
  23. public function infoMessage($message)
  24. {
  25. echo date('H:i:s') . $message . "\r\n";
  26. }
  27. public function handle()
  28. {
  29. $this->infoMessage(' 开始执行;内存占用:'.round(memory_get_usage()/1024/1024, 2).'MB');
  30. \DB::connection()->disableQueryLog();
  31. $time = time();
  32. while(1) {
  33. if(time() - $time >= 30*60) {
  34. //每隔半小时自动杀死
  35. die;
  36. }
  37. $taskList = $this->getTaskList();
  38. if(empty($taskList)) {
  39. sleep(60);
  40. continue;
  41. }
  42. $this->handleData($taskList);
  43. }
  44. }
  45. public function getTaskList()
  46. {
  47. //获取待执行导出任务列表
  48. $taskList = Task::where('status', 1)->get()->toArray();
  49. return $taskList;
  50. }
  51. public function handleData($taskList)
  52. {
  53. try{
  54. $typeData = Task::$typeData;
  55. $typeArr = array_keys($typeData);
  56. foreach($taskList as $task) {
  57. if(!in_array($task['data_type'], $typeArr)) {
  58. continue;
  59. }
  60. $this->taskId = $task['id'];
  61. switch($task['data_type']) {
  62. case 1: # 公众号总数据
  63. $this->dataCollectExport($task);
  64. break;
  65. case 2: # 广告投放数据
  66. $this->adServingExport($task);
  67. break;
  68. case 3: # 今日消耗排行榜
  69. $this->expendExport($task);
  70. break;
  71. case 4: # 付费粉丝激活趋势
  72. $this->paidFansExport($task);
  73. break;
  74. case 5: # 公众号付费趋势
  75. $this->paymentTrendExport($task);
  76. break;
  77. case 6: # 小说数据趋势
  78. $this->novelDataExport($task);
  79. break;
  80. case 7: # 投手大盘数据
  81. $this->promoteCondition($task);
  82. break;
  83. case 8: # 每日付费统计
  84. $this->orderList($task);
  85. break;
  86. case 9: # 每日运营数据汇总
  87. $this->omDataExport($task);
  88. break;
  89. case 10: # 小说人气数据汇总
  90. $this->novelPopularityExport($task);
  91. break;
  92. case 11: # 短剧粉丝激活趋势
  93. $this->activateTrendExport($task);
  94. break;
  95. case 12: # 公众号数据趋势
  96. $this->npDataTrend($task);
  97. break;
  98. case 13: # 短剧数据趋势
  99. $this->playletDataTreand($task);
  100. }
  101. sleep(60);
  102. }
  103. } catch (\Exception $exception) {
  104. $this->updateTaskStatus($this->taskId, -1, json_encode([
  105. 'file'=>$exception->getFile(),
  106. 'line'=>$exception->getLine(),
  107. 'message'=>$exception->getMessage()])
  108. );
  109. $this->infoMessage('error#file:'.$exception->getFile().';line:'.$exception->getLine().';message:'.$exception->getMessage());
  110. }
  111. }
  112. public function playletDataTreand($task)
  113. {
  114. $this->updateTaskStatus($task['id'], 2);
  115. $params = json_decode($task['params'], 1);
  116. if($params['start_date'] || $params['end_date']) {
  117. list($data, $count) = PlayletService::playletData($params['playlet_id'], $params['start_date'], $params['end_date'], 1, 5000, $params['min_day_paid'], $params['max_day_paid']);
  118. } else {
  119. list($data, $count) = PlayletService::playletDataTotal($params['playlet_id'], 1, 5000, $params['min_day_paid'], $params['max_day_paid']);
  120. }
  121. $data = json_decode(json_encode($data), 1);
  122. $titleArr = [
  123. "时间",
  124. "短剧",
  125. "首日ROI",
  126. "当天消耗",
  127. "当日新用户累计充值",
  128. '回本率',
  129. "新增粉丝数",
  130. "新增粉丝成本",
  131. "充值人数",
  132. "充值次数",
  133. "充值用户成本"
  134. ];
  135. $DArr = [];
  136. for($i=1;$i<=WxAccountService::DAYS;$i++){
  137. $DArr[] = 'D'.$i;
  138. }
  139. $titleArr = array_merge($titleArr, $DArr);
  140. $cellData = $extra = [];
  141. foreach ($data as $index => $item) {
  142. $cellData[] = [
  143. $item['start_date'].'~'.$item['end_date'],
  144. $item['playlet_name'],
  145. (round($item['first_day_roi'], 4)) * 100 . '%',
  146. $item['day_paid'],
  147. $item['charge_total'],
  148. (round($item['cost_cover_rate'], 4)) * 100 .'%',
  149. $item['fans_increase'],
  150. round($item['new_user_cost'], 2),
  151. $item['new_user_charge_uv'],
  152. $item['new_user_charge_pv'],
  153. round($item['charge_user_cost'], 2),
  154. ];
  155. $chargeData = isset($item['charge_data']) ? $item['charge_data'] : [];
  156. $chargeDataNew = [];
  157. foreach ($chargeData as $key=>$val) {
  158. $charge = isset($val['day_charge']) ? $val['day_charge'] : 0;
  159. $add = isset($val['day_add']) ? (round($val['day_add'], 4)) *100 . '%' : 0;
  160. $cover = isset($val['day_cover']) ? (round($val['day_cover'], 4)) *100 . '%' : 0;
  161. $times = isset($val['day_times']) ? (round($val['day_times'], 2)) : 0;
  162. $chargeDataNew[$key] = ['充:'.$charge, '增:'.$add, '回:'.$cover, '倍:'.$times];
  163. }
  164. $extra[] = $chargeDataNew;
  165. }
  166. $name = '短剧数据趋势_' . date('Ymd_H_i');
  167. Excel::create($name, function($excel) use($cellData, $titleArr, $extra){
  168. $excel->sheet('', function($sheet) use ($cellData, $titleArr, $extra){
  169. $merge = [];
  170. $sheet->row(1, $titleArr);
  171. foreach ($cellData as $index =>$row) {
  172. $line = $index*4 + 2;
  173. $sheet->row($line, $row);
  174. $sheet->rows([
  175. ['', '', '','', '', '','', '', '','', ''],
  176. ['', '', '','', '', '','', '', '','', ''],
  177. ['', '', '','', '', '','', '', '','', '']
  178. ]);
  179. $merge[] = [$line, $line + 3];
  180. }
  181. $basicLength = 11;
  182. $cellLabel = PlayletService::CELL_LABEL;
  183. $cellLabelUse = array_slice($cellLabel, $basicLength);
  184. foreach ($extra as $key => $cell) {
  185. foreach($cell as $k => $val) {
  186. foreach ($val as $i=>$v) {
  187. $sheet->cell($cellLabelUse[$k].($key*4+$i+2), function($cell) use($v) {
  188. $cell->setValue($v);
  189. });
  190. }
  191. }
  192. }
  193. $sheet->setMergeColumn(array(
  194. 'columns' => array('A','B','C','D','E','F','G','H','I','J','K'),
  195. 'rows' => $merge
  196. ));
  197. });
  198. })->store('xls');
  199. $this->updateTaskStatus($task['id'], 3, $name);
  200. }
  201. /**
  202. * 短剧粉丝激活趋势
  203. * */
  204. public function activateTrendExport($task)
  205. {
  206. $this->updateTaskStatus($task['id'], 2);
  207. $params = json_decode($task['params'], 1);
  208. list($data, $count) = PlayletService::activateTrend(
  209. $params['keyword'], $params['start_date'], $params['end_date'], 1, 10000
  210. );
  211. $titleArr = [
  212. "时间",
  213. "公众号",
  214. "当天消耗",
  215. "当日新用户累计充值",
  216. "回本率",
  217. '企微关注数',
  218. "企微关注成本",
  219. "首日用户成本",
  220. "累计用户成本"
  221. ];
  222. $DArr = [];
  223. for($i=1; $i<=PlayletService::ACTIVATE_FANS_DAYS; $i++){
  224. $DArr[] = 'D'.$i;
  225. }
  226. $titleArr = array_merge($titleArr, $DArr);
  227. $cellData = [
  228. $titleArr
  229. ];
  230. foreach ($data as $index => $item) {
  231. $cellData[] = [
  232. $item->subscribed_date,
  233. $item->name,
  234. $item->day_paid,
  235. $item->new_user_charge,
  236. (round($item->cost_cover_rate, 4)) * 100 .'%',
  237. $item->scan_follow_count,
  238. $item->follow_cost,
  239. round($item->charge_user_cost, 2),
  240. round($item->charge_user_cost_all, 2)
  241. ];
  242. $activeData = isset($item->activeData) ? $item->activeData : [];
  243. $extra = [];
  244. foreach ($activeData as $val) {
  245. $fans = isset($val['fans']) ? $val['fans'] : 0;
  246. $cost = isset($val['cost']) ? (round($val['cost'], 2)) : 0;
  247. $percent = 0;
  248. if(isset($val['active_fans_total'])) {
  249. $percent = $val['active_fans_total'] != 0 ? (round($fans / $item['active_fans_total'], 4)) *100 . '%' : 0;
  250. }
  251. $extra[] = "人数: ". $fans ."\r\n"
  252. . "成本: " . $cost ."\r\n"
  253. . "占比: " .$percent ;
  254. }
  255. $cellData[$index+1] = array_merge($cellData[$index+1], $extra);
  256. }
  257. $name = '短剧粉丝激活趋势_' . date('Ymd_H_i');
  258. Excel::create($name, function($excel) use($cellData){
  259. $excel->sheet('', function($sheet) use ($cellData){
  260. $sheet->rows($cellData);
  261. });
  262. })->store('csv');
  263. $this->updateTaskStatus($task['id'], 3, $name);
  264. }
  265. /**
  266. * 小说人气数据汇总
  267. * */
  268. public function novelPopularityExport($task)
  269. {
  270. $this->updateTaskStatus($task['id'], 2);
  271. $params = json_decode($task['params'], 1);
  272. list($data, $count) = NovelService::popularityNovelData($params['keyword'], $params['sort'], $params['start_date'], $params['end_date'], 1, 10000);
  273. $cellData = [
  274. [
  275. "小说",
  276. "平台",
  277. "充值金额",
  278. "在看人数",
  279. "ARPU",
  280. ]
  281. ];
  282. $platformInfo = config('platform');
  283. foreach ($data as $item) {
  284. $cellData[] = [
  285. $item->from_novel,
  286. isset($platformInfo[$item->platform_id]) ? $platformInfo[$item->platform_id] : '',
  287. $item->charge_total / 100,
  288. $item->watching_user,
  289. round($item->charge_total / 100 / $item->watching_user, 3),
  290. ];
  291. }
  292. $name = '小说人气数据统计_' . date('Ymd_H_i');
  293. Excel::create($name, function($excel) use($cellData){
  294. $excel->sheet('', function($sheet) use ($cellData){
  295. $sheet->rows($cellData);
  296. });
  297. })->store('csv');
  298. $this->updateTaskStatus($task['id'], 3, $name);
  299. }
  300. /**
  301. * 每日运营数据汇总导出
  302. * */
  303. public function omDataExport($task)
  304. {
  305. $this->updateTaskStatus($task['id'], 2);
  306. $params = json_decode($task['params'], 1);
  307. list($data, $total, $currentMonth, $lastYear, $condition) = OmDataService::getDataOfDay($params['start_date'], $params['end_date'], 1, 5000);
  308. $cellData = [
  309. "日期",
  310. "投放消耗",
  311. "新用户充值",
  312. "新用户ROI",
  313. ];
  314. $currentMonth = $currentMonth -1;
  315. $DArr = [];
  316. for($i=$currentMonth;$i>0;$i--){
  317. $DArr[] = $i . '月份用户回收';
  318. }
  319. $DArr[] = $lastYear . '年用户回收';
  320. $titleArr = array_merge($cellData, $DArr);
  321. $cellData = [
  322. $titleArr
  323. ];
  324. foreach ($data as $index => $item) {
  325. $lastYearData = [];
  326. for($i=$currentMonth; $i>0; $i--) {
  327. $key = 'recycle'.$i;
  328. $lastYearData[] = $item->$key;
  329. }
  330. $cellDataMid = [
  331. $item->ref_date,
  332. $item->paid_total,
  333. $item->new_user_charge,
  334. $item->roi,
  335. ];
  336. $midData = array_merge($cellDataMid, $lastYearData, [$item->last_year_recycle]);
  337. $cellData[] = $midData;
  338. }
  339. $name = '每日运营数据汇总_' . date('Ymd_H_i');
  340. Excel::create($name, function($excel) use($cellData){
  341. $excel->sheet('', function($sheet) use ($cellData){
  342. $sheet->rows($cellData);
  343. });
  344. })->store('csv');
  345. $this->updateTaskStatus($task['id'], 3, $name);
  346. }
  347. public function orderList($task)
  348. {
  349. $this->updateTaskStatus($task['id'], 2);
  350. $params = json_decode($task['params'], 1);
  351. list($data, $total, $accountList, $platformList, $condition) = OrderService::getList($params['channel_id'], $params['platform_id'], $params['reg_time_st'], $params['reg_time_et'], $params['order_time_st'], $params['order_time_et'], $params['min_money'], $params['max_money'], 1, 5000);
  352. $cellData = [
  353. [
  354. "注册时间",
  355. "金额",
  356. "公众号",
  357. "下单时间",
  358. "书城"
  359. ]
  360. ];
  361. foreach ($data as $item) {
  362. $cellData[] = [
  363. $item->user_created_at,
  364. $item->price,
  365. $item->nickname,
  366. $item->platform_created_at,
  367. $item->platform
  368. ];
  369. }
  370. $name = '每日付费统计_' . date('Ymd_H_i');
  371. Excel::create($name, function($excel) use($cellData){
  372. $excel->sheet('', function($sheet) use ($cellData){
  373. $sheet->rows($cellData);
  374. });
  375. })->store('csv');
  376. $this->updateTaskStatus($task['id'], 3, $name);
  377. }
  378. /**
  379. * 投手大盘数据导出
  380. * */
  381. public function promoteCondition($task)
  382. {
  383. $this->updateTaskStatus($task['id'], 2);
  384. $params = json_decode($task['params'], 1);
  385. list($list, $count, $condition) = PromoterService::getPromoteCondition($params['promoter_id'], $params['start_date'], $params['end_date'], 1, 2000);
  386. $cellData = [
  387. [
  388. "公众号名称",
  389. "投手",
  390. "书籍",
  391. "开始日期",
  392. "截止日期",
  393. '累计消耗',
  394. "累计充值",
  395. "当日ROI",
  396. "总毛利额",
  397. "回本率"
  398. ]
  399. ];
  400. foreach ($list as $item) {
  401. $cellData[] = [
  402. $item->nickname,
  403. $item->promoter,
  404. $item->novel,
  405. $item->start_date,
  406. $item->end_date,
  407. $item->paid_total,
  408. $item->charge_total,
  409. (round($item->roi, 4) * 100) . '%',
  410. $item->margin_rate,
  411. (round($item->cost_recovery_rate, 4) * 100) . '%',
  412. ];
  413. }
  414. $name = '投手大盘数据_' . date('Ymd_H_i');
  415. Excel::create($name, function($excel) use($cellData){
  416. $excel->sheet('', function($sheet) use ($cellData){
  417. $sheet->rows($cellData);
  418. });
  419. })->store('csv');
  420. $this->updateTaskStatus($task['id'], 3, $name);
  421. }
  422. public function dataCollectExport($task)
  423. {
  424. $this->updateTaskStatus($task['id'], 2);
  425. $params = json_decode($task['params'], 1);
  426. list($data, $total) = WxAccountService::accountDataCollect($params['keyword'], $params['enable'], 1, 2000);
  427. $cellData = [
  428. [
  429. "公众号名称",
  430. "投手",
  431. "最早消耗日期",
  432. "最晚消耗日期",
  433. "累计消耗",
  434. '累计充值',
  435. "总毛利额",
  436. "回本率",
  437. "总关注人数",
  438. "平均关注人数成本",
  439. "总充值人数",
  440. "充值转化比率",
  441. "充值转化成本",
  442. "今日充值",
  443. "昨日充值",
  444. "前日充值"
  445. ]
  446. ];
  447. foreach ($data as $item) {
  448. $cellData[] = [
  449. $item->name,
  450. $item->promoter_name,
  451. $item->start_paid,
  452. $item->end_paid,
  453. $item->paid_total,
  454. $item->charge_total,
  455. $item->margin_rate,
  456. (round($item->cost_recovery_rate, 4) * 100) . '%',
  457. $item->follow_total,
  458. round($item->cost_recovery_rate, 3),
  459. $item->charge_user_total,
  460. (round($item->charge_transform, 4) * 100) . '%',
  461. round($item->charge_transform_cost, 3),
  462. $item->yesterday,
  463. $item->before_yesterday,
  464. $item->three_das_ago
  465. ];
  466. }
  467. $name = '公众号总数据_' . date('Ymd_H_i');
  468. Excel::create($name, function($excel) use($cellData){
  469. $excel->sheet('', function($sheet) use ($cellData){
  470. $sheet->rows($cellData);
  471. });
  472. })->store('csv');
  473. $this->updateTaskStatus($task['id'], 3, $name);
  474. }
  475. public function adServingExport($task)
  476. {
  477. $this->updateTaskStatus($task['id'], 2);
  478. $params = json_decode($task['params'], 1);
  479. list($data, $total) = WxAccountService::adServing($params['keyword'], $params['sort'], $params['start_date'], $params['end_date'], 1, 5000);
  480. $cellData = [
  481. [
  482. "时间",
  483. "公众号",
  484. "消耗",
  485. "首日充值金额",
  486. "首日roi",
  487. '首日下单人数',
  488. "首日下单成本",
  489. "点击率",
  490. "下单率",
  491. "关注率",
  492. "关注成本"
  493. ]
  494. ];
  495. foreach ($data as $item) {
  496. $cellData[] = [
  497. $item->ref_date,
  498. $item->name,
  499. $item->paid,
  500. $item->paid_total_new,
  501. (round($item->roi, 4) * 100) . '%',
  502. $item->charge_uv_new,
  503. round($item->order_cost, 2),
  504. ($item->ctr * 100) . '%',
  505. (round($item->order_rate, 4) * 100) . '%',
  506. (round($item->clk_com_rate, 4) * 100) . '%',
  507. round($item->com_cost, 2),
  508. ];
  509. }
  510. $name = '广告投放数据_' . date('Ymd_H_i');
  511. Excel::create($name, function($excel) use($cellData){
  512. $excel->sheet('', function($sheet) use ($cellData){
  513. $sheet->rows($cellData);
  514. });
  515. })->store('csv');
  516. $this->updateTaskStatus($task['id'], 3, $name);
  517. }
  518. public function expendExport($task)
  519. {
  520. $this->updateTaskStatus($task['id'], 2);
  521. $params = json_decode($task['params'], 1);
  522. list($data, $total) = WxAccountService::expendRank($params['keyword'], $params['start_date'], $params['end_date'], 1, 5000);
  523. $cellData = [
  524. [
  525. "日期",
  526. "公众号",
  527. "消耗",
  528. "曝光量",
  529. "点击量",
  530. "点击率",
  531. "千次曝光成本"
  532. ]
  533. ];
  534. foreach ($data as $item) {
  535. $cellData[] = [
  536. $params['start_date'] . '~' . $params['end_date'],
  537. $item->name,
  538. $item->paid_total,
  539. $item->exp_total,
  540. $item->clk_total,
  541. round($item->clk_rate, 3),
  542. round($item->exp_thousand_cost)
  543. ];
  544. }
  545. $name = '消耗排行数据_' . date('Ymd_H_i');
  546. Excel::create($name, function($excel) use($cellData){
  547. $excel->sheet('', function($sheet) use ($cellData){
  548. $sheet->rows($cellData);
  549. });
  550. })->store('csv');
  551. $this->updateTaskStatus($task['id'], 3, $name);
  552. }
  553. public function paidFansExport($task)
  554. {
  555. $this->updateTaskStatus($task['id'], 2);
  556. $params = json_decode($task['params'], 1);
  557. list($data, $total) = WxAccountService::paidFans($params['keyword'], $params['start_date'], $params['end_date'], 1, 5000);
  558. $titleArr = [
  559. "时间",
  560. "公众号",
  561. "当天消耗",
  562. "当日新用户累计充值",
  563. "回本率",
  564. '新增粉丝数',
  565. "新增粉丝成本",
  566. "充值人数",
  567. "充值次数",
  568. "充值用户成本",
  569. "7日平均激活率",
  570. "7日平均激活成本",
  571. "30日平均激活率",
  572. "30日平均激活成本",
  573. "总激活成本"
  574. ];
  575. $DArr = [];
  576. for($i=1;$i<=WxAccountService::DAYS;$i++){
  577. $DArr[] = 'D'.$i;
  578. }
  579. $titleArr = array_merge($titleArr, $DArr);
  580. $cellData = [
  581. $titleArr
  582. ];
  583. foreach ($data as $index => $item) {
  584. $cellData[] = [
  585. $item->subscribed_date,
  586. $item->name,
  587. $item->day_paid,
  588. $item->charge_total,
  589. (round($item->cost_cover_rate, 4)) * 100 .'%',
  590. $item->fans_increase,
  591. round($item->cost_cover_rate, 2),
  592. $item->new_user_charge_uv,
  593. $item->new_user_charge_pv,
  594. round($item->charge_user_cost, 2),
  595. round($item->active_avg_seven, 2),
  596. round($item->charge_user_cost_seven, 2),
  597. round($item->active_avg_thirty, 2),
  598. round($item->charge_user_cost_thirty, 2),
  599. round($item->charge_user_cost_all, 2)
  600. ];
  601. $activeData = isset($item->activeData) ? $item->activeData : [];
  602. $extra = [];
  603. foreach ($activeData as $val) {
  604. $fans = isset($val['fans']) ? $val['fans'] : 0;
  605. $cost = isset($val['cost']) ? (round($val['cost'], 2)) : 0;
  606. $percent = 0;
  607. if(isset($val['active_fans_total'])) {
  608. $percent = $val['active_fans_total'] != 0 ? (round($fans / $item['active_fans_total'], 4)) *100 . '%' : 0;
  609. }
  610. $extra[] = "人数: ". $fans ."\r\n"
  611. . "成本: " . $cost ."\r\n"
  612. . "占比: " .$percent ;
  613. }
  614. $cellData[$index+1] = array_merge($cellData[$index+1], $extra);
  615. }
  616. $name = '付费粉丝激活趋势数据_' . date('Ymd_H_i');
  617. Excel::create($name, function($excel) use($cellData){
  618. $excel->sheet('', function($sheet) use ($cellData){
  619. $sheet->rows($cellData);
  620. });
  621. })->store('csv');
  622. $this->updateTaskStatus($task['id'], 3, $name);
  623. }
  624. /**
  625. * 公众号数据趋势
  626. * */
  627. public function npDataTrend($task)
  628. {
  629. $this->updateTaskStatus($task['id'], 2);
  630. $params = json_decode($task['params'], 1);
  631. list($data, $count) = PlayletService::dataTrend(
  632. $params['keyword'], $params['start_date'], $params['end_date'], 1, 10000
  633. );
  634. $titleArr = [
  635. "时间",
  636. "公众号",
  637. "首日ROI",
  638. "当天消耗",
  639. '企微关注数',
  640. "企微关注成本",
  641. "当日回收",
  642. "累计充值金额",
  643. "新增用户",
  644. '回本率',
  645. "充值人数",
  646. "总充值人数",
  647. "充值次数",
  648. "总充值次数",
  649. "充值用户成本",
  650. ''
  651. ];
  652. $DArr = [];
  653. for($i=1; $i<=PlayletService::DAYS; $i++){
  654. $DArr[] = 'D'.$i;
  655. }
  656. $titleArr = array_merge($titleArr, $DArr);
  657. $cellData = $extra = [];
  658. foreach ($data as $index => $item) {
  659. $cellData[] = [
  660. $item->subscribed_date,
  661. $item->name,
  662. (round($item->first_day_roi, 4)) * 100 .'%',
  663. $item->day_paid,
  664. $item->scan_follow_count,
  665. $item->follow_cost,
  666. $item->new_user_charge,
  667. $item->charge_total,
  668. $item->fans_increase,
  669. (round($item->cost_cover_rate, 4)) * 100 .'%',
  670. $item->new_user_charge_uv,
  671. $item->new_user_charge_uv_count,
  672. $item->new_user_charge_pv,
  673. $item->new_user_charge_pv_count,
  674. round($item->charge_user_cost, 2),
  675. '充'
  676. ];
  677. $chargeData = isset($item->chargeData) ? $item->chargeData : [];
  678. $chargeDataNew = [];
  679. foreach ($chargeData as $key=>$val) {
  680. $charge = isset($val['day_charge']) ? $val['day_charge'] : 0;
  681. $add = isset($val['day_add']) ? (round($val['day_add'], 4)) *100 . '%' : 0;
  682. $cover = isset($val['day_cover']) ? (round($val['day_cover'], 4)) *100 . '%' : 0;
  683. $times = isset($val['day_times']) ? (round($val['day_times'], 2)) : 0;
  684. $chargeDataNew[$key] = [$charge, $add, $cover, $times];
  685. }
  686. $extra[] = $chargeDataNew;
  687. }
  688. $name = '短剧数据趋势_' . date('Ymd_H_i');
  689. Excel::create($name, function($excel) use($cellData, $titleArr, $extra){
  690. $excel->sheet('', function($sheet) use ($cellData, $titleArr, $extra){
  691. $merge = [];
  692. $sheet->row(1, $titleArr);
  693. foreach ($cellData as $index =>$row) {
  694. $line = $index*4 + 2;
  695. $sheet->row($line, $row);
  696. $sheet->rows([
  697. ['', '', '','', '', '','', '', '','', '','','增'],
  698. ['', '', '','', '', '','', '', '','', '','','回'],
  699. ['', '', '','', '', '','', '', '','', '','','倍']
  700. ]);
  701. $merge[] = [$line, $line + 3];
  702. }
  703. $basicLength = 13;
  704. $cellLabel = WxAccountService::CELL_LABEL;
  705. $cellLabelUse = array_slice($cellLabel, $basicLength);
  706. foreach ($extra as $key => $cell) {
  707. foreach($cell as $k => $val) {
  708. foreach ($val as $i=>$v) {
  709. $sheet->cell($cellLabelUse[$k].($key*4+$i+2), function($cell) use($v) {
  710. $cell->setValue($v);
  711. });
  712. }
  713. }
  714. }
  715. $sheet->setMergeColumn(array(
  716. 'columns' => array('A','B','C','D','E','F','G','H','I','J','K','L'),
  717. 'rows' => $merge
  718. ));
  719. });
  720. })->store('xls');
  721. $this->updateTaskStatus($task['id'], 3, $name);
  722. }
  723. public function paymentTrendExport($task)
  724. {
  725. $this->updateTaskStatus($task['id'], 2);
  726. $params = json_decode($task['params'], 1);
  727. list($data, $total, $condition) = WxAccountService::paymentTrend($params['keyword'], $params['novel_id'], $params['start_date'], $params['end_date'], 1, 5000);
  728. $titleArr = [
  729. "时间",
  730. "公众号",
  731. "首日ROI",
  732. "当天消耗",
  733. "当日新用户累计充值",
  734. '回本率',
  735. "新增粉丝数",
  736. "新增粉丝成本",
  737. "充值人数",
  738. "充值次数",
  739. "充值用户成本",
  740. ""
  741. ];
  742. $DArr = [];
  743. for($i=1;$i<=WxAccountService::DAYS;$i++){
  744. $DArr[] = 'D'.$i;
  745. }
  746. $titleArr = array_merge($titleArr, $DArr);
  747. $cellData = $extra = [];
  748. foreach ($data as $index => $item) {
  749. $cellData[] = [
  750. $item->subscribed_date,
  751. $item->name,
  752. (round($item->first_day_roi, 4)) * 100 . '%',
  753. $item->day_paid,
  754. $item->charge_total,
  755. (round($item->cost_cover_rate, 4)) * 100 .'%',
  756. $item->fans_increase,
  757. round($item->new_user_cost, 2),
  758. $item->new_user_charge_uv,
  759. $item->new_user_charge_pv,
  760. round($item->charge_user_cost, 2),
  761. '充'
  762. ];
  763. $chargeData = isset($item->chargeData) ? $item->chargeData : [];
  764. $chargeDataNew = [];
  765. foreach ($chargeData as $key=>$val) {
  766. $charge = isset($val['day_charge']) ? $val['day_charge'] : 0;
  767. $add = isset($val['day_add']) ? (round($val['day_add'], 4)) *100 . '%' : 0;
  768. $cover = isset($val['day_cover']) ? (round($val['day_cover'], 4)) *100 . '%' : 0;
  769. $times = isset($val['day_times']) ? (round($val['day_times'], 2)) : 0;
  770. $chargeDataNew[$key] = [$charge, $add, $cover, $times];
  771. }
  772. $extra[] = $chargeDataNew;
  773. }
  774. $name = '粉丝付费趋势数据_' . date('Ymd_H_i');
  775. Excel::create($name, function($excel) use($cellData, $titleArr, $extra){
  776. $excel->sheet('', function($sheet) use ($cellData, $titleArr, $extra){
  777. $sheet->row(1, $titleArr);
  778. $merge = [];
  779. foreach ($cellData as $index =>$row) {
  780. $line = $index*4 + 2;
  781. $sheet->row($line, $row);
  782. $sheet->rows([
  783. ['', '', '','', '', '','', '', '','', '','增'],
  784. ['', '', '','', '', '','', '', '','', '','回'],
  785. ['', '', '','', '', '','', '', '','', '','倍']
  786. ]);
  787. $merge[] = [$line, $line + 3];
  788. }
  789. $basicLength = 12;
  790. $cellLabel = WxAccountService::CELL_LABEL;
  791. $cellLabelUse = array_slice($cellLabel, $basicLength);
  792. foreach ($extra as $key => $cell) {
  793. foreach($cell as $k => $val) {
  794. foreach ($val as $i=>$v) {
  795. $sheet->cell($cellLabelUse[$k].($key*4+$i+2), function($cell) use($v) {
  796. $cell->setValue($v);
  797. });
  798. }
  799. }
  800. }
  801. $sheet->setMergeColumn(array(
  802. 'columns' => array('A','B','C','D','E','F','G','H','I','J','K'),
  803. 'rows' => $merge
  804. ));
  805. });
  806. })->store('xls');
  807. $this->updateTaskStatus($task['id'], 3, $name);
  808. }
  809. public function novelDataExport($task)
  810. {
  811. $this->updateTaskStatus($task['id'], 2);
  812. $params = json_decode($task['params'], 1);
  813. if($params['start_date'] || $params['end_date']) {
  814. list($data, $count) = WxAccountService::novelData($params['novel_id'], $params['start_date'], $params['end_date'], 1, 5000, $params['min_day_paid'], $params['max_day_paid']);
  815. } else {
  816. list($data, $count) = WxAccountService::novelDataTotal($params['novel_id'], 1, 5000, $params['min_day_paid'], $params['max_day_paid']);
  817. }
  818. $data = json_decode(json_encode($data), 1);
  819. $titleArr = [
  820. "时间",
  821. "小说",
  822. "首日ROI",
  823. "当天消耗",
  824. "当日新用户累计充值",
  825. '回本率',
  826. "新增粉丝数",
  827. "新增粉丝成本",
  828. "充值人数",
  829. "充值次数",
  830. "充值用户成本"
  831. ];
  832. $DArr = [];
  833. for($i=1;$i<=WxAccountService::DAYS;$i++){
  834. $DArr[] = 'D'.$i;
  835. }
  836. $titleArr = array_merge($titleArr, $DArr);
  837. $cellData = $extra = [];
  838. foreach ($data as $index => $item) {
  839. $cellData[] = [
  840. $item['start_date'].'~'.$item['end_date'],
  841. $item['novel_name'],
  842. (round($item['first_day_roi'], 4)) * 100 . '%',
  843. $item['day_paid'],
  844. $item['charge_total'],
  845. (round($item['cost_cover_rate'], 4)) * 100 .'%',
  846. $item['fans_increase'],
  847. round($item['new_user_cost'], 2),
  848. $item['new_user_charge_uv'],
  849. $item['new_user_charge_pv'],
  850. round($item['charge_user_cost'], 2),
  851. ];
  852. $chargeData = isset($item['charge_data']) ? $item['charge_data'] : [];
  853. $chargeDataNew = [];
  854. foreach ($chargeData as $key=>$val) {
  855. $charge = isset($val['day_charge']) ? $val['day_charge'] : 0;
  856. $add = isset($val['day_add']) ? (round($val['day_add'], 4)) *100 . '%' : 0;
  857. $cover = isset($val['day_cover']) ? (round($val['day_cover'], 4)) *100 . '%' : 0;
  858. $times = isset($val['day_times']) ? (round($val['day_times'], 2)) : 0;
  859. $chargeDataNew[$key] = ['充:'.$charge, '增:'.$add, '回:'.$cover, '倍:'.$times];
  860. }
  861. $extra[] = $chargeDataNew;
  862. }
  863. $name = '小说数据趋势_' . date('Ymd_H_i');
  864. Excel::create($name, function($excel) use($cellData, $titleArr, $extra){
  865. $excel->sheet('', function($sheet) use ($cellData, $titleArr, $extra){
  866. $merge = [];
  867. $sheet->row(1, $titleArr);
  868. foreach ($cellData as $index =>$row) {
  869. $line = $index*4 + 2;
  870. $sheet->row($line, $row);
  871. $sheet->rows([
  872. ['', '', '','', '', '','', '', '','', ''],
  873. ['', '', '','', '', '','', '', '','', ''],
  874. ['', '', '','', '', '','', '', '','', '']
  875. ]);
  876. $merge[] = [$line, $line + 3];
  877. }
  878. $basicLength = 11;
  879. $cellLabel = WxAccountService::CELL_LABEL;
  880. $cellLabelUse = array_slice($cellLabel, $basicLength);
  881. foreach ($extra as $key => $cell) {
  882. foreach($cell as $k => $val) {
  883. foreach ($val as $i=>$v) {
  884. $sheet->cell($cellLabelUse[$k].($key*4+$i+2), function($cell) use($v) {
  885. $cell->setValue($v);
  886. });
  887. }
  888. }
  889. }
  890. $sheet->setMergeColumn(array(
  891. 'columns' => array('A','B','C','D','E','F','G','H','I','J','K'),
  892. 'rows' => $merge
  893. ));
  894. });
  895. })->store('xls');
  896. $this->updateTaskStatus($task['id'], 3, $name);
  897. }
  898. public function updateTaskStatus($taskId, $status, $message = null)
  899. {
  900. $update['status'] = $status;
  901. if(-1 == $status) {
  902. $update['exception'] = $message;
  903. } else {
  904. if($message) {
  905. $update['file_url'] = $message;
  906. }
  907. }
  908. Task::where('id', $taskId)->update($update);
  909. }
  910. }