MaterialLogic.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // 作者筛选
  31. if (!empty($params['author_id'])) {
  32. $whereSql .= " AND author_id = {$params['author_id']}";
  33. }
  34. // 消耗筛选
  35. if (!empty($params['cost_type'])) {
  36. switch ($params['cost_type']) {
  37. case '1':
  38. $whereSql .= " AND ori_money >= 2000";
  39. break;
  40. case '2':
  41. $whereSql .= " AND ori_money < 2000";
  42. break;
  43. }
  44. }
  45. // 素材名称筛选
  46. if (!empty($params['material_name'])) {
  47. $whereSql .= " AND material_name LIKE '%{$params['material_name']}%'";
  48. }
  49. // 素材id筛选
  50. if (!empty($params['material_id'])) {
  51. $whereSql .= " AND material_id = {$params['material_id']}";
  52. }
  53. // 分组筛选
  54. $group = 'material_id';
  55. switch ($params['group']) {
  56. case 1:
  57. $group = 'material_id';
  58. break;
  59. case 2:
  60. $group = 'author_id';
  61. break;
  62. default:
  63. $group = 'material_id,author_id';
  64. break;
  65. }
  66. $sql = " SELECT $field FROM $tableName WHERE 1=1 $whereSql GROUP BY $group";
  67. $data = Db::connect('db_advert')->query($sql);
  68. if(empty($data)){
  69. return [
  70. 'data' => [],
  71. 'totalRow' => []
  72. ];
  73. }
  74. $totalData = [];
  75. foreach ($data as &$row) {
  76. if ($params['group'] == 2) {
  77. $row['material_name'] = '';
  78. $row['material_id'] = '';
  79. }
  80. $row['cost'] = round($row['cost'], 2);
  81. $row['ad_click_rate'] = ToolLogic::getPercent($row['click'], $row['ad_show'], 2);
  82. $row['reg_cost'] = ToolLogic::getRound($row['cost'], $row['register'], 2);
  83. $row['pay_cost'] = ToolLogic::getRound($row['cost'], (int)$row['pay_count'], 2);
  84. $totalData['cost'] = !empty($totalData['cost']) ? round($totalData['cost'] + $row['cost'], 2) : $row['cost'];
  85. $totalData['ad_show'] = !empty($totalData['ad_show']) ? $totalData['ad_show'] + $row['ad_show'] : $row['ad_show'];
  86. $totalData['click'] = !empty($totalData['click']) ? $totalData['click'] + $row['click'] : $row['click'];
  87. $totalData['active'] = !empty($totalData['active']) ? $totalData['active'] + $row['active'] : $row['active'];
  88. $totalData['register'] = !empty($totalData['register']) ? $totalData['register'] + $row['register'] : $row['register'];
  89. $totalData['pay_count'] = !empty($totalData['pay_count']) ? $totalData['pay_count'] + $row['pay_count'] : $row['pay_count'];
  90. $totalData['pay_amount'] = !empty($totalData['pay_amount']) ? $totalData['pay_amount'] + $row['pay_amount'] : $row['pay_amount'];
  91. }
  92. $totalData['ad_click_rate'] = ToolLogic::getPercent($totalData['click']??0, $totalData['ad_show']??0, 2);
  93. $totalData['reg_cost'] = ToolLogic::getRound($totalData['cost']??0, $totalData['register']??0, 2);
  94. $totalData['pay_cost'] = ToolLogic::getRound($totalData['cost']??0, (int)$totalData['pay_count']??0, 2);
  95. return [
  96. 'data' => $data,
  97. 'totalRow' => $totalData
  98. ];
  99. }
  100. }