Alarm.php 14 KB

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