Report.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 报表类
  5. */
  6. class Report extends Base
  7. {
  8. /**
  9. * 工作报表
  10. *
  11. * @access public
  12. * @return array JsonString
  13. */
  14. public function index()
  15. {
  16. $param = input('param.');
  17. $startTime = isset($param['start']) ? $param['start'] : date('Y-m-d', strtotime("-6 day"));
  18. $endTime = isset($param['end']) ? $param['end'] : date('Y-m-d');
  19. $timeInterval = $this->Date_segmentation($startTime,$endTime);
  20. $chatData = [];
  21. $serviceData = [];
  22. $xData = [];
  23. foreach ($timeInterval['days_list'] as $k => $value) {
  24. $allCount = 0;
  25. $evaluateGood = 0;
  26. $evaluateSecondary = 0;
  27. $evaluateBad = 0;
  28. $evaluateNo = 0;
  29. $dayBegin = strtotime($value);
  30. $dayEnd = strtotime($value) + 24 * 60 * 60;
  31. $chatLogCountWhere['start_time'] = ['between', [$dayBegin, $dayEnd]];
  32. $join = [
  33. 'alarm b' => 'a.servicelog_id = b.servicelog_id',
  34. ];
  35. $field = ['alarm_count', 'evaluate_id'];
  36. // 查询当天的工单.
  37. $serviceLog = model('serviceLog')->selectServiceLog($field, $join, $chatLogCountWhere);
  38. foreach ($serviceLog as $ke => $va) {
  39. $allCount += $va['alarm_count'];
  40. if ($va['evaluate_id'] == 1) {
  41. $evaluateGood++;
  42. } elseif ($va['evaluate_id'] == 2) {
  43. $evaluateSecondary++;
  44. } elseif ($va['evaluate_id'] == 3) {
  45. $evaluateBad++;
  46. } elseif ($va['evaluate_id'] == 0) {
  47. $evaluateNo++;
  48. }
  49. }
  50. // 会话总数.
  51. $xData[] = $value;
  52. $chatData[$k] = $allCount;
  53. // 当天工单总数.
  54. $serviceData[$k] = model('serviceLog')->countServiceLog($chatLogCountWhere);
  55. }
  56. $this->assign([
  57. 'xData' => json_encode($xData),
  58. 'chatData' => json_encode($chatData),
  59. 'serviceData' => json_encode($serviceData),
  60. 'evaluate' => json_encode([
  61. 'evaluateGood' => $evaluateGood,
  62. 'evaluateSecondary' => $evaluateSecondary,
  63. 'evaluateBad' => $evaluateBad,
  64. 'evaluateNo' => $evaluateNo,
  65. ]),
  66. ]);
  67. return $this->fetch('index');
  68. }//end index()
  69. /**
  70. * 时间分割
  71. *
  72. * @access public
  73. * @return array JsonString
  74. */
  75. function Date_segmentation($start_date, $end_date)
  76. {
  77. //如果为空,则从今天的0点为开始时间
  78. if(!empty($start_date))
  79. $start_date=date('Y-m-d H:i:s',strtotime($start_date));
  80. else {
  81. $start_date=date('Y-m-d 00:00:00',time());
  82. }
  83. //如果为空,则以明天的0点为结束时间(不存在24:00:00,只会有00:00:00)
  84. if(!empty($end_date))
  85. $end_date=date('Y-m-d H:i:s',strtotime($end_date));
  86. else
  87. $end_date=date('Y-m-d 00:00:00',strtotime('+1 day'));
  88. //between 查询 要求必须是从低到高
  89. if($start_date>$end_date)
  90. {
  91. $ttt=$start_date;
  92. $start_date=$end_date;
  93. $end_date=$ttt;
  94. }elseif($start_date==$end_date){
  95. echo '时间输入错误';die;
  96. }
  97. $time_s=strtotime($start_date);
  98. $time_e=strtotime($end_date);
  99. $seconds_in_a_day=86400;
  100. //生成中间时间点数组(时间戳格式、日期时间格式、日期序列)
  101. $days_inline_array=array();
  102. $times_inline_array=array();
  103. //日期序列
  104. $days_list=array();
  105. //判断开始和结束时间是不是在同一天
  106. $days_inline_array[0]=$start_date; //初始化第一个时间点
  107. $times_inline_array[0]=$time_s; //初始化第一个时间点
  108. $days_list[]=date('Y-m-d',$time_s);//初始化第一天
  109. if(
  110. date('Y-m-d',$time_s)
  111. ==date('Y-m-d',$time_e)
  112. ){
  113. $days_inline_array[1]=$end_date;
  114. $times_inline_array[1]=$time_e;
  115. }
  116. else
  117. {
  118. /**
  119. * A.取开始时间的第二天凌晨0点
  120. * B.用结束时间减去A
  121. * C.用B除86400取商,取余
  122. * D.用A按C的商循环+86400,取得分割时间点,如果C没有余数,则最后一个时间点 与 循环最后一个时间点一致
  123. */
  124. $A_temp=date('Y-m-d 00:00:00',$time_s+$seconds_in_a_day);
  125. $A=strtotime($A_temp);
  126. $B=$time_e-$A;
  127. $C_quotient=floor($B/$seconds_in_a_day); //商舍去法取整
  128. $C_remainder=fmod($B,$seconds_in_a_day); //余数
  129. $days_inline_array[1]=$A_temp;
  130. $times_inline_array[1]=$A;
  131. $days_list[]=date('Y-m-d',$A); //第二天
  132. for($increase_time=$A,$c_count_t=1;$c_count_t<=$C_quotient;$c_count_t++)
  133. {
  134. $increase_time+=$seconds_in_a_day;
  135. $days_inline_array[]=date('Y-m-d H:i:s',$increase_time);
  136. $times_inline_array[]=$increase_time;
  137. $days_list[]=date('Y-m-d',$increase_time);
  138. }
  139. $days_inline_array[]=$end_date;
  140. $times_inline_array[]=$time_e;
  141. }
  142. return array(
  143. 'start_date'=>$start_date,
  144. 'end_date'=>$end_date,
  145. 'days_list'=>$days_list,
  146. 'days_inline'=>$days_inline_array,
  147. 'times_inline'=>$times_inline_array
  148. );
  149. }
  150. }//end class