MaterialLogic.php 3.9 KB

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