Alarm_model.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Alarm_model extends Base_model {
  5. protected $table = 'alarm_info_log';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. // 告警信息列表
  10. public function getList($filter,$role,$user_id,$page = null,$limit = null,$type = 0){
  11. $sql = '';
  12. $sqlSel = "";
  13. $implode = array();
  14. if (isset($filter['id']) && !empty($filter['id'])) {
  15. $implode[] = "A.id='" . intval($filter['id']) . "'";
  16. }
  17. // 告警状态筛选
  18. if (isset($filter['status']) && is_numeric($filter['status'])) {
  19. $implode[] = "A.status='" . $filter['status'] . "'";
  20. if ($filter['status'] == 0) {
  21. $implode[] = "L.isfaulted=1";
  22. }else{
  23. $implode[] = "L.isfaulted=0";
  24. }
  25. }
  26. if (isset($filter['lampid']) && is_numeric($filter['lampid'])) {
  27. $implode[] = "A.lampid='" . $filter['lampid'] . "'";
  28. }
  29. // 告警类型筛选
  30. if (isset($filter['alarmtype']) && !empty($filter['alarmtype'])) {
  31. $implode[] = "A.stralarmtype like '%" . $filter['alarmtype'] . "%'";
  32. }
  33. if (isset($filter['projectid']) && is_numeric($filter['projectid'])) {
  34. $implode[] = "L.projectid='" . $filter['projectid'] . "'";
  35. }
  36. if (isset($filter['section']) && !empty($filter['section'])) {
  37. $implode[] = "L.section='" . $filter['section'] . "'";
  38. }
  39. // 关键字搜索
  40. if (isset($filter['keyword']) && !empty($filter['keyword'])) {
  41. $implode[] = "(L.number LIKE '%{$filter['keyword']}%' or P.projectname LIKE '%{$filter['keyword']}%')";
  42. }
  43. // 时间筛选
  44. if (!empty($filter['startDate']) && !empty($filter['endDate'])) {
  45. $implode[] = "A.updatetime >= '{$filter['startDate']}'";
  46. $implode[] = "A.updatetime <= '{$filter['endDate']}'";
  47. }
  48. $this->load->model('User_model');
  49. $user_info = $this->User_model->get_user_by_id($user_id);
  50. $companyid = $user_info['companyid'];
  51. $projectIds = $this->get_projectid_by_role($role,$user_id,$companyid);
  52. if(empty($projectIds)){
  53. $implode[] = "L.projectid in (0)";
  54. }else{
  55. $implode[] = "L.projectid in ({$projectIds})";
  56. }
  57. $field = 'A.id as id,
  58. A.lampid AS lampid,
  59. A.alarmtype AS alarmtype,
  60. A.stralarmtype AS stralarmtype,
  61. A.status AS status,
  62. L.number AS number,
  63. P.projectname AS project,
  64. N.networkname AS network,
  65. L.address,
  66. L.isfaulted,
  67. L.protocoltype as lampprotocoltype';
  68. if (isset($filter['type']) && $filter['type'] == 0){
  69. $field .= ',max(A.updatetime) AS updatetime';
  70. $implode[] = "A.updatetime = T.time";
  71. $where = $implode ? " WHERE " . implode(" AND ", $implode) : '';
  72. $query = "SELECT {$field}
  73. FROM alarm_info_log AS A
  74. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  75. LEFT JOIN network AS N ON N.id = L.networkid
  76. LEFT JOIN project AS P ON P.id = L.projectid
  77. LEFT JOIN (select max(updatetime) as time,lampid FROM alarm_info_log group by lampid) as T on A.lampid = T.lampid
  78. ".$where;
  79. $query .= " group by A.lampid";
  80. }else{
  81. $field .= ',A.updatetime AS updatetime';
  82. $where = $implode ? " WHERE " . implode(" AND ", $implode) : '';
  83. $query = "SELECT {$field}
  84. FROM alarm_info_log AS A
  85. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  86. LEFT JOIN network AS N ON N.id = L.networkid
  87. LEFT JOIN project AS P ON P.id = L.projectid ".$where;
  88. }
  89. $query .= " ORDER BY updatetime desc";
  90. $limit_sql = "";
  91. if (empty($type) && $limit < 1000) {
  92. if ($page != null && $limit != null){
  93. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  94. }
  95. }else{
  96. if (isset($page) && isset($limit)){
  97. $limit_sql = " LIMIT ".$page.",".$limit;
  98. }
  99. }
  100. $query .= $limit_sql;
  101. // echo $query;;die;
  102. $query = $this->db->query($query);
  103. return $query->result_array();
  104. }
  105. // 获取故障信息
  106. public function get_fault_info($lampid){
  107. $field = "P.projectname,L.number,AI.stralarmtype,AI.id as alarmid,AI.updatetime,AI.status,RU.id as userid";
  108. $sql = "select {$field} FROM
  109. lampinfo as L LEFT join {$this->table} as AI on L.id=AI.lampid
  110. LEFT join project as P on P.id=L.projectid
  111. LEFT join repair_dispatch as RD on RD.lampid=L.id
  112. LEFT join repair_user as RU on RU.id=RD.repair_userid
  113. where L.id={$lampid} order by AI.updatetime desc limit 1";
  114. return $this->db->query($sql)->row_array();
  115. }
  116. public function get_list_by_filter($filter,$page = null,$limit = null) {
  117. if (isset($filter['keyword']) || isset($filter['s_type'])){
  118. $project_ids_where_in = "";
  119. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  120. $this->load->model('User_model');
  121. $user_project = $this->User_model->get_user_zone();
  122. $project_ids_where_in = " AND L.projectid IN({$user_project}) ";
  123. } else if ($this->session->userdata('role') == COMPANY_ADMIN) {
  124. $id = $this->session->userdata('id');
  125. $this->load->model('User_model');
  126. $user_info = $this->User_model->get_user_by_id($id);
  127. $companyid = $user_info['companyid'];
  128. if ($companyid) {
  129. $this->load->model('Project_model');
  130. $project_ids = $this->Project_model->get_project_ids(array('company'=>$companyid));
  131. $user_project = implode(',', $project_ids);
  132. if ($user_project) {
  133. $project_ids_where_in = " AND L.projectid IN({$user_project}) ";
  134. } else {
  135. $project_ids_where_in = " AND L.projectid in (0)";
  136. }
  137. } else {
  138. $project_ids_where_in = " AND L.projectid in (0)";
  139. }
  140. }
  141. if (in_array($filter['s_type'], array('project'))){
  142. $keywork_name_like = " P.projectname LIKE '%{$filter['keyword']}%' ";
  143. }elseif (in_array($filter['s_type'], array('network'))){
  144. $keywork_name_like = " N.networkname LIKE '%{$filter['keyword']}%' ";
  145. }elseif (in_array($filter['s_type'], array('lamp'))){
  146. $keywork_name_like = " L.number LIKE '%{$filter['keyword']}%' ";
  147. }
  148. $sql = "SELECT A.id as id,
  149. A.lampid AS lampid,
  150. A.alarmtype AS alarmtype,
  151. A.stralarmtype AS stralarmtype,
  152. A.status AS status,
  153. A.updatetime AS updatetime,
  154. L.number AS number,
  155. P.projectname AS project,
  156. N.networkname AS network
  157. FROM alarm_info_log AS A
  158. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  159. LEFT JOIN network AS N ON N.id = L.networkid
  160. LEFT JOIN project AS P ON P.id = L.projectid
  161. WHERE {$keywork_name_like} {$project_ids_where_in} ORDER BY A.updatetime desc";
  162. $limit_sql = "";
  163. if ($page != null && $limit != null){
  164. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  165. $sql .= $limit_sql;
  166. $query = $this->db->query($sql);
  167. return $query->result_array();
  168. }
  169. }
  170. if (isset($filter['network'])){
  171. $status_where = "";
  172. if (isset($filter['status']) && in_array($filter['status'], array(0,1))){
  173. $status_where = " AND A.status=".$filter['status']." ";
  174. }
  175. $sql = "SELECT A.id as id,
  176. A.lampid AS lampid,
  177. A.alarmtype AS alarmtype,
  178. A.stralarmtype AS stralarmtype,
  179. A.status AS status,
  180. A.updatetime AS updatetime,
  181. L.number AS number,
  182. P.projectname AS project,
  183. N.networkname AS network
  184. FROM alarm_info_log AS A
  185. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  186. LEFT JOIN network AS N ON N.id = L.networkid
  187. LEFT JOIN project AS P ON P.id = L.projectid
  188. WHERE L.networkid = {$filter['network']} ".$status_where."ORDER BY A.updatetime desc";
  189. $limit_sql = "";
  190. if ($page != null && $limit != null){
  191. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  192. }
  193. $sql .= $limit_sql;
  194. $query = $this->db->query($sql);
  195. return $query->result_array();
  196. }
  197. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  198. $this->load->model('User_model');
  199. $user_project = $this->User_model->get_user_zone();
  200. if ($user_project) {
  201. $projects = explode(',', $user_project);
  202. $this->db->where_in('id', $projects);
  203. } else {
  204. $this->db->where_in('id', array(0));
  205. }
  206. } else if ($this->session->userdata('role') == COMPANY_ADMIN) {
  207. $id = $this->session->userdata('id');
  208. $this->load->model('User_model');
  209. $user_info = $this->User_model->get_user_by_id($id);
  210. $companyid = $user_info['companyid'];
  211. if ($companyid) {
  212. $this->load->model('Project_model');
  213. $project_ids = $this->Project_model->get_project_ids(array('company'=>$companyid));
  214. $project_ids = empty($project_ids) ? array(0) : $project_ids;
  215. $this->db->where_in('id', $project_ids);
  216. } else {
  217. $this->db->where_in('id', array(0));
  218. }
  219. }
  220. $status_where = "";
  221. if (!empty($filter)){
  222. if (isset($filter['status']) && in_array($filter['status'], array(0,1))){
  223. $status_where = " AND A.status=".$filter['status']." ";
  224. unset($filter['status']);
  225. }
  226. foreach ($filter as $k => $v) {
  227. $this->db->where($k,$v);
  228. }
  229. }
  230. $this->db->select('*');
  231. $this->db->order_by("id","DESC");
  232. $query = $this->db->get('project');
  233. $project = $query->result_array();
  234. if (empty($project)){
  235. return array();
  236. }
  237. $Ids = array();
  238. $projectIds = array();
  239. foreach ($project as $v){
  240. $Ids[] = $v['id'];
  241. $projectIds[$v['id']] = $v['projectid'];
  242. }
  243. $condition = "L.projectid in (".implode(',', $Ids).")";
  244. $sql = "SELECT A.id as id,
  245. A.lampid AS lampid,
  246. A.alarmtype AS alarmtype,
  247. A.stralarmtype AS stralarmtype,
  248. A.status AS status,
  249. A.updatetime AS updatetime,
  250. L.number AS number,
  251. P.projectname AS project,
  252. N.networkname AS network
  253. FROM alarm_info_log AS A
  254. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  255. LEFT JOIN network AS N ON N.id = L.networkid
  256. LEFT JOIN project AS P ON P.id = L.projectid
  257. WHERE {$condition} {$status_where} ORDER BY A.updatetime desc";
  258. $limit_sql = "";
  259. if ($page != null && $limit != null){
  260. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  261. }
  262. $sql .= $limit_sql;
  263. $query = $this->db->query($sql);
  264. return $query->result_array();
  265. }
  266. public function get_list_by_projectid($projectid,$filter=array(),$field='*'){
  267. $this->db->select($field);
  268. if (!empty($filter['group'])) {
  269. $this->db->group_by($filter['group']);
  270. unset($filter['group']);
  271. }
  272. if (!empty($filter)) {
  273. foreach ($filter as $key => $value) {
  274. $this->db->where($key,$value);
  275. }
  276. }
  277. if (!empty($projectid)) {
  278. $this->db->where('L.projectid',$projectid);
  279. }
  280. $this->db->join('lampinfo as L','on L.id = AI.lampid','LEFT');
  281. $this->db->order_by('AI.updatetime','desc');
  282. return $this->db->get($this->table.' as AI')->result_array();
  283. }
  284. public function getTotal($filter){
  285. if (isset($filter['keyword']) || isset($filter['s_type'])){
  286. $project_ids_where_in = "";
  287. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  288. $this->load->model('User_model');
  289. $user_project = $this->User_model->get_user_zone();
  290. $project_ids_where_in = " AND L.projectid IN({$user_project}) ";
  291. } else if ($this->session->userdata('role') == COMPANY_ADMIN) {
  292. $id = $this->session->userdata('id');
  293. $this->load->model('User_model');
  294. $user_info = $this->User_model->get_user_by_id($id);
  295. $companyid = $user_info['companyid'];
  296. if ($companyid) {
  297. $this->load->model('Project_model');
  298. $project_ids = $this->Project_model->get_project_ids(array('company'=>$companyid));
  299. $user_project = implode(',', $project_ids);
  300. if ($user_project) {
  301. $project_ids_where_in = " AND L.projectid IN({$user_project}) ";
  302. } else {
  303. $project_ids_where_in = " AND L.projectid in (0)";
  304. }
  305. } else {
  306. $project_ids_where_in = " AND L.projectid in (0)";
  307. }
  308. }
  309. if (in_array($filter['s_type'], array('project'))){
  310. $keywork_name_like = " P.projectname LIKE '%{$filter['keyword']}%' ";
  311. }elseif (in_array($filter['s_type'], array('network'))){
  312. $keywork_name_like = " N.networkname LIKE '%{$filter['keyword']}%' ";
  313. }elseif (in_array($filter['s_type'], array('lamp'))){
  314. $keywork_name_like = " L.number LIKE '%{$filter['keyword']}%' ";
  315. }
  316. $sql = "SELECT count(A.id) as total
  317. FROM alarm_info_log AS A
  318. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  319. LEFT JOIN network AS N ON N.id = L.networkid
  320. LEFT JOIN project AS P ON P.id = L.projectid
  321. WHERE {$keywork_name_like} {$project_ids_where_in} ORDER BY A.updatetime desc";
  322. $query = $this->db->query($sql);
  323. $arr = $query->row_array();
  324. return $arr['total'];
  325. }
  326. if (isset($filter['network'])){
  327. $status_where = "";
  328. if (isset($filter['status']) && in_array($filter['status'], array(0,1))){
  329. $status_where = " AND A.status=".$filter['status']." ";
  330. unset($filter['status']);
  331. }
  332. $sql = "SELECT count(A.id) as total
  333. FROM alarm_info_log AS A LEFT JOIN lampinfo AS L
  334. ON A.lampid = L.id WHERE L.networkid = {$filter['network']}".$status_where;
  335. $query = $this->db->query($sql);
  336. $arr = $query->row_array();
  337. return $arr['total'];
  338. }
  339. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  340. $this->load->model('User_model');
  341. $user_project = $this->User_model->get_user_zone();
  342. if ($user_project) {
  343. $projects = explode(',', $user_project);
  344. $this->db->where_in('id',$projects);
  345. } else {
  346. $this->db->where_in('id', array(0));
  347. }
  348. } else if ($this->session->userdata('role') == COMPANY_ADMIN) {
  349. $id = $this->session->userdata('id');
  350. $this->load->model('User_model');
  351. $user_info = $this->User_model->get_user_by_id($id);
  352. $companyid = $user_info['companyid'];
  353. if ($companyid) {
  354. $this->load->model('Project_model');
  355. $project_ids = $this->Project_model->get_project_ids(array('company'=>$companyid));
  356. $project_ids = empty($project_ids) ? array(0) : $project_ids;
  357. $this->db->where_in('id', $project_ids);
  358. } else {
  359. $this->db->where_in('id', array(0));
  360. }
  361. }
  362. $status_where = "";
  363. if (!empty($filter)){
  364. if (isset($filter['status']) && in_array($filter['status'], array(0,1))){
  365. $status_where = " AND A.status=".$filter['status']." ";
  366. unset($filter['status']);
  367. }
  368. foreach ($filter as $k => $v) {
  369. $this->db->where($k,$v);
  370. }
  371. }
  372. $this->db->select('*');
  373. $this->db->order_by("id","DESC");
  374. $query = $this->db->get('project');
  375. $project = $query->result_array();
  376. if (empty($project)){
  377. return 0;
  378. }
  379. $Ids = array();
  380. $projectIds = array();
  381. foreach ($project as $v){
  382. $Ids[] = $v['id'];
  383. $projectIds[$v['id']] = $v['projectid'];
  384. }
  385. $condition = "L.projectid in (".implode(',', $Ids).")";
  386. $query = "SELECT count(A.id) as total
  387. FROM alarm_info_log AS A LEFT JOIN lampinfo AS L
  388. ON A.lampid = L.id WHERE {$condition} {$status_where}";
  389. $query = $this->db->query($query);
  390. $arr = $query->row_array();
  391. return $arr['total'];
  392. }
  393. public function get_one_by_filter($filter=array(),$field="*"){
  394. $this->db->select($field);
  395. foreach ($filter as $key => $value) {
  396. $this->db->where($key,$value);
  397. }
  398. $this->db->order_by('updatetime','desc');
  399. $this->db->limit(1);
  400. $data = $this->db->get($this->table)->row_array();
  401. return $data;
  402. }
  403. public function getOne($id,$field=null){
  404. if (empty($field)) {
  405. $field = "P.projectname AS project_name,
  406. N.networkid AS network_no,
  407. L.number AS lamp_no,
  408. L.section AS location,
  409. A.stralarmtype AS alarm_event,
  410. A.updatetime AS alarm_time,
  411. NOW() AS network_time,
  412. A.lampid AS lampid,
  413. A.status AS status";
  414. }
  415. $sql = "SELECT {$field}
  416. FROM alarm_info_log AS A
  417. LEFT JOIN lampinfo AS L ON A.lampid = L.id
  418. LEFT JOIN network AS N ON N.id = L.networkid
  419. LEFT JOIN project AS P ON P.id = L.projectid WHERE A.id = {$id} order by A.updatetime";
  420. $query = $this->db->query($sql);
  421. return $query->row_array();
  422. }
  423. public function delData($condition) {
  424. if (!empty($condition)){
  425. foreach ($condition as $k => $v) {
  426. if (is_array($v)) {
  427. if (!empty($v)) {
  428. $this->db->where_in($k,$v);
  429. }else{
  430. $this->db->where_in($k,array(0));
  431. }
  432. }else{
  433. $this->db->where($k,$v);
  434. }
  435. }
  436. }else{
  437. return false;
  438. }
  439. $this->db->delete($this->table);
  440. if ($this->db->affected_rows() > 0) {
  441. return true;
  442. } else {
  443. return false;
  444. }
  445. }
  446. public function delBatch($data, $field = 'id'){
  447. $this->db->where_in($field, $data);
  448. $this->db->delete($this->table);
  449. if ($this->db->affected_rows()) {
  450. return true;
  451. } else {
  452. return false;
  453. }
  454. }
  455. public function delDataByCondition($condition){
  456. $query = "DELETE FROM alarm_info_log WHERE lampid in (SELECT id FROM lampinfo WHERE ".key($condition)." = ".current($condition).")";
  457. $this->db->query($query);
  458. if ($this->db->affected_rows()) {
  459. return true;
  460. } else {
  461. return false;
  462. }
  463. }
  464. public function delBatchByCondition($data, $field = 'id'){
  465. if (empty($data)) {
  466. return false;
  467. }
  468. $data = trim(implode(',', $data), ',');
  469. $query = "DELETE FROM alarm_info_log WHERE lampid in ( SELECT DISTINCT(id) FROM lampinfo WHERE {$field} in ({$data}) )";
  470. $this->db->query($query);
  471. if ($this->db->affected_rows()) {
  472. return true;
  473. } else {
  474. return false;
  475. }
  476. }
  477. }