企微短剧业务系统

ShortPlayDataTrendService.php 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/9/18
  6. * Time: 21:21
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. use App\Models\AdqPlayletTrendData;
  11. use App\Models\DramaSeries;
  12. use App\Models\Es\PlayletAccountDataTrend;
  13. use App\Models\OfficialAccount;
  14. use App\Models\PlayletAccountTrendData;
  15. use App\Models\Report\PlayletDataTrend;
  16. use App\Models\MpPlayletTrendData;
  17. class ShortPlayDataTrendService
  18. {
  19. public static function adqShortPlayDataTrendList($params, $page, $pageSize, $sortField, $sortType)
  20. {
  21. # 表头处理
  22. $commonHeader = config('shortPlayDataTrendHeader.basic');
  23. # 获取day1至day150表头
  24. $daysHeader = [];
  25. for($i = 0;$i < AdqPlayletTrendData::PLAYLET_TREND_DATA_RECORD_DAYS;$i++) {
  26. $title = [
  27. 'name' => 'DAY'.($i+1),
  28. 'column' => 'day'.($i+1)
  29. ];
  30. $daysHeader[] = $title;
  31. }
  32. $header = array_merge($commonHeader, $daysHeader);
  33. # 查询数据
  34. if(isset($params['playlet_id']) && $params['playlet_id']) {
  35. list($list, $count) = AdqPlayletTrendData::getShortPlayDataTrendTrend($params, $sortField, $sortType, $page, $pageSize);
  36. } else {
  37. # 获取企业下的adq账号
  38. $playletIdList = DramaSeries::query()
  39. ->select('id')
  40. ->where('enable', 1)
  41. ->where('sys_group_id', $params['sys_group_id'])
  42. ->distinct()
  43. ->pluck('id');
  44. if(empty($playletIdList)) return [0, [], $header];
  45. list($list, $count) = AdqPlayletTrendData::getShortPlayDataTrendSummaryData(
  46. $params, $playletIdList, $sortField, $sortType, $page, $pageSize
  47. );
  48. }
  49. if(empty($list)) return [0, [], $header];
  50. # 获取短剧信息
  51. $playletIds = $list->pluck('playlet_id')->unique();
  52. $playletData = DramaSeries::query()
  53. ->select('id', 'name')
  54. ->whereIn('id', $playletIds)
  55. ->where('sys_group_id', $params['sys_group_id'])
  56. ->get();
  57. foreach ($list as $item) {
  58. # 短剧信息处理
  59. $playletInfo = $playletData->where('id', $item->playlet_id)->first();
  60. $item->playlet_name = $playletInfo->name ?? '-';
  61. if(isset($params['playlet_id']) && $params['playlet_id']) {
  62. unset($item->expense_date_end);
  63. }
  64. # 消耗金额单位处理
  65. $item->paid = $item->paid / 100;
  66. # 企微关注成本
  67. $item->follow_cost = round($item->follow_cost / 100, 2);
  68. # 首日roi
  69. $item->first_roi = round($item->first_roi * 100, 2) . '%';
  70. # 用户累计充值金额
  71. $item->pay_money_total = $item->pay_money_total / 100;
  72. # 首日下单成本
  73. $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
  74. # 首日新增用户充值金额
  75. $item->pay_money = $item->pay_money / 100;
  76. # 当天新增用户arpu
  77. $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
  78. # 总回本率
  79. $item->cost_cover_rate = round($item->cost_cover_rate * 100, 2) . '%';
  80. # 企微关注人数
  81. $item->scan_follow_count = intval($item->scan_follow_count);
  82. # 查询150天数据
  83. $dayInfo = [];
  84. $dayData = AdqPlayletTrendData::query()
  85. ->where('enable', 1)
  86. ->where(function($query) use ($params) {
  87. if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
  88. $query->where('plat_order_type', $params['plat_order_type']);
  89. })
  90. ->where('playlet_id', $item->playlet_id);
  91. if(isset($params['playlet_id']) && $params['playlet_id']) {
  92. $dayData = $dayData->where('expense_date', '=', $item->expense_date)
  93. ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date.' +149 day')));
  94. } else {
  95. $dayData = $dayData->where('expense_date', '>=', $item->expense_date)
  96. ->where('expense_date', '<=', $item->expense_date_end)
  97. ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date_end.' +149 day')));
  98. }
  99. $dayData = $dayData->selectRaw("sum(pay_user) as pay_user,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date, expense_date, days_type")
  100. ->groupBy(['days_type'])
  101. ->get();
  102. $paidDataMid = [];
  103. $payMoneyTotal = 0;
  104. if(!empty($dayData)){
  105. foreach($dayData as $val){
  106. $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
  107. $payMoneyTotal+=$val['pay_money'];
  108. if(isset($paidDataMid[$nd])) {
  109. $paidDataMid[$nd]['pay_user'] += $val['pay_user'];
  110. $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
  111. $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
  112. } else {
  113. $paidDataMid[$nd]['pay_user'] = $val['pay_user'];
  114. $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
  115. $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
  116. }
  117. }
  118. foreach ($paidDataMid as $i=>$v) {
  119. $new_roi = $item->paid ? round($v['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
  120. $total_roi = $item->paid ? round($v['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
  121. $income_times = $item->pay_money ? round($v['pay_money_total'] / 100 / $item->pay_money, 2) : '0%';
  122. $dayInfo[$i] = [
  123. 'days' => 'day' . ($i+1),
  124. 'pay_money' => round($v['pay_money']/100, 2),
  125. 'new_roi' => $new_roi,
  126. 'total_roi' => $total_roi,
  127. 'income_times' => $income_times,
  128. 'charge_user' => $v['pay_user']
  129. ];
  130. }
  131. }
  132. $item->day_info = $dayInfo;
  133. }
  134. return [$count, $list, $header];
  135. }
  136. public static function adqSummaryOfShortDramaDataTrends($params)
  137. {
  138. # 表头处理
  139. $header = config('shortPlayDataTrendHeader.summary');
  140. # 数据处理
  141. if(isset($params['playlet_id']) && $params['playlet_id']) {
  142. $data = AdqPlayletTrendData::shortPlayDataTrendCollect($params, [$params['playlet_id']]);
  143. $playletIdList = [$params['playlet_id']];
  144. } else {
  145. # 获取企业下的adq账号
  146. $playletIdList = DramaSeries::query()
  147. ->select('id')
  148. ->where('enable', 1)
  149. ->where('sys_group_id', $params['sys_group_id'])
  150. ->distinct()
  151. ->pluck('id');
  152. $data = AdqPlayletTrendData::shortPlayDataTrendCollect($params, $playletIdList);
  153. }
  154. if(empty($data)) return [[], $header];
  155. # 获取累计回收金额
  156. $recycleData = AdqPlayletTrendData::getShortPlayDataTrendCumulativeMoney($params, $playletIdList);
  157. # 获取首日累计回收金额
  158. $firstDayRecycleData = AdqPlayletTrendData::getShortPlayDataTrendFirstDayCumulativeMoney($params, $playletIdList);
  159. # 3天累计回收金额
  160. $threeDaysInfo = $recycleData->where('days_type', 3)->first();
  161. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  162. $data['three_days_rate'] = $firstDayRecycleData['three_days_pay_money'] ? round($threeDaysMoney / $firstDayRecycleData['three_days_pay_money'], 2) : 0;
  163. # 获取7天累计回收金额
  164. $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
  165. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  166. $data['seven_days_rate'] = $firstDayRecycleData['seven_days_pay_money'] ? round($sevenDaysMoney / $firstDayRecycleData['seven_days_pay_money'], 2) : 0;
  167. # 获取15天累计回收金额
  168. $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
  169. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  170. $data['fifteen_days_rate'] = $firstDayRecycleData['fifteen_days_pay_money'] ? round($fifteenDaysMoney / $firstDayRecycleData['fifteen_days_pay_money'], 2) : 0;
  171. # 获取30天累计回收金额
  172. $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
  173. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  174. $data['thirty_days_rate'] = $firstDayRecycleData['thirty_days_pay_money'] ? round($thirtyDaysMoney / $firstDayRecycleData['thirty_days_pay_money'], 2) : 0;
  175. # 消耗金额单位处理
  176. $data['paid'] = $data['paid'] / 100;
  177. # 企微关注成本
  178. $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
  179. # 首日roi
  180. $data['first_roi'] = round($data['first_roi'] * 100, 2) . '%';
  181. # 总回本率
  182. $data['cost_cover_rate'] = round($data['cost_cover_rate'] * 100, 2) . '%';
  183. # 首日新增用户充值金额
  184. $data['pay_money'] = $data['pay_money'] / 100;
  185. # 用户累计充值金额
  186. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  187. # 首日下单成本
  188. $data['first_charge_user_cost'] = round($data['first_charge_user_cost'] / 100, 2);
  189. # 累计下单成本
  190. $data['charge_user_cost'] = round($data['charge_user_cost'] / 100, 2);
  191. # 首日付费用户arpu
  192. $data['day_paid_user_arpu'] = round($data['day_paid_user_arpu'] / 100, 2);
  193. # 企微关注人数
  194. $data['scan_follow_count'] = intval($data['scan_follow_count']);
  195. # 短剧
  196. $data['playlet_name'] = '汇总';
  197. return [$data, $header];
  198. }
  199. public static function adqShortPlayDataTrendCurve($params)
  200. {
  201. if(isset($params['playlet_id']) && $params['playlet_id']) {
  202. $playletIdList = [$params['playlet_id']];
  203. } else {
  204. # 获取企业下的adq账号
  205. $playletIdList = DramaSeries::query()
  206. ->select('id')
  207. ->where('enable', 1)
  208. ->where('sys_group_id', $params['sys_group_id'])
  209. ->distinct()
  210. ->pluck('id');
  211. if(empty($playletIdList)) return [];
  212. }
  213. # Adq账号数据汇总曲线
  214. $data = AdqPlayletTrendData::getShortPlayDataTrendCurve($params, $playletIdList);
  215. # 获取累计回收金额
  216. $recycleData = AdqPlayletTrendData::getShortPlayDataTrendCumulativeMoney($params, $playletIdList, true);
  217. if(empty($data)) return [];
  218. # 数据处理
  219. foreach($data as $item) {
  220. # 消耗金额
  221. $item->paid = $item->paid / 100;
  222. # 首日roi
  223. $item->first_roi = round($item->first_roi * 100, 2);
  224. # 3天累计回收金额
  225. $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item->expense_date)->first();
  226. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  227. $item->three_days_rate = $item->pay_money ? round($threeDaysMoney * 100 / $item->pay_money, 2) : 0;
  228. # 获取7天累计回收金额
  229. $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item->expense_date)->first();
  230. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  231. $item->seven_days_rate = $item->pay_money ? round($sevenDaysMoney * 100 / $item->pay_money, 2) : 0;
  232. # 获取15天累计回收金额
  233. $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item->expense_date)->first();
  234. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  235. $item->fifteen_days_rate = $item->pay_money ? round($fifteenDaysMoney * 100 / $item->pay_money, 2) : 0;
  236. # 获取30天累计回收金额
  237. $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item->expense_date)->first();
  238. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  239. $item->thirty_days_rate = $item->pay_money ? round($thirtyDaysMoney * 100 / $item->pay_money, 2) : 0;
  240. # 首日新增用户充值金额
  241. $item->pay_money = $item->pay_money / 100;
  242. $item->day_paid_user_arpu = empty($item->day_paid_user_arpu) ? 0 : round($item->day_paid_user_arpu/100, 2);
  243. $item->first_charge_user_cost = empty($item->first_charge_user_cost) ? 0 : round($item->first_charge_user_cost/100, 2);
  244. }
  245. return $data;
  246. }
  247. public static function mpShortPlayDataTrendList($params, $page, $pageSize, $sortField, $sortType)
  248. {
  249. # 表头处理
  250. $commonHeader = config('shortPlayDataTrendHeader.basic');
  251. # 获取day1至day150表头
  252. $daysHeader = [];
  253. for($i = 0;$i < MpPlayletTrendData::PLAYLET_TREND_DATA_RECORD_DAYS;$i++) {
  254. $title = [
  255. 'name' => 'DAY'.($i+1),
  256. 'column' => 'day'.($i+1)
  257. ];
  258. $daysHeader[] = $title;
  259. }
  260. $header = array_merge($commonHeader, $daysHeader);
  261. # 查询数据
  262. if(isset($params['playlet_id']) && $params['playlet_id']) {
  263. list($list, $count) = MpPlayletTrendData::getShortPlayDataTrendTrend($params, $sortField, $sortType, $page, $pageSize);
  264. } else {
  265. # 获取企业下的adq账号
  266. $playletIdList = DramaSeries::query()
  267. ->select('id')
  268. ->where('enable', 1)
  269. ->where('sys_group_id', $params['sys_group_id'])
  270. ->distinct()
  271. ->pluck('id');
  272. if(empty($playletIdList)) return [0, [], $header];
  273. list($list, $count) = MpPlayletTrendData::getShortPlayDataTrendSummaryData(
  274. $params, $playletIdList, $sortField, $sortType, $page, $pageSize
  275. );
  276. }
  277. if(empty($list)) return [0, [], $header];
  278. # 获取短剧信息
  279. $playletIds = $list->pluck('playlet_id')->unique();
  280. $playletData = DramaSeries::query()
  281. ->select('id', 'name')
  282. ->whereIn('id', $playletIds)
  283. ->where('sys_group_id', $params['sys_group_id'])
  284. ->get();
  285. foreach ($list as $item) {
  286. # 短剧信息处理
  287. $playletInfo = $playletData->where('id', $item->playlet_id)->first();
  288. $item->playlet_name = $playletInfo->name ?? '-';
  289. if(isset($params['playlet_id']) && $params['playlet_id']) {
  290. unset($item->expense_date_end);
  291. }
  292. # 消耗金额单位处理
  293. $item->paid = $item->paid / 100;
  294. # 企微关注成本
  295. $item->follow_cost = round($item->follow_cost / 100, 1);
  296. # 首日roi
  297. $item->first_roi = round($item->first_roi * 100, 2) . '%';
  298. # 用户累计充值金额
  299. $item->pay_money_total = $item->pay_money_total / 100;
  300. # 首日下单成本
  301. $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
  302. # 首日新增用户充值金额
  303. $item->pay_money = $item->pay_money / 100;
  304. # 当天新增用户arpu
  305. $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
  306. # 总回本率
  307. $item->cost_cover_rate = round($item->cost_cover_rate * 100, 2) . '%';
  308. # 企微关注人数
  309. $item->scan_follow_count = intval($item->scan_follow_count);
  310. # 查询150天数据
  311. $dayInfo = [];
  312. $dayData = MpPlayletTrendData::query()
  313. ->where('enable', 1)
  314. ->where(function($query) use ($params) {
  315. if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
  316. $query->where('plat_order_type', $params['plat_order_type']);
  317. })
  318. ->where('playlet_id', $item->playlet_id);
  319. if(isset($params['playlet_id']) && $params['playlet_id']) {
  320. $dayData = $dayData->where('expense_date', '=', $item->expense_date)
  321. ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date.' +149 day')));
  322. } else {
  323. $dayData = $dayData->where('expense_date', '>=', $item->expense_date)
  324. ->where('expense_date', '<=', $item->expense_date_end)
  325. ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date_end.' +149 day')));
  326. }
  327. $dayData = $dayData->selectRaw("sum(pay_user) as pay_user,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date, expense_date, days_type")
  328. ->groupBy(['days_type'])
  329. ->get();
  330. $paidDataMid = [];
  331. $payMoneyTotal = 0;
  332. if(!empty($dayData)){
  333. foreach($dayData as $val){
  334. $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
  335. $payMoneyTotal += $val['pay_money'];
  336. if(isset($paidDataMid[$nd])) {
  337. $paidDataMid[$nd]['pay_user'] += $val['pay_user'];
  338. $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
  339. $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
  340. // $paidDataMid[$nd]['pay_money_total'] += $val['pay_money_total'];
  341. } else {
  342. $paidDataMid[$nd]['pay_user'] = $val['pay_user'];
  343. $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
  344. // $paidDataMid[$nd]['pay_money_total'] = $val['pay_money_total'];
  345. $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
  346. }
  347. }
  348. foreach ($paidDataMid as $i=>$v) {
  349. $new_roi = $item->paid ? round($v['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
  350. $total_roi = $item->paid ? round($v['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
  351. $income_times = $item->pay_money ? round($v['pay_money_total'] / 100 / $item->pay_money, 2) : '0%';
  352. $dayInfo[$i] = [
  353. 'days' => 'day' . ($i+1),
  354. 'pay_money' => round($v['pay_money']/100, 2),
  355. 'new_roi' => $new_roi,
  356. 'total_roi' => $total_roi,
  357. 'income_times' => $income_times,
  358. 'charge_user' => $v['pay_user']
  359. ];
  360. }
  361. }
  362. $item->day_info = $dayInfo;
  363. }
  364. return [$count, $list, $header];
  365. }
  366. public static function mpSummaryOfShortDramaDataTrends($params)
  367. {
  368. # 表头处理
  369. $header = config('shortPlayDataTrendHeader.summary');
  370. # 数据处理
  371. if(isset($params['playlet_id']) && $params['playlet_id']) {
  372. $data = MpPlayletTrendData::shortPlayDataTrendCollect($params, [$params['playlet_id']]);
  373. $playletIdList = [$params['playlet_id']];
  374. } else {
  375. # 获取企业下的adq账号
  376. $playletIdList = DramaSeries::query()
  377. ->select('id')
  378. ->where('enable', 1)
  379. ->where('sys_group_id', $params['sys_group_id'])
  380. ->distinct()
  381. ->pluck('id');
  382. $data = MpPlayletTrendData::shortPlayDataTrendCollect($params, $playletIdList);
  383. }
  384. if(empty($data)) return [[], $header];
  385. # 获取累计回收金额
  386. $recycleData = MpPlayletTrendData::getShortPlayDataTrendCumulativeMoney($params, $playletIdList);
  387. # 获取首日累计回收金额
  388. $firstDayRecycleData = MpPlayletTrendData::getShortPlayDataTrendFirstDayCumulativeMoney($params, $playletIdList);
  389. // # 3天累计回收金额
  390. // $threeDaysInfo = $recycleData->where('days_type', 3)->first();
  391. // $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  392. // $data->three_days_rate = $data->pay_money ? round($threeDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
  393. // # 获取7天累计回收金额
  394. // $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
  395. // $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  396. // $data->seven_days_rate = $data->pay_money ? round($sevenDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
  397. // # 获取15天累计回收金额
  398. // $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
  399. // $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  400. // $data->fifteen_days_rate = $data->pay_money ? round($fifteenDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
  401. // # 获取30天累计回收金额
  402. // $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
  403. // $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  404. // $data->thirty_days_rate = $data->pay_money ? round($thirtyDaysMoney / $data->pay_money, 4) * 100 . "%" : "0%";
  405. // # 消耗金额单位处理
  406. // $data->paid = $data->paid / 100;
  407. // # 企微关注成本
  408. // $data->follow_cost = round($data->follow_cost / 100, 1);
  409. // # 首日roi
  410. // $data->first_roi = $data->first_roi * 100 . '%';
  411. // # 总回本率
  412. // $data->cost_cover_rate = $data->cost_cover_rate * 100 . '%';
  413. // # 首日新增用户充值金额
  414. // $data->pay_money = $data->pay_money / 100;
  415. // # 用户累计充值金额
  416. // $data->pay_money_total = $data->pay_money_total / 100;
  417. // # 首日下单成本
  418. // $data->first_charge_user_cost = round($data->first_charge_user_cost / 100, 2);
  419. // # 累计下单成本
  420. // $data->charge_user_cost = round($data->charge_user_cost / 100, 2);
  421. // # 首日付费用户arpu
  422. // $data->day_paid_user_arpu = round($data->day_paid_user_arpu / 100, 2);
  423. // # 企微关注人数
  424. // $data->scan_follow_count = intval($data->scan_follow_count);
  425. // # 短剧
  426. // $data->playlet_name = '汇总';
  427. # 3天累计回收金额
  428. $threeDaysInfo = $recycleData->where('days_type', 3)->first();
  429. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  430. $data['three_days_rate'] = $firstDayRecycleData['three_days_pay_money'] ? round($threeDaysMoney / $firstDayRecycleData['three_days_pay_money'], 2) : 0;
  431. # 获取7天累计回收金额
  432. $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
  433. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  434. $data['seven_days_rate'] = $firstDayRecycleData['seven_days_pay_money'] ? round($sevenDaysMoney / $firstDayRecycleData['seven_days_pay_money'], 2) : 0;
  435. # 获取15天累计回收金额
  436. $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
  437. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  438. $data['fifteen_days_rate'] = $firstDayRecycleData['fifteen_days_pay_money'] ? round($fifteenDaysMoney / $firstDayRecycleData['fifteen_days_pay_money'], 2) : 0;
  439. # 获取30天累计回收金额
  440. $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
  441. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  442. $data['thirty_days_rate'] = $firstDayRecycleData['thirty_days_pay_money'] ? round($thirtyDaysMoney / $firstDayRecycleData['thirty_days_pay_money'], 2) : 0;
  443. # 消耗金额单位处理
  444. $data['paid'] = $data['paid'] / 100;
  445. # 企微关注成本
  446. $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
  447. # 首日roi
  448. $data['first_roi'] = round($data['first_roi'] * 100, 2) . '%';
  449. # 总回本率
  450. $data['cost_cover_rate'] = round($data['cost_cover_rate'] * 100, 2) . '%';
  451. # 首日新增用户充值金额
  452. $data['pay_money'] = $data['pay_money'] / 100;
  453. # 用户累计充值金额
  454. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  455. # 首日下单成本
  456. $data['first_charge_user_cost'] = round($data['first_charge_user_cost'] / 100, 2);
  457. # 累计下单成本
  458. $data['charge_user_cost'] = round($data['charge_user_cost'] / 100, 2);
  459. # 首日付费用户arpu
  460. $data['day_paid_user_arpu'] = round($data['day_paid_user_arpu'] / 100, 2);
  461. # 企微关注人数
  462. $data['scan_follow_count'] = intval($data['scan_follow_count']);
  463. # 短剧
  464. $data['playlet_name'] = '汇总';
  465. return [$data, $header];
  466. }
  467. public static function mpShortPlayDataTrendCurve($params)
  468. {
  469. if(isset($params['playlet_id']) && $params['playlet_id']) {
  470. $playletIdList = [$params['playlet_id']];
  471. } else {
  472. # 获取企业下的adq账号
  473. $playletIdList = DramaSeries::query()
  474. ->select('id')
  475. ->where('enable', 1)
  476. ->where('sys_group_id', $params['sys_group_id'])
  477. ->distinct()
  478. ->pluck('id');
  479. if(empty($playletIdList)) return [];
  480. }
  481. # Adq账号数据汇总曲线
  482. $data = MpPlayletTrendData::getShortPlayDataTrendCurve($params, $playletIdList);
  483. # 获取累计回收金额
  484. $recycleData = MpPlayletTrendData::getShortPlayDataTrendCumulativeMoney($params, $playletIdList, true);
  485. if(empty($data)) return [];
  486. # 数据处理
  487. foreach($data as $item) {
  488. # 消耗金额
  489. $item->paid = $item->paid / 100;
  490. # 首日roi
  491. $item->first_roi = round($item->first_roi * 100, 2);
  492. # 3天累计回收金额
  493. $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item->expense_date)->first();
  494. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  495. $item->three_days_rate = $item->pay_money ? round($threeDaysMoney * 100 / $item->pay_money, 2) : 0;
  496. # 获取7天累计回收金额
  497. $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item->expense_date)->first();
  498. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  499. $item->seven_days_rate = $item->pay_money ? round($sevenDaysMoney * 100 / $item->pay_money, 2) : 0;
  500. # 获取15天累计回收金额
  501. $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item->expense_date)->first();
  502. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  503. $item->fifteen_days_rate = $item->pay_money ? round($fifteenDaysMoney * 100 / $item->pay_money, 2) : 0;
  504. # 获取30天累计回收金额
  505. $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item->expense_date)->first();
  506. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  507. $item->thirty_days_rate = $item->pay_money ? round($thirtyDaysMoney * 100 / $item->pay_money, 2) : 0;
  508. # 首日新增用户充值金额
  509. $item->pay_money = $item->pay_money / 100;
  510. $item->day_paid_user_arpu = empty($item->day_paid_user_arpu) ? 0 : round($item->day_paid_user_arpu/100, 2);
  511. $item->first_charge_user_cost = empty($item->first_charge_user_cost) ? 0 : round($item->first_charge_user_cost/100, 2);
  512. }
  513. return $data;
  514. }
  515. public static function shortPlayDataTrendList($params, $page, $pageSize, $sortField, $sortType)
  516. {
  517. # 表头处理
  518. $commonHeader = config('shortPlayDataTrendHeader.basic');
  519. # 获取day1至day150表头
  520. $daysHeader = [];
  521. for($i = 0;$i < PlayletDataTrend::PLAYLET_TREND_DATA_RECORD_DAYS;$i++) {
  522. $title = [
  523. 'name' => 'DAY'.($i+1),
  524. 'column' => 'day'.($i+1)
  525. ];
  526. $daysHeader[] = $title;
  527. }
  528. $header = array_merge($commonHeader, $daysHeader);
  529. # 查询数据
  530. if(isset($params['playlet_id']) && $params['playlet_id']) {
  531. list($list, $count) = PlayletDataTrend::getShortPlayDataTrendTrend($params, $sortField, $sortType, $page, $pageSize);
  532. } else {
  533. # 获取企业下的adq账号
  534. $playletIdList = DramaSeries::query()
  535. ->select('id')
  536. ->where('enable', 1)
  537. ->where('sys_group_id', $params['sys_group_id'])
  538. ->distinct()
  539. ->pluck('id');
  540. if(empty($playletIdList)) return [0, [], $header];
  541. list($list, $count) = PlayletDataTrend::getShortPlayDataTrendSummaryData(
  542. $params, $playletIdList, $sortField, $sortType, $page, $pageSize
  543. );
  544. }
  545. if(empty($list)) return [0, [], $header];
  546. # 获取短剧信息
  547. $playletIds = $list->pluck('playlet_id')->unique();
  548. $playletData = DramaSeries::query()
  549. ->select('id', 'name')
  550. ->whereIn('id', $playletIds)
  551. ->where('sys_group_id', $params['sys_group_id'])
  552. ->get();
  553. foreach ($list as $item) {
  554. # 短剧信息处理
  555. $playletInfo = $playletData->where('id', $item->playlet_id)->first();
  556. $item->playlet_name = $playletInfo->name ?? '-';
  557. if(isset($params['playlet_id']) && $params['playlet_id']) {
  558. unset($item->expense_date_end);
  559. }
  560. # 消耗金额单位处理
  561. $item->paid = $item->paid / 100;
  562. # 企微关注成本
  563. $item->follow_cost = round($item->follow_cost / 100, 1);
  564. # 首日roi
  565. $item->first_roi = round($item->first_roi * 100, 2) . '%';
  566. # 用户累计充值金额
  567. $item->pay_money_total = $item->pay_money_total / 100;
  568. # 首日下单成本
  569. $item->first_charge_user_cost = round($item->first_charge_user_cost / 100, 2);
  570. # 首日新增用户充值金额
  571. $item->pay_money = $item->pay_money / 100;
  572. # 当天新增用户arpu
  573. $item->day_paid_user_arpu = round($item->day_paid_user_arpu / 100, 2);
  574. # 总回本率
  575. $item->cost_cover_rate = round($item->cost_cover_rate * 100, 2) . '%';
  576. # 企微关注人数
  577. $item->scan_follow_count = intval($item->scan_follow_count);
  578. # 查询150天数据
  579. $dayInfo = [];
  580. $dayData = PlayletDataTrend::query()
  581. ->where('enable', 1)
  582. ->where(function($query) use ($params) {
  583. if(isset($params['plat_order_type']) && is_numeric($params['plat_order_type']))
  584. $query->where('plat_order_type', $params['plat_order_type']);
  585. })
  586. ->where('playlet_id', $item->playlet_id);
  587. if(isset($params['playlet_id']) && $params['playlet_id']) {
  588. $dayData = $dayData->where('expense_date', '=', $item->expense_date)
  589. ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date.' +149 day')));
  590. } else {
  591. $dayData = $dayData->where('expense_date', '>=', $item->expense_date)
  592. ->where('expense_date', '<=', $item->expense_date_end)
  593. ->where('ref_date', '<=', date('Y-m-d', strtotime($item->expense_date_end.' +149 day')));
  594. }
  595. $dayData = $dayData->selectRaw("sum(pay_user) as pay_user,sum(pay_money) as pay_money, sum(pay_money_total) as pay_money_total, ref_date, expense_date, days_type")
  596. ->groupBy(['days_type'])
  597. ->get();
  598. $paidDataMid = [];
  599. $payMoneyTotal = 0;
  600. if(!empty($dayData)){
  601. foreach($dayData as $val){
  602. $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
  603. $payMoneyTotal+=$val['pay_money'];
  604. if(isset($paidDataMid[$nd])) {
  605. $paidDataMid[$nd]['pay_user'] += $val['pay_user'];
  606. $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
  607. $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
  608. } else {
  609. $paidDataMid[$nd]['pay_user'] = $val['pay_user'];
  610. $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
  611. $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
  612. }
  613. }
  614. foreach ($paidDataMid as $i=>$v) {
  615. $new_roi = $item->paid ? round($v['pay_money'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
  616. $total_roi = $item->paid ? round($v['pay_money_total'] / 100 / $item->paid, 4) * 100 . '%' : '0%';
  617. $income_times = $item->pay_money ? round($v['pay_money_total'] / 100 / $item->pay_money, 2) : '0%';
  618. $dayInfo[$i] = [
  619. 'days' => 'day' . ($i+1),
  620. 'pay_money' => round($v['pay_money']/100, 2),
  621. 'new_roi' => $new_roi,
  622. 'total_roi' => $total_roi,
  623. 'income_times' => $income_times,
  624. 'charge_user' => $v['pay_user']
  625. ];
  626. }
  627. }
  628. $item->day_info = $dayInfo;
  629. }
  630. return [$count, $list, $header];
  631. }
  632. public static function summaryOfShortDramaDataTrends($params)
  633. {
  634. # 表头处理
  635. $header = config('shortPlayDataTrendHeader.summary');
  636. # 数据处理
  637. if(isset($params['playlet_id']) && $params['playlet_id']) {
  638. $data = PlayletDataTrend::shortPlayDataTrendCollect($params, [$params['playlet_id']]);
  639. $playletIdList = [$params['playlet_id']];
  640. } else {
  641. # 获取企业下的adq账号
  642. $playletIdList = DramaSeries::query()
  643. ->select('id')
  644. ->where('enable', 1)
  645. ->where('sys_group_id', $params['sys_group_id'])
  646. ->distinct()
  647. ->pluck('id');
  648. $data = PlayletDataTrend::shortPlayDataTrendCollect($params, $playletIdList);
  649. }
  650. if(empty($data)) return [[], $header];
  651. # 获取累计回收金额
  652. $recycleData = PlayletDataTrend::getShortPlayDataTrendCumulativeMoney($params, $playletIdList);
  653. # 获取首日累计回收金额
  654. $firstDayRecycleData = PlayletDataTrend::getShortPlayDataTrendFirstDayCumulativeMoney($params, $playletIdList);
  655. # 3天累计回收金额
  656. $threeDaysInfo = $recycleData->where('days_type', 3)->first();
  657. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  658. $data['three_days_rate'] = $firstDayRecycleData['three_days_pay_money'] ? round($threeDaysMoney / $firstDayRecycleData['three_days_pay_money'], 2) : 0;
  659. # 获取7天累计回收金额
  660. $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
  661. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  662. $data['seven_days_rate'] = $firstDayRecycleData['seven_days_pay_money'] ? round($sevenDaysMoney / $firstDayRecycleData['seven_days_pay_money'], 2) : 0;
  663. # 获取15天累计回收金额
  664. $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
  665. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  666. $data['fifteen_days_rate'] = $firstDayRecycleData['fifteen_days_pay_money'] ? round($fifteenDaysMoney / $firstDayRecycleData['fifteen_days_pay_money'], 2) : 0;
  667. # 获取30天累计回收金额
  668. $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
  669. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  670. $data['thirty_days_rate'] = $firstDayRecycleData['thirty_days_pay_money'] ? round($thirtyDaysMoney / $firstDayRecycleData['thirty_days_pay_money'], 2) : 0;
  671. # 消耗金额单位处理
  672. $data['paid'] = $data['paid'] / 100;
  673. # 企微关注成本
  674. $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
  675. # 首日roi
  676. $data['first_roi'] = round($data['first_roi'] * 100, 2) . '%';
  677. # 总回本率
  678. $data['cost_cover_rate'] = round($data['cost_cover_rate'] * 100, 2) . '%';
  679. # 首日新增用户充值金额
  680. $data['pay_money'] = $data['pay_money'] / 100;
  681. # 用户累计充值金额
  682. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  683. # 首日下单成本
  684. $data['first_charge_user_cost'] = round($data['first_charge_user_cost'] / 100, 2);
  685. # 累计下单成本
  686. $data['charge_user_cost'] = round($data['charge_user_cost'] / 100, 2);
  687. # 首日付费用户arpu
  688. $data['day_paid_user_arpu'] = round($data['day_paid_user_arpu'] / 100, 2);
  689. # 企微关注人数
  690. $data['scan_follow_count'] = intval($data['scan_follow_count']);
  691. # 短剧
  692. $data['playlet_name'] = '汇总';
  693. return [$data, $header];
  694. }
  695. public static function shortPlayDataTrendCurve($params)
  696. {
  697. if(isset($params['playlet_id']) && $params['playlet_id']) {
  698. $playletIdList = [$params['playlet_id']];
  699. } else {
  700. # 获取企业下的adq账号
  701. $playletIdList = DramaSeries::query()
  702. ->select('id')
  703. ->where('enable', 1)
  704. ->where('sys_group_id', $params['sys_group_id'])
  705. ->distinct()
  706. ->pluck('id');
  707. if(empty($playletIdList)) return [];
  708. }
  709. # Adq账号数据汇总曲线
  710. $data = PlayletDataTrend::getShortPlayDataTrendCurve($params, $playletIdList);
  711. # 获取累计回收金额
  712. $recycleData = PlayletDataTrend::getShortPlayDataTrendCumulativeMoney($params, $playletIdList, true);
  713. if(empty($data)) return [];
  714. # 数据处理
  715. foreach($data as $item) {
  716. # 消耗金额
  717. $item->paid = $item->paid / 100;
  718. # 首日roi
  719. $item->first_roi = round($item->first_roi * 100, 2);
  720. # 3天累计回收金额
  721. $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item->expense_date)->first();
  722. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  723. $item->three_days_rate = $item->pay_money ? round($threeDaysMoney * 100 / $item->pay_money, 2) : 0;
  724. # 获取7天累计回收金额
  725. $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item->expense_date)->first();
  726. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  727. $item->seven_days_rate = $item->pay_money ? round($sevenDaysMoney * 100 / $item->pay_money, 2) : 0;
  728. # 获取15天累计回收金额
  729. $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item->expense_date)->first();
  730. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  731. $item->fifteen_days_rate = $item->pay_money ? round($fifteenDaysMoney * 100 / $item->pay_money, 2) : 0;
  732. # 获取30天累计回收金额
  733. $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item->expense_date)->first();
  734. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  735. $item->thirty_days_rate = $item->pay_money ? round($thirtyDaysMoney * 100 / $item->pay_money, 2) : 0;
  736. # 首日新增用户充值金额
  737. $item->pay_money = $item->pay_money / 100;
  738. $item->day_paid_user_arpu = empty($item->day_paid_user_arpu) ? 0 : round($item->day_paid_user_arpu/100, 2);
  739. $item->first_charge_user_cost = empty($item->first_charge_user_cost) ? 0 : round($item->first_charge_user_cost/100, 2);
  740. }
  741. return $data;
  742. }
  743. public static function shortPlayDataTrendListByAccount($params, $page, $pageSize, $sortField, $sortType)
  744. {
  745. $sortField = self::getSortField($sortField);
  746. # 表头处理
  747. $commonHeader = config('shortPlayDataTrendHeader.basic');
  748. # 获取day1至day150表头
  749. $daysHeader = [];
  750. for($i = 0;$i < PlayletAccountTrendData::PLAYLET_TREND_DATA_RECORD_DAYS;$i++) {
  751. $title = [
  752. 'name' => 'DAY'.($i+1),
  753. 'column' => 'day'.($i+1)
  754. ];
  755. $daysHeader[] = $title;
  756. }
  757. $header = array_merge($commonHeader, $daysHeader);
  758. # 查询当前登录用户可见投放账号列表
  759. if(!empty($params['ad_type'])) {
  760. if(2 == $params['ad_type']) {
  761. $accountList = OfficialAccount::getAccountMpAppIdList($params['admin_id'], $params['sys_group_id']
  762. , $params['is_system_admin']);
  763. } else {
  764. $accountList = PitcherService::adqAccountListForUser($params['admin_id'], $params['sys_group_id']
  765. , $params['is_system_admin']);
  766. }
  767. } else {
  768. $accountList = PitcherService::adAccountList(null, $params['sys_group_id'], $params['admin_id']
  769. , $params['is_system_admin']);
  770. $accountList = array_column($accountList, 'account_id');
  771. }
  772. if(empty($accountList)){
  773. return [0, [], $header];
  774. }
  775. # 查询数据
  776. if(isset($params['playlet_id']) && $params['playlet_id']) {
  777. list($list, $count) = PlayletAccountDataTrend::getShortPlayDataTrend($params, $sortField, $sortType
  778. , $page, $pageSize, $accountList);
  779. } else {
  780. # 获取企业下的adq账号
  781. $playletIdList = DramaSeries::query()
  782. ->select('id')
  783. ->where('enable', 1)
  784. ->where('sys_group_id', $params['sys_group_id'])
  785. ->distinct()
  786. ->pluck('id')->toArray();
  787. if(empty($playletIdList)) return [0, [], $header];
  788. list($list, $count) = PlayletAccountDataTrend::getShortPlayDataTrendSummaryData($params, $playletIdList
  789. , $sortField, $sortType, $page, $pageSize, $accountList
  790. );
  791. }
  792. if(empty($list)) return [0, [], $header];
  793. # 获取短剧信息
  794. $playletIds = array_unique(array_column($list, 'playlet_id'));
  795. $playletData = DramaSeries::query()
  796. ->select('id', 'name')
  797. ->whereIn('id', $playletIds)
  798. ->where('sys_group_id', $params['sys_group_id'])
  799. ->get();
  800. foreach ($list as &$item) {
  801. # 短剧信息处理
  802. $playletInfo = $playletData->where('id', $item['playlet_id'])->first();
  803. $item['playlet_name'] = $playletInfo->name ?? '-';
  804. # 消耗金额单位处理
  805. $item['paid'] = $item['paid'] / 100;
  806. # 企微关注成本
  807. $item['follow_cost'] = round($item['follow_cost'] / 100, 2);
  808. # 首日roi
  809. $item['first_roi'] = round($item['first_roi'] * 100, 2) . '%';
  810. # 用户累计充值金额
  811. $item['pay_money_total'] = $item['pay_money_total'] / 100;
  812. # 首日下单成本
  813. $item['first_charge_user_cost'] = round($item['first_charge_user_cost'] / 100, 2);
  814. # 首日新增用户充值金额
  815. $item['pay_money'] = $item['pay_money'] / 100;
  816. # 当天新增用户arpu
  817. $item['day_paid_user_arpu'] = round($item['day_paid_user_arpu'] / 100, 2);
  818. # 总回本率
  819. $item['cost_cover_rate'] = round($item['cost_cover_rate'] * 100, 2) . '%';
  820. # 企微关注人数
  821. $item['scan_follow_count'] = intval($item['scan_follow_count']);
  822. # 首单充值金额
  823. $item['day_first_pay_money'] = $item['day_first_pay_money'] / 100;
  824. # 首单roi
  825. $item['first_pay_roi'] = round($item['first_pay_roi'] * 100) . '%';
  826. # 企微加粉人数
  827. $item['followers_number'] = intval($item['followers_number']);
  828. # 企微加粉成本
  829. $item['followers_cost'] = round($item['followers_cost'] / 100, 2);
  830. # 查询150天数据
  831. $dayInfo = [];
  832. if(isset($params['playlet_id']) && $params['playlet_id']) {
  833. $dayData = PlayletAccountDataTrend::getDataAfterExpense($params, $item['expense_date'], null,
  834. $item['playlet_id'], $accountList);
  835. if(!empty($dayData)){
  836. foreach($dayData as $val){
  837. $nd = (strtotime($val['ref_date']) - strtotime($item['expense_date'])) / 86400;
  838. $new_roi = $item['paid'] ? round($val['pay_money'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
  839. $total_roi = $item['paid'] ? round($val['pay_money_total'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
  840. $income_times = $item['pay_money'] ? round($val['pay_money_total'] / 100 / $item['pay_money'], 2) : 0;
  841. $dayInfo[$nd] = [
  842. 'days' => 'day' . ($nd+1),
  843. 'pay_money' => round($val['pay_money']/100, 2),
  844. 'new_roi' => $new_roi,
  845. 'total_roi' => $total_roi,
  846. 'income_times' => $income_times,
  847. 'charge_user' => $val['pay_user']
  848. ];
  849. }
  850. }
  851. } else {
  852. $dayData = PlayletAccountDataTrend::getDataAfterExpense($params, $item['expense_date'], $item['expense_date_end'],
  853. $item['playlet_id'], $accountList);
  854. $paidDataMid = [];
  855. $payMoneyTotal = 0;
  856. if(!empty($dayData)){
  857. foreach($dayData as $val){
  858. $nd = (strtotime($val['ref_date']) - strtotime($val['expense_date'])) / 86400;
  859. $payMoneyTotal += $val['pay_money'];
  860. if(isset($paidDataMid[$nd])) {
  861. $paidDataMid[$nd]['active_fans'] += $val['active_fans'];
  862. $paidDataMid[$nd]['pay_user'] += $val['pay_user'];
  863. $paidDataMid[$nd]['pay_money'] += $val['pay_money'];
  864. $paidDataMid[$nd]['pay_money_total'] += $payMoneyTotal;
  865. } else {
  866. $paidDataMid[$nd]['active_fans'] = $val['active_fans'];
  867. $paidDataMid[$nd]['pay_user'] = $val['pay_user'];
  868. $paidDataMid[$nd]['pay_money'] = $val['pay_money'];
  869. $paidDataMid[$nd]['pay_money_total'] = $payMoneyTotal;
  870. }
  871. }
  872. foreach ($paidDataMid as $i=>$v) {
  873. $new_roi = $item['paid'] ? round($v['pay_money'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
  874. $total_roi = $item['paid'] ? round($v['pay_money_total'] / 100 / $item['paid'], 4) * 100 . '%' : '0%';
  875. $income_times = $item['pay_money'] ? round($v['pay_money_total'] / 100 / $item['pay_money'], 2) : 0;
  876. $dayInfo[$i] = [
  877. 'days' => 'day' . ($i+1),
  878. 'pay_money' => round($v['pay_money']/100, 2),
  879. 'new_roi' => $new_roi,
  880. 'total_roi' => $total_roi,
  881. 'income_times' => $income_times,
  882. 'charge_user' => $v['pay_user']
  883. ];
  884. }
  885. }
  886. }
  887. $item['day_info'] = $dayInfo;
  888. }
  889. return [$count, $list, $header];
  890. }
  891. public static function summaryOfShortDramaDataTrendsByAccount($params)
  892. {
  893. # 表头处理
  894. $header = config('shortPlayDataTrendHeader.summary');
  895. # 查询当前登录用户可见投放账号列表
  896. if(!empty($params['ad_type'])) {
  897. if(2 == $params['ad_type']) {
  898. $accountList = OfficialAccount::getAccountMpAppIdList($params['admin_id'], $params['sys_group_id'], $params['is_system_admin']);
  899. } else {
  900. $accountList = PitcherService::adqAccountListForUser($params['admin_id'], $params['sys_group_id'], $params['is_system_admin']);
  901. }
  902. } else {
  903. $accountList = PitcherService::adAccountList(null, $params['sys_group_id'], $params['admin_id'], $params['is_system_admin']);
  904. $accountList = array_column($accountList, 'account_id');
  905. }
  906. if(empty($accountList)){
  907. return [[], $header];
  908. }
  909. # 数据处理
  910. if(isset($params['playlet_id']) && $params['playlet_id']) {
  911. $data = PlayletAccountDataTrend::shortPlayDataTrendCollect($params, [$params['playlet_id']], $accountList);
  912. $playletIdList = [$params['playlet_id']];
  913. } else {
  914. # 获取企业下的adq账号
  915. $playletIdList = DramaSeries::query()
  916. ->select('id')
  917. ->where('enable', 1)
  918. ->where('sys_group_id', $params['sys_group_id'])
  919. ->distinct()
  920. ->pluck('id');
  921. $data = PlayletAccountDataTrend::shortPlayDataTrendCollect($params, $playletIdList, $accountList);
  922. }
  923. if(empty($data)) return [[], $header];
  924. # 获取累计回收金额
  925. $recycleData = collect(PlayletAccountDataTrend::getCumulativeMoney($params, $playletIdList, $accountList, [1, 3, 7, 15, 30]));
  926. # 3日消耗最早日期和最晚日期
  927. $threeDaysBegin = $recycleData->where('days_type', 3)->min('expense_date');
  928. $threeDaysEnd = $recycleData->where('days_type', 3)->max('expense_date');
  929. # 首日新增用户累计充值金额
  930. $threeDaysFirstPaid = $recycleData->where('expense_date', '>=', $threeDaysBegin)
  931. ->where('expense_date', '<=', $threeDaysEnd)->where('days_type', 1)
  932. ->sum('pay_money');
  933. # 3天累计回收金额
  934. $threeDaysMoney = $recycleData->where('days_type', 3)->sum('pay_money_total');
  935. $data['three_days_rate'] = $threeDaysFirstPaid ? round($threeDaysMoney / $threeDaysFirstPaid, 2) : "0";
  936. # 7日消耗最早日期和最晚日期
  937. $sevenDaysBegin = $recycleData->where('days_type', 7)->min('expense_date');
  938. $sevenDaysEnd = $recycleData->where('days_type', 7)->max('expense_date');
  939. # 首日新增用户累计充值金额
  940. $sevenDaysFirstPaid = $recycleData->where('expense_date', '>=', $sevenDaysBegin)
  941. ->where('expense_date', '<=', $sevenDaysEnd)->where('days_type', 1)
  942. ->sum('pay_money');
  943. # 7天累计回收金额
  944. $sevenDaysMoney = $recycleData->where('days_type', 7)->sum('pay_money_total');
  945. $data['seven_days_rate'] = $sevenDaysFirstPaid ? round($sevenDaysMoney / $sevenDaysFirstPaid, 2) : "0";
  946. # 15日消耗最早日期和最晚日期
  947. $fifteenDaysBegin = $recycleData->where('days_type', 15)->min('expense_date');
  948. $fifteenDaysEnd = $recycleData->where('days_type', 15)->max('expense_date');
  949. # 首日新增用户累计充值金额
  950. $fifteenDaysFirstPaid = $recycleData->where('expense_date', '>=', $fifteenDaysBegin)
  951. ->where('expense_date', '<=', $fifteenDaysEnd)->where('days_type', 1)
  952. ->sum('pay_money');
  953. # 15天累计回收金额
  954. $fifteenDaysMoney = $recycleData->where('days_type', 15)->sum('pay_money_total');
  955. $data['fifteen_days_rate'] = $fifteenDaysFirstPaid ? round($fifteenDaysMoney / $fifteenDaysFirstPaid, 2) : "0";
  956. # 30日消耗最早日期和最晚日期
  957. $thirtyDaysBegin = $recycleData->where('days_type', 30)->min('expense_date');
  958. $thirtyDaysEnd = $recycleData->where('days_type', 30)->max('expense_date');
  959. # 首日新增用户累计充值金额
  960. $thirtyDaysFirstPaid = $recycleData->where('expense_date', '>=', $thirtyDaysBegin)
  961. ->where('expense_date', '<=', $thirtyDaysEnd)->where('days_type', 1)
  962. ->sum('pay_money');
  963. # 30天累计回收金额
  964. $thirtyDaysMoney = $recycleData->where('days_type', 30)->sum('pay_money_total');
  965. $data['thirty_days_rate'] = $thirtyDaysFirstPaid ? round($thirtyDaysMoney / $thirtyDaysFirstPaid, 2) : "0";
  966. // # 3天累计回收金额
  967. // $threeDaysInfo = $recycleData->where('days_type', 3)->first();
  968. // $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  969. // $data['three_days_rate'] = $firstDayRecycleData['three_days_pay_money'] ?
  970. // round($threeDaysMoney / $firstDayRecycleData['three_days_pay_money'], 2) : 0;
  971. // # 获取7天累计回收金额
  972. // $sevenDaysInfo = $recycleData->where('days_type', 7)->first();
  973. // $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  974. // $data['seven_days_rate'] = $firstDayRecycleData['seven_days_pay_money'] ?
  975. // round($sevenDaysMoney / $firstDayRecycleData['seven_days_pay_money'], 2) : 0;
  976. // # 获取15天累计回收金额
  977. // $fifteenDaysInfo = $recycleData->where('days_type', 15)->first();
  978. // $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  979. // $data['fifteen_days_rate'] = $firstDayRecycleData['fifteen_days_pay_money'] ?
  980. // round($fifteenDaysMoney / $firstDayRecycleData['fifteen_days_pay_money'], 2) : 0;
  981. // # 获取30天累计回收金额
  982. // $thirtyDaysInfo = $recycleData->where('days_type', 30)->first();
  983. // $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  984. // $data['thirty_days_rate'] = $firstDayRecycleData['thirty_days_pay_money'] ?
  985. // round($thirtyDaysMoney / $firstDayRecycleData['thirty_days_pay_money'], 2) : 0;
  986. # 消耗金额单位处理
  987. $data['paid'] = $data['paid'] / 100;
  988. # 企微关注成本
  989. $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
  990. # 首日roi
  991. $data['first_roi'] = round($data['first_roi'] * 100, 2) . '%';
  992. # 总回本率
  993. $data['cost_cover_rate'] = round($data['cost_cover_rate'] * 100, 2) . '%';
  994. # 首日新增用户充值金额
  995. $data['pay_money'] = $data['pay_money'] / 100;
  996. # 用户累计充值金额
  997. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  998. # 首日下单成本
  999. $data['first_charge_user_cost'] = round($data['first_charge_user_cost'] / 100, 2);
  1000. # 累计下单成本
  1001. // $data['charge_user_cost'] = round($data['charge_user_cost'] / 100, 2);
  1002. # 首日付费用户arpu
  1003. $data['day_paid_user_arpu'] = round($data['day_paid_user_arpu'] / 100, 2);
  1004. # 企微关注人数
  1005. $data['scan_follow_count'] = intval($data['scan_follow_count']);
  1006. # 短剧
  1007. $data['playlet_name'] = '汇总';
  1008. # 企微加粉人数
  1009. $data['followers_number'] = intval($data['followers_number']);
  1010. # 企微加粉成本
  1011. $data['followers_cost'] = round($data['followers_cost'] / 100, 2);
  1012. return [$data, $header];
  1013. }
  1014. public static function shortPlayDataTrendCurveByAccount($params)
  1015. {
  1016. # 查询当前登录用户可见投放账号列表
  1017. if(!empty($params['ad_type'])) {
  1018. if(2 == $params['ad_type']) {
  1019. $accountList = OfficialAccount::getAccountMpAppIdList($params['admin_id'], $params['sys_group_id'], $params['is_system_admin']);
  1020. } else {
  1021. $accountList = PitcherService::adqAccountListForUser($params['admin_id'], $params['sys_group_id'], $params['is_system_admin']);
  1022. }
  1023. } else {
  1024. $accountList = PitcherService::adAccountList(null, $params['sys_group_id'], $params['admin_id'], $params['is_system_admin']);
  1025. $accountList = array_column($accountList, 'account_id');
  1026. }
  1027. if(empty($accountList)){
  1028. return [];
  1029. }
  1030. if(isset($params['playlet_id']) && $params['playlet_id']) {
  1031. $playletIdList = [$params['playlet_id']];
  1032. } else {
  1033. # 获取企业下的adq账号
  1034. $playletIdList = DramaSeries::query()
  1035. ->select('id')
  1036. ->where('enable', 1)
  1037. ->where('sys_group_id', $params['sys_group_id'])
  1038. ->distinct()
  1039. ->pluck('id');
  1040. if(empty($playletIdList)) return [];
  1041. }
  1042. # Adq账号数据汇总曲线
  1043. $data = PlayletAccountDataTrend::getAccountDataCurve($params, $playletIdList, $accountList);
  1044. # 获取累计回收金额
  1045. $recycleData = collect(PlayletAccountDataTrend::getCumulativeMoney($params, $playletIdList, $accountList, [3, 7, 15, 30]));
  1046. if(empty($data)) return [];
  1047. # 数据处理
  1048. foreach($data as &$item) {
  1049. # 消耗金额
  1050. $item['paid'] = $item['paid'] / 100;
  1051. # 首日roi
  1052. $item['first_roi'] = round($item['first_roi'] * 100, 2);
  1053. # 3天累计回收金额
  1054. $threeDaysInfo = $recycleData->where('days_type', 3)->where('expense_date', $item['expense_date'])->first();
  1055. $threeDaysMoney = $threeDaysInfo->pay_money_total ?? 0;
  1056. $item['three_days_rate'] = $item['pay_money'] ? round($threeDaysMoney * 100 / $item['pay_money'], 2) : 0;
  1057. # 获取7天累计回收金额
  1058. $sevenDaysInfo = $recycleData->where('days_type', 7)->where('expense_date', $item['expense_date'])->first();
  1059. $sevenDaysMoney = $sevenDaysInfo->pay_money_total ?? 0;
  1060. $item['seven_days_rate'] = $item['pay_money'] ? round($sevenDaysMoney * 100 / $item['pay_money'], 2) : 0;
  1061. # 获取15天累计回收金额
  1062. $fifteenDaysInfo = $recycleData->where('days_type', 15)->where('expense_date', $item['expense_date'])->first();
  1063. $fifteenDaysMoney = $fifteenDaysInfo->pay_money_total ?? 0;
  1064. $item['fifteen_days_rate'] = $item['pay_money'] ? round($fifteenDaysMoney * 100 / $item['pay_money'], 2) : 0;
  1065. # 获取30天累计回收金额
  1066. $thirtyDaysInfo = $recycleData->where('days_type', 30)->where('expense_date', $item['expense_date'])->first();
  1067. $thirtyDaysMoney = $thirtyDaysInfo->pay_money_total ?? 0;
  1068. $item['thirty_days_rate'] = $item['pay_money'] ? round($thirtyDaysMoney * 100 / $item['pay_money'], 2) : 0;
  1069. # 首日新增用户充值金额
  1070. $item['pay_money'] = $item['pay_money'] / 100;
  1071. $item['day_paid_user_arpu'] = empty($item['day_paid_user_arpu']) ? 0 : round($item['day_paid_user_arpu']/100, 2);
  1072. $item['first_charge_user_cost'] = empty($item['first_charge_user_cost']) ? 0 : round($item['first_charge_user_cost']/100, 2);
  1073. $item['cost_cover_rate'] = round($item['cost_cover_rate'] * 100, 2);
  1074. }
  1075. return $data;
  1076. }
  1077. /**
  1078. * 总表排序字段处理
  1079. * */
  1080. public static function getSortField($field)
  1081. {
  1082. $data = [
  1083. 'expense_date' => '_key',
  1084. 'paid' => 'day_paid_bucket > day_paid',
  1085. 'scan_follow_count' => 'scan_follow_count_bucket > day_scan_follow_count',
  1086. 'follow_cost' => 'follow_cost',
  1087. 'first_roi' => 'first_roi',
  1088. 'pay_money' => 'day_pay_money_bucket > day_pay_money',
  1089. 'pay_money_total' => 'pay_money_total',
  1090. 'pay_user' => 'day_pay_user_bucket > day_pay_user',
  1091. 'pay_user_total' => 'pay_user_total',
  1092. 'pay_count' => 'day_pay_count_bucket > day_pay_count',
  1093. 'pay_count_total' => 'pay_count_total',
  1094. 'first_charge_user_cost' => 'first_charge_user_cost',
  1095. 'cost_cover_rate' => 'cost_cover_rate',
  1096. 'day_paid_user_arpu' => 'day_paid_user_arpu'
  1097. ];
  1098. return isset($data[$field]) ? $data[$field] : 'day_paid_bucket > day_paid';
  1099. }
  1100. }