Report.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace catchAdmin\report\model;
  3. use catcher\base\CatchModel;
  4. use catcher\exceptions\FailedException;
  5. use catcher\Utils;
  6. use catchAdmin\permissions\model\DataRangScopeTrait;
  7. use catchAdmin\stations\model\Station as StationModel;
  8. use catchAdmin\school\model\KqBuilding as BuildingModel;
  9. use catchAdmin\permissions\model\Users as UsersModel;
  10. use catchAdmin\permissions\model\Department as DepartmentModel;
  11. use catchAdmin\permissions\model\Roles;
  12. use catchAdmin\system\model\SysDictData as SysDictDataModel;
  13. use catchAdmin\system\model\SysDictType as SysDictTypeModel;
  14. use catchAdmin\permissions\model\SysConfig;
  15. class Report extends CatchModel
  16. {
  17. //权限过滤
  18. use DataRangScopeTrait;
  19. protected $name = 'report';
  20. protected $field = [];
  21. /**
  22. * 获取所有基站类型
  23. */
  24. public function getStationType()
  25. {
  26. return (new SysDictDataModel())->getTypesByCode('StationType');
  27. }
  28. /**
  29. * 获取所有用户设备类型
  30. */
  31. public function getDeviceType()
  32. {
  33. return (new SysDictDataModel())->getTypesByCode('DeviceType');
  34. }
  35. /**
  36. * 基站数量数据
  37. */
  38. public function getStationCountData()
  39. {
  40. $station_type_arr = []; // 基站类型数组
  41. $total_count = 0; // 基站总数量
  42. $online_count = 0; // 基站总在线数量
  43. $yesterday_add_count = 0; // 基站昨日新增总数
  44. $today_add_count = 0; // 基站今日新增总数
  45. $station_type_data = [];
  46. $time = time();
  47. $today_time = strtotime(date('Y-m-d'));
  48. // 获取基站类型
  49. $station_types = $this->getStationType();
  50. foreach ($station_types as $type) {
  51. $station_type_arr[$type['value']] = $type;
  52. }
  53. // 基站离线时间间隔
  54. $offline_interval = SysConfig::getConfigValueBy('station_offline_interval', 'station_config') ?? 86400;
  55. // 基站总数据
  56. $cursor = (new StationModel)->dataRange()->field('station_mac, station_type, online_time, created_at')->cursor();
  57. foreach ($cursor as $station) {
  58. $total_count++;
  59. // 按基站类型分类
  60. if (isset($station_type_arr[$station['station_type']])) {
  61. $key = $station_type_arr[$station['station_type']]['text'];
  62. } else {
  63. $key = '未知类型';
  64. }
  65. if (!isset($station_type_data[$key])) {
  66. $station_type_data[$key] = isset($station_type_arr[$station['station_type']]) ? $station_type_arr[$station['station_type']] : [];
  67. }
  68. // 总数量
  69. $station_type_data[$key]['total'] = isset($station_type_data[$key]['total']) ? ($station_type_data[$key]['total'] + 1) : 1;
  70. // 昨日新增
  71. $create_time = $station->getData('created_at');
  72. if ($today_time - $create_time > 0 && $today_time - $create_time < 86400) {
  73. $yesterday_add_count++;
  74. $station_type_data[$key]['yesterday_add'] = isset($station_type_data[$key]['yesterday_add']) ? ($station_type_data[$key]['yesterday_add'] + 1) : 1;
  75. }
  76. // 今日新增
  77. if ($today_time - $create_time <= 0) {
  78. $today_add_count++;
  79. $station_type_data[$key]['today_add'] = isset($station_type_data[$key]['today_add']) ? ($station_type_data[$key]['today_add'] + 1) : 1;
  80. }
  81. // 在线数
  82. if ($time - intval(strtotime($station['online_time'])) > $offline_interval) { // 离线时间,秒
  83. $online_count++;
  84. $station_type_data[$key]['online'] = isset($station_type_data[$key]['online']) ? ($station_type_data[$key]['online'] + 1) : 1;
  85. }
  86. }
  87. // 转化为输出数据
  88. $type_data = [];
  89. foreach ($station_type_data as $data) {
  90. $data['yesterday_add'] = isset($data['yesterday_add']) ? $data['yesterday_add'] : 0;
  91. $data['today_add'] = isset($data['today_add']) ? $data['today_add'] : 0;
  92. $data['online'] = isset($data['online']) ? $data['online'] : 0;
  93. $data['offline'] = $data['total'] - $data['online'];
  94. $type_data[] = $data;
  95. }
  96. return [
  97. 'station_total_count' => $total_count,
  98. 'station_online_count' => $online_count,
  99. 'station_offline_count' => $total_count - $online_count,
  100. 'station_yesterday_add_count' => $yesterday_add_count,
  101. 'station_today_add_count' => $today_add_count,
  102. 'station_type_data' => $type_data,
  103. ];
  104. }
  105. /**
  106. * 基站设备增长趋势数据
  107. */
  108. public function getStationGrowthTrendData($start_time = '', $end_time = '')
  109. {
  110. // 默认最近1周
  111. if (!$start_time) {
  112. $start_time = date('Y-m-d', strtotime('-6 day'));
  113. }
  114. if (!$end_time) {
  115. $end_time = date('Y-m-d');
  116. }
  117. // 获取基站类型
  118. $station_types = $this->getStationType();
  119. $data = [];
  120. $station_model = new StationModel();
  121. while ($start_time <= $end_time) {
  122. foreach($station_types as $type) {
  123. if (!isset($data[$type['text']]['name'])) {
  124. $data[$type['text']]['name'] = $type['text'];
  125. }
  126. $count = $station_model->dataRange()
  127. ->where('station_type', $type['value'])
  128. ->whereBetweenTime('created_at', 0, date('Y-m-d 23:59:59', strtotime($start_time)))
  129. ->count();
  130. // var_dump($station_model->getLastSql());
  131. $data[$type['text']]['data'][$start_time] = $count;
  132. }
  133. $start_time = date('Y-m-d', strtotime($start_time . ' +1 day'));
  134. }
  135. return array_values($data);
  136. }
  137. /**
  138. * 用户设备增长趋势数据
  139. */
  140. public function getDeviceGrowthTrendData($start_time = '', $end_time = '')
  141. {
  142. // 默认最近1周
  143. if (!$start_time) {
  144. $start_time = date('Y-m-d', strtotime('-6 day'));
  145. }
  146. if (!$end_time) {
  147. $end_time = date('Y-m-d');
  148. }
  149. // 获取设备类型
  150. $device_types = array(
  151. array('text'=>'法兰'),
  152. array('text'=>'液压泵'),
  153. array('text'=>'液压扳手'),
  154. );
  155. // var_dump($device_types);
  156. $data = [];
  157. while ($start_time <= $end_time) {
  158. foreach($device_types as $type) {
  159. if (!isset($data[$type['text']]['name'])) {
  160. $data[$type['text']]['name'] = $type['text'];
  161. }
  162. $count=mt_rand(1,10);
  163. $data[$type['text']]['data'][$start_time] = $count;
  164. }
  165. $start_time = date('Y-m-d', strtotime($start_time . ' +1 day'));
  166. }
  167. return array_values($data);
  168. }
  169. /**
  170. * 部门数量数据:统计含本部门、及子部门数量
  171. */
  172. public function getDepartmentCountData()
  173. {
  174. $depart_ids = DepartmentModel::getChildrenDepartmentIds(request()->user()->department_id);
  175. // 去掉部门为0的
  176. $depart_ids = array_filter($depart_ids);
  177. return [
  178. 'department_count' => count($depart_ids),
  179. ];
  180. }
  181. /**
  182. * 设备用户数量统计
  183. */
  184. public function getDeviceUserCountData()
  185. {
  186. // 当前时间
  187. $now_time = time();
  188. // 离线时间间隔
  189. $offline_interval = SysConfig::getConfigValueBy('card_offline_interval', 'basic_config') ?: 86400;
  190. $offline_time = date('Y-m-d H:i:s', $now_time - $offline_interval); // 离线时间
  191. // 设备类型
  192. $device_types = $this->getDeviceType();
  193. $deviceModel = (new \catchAdmin\device\model\Device());
  194. $devices = $deviceModel->dataRange()
  195. ->field('id,device_type,alarm_state,online_time,wifi_online_time')
  196. ->append(['last_online_time'])
  197. ->select();
  198. // 总设备数
  199. $totle = 0;
  200. // 总在线设备数
  201. $totle_online = 0;
  202. // 按设备类型分组
  203. $device_type_data = [];
  204. foreach ($device_types as $type) {
  205. // 数量
  206. $type_count = $devices->where('device_type', $type['value'])->count();
  207. // 在线数量
  208. $type_online_count = $devices->where('device_type', $type['value'])->where('last_online_time', '>', $offline_time)->count();
  209. // 告警数量
  210. $type_alarm_count = $devices->where('device_type', $type['value'])->where('alarm_state', '>', 0)->count();
  211. $device_type_data[] = [
  212. 'text' => $type['text'], // 设备类型文本
  213. 'value' => $type['value'], // 设备类型值
  214. 'total' => $type_count,
  215. 'online' => $type_online_count,
  216. 'alarm' => $type_alarm_count,
  217. ];
  218. // 计算总数
  219. $totle += $type_count;
  220. // 计算总在线数
  221. $totle_online += $type_online_count;
  222. }
  223. return [
  224. 'device_total_count' => $totle,
  225. 'device_online_count' => $totle_online,
  226. 'device_type_data' => $device_type_data,
  227. ];
  228. }
  229. }