MaterialLogic.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // 素材数据总览
  3. namespace app\v1\logic\dataReport;
  4. use app\v1\logic\tool\ToolLogic;
  5. use plugin\saiadmin\basic\BaseLogic;
  6. use support\think\Db;
  7. class MaterialLogic extends BaseLogic
  8. {
  9. public function getMaterialList($where)
  10. {
  11. $params = $this->searchByAuth($where);
  12. $tableName = 'media_cost_material';
  13. $field = "
  14. material_name,
  15. material_id,
  16. author_id,
  17. auth_id,
  18. media_id,
  19. tdate,
  20. SUM(money) as cost,
  21. SUM(ad_show) as ad_show,
  22. SUM(click) as click,
  23. SUM(active) as active,
  24. SUM(register) as register,
  25. SUM(pay_count) as pay_count,
  26. SUM(pay_amount) as pay_amount
  27. ";
  28. // 基础筛选
  29. $whereSql = $this->generateWhereSql($params);
  30. echo $whereSql;
  31. // 作者筛选
  32. if (!empty($params['author_id'])) {
  33. $whereSql .= " AND author_id = {$params['author_id']}";
  34. }
  35. // 消耗筛选
  36. if (!empty($params['cost_type'])) {
  37. switch ($params['cost_type']) {
  38. case '1':
  39. $whereSql .= " AND ori_money >= 2000";
  40. break;
  41. case '2':
  42. $whereSql .= " AND ori_money < 2000";
  43. break;
  44. }
  45. }
  46. // 素材名称筛选
  47. if (!empty($params['material_name'])) {
  48. $whereSql .= " AND material_name LIKE '%{$params['material_name']}%'";
  49. }
  50. // 素材id筛选
  51. if (!empty($params['material_id'])) {
  52. $whereSql .= " AND material_id = {$params['material_id']}";
  53. }
  54. // 分组筛选
  55. $group = 'material_id';
  56. switch ($params['group']) {
  57. case 1:
  58. $group = 'material_id';
  59. break;
  60. case 2:
  61. $group = 'author_id';
  62. break;
  63. default:
  64. $group = 'material_id,author_id';
  65. break;
  66. }
  67. $sql = " SELECT $field FROM $tableName WHERE 1=1 $whereSql GROUP BY $group";
  68. $data = Db::connect('db_advert')->query($sql);
  69. if(empty($data)){
  70. return [
  71. 'data' => [],
  72. 'totalRow' => []
  73. ];
  74. }
  75. $totalData = [];
  76. foreach ($data as &$row) {
  77. if ($params['group'] == 2) {
  78. $row['material_name'] = '';
  79. $row['material_id'] = '';
  80. }
  81. $row['cost'] = round($row['cost'], 2);
  82. $row['ad_click_rate'] = ToolLogic::getPercent($row['click'], $row['ad_show'], 2);
  83. $row['reg_cost'] = ToolLogic::getRound($row['cost'], $row['register'], 2);
  84. $row['pay_cost'] = ToolLogic::getRound($row['cost'], (int)$row['pay_count'], 2);
  85. $totalData['cost'] = !empty($totalData['cost']) ? round($totalData['cost'] + $row['cost'], 2) : $row['cost'];
  86. $totalData['ad_show'] = !empty($totalData['ad_show']) ? $totalData['ad_show'] + $row['ad_show'] : $row['ad_show'];
  87. $totalData['click'] = !empty($totalData['click']) ? $totalData['click'] + $row['click'] : $row['click'];
  88. $totalData['active'] = !empty($totalData['active']) ? $totalData['active'] + $row['active'] : $row['active'];
  89. $totalData['register'] = !empty($totalData['register']) ? $totalData['register'] + $row['register'] : $row['register'];
  90. $totalData['pay_count'] = !empty($totalData['pay_count']) ? $totalData['pay_count'] + $row['pay_count'] : $row['pay_count'];
  91. $totalData['pay_amount'] = !empty($totalData['pay_amount']) ? $totalData['pay_amount'] + $row['pay_amount'] : $row['pay_amount'];
  92. }
  93. $totalData['ad_click_rate'] = ToolLogic::getPercent($totalData['click']??0, $totalData['ad_show']??0, 2);
  94. $totalData['reg_cost'] = ToolLogic::getRound($totalData['cost']??0, $totalData['register']??0, 2);
  95. $totalData['pay_cost'] = ToolLogic::getRound($totalData['cost']??0, (int)$totalData['pay_count']??0, 2);
  96. return [
  97. 'data' => $data,
  98. 'totalRow' => $totalData
  99. ];
  100. }
  101. // 生成wheresql
  102. public function generateWhereSql($params)
  103. {
  104. $whereSql = "";
  105. // 游戏id
  106. if (!empty($params['game_id'])) {
  107. if (is_array($params['game_id'])) {
  108. $whereSql .= " AND game_id IN(" . implode(',', $params['game_id']) . ")";
  109. } else {
  110. $whereSql .= " AND game_id = {$params['game_id']}";
  111. }
  112. }
  113. // 媒体id
  114. if (!empty($params['media_id'])) {
  115. $whereSql .= " AND media_id = {$params['media_id']}";
  116. }
  117. // 渠道id
  118. if (!empty($params['agent_id'])) {
  119. $whereSql .= " AND agent_id = {$params['agent_id']}";
  120. }
  121. // 广告位id
  122. if (!empty($params['site_id'])) {
  123. if (is_array($params['site_id'])) {
  124. $whereSql .= " AND site_id IN(" . implode(',', $params['site_id']) . ")";
  125. } else {
  126. $whereSql .= " AND site_id = {$params['site_id']}";
  127. }
  128. }
  129. // 负责人
  130. if (!empty($params['auth_id'])) {
  131. if (is_array($params['auth_id'])) {
  132. $whereSql .= " AND auth_id IN(" . implode(',', $params['auth_id']) . ")";
  133. } else {
  134. $whereSql .= " AND auth_id = {$params['auth_id']}";
  135. }
  136. }
  137. // 注册日期
  138. if (!empty($params['reg_date']) && is_array($params['reg_date'])) {
  139. $whereSql .= " AND tdate BETWEEN '{$params['reg_date'][0]}' AND '{$params['reg_date'][1]}'";
  140. }
  141. // 注册日期,不是数组,则认为是单个日期
  142. if (!empty($params['reg_date']) && !is_array($params['reg_date'])) {
  143. $whereSql .= " AND tdate = '{$params['reg_date']}'";
  144. }
  145. // 自然量ID, auth_id=0为自然量
  146. if(!empty($params['nomal_game_id'])){
  147. for($i=0;$i<count($params['nomal_game_id']);$i++){
  148. $whereSql .= " OR (game_id = {$params['nomal_game_id'][$i]} AND auth_id=0)";
  149. }
  150. }
  151. return $whereSql;
  152. }
  153. }