Patrol_model.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Patrol_model extends Base_model {
  5. protected $table = 'patrol_cmd';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function get_list_in($field, $array, $fields='*') {
  10. // $sql =
  11. if(!empty($field) && !empty($array)){
  12. // $this->db->where_in($field, $array);
  13. $s = implode(',', $array);
  14. $where = "{$field} in ({$s})";
  15. }else{
  16. return array();
  17. }
  18. // $this->db->select($fields);
  19. $query = "select {$fields} FROM {$this->table} where {$where}";
  20. $data = $this->db->query($query);
  21. return $data->result_array();
  22. }
  23. public function queryData($start,$offset){
  24. $this->db->order_by("id","DESC");
  25. $query = $this->db->get($this->table);
  26. return $query->result_array();
  27. }
  28. public function getList($role,$companyid,$projectid,$userid,$filter,$field = '*',$page = null,$limit = null,$type = 0){
  29. $sql_in = array();
  30. // if (isset($filter['keyword']) && !empty($filter['keyword'])){
  31. // $lampIds = explode(",",$filter['keyword']);
  32. // $arrIds = array();
  33. // foreach ($lampIds as $id){
  34. // if(is_numeric($id) && !empty($id)){
  35. // $arrIds[] = trim($id);
  36. // }
  37. // }
  38. // if(!empty($arrIds)){
  39. // $sql_in_ids = implode(',',$arrIds);
  40. // $sql_in =" AND L.number IN (".$sql_in_ids.")";
  41. // }
  42. // }
  43. if ($projectid > 0) {
  44. $sql_in[] = "L.projectid = {$projectid}";
  45. } elseif ($role != SYSTEM_ADMIN) {
  46. $idStr = $this->get_projectid_by_role($role,$userid,$companyid);
  47. if (!empty($idStr)) {
  48. $sql_in[] = "L.projectid in ({$idStr})";
  49. }else{
  50. $sql_in[] = "L.projectid in (0)";
  51. }
  52. }
  53. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  54. $sql_in[] ="(L.number like '%".$filter['keyword']."%' OR N.networkname like '%".$filter['keyword']."%' OR P.projectname like '%".$filter['keyword']."%')";
  55. }
  56. // var_dump($filter['status']);die;
  57. if(isset($filter['status']) && $filter['status'] == 1){
  58. $sql_in[] = "AI.status = 0 ";
  59. }
  60. if(isset($filter['section']) && !empty($filter['section'])){
  61. $sql_in[] = "L.section = '".$filter['section']."' ";
  62. }
  63. $sql_limit = "";
  64. if (empty($type) && $limit < 1000) {
  65. if(is_numeric($page) && is_numeric($limit)){
  66. $sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
  67. }
  68. }else{
  69. if(is_numeric($page) && is_numeric($limit)){
  70. $sql_limit = " LIMIT ".$page.",".$limit;
  71. }
  72. }
  73. $where = '';
  74. if (!empty($sql_in)) {
  75. $where = 'WHERE '.implode(' AND ', $sql_in);
  76. }
  77. $sql = "SELECT
  78. {$field} FROM lampinfo AS L
  79. LEFT JOIN project AS P ON P.id = L.projectid
  80. LEFT JOIN network AS N ON N.id = L.networkid
  81. LEFT JOIN (SELECT * FROM ".$this->table." ORDER BY updatetime DESC) AS cmd ON L.id = cmd.id
  82. LEFT JOIN (select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI on AI.lampid = L.id
  83. {$where} order by L.number asc, L.id asc {$sql_limit}";
  84. $query = $this->db->query($sql);
  85. return $query->result_array();
  86. }
  87. public function getTotal($role,$companyid,$projectid,$userid,$filter){
  88. $sql_in = array();
  89. // if (isset($filter['keyword']) && !empty($filter['keyword'])){
  90. // $lampIds = explode(",",$filter['keyword']);
  91. // $arrIds = array();
  92. // foreach ($lampIds as $id){
  93. // if(is_numeric($id) && !empty($id)){
  94. // $arrIds[] = trim($id);
  95. // }
  96. // }
  97. // if(!empty($arrIds)){
  98. // $sql_in_ids = implode(',',$arrIds);
  99. // $sql_in =" AND L.number IN (".$sql_in_ids.")";
  100. // }
  101. // }
  102. if ($projectid > 0) {
  103. $sql_in[] = "L.projectid = {$projectid}";
  104. } elseif ($role != SYSTEM_ADMIN) {
  105. $idStr = $this->get_projectid_by_role($role,$userid,$companyid);
  106. if (!empty($idStr)) {
  107. $sql_in[] = "L.projectid in ({$idStr})";
  108. }else{
  109. $sql_in[] = "L.projectid in (0)";
  110. }
  111. }
  112. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  113. $sql_in[] ="L.number like '%".$filter['keyword']."%'";
  114. $sql_in[] ="N.networkname like '%".$filter['keyword']."%'";
  115. $sql_in[] ="P.projectname like '%".$filter['keyword']."%'";
  116. }
  117. if(isset($filter['status']) && $filter['status'] == 1){
  118. $sql_in[] = "AI.status = 0 ";
  119. }
  120. if(isset($filter['section']) && !empty($filter['section'])){
  121. $sql_in[] = "L.section = '".$filter['section']."' ";
  122. }
  123. // $sql_limit = "";
  124. // if(is_numeric($page) && is_numeric($limit)){
  125. // $sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
  126. // }
  127. $where = '';
  128. if (!empty($sql_in)) {
  129. $where = 'WHERE '.implode(' AND ', $sql_in);
  130. }
  131. $sql = "SELECT
  132. count(L.id) as total FROM lampinfo AS L
  133. LEFT JOIN project AS P ON P.id = L.projectid
  134. LEFT JOIN network AS N ON N.id = L.networkid
  135. LEFT JOIN (SELECT * FROM ".$this->table." ORDER BY updatetime DESC) AS cmd ON L.id = cmd.id
  136. LEFT JOIN (select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI on AI.lampid = L.id
  137. {$where} order by L.number asc, L.id asc";
  138. // (select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI on AI.lampid = L.id
  139. // (SELECT * FROM alarm_info_log as t1 JOIN (SELECT max(updatetime) as time,lampid FROM alarm_info_log GROUP BY lampid) as t2 on t1.lampid = t2.lampid AND t1.updatetime = t2.time) as AI on AI.lampid = L.id
  140. $data = $this->db->query($sql)->row_array();
  141. return $data['total'];
  142. }
  143. // 通过角色获取巡检信息列表
  144. public function getPatrolInfoList($filter,$role,$userid,$companyid,$field = '*',$page = null,$limit = null,$projectid=0){
  145. if ($projectid > 0 ) {
  146. $projectIds = $projectid;
  147. }else{
  148. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  149. $projectIds = empty($projectIds) ? '0' : $projectIds;
  150. }
  151. $sql = "SELECT {$field}
  152. FROM patrol_info_log AS PIL
  153. LEFT JOIN network AS N ON PIL.networkid=N.id
  154. LEFT JOIN project AS P ON N.projectid=P.id
  155. WHERE P.id IN(".$projectIds.")" ;
  156. if (isset($filter['keyword'])) {
  157. $sql .= " AND (N.networkname like '%{$filter['keyword']}%'";
  158. $sql .= " or PIL.patroltime like '%{$filter['keyword']}%'";
  159. $sql .= " or N.section like '%{$filter['keyword']}%')";
  160. unset($filter['keyword']);
  161. }
  162. $limit_sql = "";
  163. if ($page != null && $limit != null){
  164. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  165. }
  166. $query = $sql." ORDER BY patroltime DESC ".$limit_sql;
  167. $query = $this->db->query($query);
  168. return $query->result_array();
  169. }
  170. // 通过角色获取巡检信息数目
  171. public function getPatrolInfoTotal($filter,$role,$userid,$companyid,$projectid=0){
  172. if ($projectid > 0 ) {
  173. $projectIds = $projectid;
  174. }else{
  175. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  176. $projectIds = empty($projectIds) ? '0' : $projectIds;
  177. }
  178. $sql = "SELECT COUNT(*) AS total
  179. FROM patrol_info_log as PIL
  180. left join network as N on PIL.networkid = N.id
  181. left join project as P on P.id = N.projectid
  182. WHERE P.id IN('".$projectIds."')" ;
  183. if (isset($filter['keyword'])) {
  184. $sql .= " AND (N.networkname like '%{$filter['keyword']}%'";
  185. $sql .= " or PIL.patroltime like '%{$filter['keyword']}%'";
  186. $sql .= " or N.section like '%{$filter['keyword']}%')";
  187. unset($filter['keyword']);
  188. }
  189. $query = $this->db->query($sql);
  190. $row = $query->row_array();
  191. return $row['total'];
  192. }
  193. public function getNetworkIds($filter){
  194. foreach ($filter as $k => $v) {
  195. if(in_array($k, array('company','zone','province'))){
  196. $this->db->where($k,$v);
  197. }elseif ($k == 'projectid'){
  198. $this->db->where('id',$v);
  199. }elseif ($k == 'projectname'){
  200. $this->db->like('projectname', $v);
  201. }
  202. }
  203. if ($this->session->userdata('role') == COMPANY_ADMIN) {
  204. $companyid = $this->session->userdata('company_id');
  205. $this->db->where('company', $companyid);
  206. }
  207. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  208. $this->load->model('User_model');
  209. $user_project = $this->User_model->get_user_zone();
  210. if ($user_project) {
  211. $projects = explode(',', $user_project);
  212. $this->db->where_in('id',$projects);
  213. } else {
  214. $this->db->where_in('id', array(0));
  215. }
  216. }
  217. $projectIds = array(0);
  218. $this->db->select('id');
  219. $query = $this->db->get('project');
  220. $project = $query->result_array();
  221. foreach ($project as $v){
  222. $projectIds[] = $v['id'];
  223. }
  224. // 查询networkIds
  225. $networkIds = array(0);
  226. if (isset($filter['networkid'])){
  227. $this->db->where('id',$filter['networkid']);
  228. }elseif (isset($filter['networkname'])){
  229. $this->db->like('networkname', $filter['networkname']);
  230. }
  231. $this->db->where_in('projectid', $projectIds);
  232. $this->db->select('id');
  233. $query = $this->db->get('network');
  234. $networks = $query->result_array();
  235. foreach ($networks as $v){
  236. $networkIds[] = $v['id'];
  237. }
  238. return $networkIds;
  239. }
  240. }
  241. ?>