Alarm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Alarm extends Base_Controller {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->load->model('Alarm_model');
  7. $this->load->model('Project_model');
  8. $this->load->model('Network_model');
  9. $this->load->model('Lamp_model');
  10. $this->load->model('Patrol_model');
  11. $this->load->model('Repair_model');
  12. }
  13. // 获取告警信息列表
  14. public function get(){
  15. $filter = array();
  16. $where = array('1=1');
  17. $role = $this->get_user_info('role');
  18. $user_id = $this->get_user_info('id');
  19. $lampid = $this->input->post('lampid',true);
  20. $page = intval($this->input->post('page',true));
  21. $count = intval($this->input->post('count',true));
  22. $print = $this->input->post('print',true);
  23. // 告警状态筛选
  24. $status = $this->input->post('status',true);
  25. // 关键字搜索
  26. $keyword = $this->input->post('keyword',true);
  27. $projectid = $this->input->post('projectid',true);
  28. // 是否导出
  29. $download = $this->input->post('download',true);
  30. // 告警类型筛选
  31. $alarmtype = trim($this->input->post('alarmtype',true));
  32. // 时间筛选
  33. $endDate = $this->input->post('endDate',true);
  34. $startDate = $this->input->post('startDate',true);
  35. $type = intval($this->input->post('type',true));
  36. $version = $this->session->userdata('version');
  37. if (!empty($version)) {
  38. $alarmtype = alarm_translate_ch($alarmtype);
  39. }
  40. $page = !isset($page) ? 1 : $page;
  41. $limit = empty($count) ? 20 : $count;
  42. $offset = ($page - 1)*$count;
  43. $section = $this->input->post('section',true);
  44. if (!empty($section)) $filter['section'] = $section;
  45. if (!empty($lampid)) $filter['lampid'] = $lampid;
  46. if (!empty($keyword)) $filter['keyword'] = $keyword;
  47. if (!empty($projectid)) $filter['projectid'] = $projectid;
  48. if(!isset($lampid) && isset($status) && $status >= 0) $filter['status'] = $status;
  49. if ($type == 0) {
  50. $filter['status'] = empty($filter['status']) ? 0 : $filter['status'];
  51. }
  52. if(!empty($alarmtype)) $filter['alarmtype'] = $alarmtype;
  53. if(isset($endDate)) $filter['endDate'] = $endDate;
  54. if(isset($startDate)) $filter['startDate'] = $startDate;
  55. if (empty($download)) {
  56. if (empty($type)) {
  57. $join = array();
  58. $join[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as WI','cond'=>'WI.lampid = AI.lampid AND WI.maxTime = AI.updatetime','type'=>'inner'];
  59. $join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];
  60. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];
  61. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];
  62. $where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  63. $where = implode(' AND ', $where);
  64. $alarm = $this->Alarm_model->get_list_by_join($where, 'AI.batstatus,AI.panelstatus,AI.lampstatus,AI.tempstatus,AI.status,AI.updatetime,G.timezone,L.number,P.projectname as project,AI.id',$limit, $offset, $join, 'AI.updatetime DESC,id DESC', NUll, 'AI');
  65. $total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);
  66. $total = $total['total'];
  67. // $alarm = $this->Alarm_model->getList($filter, $role, $user_id, $page, $count,1);
  68. }else{
  69. $join = array();
  70. // $join[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as WI','cond'=>'WI.lampid = AI.lampid AND WI.maxTime = AI.updatetime','type'=>'inner'];
  71. $join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];
  72. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];
  73. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];
  74. $where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  75. $where = implode(' AND ', $where);
  76. $alarm = $this->Alarm_model->get_list_by_join($where, 'AI.batstatus,AI.panelstatus,AI.lampstatus,AI.tempstatus,AI.status,AI.updatetime,G.timezone,L.number,P.projectname as project,AI.id',$limit, $offset, $join, 'AI.updatetime DESC,id DESC', NUll, 'AI');
  77. $total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);
  78. $total = $total['total'];
  79. }
  80. $alarm = empty($alarm) ? array() : $alarm;
  81. $batstatus = $this->config->item('batstatus');
  82. $panelstatus = $this->config->item('panelstatus');
  83. $lampstatus = $this->config->item('lampstatus');
  84. $tempstatus = $this->config->item('tempstatus');
  85. foreach($alarm as &$value){
  86. $temp2 = array();
  87. if (isset($batstatus[$value['batstatus']])) $temp2[] = $batstatus[$value['batstatus']];
  88. if (isset($panelstatus[$value['panelstatus']])) $temp2[] = $panelstatus[$value['panelstatus']];
  89. if (isset($lampstatus[$value['lampstatus']])) $temp2[] = $lampstatus[$value['lampstatus']];
  90. if (isset($tempstatus[$value['tempstatus']])) $temp2[] = $tempstatus[$value['tempstatus']];
  91. $value['stralarmtype'] = implode(',', $temp2);
  92. $value['updatetime'] = set_timezone($value['updatetime'],$value['timezone']);
  93. }
  94. $data = array('history_list'=>$alarm, 'total'=>ceil($total/$count));
  95. if (empty($type)) {
  96. $filter['status'] = 0;
  97. $filter['fault'] = 0;
  98. // $data['fault'] = $this->Alarm_model->getDataCount($filter, $role, $user_id);
  99. }
  100. exit(json_result('0000', $this->response['0000'], array('list'=>$data)));
  101. }else{
  102. $alarm = $this->Alarm_model->getList($filter, $role, $user_id, ($page-1)*$count, 3000);
  103. $alarm = empty($alarm) ? array() : $alarm;
  104. // 导出到Excel
  105. if (empty($version)) {
  106. $title = array(
  107. array('编号','项目名称','报警路灯','报警事件','是否已处理','更新时间'),
  108. );
  109. }else{ // 英文导出
  110. $title = array(
  111. array('Number','Project name','Alarm lamp','Alarm event','Has it been processed','Update time'),
  112. );
  113. }
  114. $temp = array();
  115. $i = ($page-1)*$count + 1;
  116. foreach ($alarm as $k=>$v) {
  117. $temp[$k][] = $i;
  118. $temp[$k][] = $v['project'];
  119. $temp[$k][] = $v['number'];
  120. if (empty($version)) {
  121. $stralarmtype = trim($v['stralarmtype']);
  122. if (!empty($stralarmtype)) {
  123. $alarmArr = explode(',', $stralarmtype);
  124. $temp1 = array();
  125. foreach ($alarmArr as $a) {
  126. if ($a == '电池故障') {
  127. $a = '电池电压异常';
  128. }
  129. $temp1[] = $a;
  130. }
  131. $v['stralarmtype'] = implode(',', $temp1);
  132. }
  133. $temp[$k][] = $v['stralarmtype'];
  134. $temp[$k][] = empty($v['status']) ? '未处理' : '已处理';
  135. }else{
  136. $stralarmtype = trim($v['stralarmtype']);
  137. if (!empty($stralarmtype)) {
  138. $alarmArr = explode(',', $stralarmtype);
  139. $arr = array();
  140. foreach ($alarmArr as $a) {
  141. if ($a == '电池故障') {
  142. $a = '电池电压异常';
  143. }
  144. $arr[] = alarm_translate($a);
  145. }
  146. // var_dump($temp);
  147. $temp[$k][] = empty($arr) ? '' : implode('.', $arr);
  148. }else{
  149. $temp[$k][] = '';
  150. }
  151. $temp[$k][] = empty($v['status']) ? 'Untreated' : 'Already processed';
  152. }
  153. $temp[$k][] = $v['updatetime'];
  154. $i ++;
  155. }
  156. $data = array_merge($title,$temp);
  157. $path = push_excel($data,'alarmExcel_'.date('Ymd'));
  158. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  159. }
  160. }
  161. // 查看故障详情
  162. public function detail(){
  163. $id = $this->input->post('id',true);
  164. if (empty($id)) {
  165. exit(json_result('0604', $this->response['0604'], array()));
  166. }
  167. $data = $this->Alarm_model->getOne($id);
  168. exit(json_result('0000', $this->response['0000'], $data));
  169. }
  170. // 更改故障状态
  171. public function set(){
  172. $role = $this->get_user_info('role');
  173. // if ($role == COMPANY_CUSTOMER) {
  174. // exit(json_result('0011', $this->response['0011'], array()));
  175. // }
  176. $ids = $this->input->post("ids",true);
  177. if (empty($ids)) {
  178. exit(json_result('0604', $this->response['0604'], array()));
  179. }
  180. $status = $this->input->post("status",true);
  181. $ids = explode(",", $ids);
  182. $filter = array('id' => $ids);
  183. $data = array('status' => intval($status));
  184. $res = $this->Alarm_model->update($filter, $data);
  185. if ($status == 1) {
  186. foreach ($ids as $v) {
  187. $alarmData = $this->Alarm_model->getOne($v,'A.lampid,A.alarmtype');
  188. if ($status == 0) {
  189. $this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>1));
  190. }else{
  191. $this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>0));
  192. }
  193. $this->Alarm_model->update(array('lampid'=>$alarmData['lampid'],'status'=>0),array('status'=>1));
  194. }
  195. }
  196. if ($res) {
  197. exit(json_result('0000', $this->response['0000'], array()));
  198. } else {
  199. exit(json_result('0009', $this->response['0009'], array()));
  200. }
  201. }
  202. // 删除故障信息
  203. public function del(){
  204. $role = $this->get_user_info('role');
  205. // if ($role == COMPANY_CUSTOMER) {
  206. // exit(json_result('0011', $this->response['0011'], array()));
  207. // }
  208. $ids = $this->input->post("ids",true);
  209. $lamp_ids = $this->input->post('lamp_ids', true);
  210. if (empty($ids) && empty($lamp_ids)) {
  211. exit(json_result('0603', $this->response['0603'], array()));
  212. }
  213. if (!empty($ids)) {
  214. $ids = explode(",", $ids);
  215. $res = $this->Alarm_model->delBatch($ids);
  216. } else {
  217. $lamp_ids = explode(",", $lamp_ids);
  218. $res = $this->Alarm_model->delBatch($lamp_ids, 'lampid');
  219. $this->Lamp_model->markNormal($lamp_ids);
  220. $dataArr = $this->Lamp_model->getBatch($lamp_ids);
  221. foreach ($dataArr as $data) {
  222. $this->Network_model->minus_fault_count($data['networkid']);
  223. $this->Project_model->minus_fault_count($data['projectid']);
  224. }
  225. }
  226. if ($res) {
  227. exit(json_result('0000', $this->response['0000'], array()));
  228. } else {
  229. exit(json_result('0005', $this->response['0005'], array()));
  230. }
  231. }
  232. // 巡检信息列表
  233. public function patrol_list(){
  234. $role = $this->get_user_info('role');
  235. $userid = $this->get_user_info('id');
  236. $companyid = $this->get_user_info('companyid');
  237. $projectid = $this->input->post('projectid',true);
  238. $page = $this->input->post('page',true);
  239. $count = $this->input->post('count',true);
  240. $keyword = $this->input->post('keyword',true);
  241. $download = $this->input->post('download',true);
  242. $status = $this->input->post('status',true);
  243. $print = $this->input->post('print',true);
  244. $page = !isset($page) ? 1 : $page;
  245. $count = empty($count) ? 20 : $count;
  246. // 信息字段
  247. $field = "L.id,
  248. N.id as networkid,
  249. P.id as projectid,
  250. N.networkname as network_name,
  251. P.projectname as project_name,
  252. L.number,
  253. cmd.patroltype,
  254. cmd.patrolinterval,
  255. cmd.updatetime";
  256. $fiter =array();
  257. $section = $this->input->post('section',true);
  258. if (!empty($section)) $fiter['section'] = $section;
  259. if (!empty($keyword)) $fiter['keyword'] = $keyword;
  260. if (isset($status)) $fiter['status'] = $status;
  261. if (empty($download)) {
  262. // 获取巡检信息列表
  263. if (isset($print) && !empty($print)) {
  264. $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,$page,$count,1);
  265. }else{
  266. $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,$page,$count);
  267. }
  268. // 获取巡检信息总数
  269. $total = $this->Patrol_model->getTotal($role,$companyid,$projectid,$userid,$fiter);
  270. json_result('0000',$this->response['0000'],array('patrol_list'=>$data,'total'=>ceil($total/$count)));
  271. }else{
  272. // 获取巡检信息列表
  273. $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,($page-1)*$count,3000);
  274. $version = $this->session->userdata('version');
  275. if (empty($version)) {
  276. $title = array(
  277. array('编号','项目名称','网络名称','路灯编号','巡检命令','巡检间隔时间(min)','参数设置时间'),
  278. );
  279. }else{
  280. $title = array(
  281. array('Number','Project name','Network name','Lamp Number','Patrol order','Inspection interval time(min)','Parameter setting time'),
  282. );
  283. }
  284. $temp = array();
  285. $i = ($page-1)*$count + 1;
  286. foreach ($data as $k=>$v) {
  287. $temp[$k][] = $i;
  288. $temp[$k][] = $v['project_name'];
  289. $temp[$k][] = $v['network_name'];
  290. $temp[$k][] = $v['number'];
  291. if (empty($version)) {
  292. $temp[$k][] = $v['patroltype'] == 1 ? '开' : '关';
  293. }else{
  294. $temp[$k][] = $v['patroltype'] == 1 ? 'on' : 'off';
  295. }
  296. $temp[$k][] = $v['patrolinterval'];
  297. $temp[$k][] = $v['updatetime'];
  298. $i ++;
  299. }
  300. $res = array_merge($title,$temp);
  301. $path = push_excel($res,'patrolExcel_'.date('Ymd'));
  302. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  303. }
  304. }
  305. // 单个路灯维修信息
  306. public function repair_info(){
  307. $alarmid = $this->input->post('alarmid');
  308. if (empty($alarmid)) {
  309. exit(json_result('0007',$this->response['0007'],array()));
  310. }
  311. $page = $this->input->post('page',true);
  312. $count = $this->input->post('count',true);
  313. $page = empty($page) ? 1 : $page;
  314. $count = empty($count) ? 1 : $count;
  315. $alarmData = $this->Alarm_model->getOne($alarmid,'A.lampid,A.alarmtype');
  316. $field = 'RI.repair_hitch,RI.repair_path,RI.repair_solution,RI.repair_time,RU.name';
  317. $repair_list = $this->Repair_model->get_repair_info_list(array('RI.lampid'=>$alarmData['lampid'],'page'=>$page,'count'=>$count),$field);
  318. $total = $this->Repair_model->get_repair_total(array('RI.lampid'=>$alarmData['lampid']));
  319. if (empty($repair_list)) {
  320. exit(json_result('0616',$this->response['0616'],array()));
  321. }
  322. exit(json_result('0000',$this->response['0000'],array('list'=>$repair_list,'total'=>ceil($total/$count))));
  323. }
  324. }