db->query($projectsql); $projectList = $query->result_array(); $projectIds = array(); foreach ($projectList as $p){ $projectIds[] = $p['id']; } if (empty($projectIds)){ $projectIds[] = 0; } $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")"; } } if (isset($filter['projectid']) || !empty($filter['projectid'])){ $where_sql.= " AND RI.projectid='".$filter['projectid']."'"; } if (isset($filter['networkid']) || !empty($filter['networkid'])){ $where_sql.= " AND RI.networkid='".$filter['networkid']."'"; } if (isset($filter['projectname']) || !empty($filter['projectname'])){ $projectsql = "SELECT * FROM project WHERE projectname LIKE '%".$filter['projectname']."%'"; $query = $this->db->query($projectsql); $projectList = $query->result_array(); $projectIds = array(); foreach ($projectList as $p){ $projectIds[] = $p['id']; } if (empty($projectIds)){ $projectIds[] = 0; } $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")"; } if (isset($filter['networkname']) || !empty($filter['networkname'])){ $networksql = "SELECT * FROM network WHERE networkname LIKE '%".$filter['networkname']."%'"; $query = $this->db->query($networksql); $networkList = $query->result_array(); $networkIds = array(); foreach ($networkList as $n){ $networkIds[] = $n['id']; } if (empty($networkIds)){ $networkIds[] = 0; } $where_sql.= " AND RI.networkid IN(".implode(',', $networkIds).")"; } if (isset($filter['lampnumber']) || !empty($filter['lampnumber'])){ $lampsql = "SELECT * FROM lampinfo WHERE number LIKE '%".$filter['lampnumber']."%'"; $query = $this->db->query($lampsql); $lampList = $query->result_array(); $lampIds = array(); foreach ($lampList as $l){ $lampIds[] = $l['id']; } if (empty($lampIds)){ $lampIds[] = 0; } $where_sql.= " AND RI.lampid IN(".implode(',', $lampIds).")"; } if (isset($filter['repair_path']) || !empty($filter['repair_path'])){ $where_sql.= " AND RI.repair_path LIKE'%".$filter['repair_path']."%'"; } if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){ $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['repair_username']) || !empty($filter['repair_username'])){ $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['id'])){ $where_sql = " AND RI.id=".intval($filter['id'])." "; } if (isset($filter['userid'])){ $where_sql = " AND RI.repair_userid=".intval($filter['userid'])." "; } $sql = "SELECT RI.id AS id, RI.repair_userid AS repair_userid, RI.projectid AS projectid, RI.networkid AS networkid, RI.lampid AS lampid, RI.repair_hitch AS repair_hitch, RI.repair_solution AS repair_solution, RI.repair_time AS repair_time, RI.repair_path AS repair_path, RI.created AS created, P.projectname AS projectname, N.networkname AS networkname, RU.number AS repair_usernumber, L.number AS lampnumber, RU.name AS repair_username FROM repair_info AS RI LEFT JOIN project AS P ON P.id=RI.projectid LEFT JOIN network AS N ON N.id=RI.networkid LEFT JOIN lampinfo AS L ON L.id=RI.lampid LEFT JOIN repair_user AS RU ON RU.id=RI.repair_userid WHERE 1=1"; $limit_sql = ""; if ($page != null && $limit != null){ $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit; } $query = $sql.$where_sql.$limit_sql; $query = $this->db->query($query); return $query->result_array(); } public function getDispatchList($filter,$page = null,$limit = null){ $where_sql = ""; if (isset($filter['user_id'])){ $repairuserIds = $this->get_repair_user_list($filter['user_id']); if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){ $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['repair_username']) || !empty($filter['repair_username'])){ $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['id'])){ $where_sql = " AND RD.id=".intval($filter['id'])." "; } $sql = "SELECT RD.id AS id, RD.repair_userid AS repair_userid, RD.projectid AS projectid, RD.networkid AS networkid, RD.starttime AS starttime, RD.plantime AS plantime, RD.finishtime AS finishtime, RD.created AS created, RD.address AS address, P.projectname AS projectname, N.networkname AS networkname, RU.number AS repair_usernumber, RU.name AS repair_username FROM repair_dispatch AS RD LEFT JOIN project AS P ON P.id=RD.projectid LEFT JOIN network AS N ON N.id=RD.networkid LEFT JOIN repair_user AS RU ON RU.id=RD.repair_userid WHERE 1=1"; $limit_sql = ""; if ($page != null && $limit != null){ $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit; } $query = $sql.$where_sql.$limit_sql; $query = $this->db->query($query); return $query->result_array(); } // 通过权限获取故障总数 public function get_total_by_role($filter,$role,$userid,$companyid,$projectid=0){ $data = $this->get_list_by_role($filter,$role,$userid,$companyid,$projectid,"count(*) as total"); return $data[0]['total']; } // 通过权限获取维修信息列表 public function get_list_by_role($filter,$role,$userid,$companyid,$projectid=0,$field='*',$page=null,$limit=null){ // 设置筛选条件 $keyword = ''; if (isset($filter['keyword'])) { $keyword = $filter['keyword']; unset($filter['keyword']); } $temp = array(); foreach ($filter as $key => $value) { $temp[] = "{$key} = '{$value}'"; } // 获取用户查看权限 if ($projectid > 0) { $projectIds = $projectid; }else{ $projectIds = $this->get_projectid_by_role($role,$userid,$companyid); } $where = 'role_'.$role.'='.$userid; if (empty($projectIds)) { $where .= " and P.id in (0)"; }else{ $where .= " and P.id in ({$projectIds})"; } if (!empty($temp)) { $where .= implode(' AND ', $temp); } if (!empty($keyword)) { $where .= " and (N.networkname LIKE '%{$keyword}%'"; $where .= " or L.number LIKE '%{$keyword}%') "; } // 设置分页 $limit = ''; if ($page != null && $limit != null) { $limit = " LIMIT {($page-1)*$limit},{$limit}"; } $sql = "SELECT {$field} FROM {$this->table} as R left join lampinfo as L on L.id = R.lampid left join project as P on P.id = R.projectid left join network as N on N.id = R.networkid left join repair_user as RU on RU.id = R.repair_userid where {$where} {$limit} order by R.created"; $data = $this->db->query($sql)->result_array(); return $data; } public function get_list_by_filter($filter,$field='*'){ $this->db->select($field); if (!empty($filter)){ foreach ($filter as $k => $v) { if (is_array($v)) { if (!empty($v)) { $this->db->where_in($k,$v); }else{ $this->db->where_in($k,array(0)); } }else{ $this->db->where($k,$v); } } } return $this->db->get($this->table)->result_array(); } // 获取维修信息列表 public function get_repair_info_list($filter,$field='*'){ $this->db->select($field); if(!empty($filter['page']) && !empty($filter['count'])){ $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']); unset($filter['count']); unset($filter['page']); } if (!empty($filter)){ foreach ($filter as $k => $v) { if (is_array($v)) { if (!empty($v)) { $this->db->where_in($k,$v); }else{ $this->db->where_in($k,array(0)); } }else{ $this->db->where($k,$v); } } } $this->db->join('repair_user as RU','RU.id = RI.repair_userid','left'); return $this->db->get($this->table.' as RI')->result_array(); } public function get_repair_total($filter){ $this->db->select('count(*) as total'); if (!empty($filter)){ foreach ($filter as $k => $v) { if (is_array($v)) { if (!empty($v)) { $this->db->where_in($k,$v); }else{ $this->db->where_in($k,array(0)); } }else{ $this->db->where($k,$v); } } } $this->db->join('repair_user as RU','RU.id = RI.repair_userid','left'); $data = $this->db->get($this->table.' as RI')->row_array(); return $data['total']; } // 获取维修人员id public function get_userid_by_alarm($alarmid){ $sql = "SELECT RD.repair_userid as userid from alarm_info_log AS AI left join repair_dispatch AS RD on AI.lampid = RD.lampid where RD.alarmid = {$alarmid}"; $data = $this->db->query($sql)->row_array(); return $data['userid']; } public function getTotal($filter){ $where_sql = ""; $arrProjectTpl = array('company','province','zone'); foreach ($arrProjectTpl as $key){ if (isset($filter[$key]) || !empty($filter[$key])){ $projectsql = "SELECT * FROM project WHERE $key='".$filter[$key]."'"; $query = $this->db->query($projectsql); $projectList = $query->result_array(); $projectIds = array(); foreach ($projectList as $p){ $projectIds[] = $p['id']; } if (empty($projectIds)){ $projectIds[] = 0; } $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")"; } } if (isset($filter['projectid']) || !empty($filter['projectid'])){ $where_sql.= " AND RI.projectid='".$filter['projectid']."'"; } if (isset($filter['networkid']) || !empty($filter['networkid'])){ $where_sql.= " AND RI.networkid='".$filter['networkid']."'"; } if (isset($filter['projectname']) || !empty($filter['projectname'])){ $projectsql = "SELECT * FROM project WHERE projectname LIKE '%".$filter['projectname']."%'"; $query = $this->db->query($projectsql); $projectList = $query->result_array(); $projectIds = array(); foreach ($projectList as $p){ $projectIds[] = $p['id']; } if (empty($projectIds)){ $projectIds[] = 0; } $where_sql.= " AND RI.projectid IN(".implode(',', $projectIds).")"; } if (isset($filter['networkname']) || !empty($filter['networkname'])){ $networksql = "SELECT * FROM network WHERE networkname LIKE '%".$filter['networkname']."%'"; $query = $this->db->query($networksql); $networkList = $query->result_array(); $networkIds = array(); foreach ($networkList as $n){ $networkIds[] = $n['id']; } if (empty($networkIds)){ $networkIds[] = 0; } $where_sql.= " AND RI.networkid IN(".implode(',', $networkIds).")"; } if (isset($filter['lampnumber']) || !empty($filter['lampnumber'])){ $lampsql = "SELECT * FROM lampinfo WHERE number LIKE '%".$filter['lampnumber']."%'"; $query = $this->db->query($lampsql); $lampList = $query->result_array(); $lampIds = array(); foreach ($lampList as $l){ $lampIds[] = $l['id']; } if (empty($lampIds)){ $lampIds[] = 0; } $where_sql.= " AND RI.lampid IN(".implode(',', $lampIds).")"; } if (isset($filter['repair_path']) || !empty($filter['repair_path'])){ $where_sql.= " AND RI.repair_path LIKE'%".$filter['repair_path']."%'"; } if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){ $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['repair_username']) || !empty($filter['repair_username'])){ $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RI.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['userid'])){ $where_sql = " AND RI.repair_userid=".intval($filter['userid'])." "; } $sql = "SELECT COUNT(RI.id) AS total FROM repair_info AS RI LEFT JOIN project AS P ON P.id=RI.projectid LEFT JOIN network AS N ON N.id=RI.networkid LEFT JOIN lampinfo AS L ON L.id=RI.lampid LEFT JOIN repair_user AS RU ON RU.id=RI.repair_userid WHERE 1=1"; $limit_sql = ""; $query = $sql.$where_sql.$limit_sql; $query = $this->db->query($query); $row = $query->row_array(); return $row['total']; } public function getDispatchTotal($filter){ $where_sql = ""; if (isset($filter['user_id'])){ $repairuserIds = $this->get_repair_user_list($filter['user_id']); if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['repair_usernumber']) || !empty($filter['repair_usernumber'])){ $repairusersql = "SELECT * FROM repair_user WHERE number LIKE '%".$filter['repair_usernumber']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['repair_username']) || !empty($filter['repair_username'])){ $repairusersql = "SELECT * FROM repair_user WHERE name LIKE '%".$filter['repair_username']."%'"; $query = $this->db->query($repairusersql); $repairuserList = $query->result_array(); $repairuserIds = array(); foreach ($repairuserList as $l){ $repairuserIds[] = $l['id']; } if (empty($repairuserIds)){ $repairuserIds[] = 0; } $where_sql.= " AND RD.repair_userid IN(".implode(',', $repairuserIds).")"; } if (isset($filter['id'])){ $where_sql = " AND RD.id=".intval($filter['id'])." "; } $sql = "SELECT COUNT(RD.id) AS total FROM repair_dispatch AS RD LEFT JOIN project AS P ON P.id=RD.projectid LEFT JOIN network AS N ON N.id=RD.networkid LEFT JOIN repair_user AS RU ON RU.id=RD.repair_userid WHERE 1=1"; $limit_sql = ""; $query = $sql.$where_sql.$limit_sql; $query = $this->db->query($query); $row = $query->row_array(); return $row['total']; } public function insertDispatch($data){ $this->db->insert('repair_dispatch', $data); return $this->db->insert_id(); } public function updateDispatch($filter,$data){ if (empty($filter)){ return false; } foreach ($data as $key => $value) { if ($value === '') { $data[$key] = NULL; } } $sql = $this->db->update_string('repair_dispatch',$data,$filter); return $this->db->query($sql); } public function delData($condition) { if (!empty($condition)){ foreach ($condition as $k => $v) { if (is_array($v)) { if (!empty($v)) { $this->db->where_in($k,$v); }else{ $this->db->where_in($k,array(0)); } }else{ $this->db->where($k,$v); } } }else{ return false; } $this->db->delete($this->table); if ($this->db->affected_rows() > 0) { return true; } else { return false; } } public function delDispatchData($condition) { if (!empty($condition)){ foreach ($condition as $k => $v) { $this->db->where($k,$v); } } $this->db->delete('repair_dispatch'); if ($this->db->affected_rows() > 0) { return true; } else { return false; } } public function getDispatchOne($id){ $this->db->where('id',$id); $this->db->limit('1'); $query = $this->db->get('repair_dispatch'); return $query->row_array(); } // 获取调度信息 public function getDispatchData($filter,$field = '*'){ if (empty($filter)) { return array(); } $this->db->select($field); foreach ($filter as $key => $value) { if (is_array($value)) { if (!empty($value)) { $this->db->where_in($key,$value); }else{ $this->db->where_in($key,array(0)); } }else{ $this->db->where($key,$value); } } $this->db->order_by('created','desc'); $query = $this->db->get('repair_dispatch'); return $query->row_array(); } public function delDispatchOne($id) { $this->db->where('id', $id); $this->db->delete('repair_dispatch'); if ($this->db->affected_rows() == 1) { return true; } else { return false; } } // 获取维修人员信息列表 public function get_repair_user_list($user_id){ $query = "SELECT * FROM user WHERE id={$user_id}"; $query = $this->db->query($query); $user = $query->row_array(); if (empty($user)) { return array(); } $colum = 'role_'.$user['role']; $query = "SELECT id AS id FROM repair_user WHERE {$colum}={$user_id}"; $query = $this->db->query($query); $result = $query->result_array(); $userIds = array(); foreach ($result as $v) { $userIds[] = $v['id']; } return $userIds; } // 获取维修人员总数 public function get_repair_user_total($userinfo){ $colum = 'role_'.$userinfo['role']; $sql = "SELECT count(*) as total from repair_user where {$colum}={$userinfo['id']}"; $data = $this->db->query($sql)->row_array(); return $data['total']; } // 删除维修人员 public function del_repair_user($filter){ foreach ($filter as $key => $value) { $this->db->where($key,$value); } if (!empty($filter)) { $this->db->delete('repair_user'); } } // 添加维修人员维修数 public function add_repaircount($userid){ $sql = "update repair_user set repaircount = repaircount + 1 where id={$userid}"; $this->db->query($sql); } // 减少维修人员维修数 public function minus_repaircount($userid){ $sql = "update repair_user set repaircount = repaircount - 1 where id={$userid} and repaircount >= 1"; $this->db->query($sql); } public function get_repair_user_id($filter,$id=0){ $temp = array(); foreach ($filter as $key => $value) { $temp[] = "{$key}='{$value}'"; } $where = ''; if (!empty($temp)) { $where = "where ".implode(' AND ', $temp); } $sql = "SELECT id from repair_user ".$where; $data = $this->db->query($sql)->row_array(); if (empty($data['id'])) { return 0; }else{ return $id == $data['id'] ? 0 : 1; } } public function get_repair_user_by_filter($filter,$field='*'){ $this->db->select($field); if(!empty($filter['page']) && !empty($filter['count'])){ $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']); unset($filter['count']); unset($filter['page']); } if (!empty($filter)){ foreach ($filter as $k => $v) { if (is_array($v)) { if (!empty($v)) { $this->db->where_in($k,$v); }else{ $this->db->where_in($k,array(0)); } }else{ $this->db->where($k,$v); } } } return $this->db->get('repair_user')->result_array(); } }