企微短剧业务系统

OperateDataService.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Service;
  3. use App\Models\Es\OperateDataTrend;
  4. use App\Models\System\Users;
  5. use App\Models\TencentAdAuth;
  6. use App\Service\Admin\AdminService;
  7. class OperateDataService
  8. {
  9. /**
  10. * 运营公众号数据
  11. * */
  12. public static function getAccountData($params, $sortField, $sortType, $page, $pageSize)
  13. {
  14. $sortField = self::getSortField($sortField);
  15. # 表头处理
  16. $header = config('operateDataHeader.list');
  17. # 数据权限
  18. $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
  19. if($params['is_super_user']) {
  20. $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
  21. if(empty($params['user_list'])) return [[], 0, $header];
  22. }
  23. list($list, $count) = OperateDataTrend::getOperateAccountList($params, $sortField, $sortType, $page, $pageSize);
  24. if(empty($list)) return [$list, $count, $header];
  25. # 获取列表中涉及的公众号和运营人员
  26. $appidList = array_column($list, 'app_id');
  27. $operatorIds = array_column($list, 'operate_uid');
  28. $mpAccountData = TencentAdAuth::selectRaw('wechat_account_id, account_name')
  29. ->where('enable', 1)
  30. ->whereIn('wechat_account_id', $appidList)
  31. ->get();
  32. $sysUserData = Users::select('id', 'name')->whereIn('id', $operatorIds)->get();
  33. foreach ($list as &$item) {
  34. $accountInfo = $mpAccountData->where('wechat_account_id', $item['app_id'])->first();
  35. $item['account_name'] = $accountInfo['account_name'] ?? null;
  36. $operatorInfo = $sysUserData->where('id', $item['operate_uid'])->first();
  37. $item['operator_name'] = $operatorInfo['name'] ?? null;
  38. $item['paid'] = $item['paid'] / 100;
  39. $item['follow_cost'] = round($item['follow_cost'] / 100, 1);
  40. $item['pay_rate'] = round($item['pay_rate'], 4) * 100 . '%';
  41. $item['seven_days_paid_rate'] = round($item['seven_days_paid_rate'], 4) * 100 . '%';
  42. $item['pay_money_total'] = $item['pay_money_total'] / 100;
  43. $item['gross_profit_rate'] = round($item['gross_profit_rate'], 5) * 100 . '%';
  44. $item['cost_cover_rate'] = round($item['cost_cover_rate'], 4) * 100 . '%';
  45. }
  46. return [$list, $count, $header];
  47. }
  48. /**
  49. * 获取运营公众号数据汇总
  50. * */
  51. public static function getAccountDataSummary($params)
  52. {
  53. # 表头处理
  54. $header = config('operateDataHeader.summary');
  55. # 数据权限
  56. $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
  57. if($params['is_super_user']) {
  58. $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
  59. if(empty($params['user_list'])) return [[], $header];
  60. }
  61. $data = OperateDataTrend::getAccountDataCollect($params);
  62. if(empty($data)) return [[], $header];
  63. $data['paid'] = $data['paid'] / 100;
  64. $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
  65. $data['pay_rate'] = round($data['pay_rate'], 4) * 100 . '%';
  66. $data['seven_days_paid_rate'] = round($data['seven_days_paid_rate'], 4) * 100 . '%';
  67. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  68. $data['gross_profit_rate'] = round($data['gross_profit_rate'], 5) * 100 . '%';
  69. $data['cost_cover_rate'] = round($data['cost_cover_rate'], 4) * 100 . '%';
  70. return [$data, $header];
  71. }
  72. /**
  73. * 获取运营数据
  74. * */
  75. public static function getOperateData($params, $sortField, $sortType, $page, $pageSize)
  76. {
  77. $sortField = self::getSortField($sortField);
  78. # 表头处理
  79. $header = config('operateDataHeader.operate_list');
  80. # 数据权限
  81. $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
  82. if($params['is_super_user']) {
  83. $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
  84. if(empty($params['user_list'])) return [[], 0, $header];
  85. }
  86. list($list, $count) = OperateDataTrend::getOperateDataList($params, $sortField, $sortType, $page, $pageSize);
  87. if(empty($list)) return [$list, $count, $header];
  88. # 获取列表中涉及的公众号和运营人员
  89. $operatorIds = array_column($list, 'operate_uid');
  90. $sysUserData = Users::select('id', 'name')->whereIn('id', $operatorIds)->get();
  91. foreach ($list as &$item) {
  92. $operatorInfo = $sysUserData->where('id', $item['operate_uid'])->first();
  93. $item['operator_name'] = $operatorInfo['name'] ?? null;
  94. $item['paid'] = $item['paid'] / 100;
  95. $item['follow_cost'] = round($item['follow_cost'] / 100, 1);
  96. $item['pay_rate'] = round($item['pay_rate'], 4) * 100 . '%';
  97. $item['seven_days_paid_rate'] = round($item['seven_days_paid_rate'], 4) * 100 . '%';
  98. $item['pay_money_total'] = $item['pay_money_total'] / 100;
  99. $item['gross_profit_rate'] = round($item['gross_profit_rate'], 5) * 100 . '%';
  100. $item['cost_cover_rate'] = round($item['cost_cover_rate'], 4) * 100 . '%';
  101. }
  102. return [$list, $count, $header];
  103. }
  104. /**
  105. * 获取运营数据汇总
  106. * */
  107. public static function getOperateDataSummary($params)
  108. {
  109. # 表头处理
  110. $header = config('operateDataHeader.operate_summary');
  111. # 数据权限
  112. $params['is_super_user'] = AdminService::isSuperUser($params['is_system_admin'], $params['sys_group_id'], $params['admin_id']);
  113. if($params['is_super_user']) {
  114. $params['user_list'] = Users::getCorpUserList($params['sys_group_id']);
  115. if(empty($params['user_list'])) return [[], $header];
  116. }
  117. $data = OperateDataTrend::getOperateDataCollect($params);
  118. if(empty($data)) return [[], $header];
  119. $data['paid'] = $data['paid'] / 100;
  120. $data['follow_cost'] = round($data['follow_cost'] / 100, 1);
  121. $data['pay_rate'] = round($data['pay_rate'], 4) * 100 . '%';
  122. $data['seven_days_paid_rate'] = round($data['seven_days_paid_rate'], 4) * 100 . '%';
  123. $data['pay_money_total'] = $data['pay_money_total'] / 100;
  124. $data['gross_profit_rate'] = round($data['gross_profit_rate'], 5) * 100 . '%';
  125. $data['cost_cover_rate'] = round($data['cost_cover_rate'], 4) * 100 . '%';
  126. return [$data, $header];
  127. }
  128. /**
  129. * 总表排序字段处理
  130. * */
  131. public static function getSortField($field)
  132. {
  133. $data = [
  134. 'paid' => 'day_paid_bucket > day_paid',
  135. 'operate_days' => 'operate_days_bucket > operate_days',
  136. 'follow_cost' => 'follow_cost',
  137. 'pay_user_total' => 'pay_user_total',
  138. 'pay_rate' => 'pay_rate',
  139. 'seven_days_paid_rate' => 'seven_days_paid_rate',
  140. 'pay_money_total' => 'pay_money_total',
  141. 'gross_profit_rate' => 'gross_profit_rate',
  142. 'cost_cover_rate' => 'cost_cover_rate',
  143. ];
  144. return isset($data[$field]) ? $data[$field] : 'day_paid_bucket > day_paid';
  145. }
  146. }