MaterialLogic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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($params)
  10. {
  11. $params = $this->searchByAuth($params);
  12. $field = "
  13. material_name,
  14. material_id,
  15. author_id,
  16. auth_id,
  17. media_id,
  18. tdate,
  19. SUM(money) as cost,
  20. SUM(ad_show) as ad_show,
  21. SUM(click) as click,
  22. SUM(active) as active,
  23. SUM(register) as register,
  24. SUM(pay_count) as pay_count,
  25. SUM(pay_amount) as pay_amount
  26. ";
  27. // 基础筛选
  28. $whereRaw = $this->getCommonWhereRaw($params);
  29. // 作者筛选
  30. if (!empty($params['author_id'])) {
  31. $whereRaw .= " AND author_id = {$params['author_id']}";
  32. }
  33. // 消耗筛选
  34. if (!empty($params['cost_type'])) {
  35. switch ($params['cost_type']) {
  36. case '1':
  37. $whereRaw .= " AND ori_money >= 2000";
  38. break;
  39. case '2':
  40. $whereRaw .= " AND ori_money < 2000";
  41. break;
  42. }
  43. }
  44. // 素材名称筛选
  45. if (!empty($params['material_name'])) {
  46. $whereRaw .= " AND material_name LIKE '%{$params['material_name']}%'";
  47. }
  48. // 素材id筛选
  49. if (!empty($params['material_id'])) {
  50. $whereRaw .= " AND material_id = {$params['material_id']}";
  51. }
  52. // 分组筛选
  53. // $group = 'material_id';
  54. switch ($params['group']) {
  55. case 1:
  56. $group = 'material_id';
  57. break;
  58. case 2:
  59. $group = 'author_id';
  60. break;
  61. default:
  62. $group = 'material_id,author_id';
  63. break;
  64. }
  65. $data = Db::connect('db_advert')->table("media_cost_material")->field($field)->whereRaw($whereRaw)->group($group)->select();
  66. if(empty($data)){
  67. return [
  68. 'data' => [],
  69. 'totalRow' => []
  70. ];
  71. }
  72. $totalData = [];
  73. foreach ($data as &$row) {
  74. if ($params['group'] == 2) {
  75. $row['material_name'] = '';
  76. $row['material_id'] = '';
  77. }
  78. $row['cost'] = round($row['cost'], 2);
  79. $row['ad_click_rate'] = ToolLogic::getPercent($row['click'], $row['ad_show'], 2);
  80. $row['reg_cost'] = ToolLogic::getRound($row['cost'], $row['register'], 2);
  81. $row['pay_cost'] = ToolLogic::getRound($row['cost'], (int)$row['pay_count'], 2);
  82. $totalData['cost'] = !empty($totalData['cost']) ? round($totalData['cost'] + $row['cost'], 2) : $row['cost'];
  83. $totalData['ad_show'] = !empty($totalData['ad_show']) ? $totalData['ad_show'] + $row['ad_show'] : $row['ad_show'];
  84. $totalData['click'] = !empty($totalData['click']) ? $totalData['click'] + $row['click'] : $row['click'];
  85. $totalData['active'] = !empty($totalData['active']) ? $totalData['active'] + $row['active'] : $row['active'];
  86. $totalData['register'] = !empty($totalData['register']) ? $totalData['register'] + $row['register'] : $row['register'];
  87. $totalData['pay_count'] = !empty($totalData['pay_count']) ? $totalData['pay_count'] + $row['pay_count'] : $row['pay_count'];
  88. $totalData['pay_amount'] = !empty($totalData['pay_amount']) ? $totalData['pay_amount'] + $row['pay_amount'] : $row['pay_amount'];
  89. }
  90. $totalData['ad_click_rate'] = ToolLogic::getPercent($totalData['click']??0, $totalData['ad_show']??0, 2);
  91. $totalData['reg_cost'] = ToolLogic::getRound($totalData['cost']??0, $totalData['register']??0, 2);
  92. $totalData['pay_cost'] = ToolLogic::getRound($totalData['cost']??0, (int)$totalData['pay_count']??0, 2);
  93. return [
  94. 'data' => $data,
  95. 'totalRow' => $totalData
  96. ];
  97. }
  98. }