Network_model.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Network_model extends Base_model {
  5. protected $table = 'network';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function queryData($where, $fields='*') {
  10. return $this->db->select($fields)->where($where)->get($this->table)->row_array();
  11. }
  12. // 通过网络编号判断网络是否存在
  13. public function project_exist_by_name($project_id,$network_num){
  14. if (empty($project_id) || empty($network_num)) return 0;
  15. $this->db->select('id');
  16. $this->db->where('projectid',$project_id);
  17. $this->db->where('networkid',$network_num);
  18. $res = $this->db->get($this->table)->row_array();
  19. if (empty($res)) {
  20. return 0;
  21. }else{
  22. return $res['id'];
  23. }
  24. }
  25. public function get_list_in($where_in_field, $where_in, $fields='*', $filter=array()) {
  26. $this->db->select($fields);
  27. if(!empty($where_in_field) && !empty($where_in)){
  28. $this->db->where_in($where_in_field, $where_in);
  29. }
  30. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  31. $networkNames = explode(",",$filter['keyword']);
  32. foreach ($networkNames as $name){
  33. $this->db->or_like('networkname', $name);
  34. }
  35. unset($filter['keyword']);
  36. }
  37. foreach ($filter as $key => $value) {
  38. if (is_array($value)) {
  39. if (!empty($value)) {
  40. $this->db->where_in($key,$value);
  41. }else{
  42. $this->db->where_in($key,array(0));
  43. }
  44. }else{
  45. $this->db->where($key,$value);
  46. }
  47. }
  48. $this->db->where('type',0);
  49. $query = $this->db->get($this->table);
  50. return $query->result_array();
  51. }
  52. public function getIdsLikeName($name){
  53. $this->db->select('id');
  54. $this->db->like('networkname', $name);
  55. $query = $this->db->get($this->table);
  56. $arrNetwork = $query->result_array();
  57. $ids = array();
  58. foreach ($arrNetwork as $k=> $network) {
  59. $ids[] = $network['id'];
  60. }
  61. return $ids;
  62. }
  63. public function getIdByName($name){
  64. $this->db->select('id');
  65. $this->db->where('networkname', $name);
  66. $query = $this->db->get($this->table);
  67. $result = $query->row_array();
  68. if (isset($result['id'])) {
  69. return $result['id'];
  70. }
  71. return false;
  72. }
  73. public function getList($filter,$page = null,$limit = null){
  74. if (isset($filter['network']) && empty($filter['network'])){
  75. unset($filter['network']);
  76. }
  77. if (isset($filter['network'])){
  78. $sql_in = "";
  79. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  80. $networkNames = explode(",",$filter['keyword']);
  81. $i = 0;
  82. foreach ($networkNames as $name){
  83. $prefix = $i==0 ? " AND " : " OR ";
  84. $sql_in .= $prefix."N.networkname like '%{$name}%'";
  85. $i++;
  86. }
  87. }
  88. if(isset($filter['status']) && $filter['status'] == 1){
  89. $sql_in = " AND N.faultcount > 0";
  90. }
  91. $sql_limit = "";
  92. if(is_numeric($page) && is_numeric($limit)){
  93. $sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
  94. }
  95. $sql = "SELECT N.* ,P.`projectname` as projectname FROM network AS N
  96. LEFT JOIN project AS P ON P.id = N.projectid WHERE N.id=".intval($filter['network']).$sql_in.$sql_limit;
  97. $query = $this->db->query($sql);
  98. return $query->result_array();
  99. }
  100. if(isset($filter['keyword']) && empty($filter['keyword'])){
  101. unset($filter['keyword']);
  102. }
  103. if (!empty($filter) && isset($filter['ids']) && count($filter) == 1){
  104. $sql_in = "";
  105. if (isset($filter['ids']) && !empty($filter['ids']) && is_array($filter['ids'])){
  106. $sql_in = " N.id IN(".implode(',', $filter['ids']).") ";
  107. }
  108. $sql_limit = "";
  109. if(is_numeric($page) && is_numeric($limit)){
  110. $sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
  111. }
  112. $sql = "SELECT N.* ,P.`projectname` as projectname FROM network AS N
  113. LEFT JOIN project AS P ON P.id = N.projectid WHERE ".$sql_in.$sql_limit;
  114. $query = $this->db->query($sql);
  115. return $query->result_array();
  116. }
  117. if (!empty($filter)){
  118. foreach ($filter as $k => $v) {
  119. if($k == 'status' || $k == 'keyword'){
  120. continue;
  121. }
  122. $this->db->where($k,$v);
  123. }
  124. }
  125. if ($this->session->userdata('role') == COMPANY_ADMIN) {
  126. $companyid = $this->session->userdata('company_id');
  127. $this->db->where('company', $companyid);
  128. }
  129. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  130. $this->load->model('User_model');
  131. $user_project = $this->User_model->get_user_zone();
  132. if ($user_project) {
  133. $projects = explode(',', $user_project);
  134. $this->db->where_in('id',$projects);
  135. } else {
  136. $this->db->where_in('id', array(0));
  137. }
  138. }
  139. $this->db->select('*');
  140. $query = $this->db->get('project');
  141. $project = $query->result_array();
  142. if (empty($project)){
  143. return array();
  144. }
  145. $Ids = array();
  146. $projectNames = array();
  147. foreach ($project as $v){
  148. $Ids[] = $v['id'];
  149. $projectNames[$v['id']] = $v['projectname'];
  150. }
  151. $this->db->select('*');
  152. $this->db->where_in('projectid',$Ids);
  153. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  154. $networkNames = explode(",",$filter['keyword']);
  155. foreach ($networkNames as $name){
  156. $this->db->or_like('networkname', $name);
  157. }
  158. }
  159. if(isset($filter['status']) && $filter['status'] == 1){
  160. $this->db->where('faultcount >',0);
  161. }
  162. $this->db->order_by("networkid","asc");
  163. if(is_numeric($page) && is_numeric($limit)){
  164. $this->db->limit($limit,($page-1)*$limit);
  165. }
  166. $query = $this->db->get($this->table);
  167. $arrRet = $query->result_array();
  168. if (empty($arrRet)){
  169. return array();
  170. }
  171. foreach ($arrRet as $k => $v){
  172. $arrRet[$k]['projectname'] = $projectNames[$v['projectid']];
  173. }
  174. return $arrRet;
  175. }
  176. public function get_list_by_filter($filter, $order = true) {
  177. $this->db->select('id');
  178. $this->db->select('networkid,networkname,gatewaytype');
  179. if (!empty($filter)){
  180. foreach ($filter as $k => $v) {
  181. $this->db->where($k,$v);
  182. }
  183. }
  184. if ($order) {
  185. $this->db->order_by("networkid","asc");
  186. }
  187. $query = $this->db->get($this->table);
  188. $arrRet = $query->result_array();
  189. // var_dump($this->db->last_query());die;
  190. if (empty($arrRet)){
  191. return array();
  192. }
  193. return $arrRet;
  194. }
  195. public function getTotalByProject($projectid){
  196. $this->db->select('count(*) as total');
  197. $this->db->where('projectid',$projectid);
  198. $this->db->where('type',0);
  199. $data = $this->db->get($this->table)->row_array();
  200. return $data['total'];
  201. }
  202. public function getTotal($filter, $role, $companyid, $userid = 0){
  203. if(isset($filter['network'])){
  204. $this->db->where('id',intval($filter['network']));
  205. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  206. $networkNames = explode(",",$filter['keyword']);
  207. foreach ($networkNames as $name){
  208. $this->db->or_like('networkname', $name);
  209. }
  210. }
  211. $query = $this->db->get($this->table);
  212. $ret = $query->row_array();
  213. return !empty($ret) ? 1 : 0;
  214. }
  215. if (!empty($filter)){
  216. if (isset($filter['ids']) && !empty($filter['ids']) && is_array($filter['ids'])){
  217. $this->db->where_in('id',$filter['ids']);
  218. unset($filter['ids']);
  219. }
  220. foreach ($filter as $k => $v) {
  221. if(in_array($k, array('status','keyword','nw_status','projectid'))){
  222. continue;
  223. }
  224. $this->db->where($k,$v);
  225. }
  226. }
  227. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  228. $Ids = empty($projectIds) ? array(0) : explode(',', $projectIds);
  229. $this->db->select('count(id) as total');
  230. $this->db->where_in('projectid',$Ids);
  231. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  232. $networkNames = explode(",",$filter['keyword']);
  233. foreach ($networkNames as $name){
  234. $this->db->or_like('networkname', $name);
  235. }
  236. }
  237. if(isset($filter['status']) && $filter['status'] == 1){
  238. $this->db->where('faultcount >',0);
  239. }
  240. if (isset($filter['nw_status']) && !empty($filter['nw_status'])){
  241. $this->db->where('status',$filter['nw_status']);
  242. }
  243. if (isset($filter['projectid']) && !empty($filter['projectid'])){
  244. $this->db->where('projectid',$filter['projectid']);
  245. }
  246. $query = $this->db->get($this->table);
  247. $arr = $query->row_array();
  248. return $arr['total'];
  249. }
  250. // 通过筛选条件删除网络
  251. public function delData($condition) {
  252. if (!empty($condition)){
  253. foreach ($condition as $k => $v) {
  254. if (is_array($v)) {
  255. if (!empty($v)) {
  256. $this->db->where_in($k,$v);
  257. }else{
  258. $this->db->where_in($k,array(0));
  259. }
  260. }else{
  261. $this->db->where($k,$v);
  262. }
  263. }
  264. }else{
  265. return false;
  266. }
  267. $this->db->delete($this->table);
  268. if ($this->db->affected_rows() > 0) {
  269. return true;
  270. } else {
  271. return false;
  272. }
  273. }
  274. public function getBatch($ids){
  275. $this->db->where_in('id',$ids);
  276. $query = $this->db->get($this->table);
  277. $ret = $query->result_array();
  278. if (empty($ret)){
  279. return array();
  280. }
  281. return $ret;
  282. }
  283. public function delBatch($data, $field = 'id'){
  284. $this->db->where_in($field,$data);
  285. $this->db->delete($this->table);
  286. if ($this->db->affected_rows()) {
  287. return true;
  288. } else {
  289. return false;
  290. }
  291. }
  292. // 更新网络信息
  293. public function update($filter,$data){
  294. if (empty($filter)){
  295. return false;
  296. }
  297. $sql = "SELECT `projectid`, `company` FROM `project` WHERE id = {$data['projectid']}";
  298. $query = $this->db->query($sql);
  299. $project = $query->row_array();
  300. $sql = "SELECT `ename` FROM `company` WHERE id = {$project['company']}";
  301. $query = $this->db->query($sql);
  302. $company = $query->row_array();
  303. $data['regpack'] = 'register-'.$company['ename'].'-'.$project['projectid'].'-'.$data['networkid'];
  304. $sql = $this->db->update_string($this->table,$data,$filter);
  305. return $this->db->query($sql);
  306. }
  307. public function insert($data){
  308. $sql = "SELECT `projectid`, `company` FROM `project` WHERE id = {$data['projectid']}";
  309. $query = $this->db->query($sql);
  310. $project = $query->row_array();
  311. $sql = "SELECT `ename` FROM `company` WHERE id = {$project['company']}";
  312. $query = $this->db->query($sql);
  313. $company = $query->row_array();
  314. $data['regpack'] = 'register-'.$company['ename'].'-'.$project['projectid'].'-'.$data['networkid'];
  315. $this->db->insert($this->table, $data);
  316. return $this->db->insert_id();
  317. }
  318. public function getDataCount($condition, $id = 0) {
  319. if (!empty($condition)){
  320. foreach ($condition as $k => $v) {
  321. $this->db->where($k,$v);
  322. }
  323. }
  324. if (!empty($id)) {
  325. $this->db->where('id !=',$id);
  326. }
  327. $query = $this->db->get($this->table);
  328. $data = $query->row_array();
  329. if (empty($data)) {
  330. return 0;
  331. } else {
  332. return $id == $data['id'] ? 0 : 1;
  333. }
  334. }
  335. public function getData($condition, $fields = '*') {
  336. $this->db->select($fields);
  337. if (!empty($condition)){
  338. foreach ($condition as $k => $v) {
  339. $this->db->where($k,$v);
  340. }
  341. }
  342. $query = $this->db->get($this->table);
  343. $data = $query->row_array();
  344. return $data;
  345. }
  346. // 添加路灯数
  347. public function add_lamp_count($data){
  348. $sql = "update `network` set `lampcount` = `lampcount` + 1, `faultcount` = `faultcount` + {$data['faultcount']} WHERE `id`={$data['networkid']}";
  349. $this->db->query($sql);
  350. if ($this->db->affected_rows() == 1) {
  351. return true;
  352. } else {
  353. return false;
  354. }
  355. }
  356. // 减少路灯数
  357. public function minus_lamp_count($data){
  358. $sql = "update `network` set `lampcount` = `lampcount` - 1, `faultcount` = `faultcount` - {$data['faultcount']} WHERE `id`={$data['networkid']} and `lampcount` >= 1 and `faultcount` >= {$data['faultcount']}";
  359. $this->db->query($sql);
  360. if ($this->db->affected_rows() == 1) {
  361. return true;
  362. } else {
  363. return false;
  364. }
  365. }
  366. // 添加监控数
  367. public function add_monitor_count($data){
  368. $sql = "update `network` set `monitorcount` = `monitorcount` + {$data['count']} WHERE `id`={$data['networkid']}";
  369. $this->db->query($sql);
  370. if ($this->db->affected_rows() == 1) {
  371. return true;
  372. } else {
  373. return false;
  374. }
  375. }
  376. // 减少监控数
  377. public function minus_monitor_count($data){
  378. $sql = "update `network` set `monitorcount` = `monitorcount` - {$data['count']} WHERE `id`={$data['networkid']} and `monitorcount` >= {$data['count']}";
  379. $this->db->query($sql);
  380. if ($this->db->affected_rows() == 1) {
  381. return true;
  382. } else {
  383. return false;
  384. }
  385. }
  386. public function getIdByLikeName($name){
  387. $sql = "SELECT id FROM network WHERE networkname LIKE '%".$name."%'";
  388. $query = $this->db->query($sql);
  389. $arr = $query->row_array();
  390. if (!empty($arr)){
  391. return $arr['id'];
  392. }
  393. return false;
  394. }
  395. public function getNameById($id, $field = 'networkname'){
  396. $this->db->where('id',$id);
  397. $query = $this->db->get($this->table);
  398. $arr = $query->row_array();
  399. return !empty($arr) ? $arr[$field] : 0;
  400. }
  401. public function minus_fault_count($id){
  402. $sql = "update `network` set `faultcount` = `faultcount` - 1 WHERE `id`= {$id} and `faultcount` >= 1";
  403. $this->db->query($sql);
  404. if ($this->db->affected_rows() == 1) {
  405. return true;
  406. } else {
  407. return false;
  408. }
  409. }
  410. }