Report.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Countmidtable;
  4. use app\admin\model\Users as Usersmodel;
  5. use app\admin\model\Groups as GroupsModel;
  6. use app\admin\model\Servicetimelog as ServicetimelogModel;
  7. use app\admin\model\Kfstatetimes as KfstatetimesModel;
  8. /**
  9. * 报表类
  10. */
  11. class Report extends Base
  12. {
  13. /**
  14. * 工作报表
  15. *
  16. * @access public
  17. * @return array JsonString
  18. */
  19. public function index()
  20. {
  21. $param = input('param.');
  22. $startTime = isset($param['start']) ? $param['start'] : date('Y-m-d', strtotime("-6 day"));
  23. $endTime = isset($param['end']) ? $param['end'] : date('Y-m-d');
  24. $timeInterval = $this->Date_segmentation($startTime, $endTime);
  25. $queueData = [];
  26. $chatData = [];
  27. $serviceData = [];
  28. $xData = [];
  29. foreach ($timeInterval['days_list'] as $k => $value) {
  30. $allCount = 0;
  31. $evaluateGood = 0;
  32. $evaluateSecondary = 0;
  33. $evaluateBad = 0;
  34. $evaluateNo = 0;
  35. $dayBegin = strtotime($value);
  36. $dayEnd = strtotime($value) + 24 * 60 * 60;
  37. $chatLogCountWhere['start_time'] = ['between', [$dayBegin, $dayEnd]];
  38. $join = [
  39. 'alarm b' => 'a.servicelog_id = b.servicelog_id',
  40. ];
  41. $field = ['alarm_count', 'evaluate_id', 'system'];
  42. $system = [];
  43. $systemCount = [];
  44. // 查询当天的工单.
  45. $serviceLog = model('serviceLog')->selectServiceLog($field, $join, $chatLogCountWhere);
  46. foreach ($serviceLog as $ke => $va) {
  47. $allCount += $va['alarm_count'];
  48. if ($va['evaluate_id'] == 1) {
  49. $evaluateGood++;
  50. } elseif ($va['evaluate_id'] == 2) {
  51. $evaluateSecondary++;
  52. } elseif ($va['evaluate_id'] == 3) {
  53. $evaluateBad++;
  54. } elseif ($va['evaluate_id'] == 0) {
  55. $evaluateNo++;
  56. }
  57. if (!isset($system[$va['system']])) {
  58. $system[$va['system']] = 1;
  59. } else {
  60. $system[$va['system']]++;
  61. }
  62. }
  63. // 会话总数.
  64. $xData[] = $value;
  65. $chatData[$k] = $allCount;
  66. // 当天工单总数.
  67. $serviceData[$k] = model('serviceLog')->countServiceLog($chatLogCountWhere);
  68. }
  69. foreach ($system as $k => $v) {
  70. $systemCount[] = [
  71. 'value' => $v,
  72. 'name' => $k,
  73. ];
  74. }
  75. $queueData = (new Countmidtable())->getQueryDay($timeInterval['days_list']);
  76. $this->assign([
  77. 'xData' => json_encode($xData),
  78. 'chatData' => json_encode($chatData),
  79. 'serviceData' => json_encode($serviceData),
  80. 'queueData' => json_encode($queueData),
  81. 'systemCount' => json_encode($systemCount),
  82. 'evaluate' => json_encode([
  83. 'evaluateGood' => $evaluateGood,
  84. 'evaluateSecondary' => $evaluateSecondary,
  85. 'evaluateBad' => $evaluateBad,
  86. 'evaluateNo' => $evaluateNo,
  87. ]),
  88. ]);
  89. return $this->fetch('index');
  90. }//end index()
  91. public function attendancereport()
  92. {
  93. $goups = (new GroupsModel)->where(['status' => 1])->select();
  94. if (request()->isAjax()) {
  95. $param = input('param.');
  96. $limit = $param['pageSize'];
  97. $offset = ($param['pageNumber'] - 1) * $limit;
  98. $param = input('param.searchText');
  99. if (empty($param)) {
  100. $param = '0|0';
  101. }
  102. list($zone, $sgroup) = explode('|', $param);
  103. if (empty($zone)) {
  104. $zoneArray1 = [date("Y-m-d")];
  105. } else {
  106. $zonesarr = explode(',', $zone);
  107. $dd = $this->Date_segmentation($zonesarr['0'], $zonesarr['1']);
  108. $zoneArray1 = $dd['days_list'];
  109. }
  110. $zoneArray = array_map(function ($i) {
  111. return "'$i'";
  112. }, $zoneArray1);
  113. $uidarr = (new Usersmodel())->getUidsBygid(intval($sgroup), 1, 1);
  114. if (!$uidarr) {
  115. return json(['rows' => [], 'total' => 0]);
  116. }
  117. $model1 = new ServicetimelogModel();
  118. $model2 = new KfstatetimesModel();
  119. $ret1 = $model1->getDayData($zoneArray1, $uidarr['uids']);
  120. $ret2 = $model2->getDayData($zoneArray1, $uidarr['uids']);
  121. $retall = $this->reportDataFormat($ret1, $ret2, $uidarr['objs'], $zoneArray1, $limit, $offset);
  122. return json($retall);
  123. }
  124. $this->assign('groups', $goups);
  125. return $this->fetch();
  126. }
  127. function reportDataFormat($onoffdata, $timesdata, $uobjarr, $dataArray, $limit, $offset)
  128. {
  129. $allData = [];
  130. $all_last = [];
  131. foreach ($dataArray as $day) {
  132. foreach ($uobjarr as $uid => $user) {
  133. $kfuid = 'KF' . $uid;
  134. if (isset($onoffdata[$day][$kfuid])) {
  135. $allData[$day][$kfuid] = ['of0' => $onoffdata[$day][$kfuid][0], 'of1' => $onoffdata[$day][$kfuid][1]];
  136. } else {
  137. $allData[$day][$kfuid] = ['of0' => '', 'of1' => ''];
  138. }
  139. if (isset($timesdata[$day][$kfuid])) {
  140. $all_last[] = array_merge($timesdata[$day][$kfuid], $allData[$day][$kfuid], ['uname' => $uobjarr[$uid]->user_name,'account' => $uobjarr[$uid]->user_account,'day'=>$day]);
  141. } else {
  142. $all_last[] = array_merge(['0' => 0, '1' => 0, '2' => 0, '3' => 0], $allData[$day][$kfuid], ['uname' => $uobjarr[$uid]->user_name, 'account' => $uobjarr[$uid]->user_account,'day'=>$day]);
  143. }
  144. }
  145. }
  146. $len = count($all_last);
  147. $return = ['rows' => [], 'total' => $len];
  148. if ($offset >= $len) {
  149. return $return;
  150. }
  151. $begin = $offset;
  152. $end = ($offset + $limit);
  153. $end = ($end > $len) ? $len - 1 : $end;
  154. for ($i = $begin; $i < $end; $i++) {
  155. $return['rows'][] = $all_last[$i];
  156. }
  157. return $return;
  158. }
  159. /**
  160. * 时间分割
  161. *
  162. * @access public
  163. * @return array JsonString
  164. */
  165. function Date_segmentation($start_date, $end_date)
  166. {
  167. //如果为空,则从今天的0点为开始时间
  168. if (!empty($start_date))
  169. $start_date = date('Y-m-d H:i:s', strtotime($start_date));
  170. else {
  171. $start_date = date('Y-m-d 00:00:00', time());
  172. }
  173. //如果为空,则以明天的0点为结束时间(不存在24:00:00,只会有00:00:00)
  174. if (!empty($end_date))
  175. $end_date = date('Y-m-d H:i:s', strtotime($end_date));
  176. else
  177. $end_date = date('Y-m-d 00:00:00', strtotime('+1 day'));
  178. //between 查询 要求必须是从低到高
  179. if ($start_date > $end_date) {
  180. $ttt = $start_date;
  181. $start_date = $end_date;
  182. $end_date = $ttt;
  183. } elseif ($start_date == $end_date) {
  184. echo '时间输入错误';
  185. die;
  186. }
  187. $time_s = strtotime($start_date);
  188. $time_e = strtotime($end_date);
  189. $seconds_in_a_day = 86400;
  190. //生成中间时间点数组(时间戳格式、日期时间格式、日期序列)
  191. $days_inline_array = array();
  192. $times_inline_array = array();
  193. //日期序列
  194. $days_list = array();
  195. //判断开始和结束时间是不是在同一天
  196. $days_inline_array[0] = $start_date; //初始化第一个时间点
  197. $times_inline_array[0] = $time_s; //初始化第一个时间点
  198. $days_list[] = date('Y-m-d', $time_s);//初始化第一天
  199. if (date('Y-m-d', $time_s) == date('Y-m-d', $time_e)) {
  200. $days_inline_array[1] = $end_date;
  201. $times_inline_array[1] = $time_e;
  202. } else {
  203. /**
  204. * A.取开始时间的第二天凌晨0点
  205. * B.用结束时间减去A
  206. * C.用B除86400取商,取余
  207. * D.用A按C的商循环+86400,取得分割时间点,如果C没有余数,则最后一个时间点 与 循环最后一个时间点一致
  208. */
  209. $A_temp = date('Y-m-d 00:00:00', $time_s + $seconds_in_a_day);
  210. $A = strtotime($A_temp);
  211. $B = $time_e - $A;
  212. $C_quotient = floor($B / $seconds_in_a_day); //商舍去法取整
  213. $C_remainder = fmod($B, $seconds_in_a_day); //余数
  214. $days_inline_array[1] = $A_temp;
  215. $times_inline_array[1] = $A;
  216. $days_list[] = date('Y-m-d', $A); //第二天
  217. for ($increase_time = $A, $c_count_t = 1; $c_count_t <= $C_quotient; $c_count_t++) {
  218. $increase_time += $seconds_in_a_day;
  219. $days_inline_array[] = date('Y-m-d H:i:s', $increase_time);
  220. $times_inline_array[] = $increase_time;
  221. $days_list[] = date('Y-m-d', $increase_time);
  222. }
  223. $days_inline_array[] = $end_date;
  224. $times_inline_array[] = $time_e;
  225. }
  226. return array(
  227. 'start_date' => $start_date,
  228. 'end_date' => $end_date,
  229. 'days_list' => $days_list,
  230. 'days_inline' => $days_inline_array,
  231. 'times_inline' => $times_inline_array
  232. );
  233. }
  234. }//end class