MaterialLogic.php 5.3 KB

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