DataAnalysisLogic.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\adminapi\logic\data_analysis;
  3. use app\common\logic\BaseLogic;
  4. use think\facade\Db;
  5. /**
  6. * DataAnalysis逻辑
  7. * Class DataAnalysisLogic
  8. * @package app\adminapi\logic
  9. */
  10. class DataAnalysisLogic extends BaseLogic
  11. {
  12. public static function serviceOrdersNumber(array $params = [])
  13. {
  14. $condition = self::paramsToSelectCondition($params);
  15. return Db::name('service_work')->alias('a')->field($condition['field'])
  16. ->where($condition['where'])
  17. ->group($condition['group'])
  18. ->order($condition['order'])
  19. ->select()->column('y', 'x');
  20. }
  21. public static function paramsToSelectCondition(array $params = [])
  22. {
  23. $where = [];
  24. $field = [];
  25. $group = [];
  26. $order = [];
  27. if($params['type'] == 'hour'){
  28. $params = $params['hour'];
  29. $field[] = Db::raw("HOUR(FROM_UNIXTIME(a.create_time)) AS x");
  30. if($params['date_range']){
  31. list($start_time,$end_time) = $params['date_range'];
  32. $where[] = ['a.create_time','between',[strtotime($start_time),strtotime($end_time)+86400-1]];
  33. }
  34. $group[] = Db::raw("HOUR(FROM_UNIXTIME(a.create_time))");
  35. $order[] = 'x';
  36. }else{
  37. $params = $params['category_type'];
  38. switch ($params['segmented']){
  39. case 1:
  40. $field[] = Db::raw("FROM_UNIXTIME(a.create_time,'%Y-%m-%d') AS x");
  41. if($params['date_range']){
  42. list($start_time,$end_time) = $params['date_range'];
  43. $where[] = ['a.create_time','between',[strtotime($start_time),strtotime($end_time)+86400-1]];
  44. }
  45. $group[] = Db::raw("x");
  46. $order[] = 'x';
  47. break;
  48. case 2:
  49. $field[] = Db::raw("FLOOR((a.create_time - UNIX_TIMESTAMP('".$params['monthdate']."-01')) / (7 * 24 * 60 * 60)) AS x");
  50. if($params['monthdate']){
  51. $dateTime = new \DateTime($params['monthdate']);
  52. $dateTime->modify('last day of this month');
  53. $where[] = ['a.create_time','between',[strtotime($params['monthdate'].'-01'),strtotime($dateTime->format('Y-m-d'))+86400-1]];
  54. }
  55. $group[] = Db::raw("x");
  56. $order[] = 'x';
  57. break;
  58. case 3:
  59. $field[] = Db::raw("FROM_UNIXTIME(a.create_time,'%m') AS x");
  60. if($params['yeardate']){
  61. $where[] = ['a.create_time','between',[strtotime($params['yeardate'].'-01-01'),strtotime($params['yeardate'].'-12-31')+86400-1]];
  62. }
  63. $group[] = Db::raw("x");
  64. $order[] = 'x';
  65. break;
  66. }
  67. }
  68. $field[] = Db::raw("COUNT(a.id) AS y");
  69. return [
  70. 'where' => $where,
  71. 'field' => $field,
  72. 'group' => $group,
  73. 'order' => $order
  74. ];
  75. }
  76. }