AnswerRanksService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\UserModel;
  13. use App\Services\BaseService;
  14. use App\Services\ConfigService;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 用户管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class UserService
  21. * @package App\Services\Common
  22. */
  23. class AnswerRanksService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * UserService constructor.
  30. */
  31. public function __construct()
  32. {
  33. }
  34. /**
  35. * 静态入口
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = new static();
  41. }
  42. return self::$instance;
  43. }
  44. public function getAnswerRanks(array $params)
  45. {
  46. $dateType = $params['dateType'] ?? 'day';
  47. $page = max(1, (int) ($params['page'] ?? 1));
  48. $limit = max(1, (int) ($params['limit'] ?? 10));
  49. // 时间戳
  50. $startTimestamp = !empty($params['start_time']) ? strtotime($params['start_time']) : strtotime(date('2000-01-01 00:00:00'));
  51. $endTimestamp = !empty($params['end_time']) ? strtotime($params['end_time']) : time();
  52. if ($endTimestamp < $startTimestamp) {
  53. $endTimestamp = $startTimestamp;
  54. }
  55. // 分组
  56. switch ($dateType) {
  57. case 'month':
  58. $groupRaw = "FROM_UNIXTIME(create_time, '%Y-%m')";
  59. break;
  60. case 'year':
  61. $groupRaw = "FROM_UNIXTIME(create_time, '%Y')";
  62. break;
  63. case 'week':
  64. $groupRaw = "CONCAT(YEAR(FROM_UNIXTIME(create_time)),'-',LPAD(WEEK(FROM_UNIXTIME(create_time),1),2,'0'))";
  65. break;
  66. case 'day':
  67. default:
  68. $groupRaw = "FROM_UNIXTIME(create_time, '%Y-%m-%d')";
  69. break;
  70. }
  71. // 基础查询
  72. $query = DB::table('member_answer_ranks')
  73. ->selectRaw("
  74. {$groupRaw} as stat_date,
  75. SUM(answer_count) as total_count,
  76. SUM(answer_time) as total_time
  77. ")
  78. ->whereBetween('create_time', [$startTimestamp, $endTimestamp])
  79. ->where('status', 1)
  80. ->where('mark', 1)
  81. ->groupBy('stat_date')
  82. ->orderBy('stat_date', 'desc');
  83. // 总条数
  84. $total = DB::table(DB::raw("({$query->toSql()}) as t"))
  85. ->mergeBindings($query)
  86. ->count();
  87. // 分页
  88. $list = $query->forPage($page, $limit)->get();
  89. return [
  90. 'list' => $list,
  91. 'total' => $total,
  92. 'page' => $page,
  93. 'limit' => $limit,
  94. ];
  95. }
  96. }