Project_model.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Project_model extends Base_model {
  5. protected $table = 'project';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. // 通过项目来获取时区
  10. public function get_timezone_by_projectid($id){
  11. $sql = "select T.value FROM {$this->table} as P left join timezone as T on P.timezone = T.id where P.id = {$id}";
  12. return $this->db->query($sql)->row_array();
  13. }
  14. // 通过项目名称判断项目是否存在
  15. public function project_exist_by_name($role,$userid,$companyid,$project_name,$company_name){
  16. $projectids = $this->get_projectid_by_role($role,$userid,$companyid);
  17. if (empty($projectids)) {
  18. return 0;
  19. }else{
  20. $idArr = explode(',', $projectids);
  21. $this->db->select('P.id');
  22. $this->db->where_in('P.id',$idArr);
  23. $this->db->where('P.projectname',$project_name);
  24. $this->db->where('C.name',$company_name);
  25. $this->db->join('company as C', 'C.id = P.company');
  26. $res = $this->db->get($this->table.' as P')->row_array();
  27. if (empty($res)) {
  28. return 0;
  29. }else{
  30. return $res['id'];
  31. }
  32. }
  33. }
  34. // 获取时区信息
  35. public function get_timezone_info($fiter=array(),$field='*'){
  36. $this->db->select($field);
  37. if (!empty($fiter)) {
  38. foreach ($fiter as $key => $value) {
  39. $this->db->where($key,$value);
  40. }
  41. }
  42. return $this->db->get('timezone')->row_array();
  43. }
  44. // 获取时区列表
  45. public function timezone_list($fields = '*'){
  46. $this->db->select($fields);
  47. return $this->db->get('timezone')->result_array();
  48. }
  49. // 项目用电量,年月日
  50. private function project_data($projectid){
  51. $res = array();
  52. // 获取总的用电量,当天总用电量
  53. $today = date('Y-m-d 00:00:00');
  54. $sql = "SELECT sum(elog.totalconsumption) as totalconsumption,sum(elog.totalgeneration) as totalgeneration FROM project as P
  55. join lampinfo as L on L.projectid = P.id
  56. join (select t1.* FROM electric_info_log as t1 join (select lampid,max(updatetime) as time FROM electric_info_log group by lampid) as t2 on t1.updatetime = t2.time and t1.lampid = t2.lampid) as elog on elog.lampid = L.id
  57. where P.id = {$projectid}
  58. group by P.id ";
  59. $total = $this->db->query($sql)->row_array();
  60. $total['totalconsumption'] = empty($total['totalconsumption']) ? 0 : $total['totalconsumption'];
  61. $total['totalgeneration'] = empty($total['totalgeneration']) ? 0 : $total['totalgeneration'];
  62. $sql = "SELECT sum(elog.dayconsumption) as dayconsumption,sum(elog.daygeneration) as daygeneration FROM project as P
  63. join lampinfo as L on L.projectid = P.id
  64. join (select t1.* FROM electric_info_log as t1 join (select lampid,max(updatetime) as time FROM electric_info_log where updatetime >= '{$today}' group by lampid) as t2 on t1.updatetime = t2.time and t1.lampid = t2.lampid) as elog on elog.lampid = L.id
  65. where P.id = {$projectid}
  66. group by P.id ";
  67. $today = $this->db->query($sql)->row_array();
  68. $res['dayconsumption'] = empty($today['dayconsumption']) ? 0 : $today['dayconsumption'];
  69. $res['daygeneration'] = empty($today['daygeneration']) ? 0 : $today['daygeneration'];
  70. //当月第一天和当年第一天
  71. $beginDateMonth = date('Y-m-01 00:00:00', time());
  72. $beginDateYear = date('Y-01-01 00:00:00', time());
  73. // 当月总发/用电量
  74. $sql = "SELECT sum(elog.totalconsumption) as totalconsumption,sum(elog.totalgeneration) as totalgeneration FROM project as P
  75. join lampinfo as L on L.projectid = P.id
  76. join (select t1.* FROM electric_info_log as t1 join (select lampid,max(updatetime) as time FROM electric_info_log where updatetime <= '{$beginDateMonth}' group by lampid) as t2 on t1.updatetime = t2.time and t1.lampid = t2.lampid) as elog on elog.lampid = L.id
  77. where P.id = {$projectid}
  78. group by P.id ";
  79. $month = $this->db->query($sql)->row_array();
  80. $month['totalconsumption'] = empty($month['totalconsumption']) ? 0 : $month['totalconsumption'];
  81. $month['totalgeneration'] = empty($month['totalgeneration']) ? 0 : $month['totalgeneration'];
  82. $res['monthconsumption'] = $total['totalconsumption'] - $month['totalconsumption'];
  83. $res['monthgeneration'] = $total['totalgeneration'] - $month['totalgeneration'];
  84. // 当年总用电量
  85. $sql = "SELECT sum(elog.totalconsumption) as totalconsumption,sum(elog.totalgeneration) as totalgeneration FROM project as P
  86. join lampinfo as L on L.projectid = P.id
  87. join (select t1.* FROM electric_info_log as t1 join (select lampid,max(updatetime) as time FROM electric_info_log where updatetime <= '{$beginDateYear}' group by lampid) as t2 on t1.updatetime = t2.time and t1.lampid = t2.lampid) as elog on elog.lampid = L.id
  88. where P.id = {$projectid}
  89. group by P.id ";
  90. $year = $this->db->query($sql)->row_array();
  91. $year['totalconsumption'] = empty($year['totalconsumption']) ? 0 : $year['totalconsumption'];
  92. $year['totalgeneration'] = empty($year['totalgeneration']) ? 0 : $year['totalgeneration'];
  93. $res['yearconsumption'] = $total['totalconsumption'] - $year['totalconsumption'];
  94. $res['yeargeneration'] = $total['totalgeneration'] - $year['totalgeneration'];
  95. return $res;
  96. }
  97. // 通过路灯id获取项目信息
  98. public function get_data_by_field($field,$value){
  99. $fields = "P.projectname as project_name,
  100. P.projectid as project_no,
  101. P.createtime as create_time,
  102. z1.name as zone,
  103. z2.name as province,
  104. C.name as company,
  105. C.id as companyid,
  106. P.id,
  107. P.lampcount as install_num";
  108. $sql = "SELECT {$fields} FROM project as P
  109. left join zone as z1 on z1.id = P.zone
  110. left join zone as z2 on z2.id = P.province
  111. left join company as C on C.id = P.company
  112. left join lampinfo as L on L.projectid = P.id
  113. where {$field} = '{$value}'";
  114. $res1 = $this->db->query($sql)->row_array();
  115. // 获取项目的日用电量,月用电量,年用电量
  116. $res2 = $this->project_data($res1['id']);
  117. $data = array_merge($res1,$res2);
  118. return $data;
  119. }
  120. public function queryData($companyid = 0){
  121. if ($this->session->userdata('role') == COMPANY_ADMIN) {
  122. $companyid = $this->session->userdata('company_id');
  123. $this->db->where('company', $companyid);
  124. } else if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  125. $this->load->model('User_model');
  126. $user_project = $this->User_model->get_user_zone();
  127. if ($user_project) {
  128. $projects = explode(',', $user_project);
  129. $this->db->where_in('id',$projects);
  130. } else {
  131. $this->db->where_in('id', array(0));
  132. }
  133. } else if ($companyid > 0) {
  134. $this->db->where('company', $companyid);
  135. }
  136. $this->db->order_by("projectid","asc");
  137. $query = $this->db->get($this->table);
  138. return $query->result_array();
  139. }
  140. public function get_list_in($where_in_field, $where_in, $fields='*', $filter=array()) {
  141. $this->db->select($fields);
  142. if(!empty($where_in_field) && !empty($where_in)){
  143. $this->db->where_in($where_in_field, $where_in);
  144. }
  145. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  146. $networkNames = explode(",",$filter['keyword']);
  147. foreach ($networkNames as $name){
  148. $this->db->or_like('projectname', $name);
  149. }
  150. }
  151. $query = $this->db->get($this->table);
  152. return $query->result_array();
  153. }
  154. public function getIdsLikeName($name){
  155. $this->db->select('id');
  156. $this->db->like('projectname', $name);
  157. $query = $this->db->get($this->table);
  158. $arrProject = $query->result_array();
  159. $ids = array();
  160. foreach ($arrProject as $k=> $project) {
  161. $ids[] = $project['id'];
  162. }
  163. return $ids;
  164. }
  165. public function getIdByName($name){
  166. $this->db->select('id');
  167. $this->db->where('projectname', $name);
  168. $query = $this->db->get($this->table);
  169. $result = $query->row_array();
  170. if (isset($result['id'])) {
  171. return $result['id'];
  172. }
  173. return false;
  174. }
  175. public function get_project_ids($filter=array()){
  176. $temp = array();
  177. if (!empty($filter)){
  178. foreach ($filter as $k => $v) {
  179. $temp[] = "{$k} = {$v}";
  180. }
  181. }
  182. $where = '';
  183. if (!empty($temp)) {
  184. $where = ' where '.implode(' and ', $temp);
  185. }
  186. $sql = "select id from {$this->table} ".$where;
  187. $query = $this->db->query($sql);
  188. $arrProject = $query->result_array();
  189. $ids = array_column($arrProject, 'id');
  190. return $ids;
  191. }
  192. public function get_projectid_list(){
  193. $this->db->select('id');
  194. $this->db->select('projectid');
  195. $query = $this->db->get($this->table);
  196. $list = $query->result_array();
  197. return $list;
  198. }
  199. public function get_projectid($role,$userid,$companyid,$page,$count,$keyword){
  200. $sql = '';
  201. if ($role == SYSTEM_ADMIN) { // 系统管理员
  202. $sql = "select id as projectid from project where 1=1";
  203. }
  204. // elseif ($role == COMPANY_ADMIN) { // 公司管理员
  205. // if(!empty($companyid)){
  206. // $sql = "select id as projectid from project where company={$companyid}";
  207. // }
  208. // }
  209. else{ // 公司客户
  210. $sql = "select zone as projectid from user where id={$userid}";
  211. $zone = $this->db->query($sql)->row_array();
  212. $zone['zone'] = empty($zone['zone']) ? '0' : $zone['zone'];
  213. $sql = "select id as projectid from project where id in ({$zone['zone']})";
  214. }
  215. if (!empty($keyword)) {
  216. $sql .= " and projectname like '%{$keyword}%'";
  217. }
  218. if (!empty($sql)) {
  219. $data = $this->db->query($sql)->result_array();
  220. $idArr = array();
  221. foreach ($data as $v) {
  222. $idArr[] = $v['projectid'];
  223. }
  224. $idStr = implode(',', $idArr);
  225. $ids = explode(',', $idStr);
  226. $k = 0;
  227. $i = 0;
  228. foreach ($ids as $v) { // 分页处理
  229. $res[$k][] = $v;
  230. $i ++;
  231. if ($i >= $count) {
  232. $i = 0;
  233. $k ++;
  234. }
  235. }
  236. return array('idArr'=>$res[$page-1],'total'=>count($ids));
  237. }else{
  238. return '';
  239. }
  240. }
  241. public function getList($filter,$page = null,$limit = null){
  242. if (!empty($filter)){
  243. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  244. $projectNames = explode(",",$filter['keyword']);
  245. foreach ($projectNames as $name){
  246. $this->db->or_like('projectname', $name);
  247. }
  248. unset($filter['keyword']);
  249. }
  250. if (isset($filter['keyword'])){
  251. unset($filter['keyword']);
  252. }
  253. if (isset($filter['ids']) && !empty($filter['ids']) && is_array($filter['ids'])){
  254. $this->db->where_in('id',$filter['ids']);
  255. unset($filter['ids']);
  256. }
  257. foreach ($filter as $k => $v) {
  258. if ($k == 'id' && empty($v)){
  259. continue;
  260. }
  261. $this->db->where($k,$v);
  262. }
  263. }
  264. if ($this->session->userdata('role') == COMPANY_ADMIN) {
  265. $companyid = $this->session->userdata('company_id');
  266. $this->db->where('company', $companyid);
  267. }
  268. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  269. $this->load->model('User_model');
  270. $user_project = $this->User_model->get_user_zone();
  271. if ($user_project) {
  272. $projects = explode(',', $user_project);
  273. $this->db->where_in('id',$projects);
  274. } else {
  275. $this->db->where_in('id', array(0));
  276. }
  277. }
  278. $this->db->order_by("projectid","asc");
  279. if(is_numeric($page) && is_numeric($limit)){
  280. $this->db->limit($limit,($page-1)*$limit);
  281. }
  282. $query = $this->db->get($this->table);
  283. $arrProject = $query->result_array();
  284. $zoneids = array(0);
  285. foreach ($arrProject as $k=> $project) {
  286. $zoneids[] = $project['zone'];
  287. $zoneids[] = $project['province'];
  288. }
  289. $zoneids = array_unique($zoneids);
  290. $this->db->select('id');
  291. $this->db->select('name');
  292. $this->db->where_in('id', $zoneids);
  293. $query = $this->db->get('zone');
  294. $zoneMap = $query->result_array();
  295. foreach ($arrProject as $pk=> $project) {
  296. $arrProject[$pk]['zone_name'] = '';
  297. $arrProject[$pk]['province_name'] = '';
  298. foreach ($zoneMap as $zk => $zone) {
  299. if ($project['zone'] == $zone['id']) {
  300. $arrProject[$pk]['zone_name'] = $zone['name'];
  301. } elseif ($project['province'] == $zone['id']) {
  302. $arrProject[$pk]['province_name'] = $zone['name'];
  303. }
  304. }
  305. }
  306. return $arrProject;
  307. }
  308. public function getTotal($filter, $role, $companyid, $userid = 0){
  309. $this->db->select('count(id) as total');
  310. if (!empty($filter)){
  311. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  312. $projectNames = explode(",",$filter['keyword']);
  313. foreach ($projectNames as $name){
  314. $this->db->or_like('projectname', $name);
  315. }
  316. unset($filter['keyword']);
  317. }
  318. if (isset($filter['keyword'])){
  319. unset($filter['keyword']);
  320. }
  321. if (isset($filter['ids']) && !empty($filter['ids']) && is_array($filter['ids'])){
  322. $this->db->where_in('id',$filter['ids']);
  323. unset($filter['ids']);
  324. }
  325. foreach ($filter as $k => $v) {
  326. $this->db->where($k,$v);
  327. }
  328. }
  329. // if ($role == COMPANY_ADMIN) {
  330. // $this->db->where('company', $companyid);
  331. // }
  332. if ($role == COMPANY_CUSTOMER || $role == COMPANY_ADMIN) {
  333. $this->load->model('User_model');
  334. $user_project = $this->User_model->get_user_zone($userid);
  335. if (!empty($user_project)) {
  336. $projects = explode(',', $user_project);
  337. $this->db->where_in('id',$projects);
  338. } else {
  339. $this->db->where_in('id', array(0));
  340. }
  341. }
  342. $query = $this->db->get($this->table);
  343. $arr = $query->row_array();
  344. return $arr['total'];
  345. }
  346. public function get_company_by_id($id){
  347. $this->db->select('company');
  348. $this->db->where('id',$id);
  349. $data = $this->db->get($this->table)->row_array();
  350. return $data['company'];
  351. }
  352. public function delBatch($ids){
  353. $this->db->where_in('id',$ids);
  354. $this->db->delete($this->table);
  355. if ($this->db->affected_rows()) {
  356. return true;
  357. } else {
  358. return false;
  359. }
  360. }
  361. public function getDataCount($condition, $id = 0) {
  362. if (!empty($condition)){
  363. foreach ($condition as $k => $v) {
  364. $this->db->where($k,$v);
  365. }
  366. }
  367. if (!empty($id)) {
  368. $this->db->where('id !=',$id);
  369. }
  370. $query = $this->db->get($this->table);
  371. $data = $query->row_array();
  372. if (empty($data)) {
  373. return 0;
  374. } else {
  375. return $id == $data['id'] ? 0 : 1;
  376. }
  377. }
  378. public function getData($condition, $fields = '*') {
  379. $this->db->select($fields);
  380. if (!empty($condition)){
  381. foreach ($condition as $k => $v) {
  382. $this->db->where($k,$v);
  383. }
  384. }
  385. $query = $this->db->get($this->table);
  386. $data = $query->row_array();
  387. return $data;
  388. }
  389. public function get_list_by_company($field, $role, $companyid, $projectid){
  390. $this->db->select($field);
  391. if ($role == SYSTEM_ADMIN) {
  392. $this->db->where('company',$companyid);
  393. }else{
  394. $this->db->where('company',$companyid);
  395. if (!empty($projectid)) {
  396. $idArr = explode(',', $projectid);
  397. }else{
  398. $idArr = array(0);
  399. }
  400. $this->db->where_in('id',$idArr);
  401. }
  402. return $this->db->get($this->table)->result_array();
  403. }
  404. public function getMultiData($filter, $fields = '*', $role = 0, $companyid = 0, $userid = 0) {
  405. $this->db->select($fields);
  406. if (!empty($filter)){
  407. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  408. $projectNames = explode(",",$filter['keyword']);
  409. foreach ($projectNames as $name){
  410. $this->db->or_like('P.projectname', $name);
  411. }
  412. unset($filter['keyword']);
  413. }
  414. if (isset($filter['keyword'])){
  415. unset($filter['keyword']);
  416. }
  417. if(!empty($filter['page']) && !empty($filter['count'])){
  418. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  419. unset($filter['page']);
  420. unset($filter['count']);
  421. }
  422. foreach ($filter as $k => $v) {
  423. $this->db->where($k,$v);
  424. }
  425. }
  426. // if ($role == COMPANY_ADMIN) {
  427. // $this->db->where('company', $companyid);
  428. // } else
  429. if ($role == COMPANY_CUSTOMER || $role == COMPANY_ADMIN) {
  430. $this->load->model('User_model');
  431. $user_project = $this->User_model->get_user_zone($userid);
  432. if (!empty($user_project)) {
  433. $projects = explode(',', $user_project);
  434. $this->db->where_in('P.id',$projects);
  435. } else {
  436. $this->db->where_in('P.id', array(0));
  437. }
  438. }
  439. $this->db->join('zone as Z','on Z.id = P.zone','left');
  440. $this->db->join('zone as S','on S.id = P.province','left');
  441. $query = $this->db->get($this->table.' as P');
  442. $data = $query->result_array();
  443. return $data;
  444. }
  445. // 添加路灯数
  446. public function add_lamp_count($data){
  447. $sql = "update `project` set `lampcount` = `lampcount` + 1, `faultcount` = `faultcount` + {$data['faultcount']} WHERE `id`={$data['projectid']}";
  448. $this->db->query($sql);
  449. if ($this->db->affected_rows() == 1) {
  450. return true;
  451. } else {
  452. return false;
  453. }
  454. }
  455. // 减少路灯数
  456. public function minus_lamp_count($data){
  457. $sql = "update `project` set `lampcount` = `lampcount` - 1, `faultcount` = `faultcount` - {$data['faultcount']} WHERE `id`={$data['projectid']} and `lampcount` >= 1 and `faultcount` >= {$data['faultcount']}";
  458. $this->db->query($sql);
  459. if ($this->db->affected_rows() == 1) {
  460. return true;
  461. } else {
  462. return false;
  463. }
  464. }
  465. // 添加网络数
  466. public function add_network_count($data){
  467. $sql = "update `project` set `networkcount` = `networkcount` + 1, `lampcount` = `lampcount` + {$data['lampcount']}, `faultcount` = `faultcount` + {$data['faultcount']} WHERE `id`={$data['projectid']}";
  468. $this->db->query($sql);
  469. if ($this->db->affected_rows() == 1) {
  470. return true;
  471. } else {
  472. return false;
  473. }
  474. }
  475. // 减少网络数
  476. public function minus_network_count($data){
  477. $sql = "update `project` set `networkcount` = `networkcount` - 1, `lampcount` = `lampcount` - {$data['lampcount']}, `faultcount` = `faultcount` - {$data['faultcount']} WHERE `id`={$data['projectid']} and `networkcount` >= 1 and `lampcount` >= {$data['lampcount']} and `faultcount` >= {$data['faultcount']}";
  478. $this->db->query($sql);
  479. if ($this->db->affected_rows() == 1) {
  480. return true;
  481. } else {
  482. return false;
  483. }
  484. }
  485. public function getIdByLikeName($name){
  486. $sql = "SELECT id FROM project WHERE projectname LIKE '%".$name."%'";
  487. $query = $this->db->query($sql);
  488. $arr = $query->row_array();
  489. if (!empty($arr)){
  490. return $arr['id'];
  491. }
  492. return false;
  493. }
  494. // 添加监控数
  495. public function add_monitor_count($data){
  496. $sql = "update `project` set `monitorcount` = `monitorcount` + {$data['count']} WHERE `id`={$data['projectid']}";
  497. $this->db->query($sql);
  498. if ($this->db->affected_rows() == 1) {
  499. return true;
  500. } else {
  501. return false;
  502. }
  503. }
  504. // 减少监控数
  505. public function minus_monitor_count($data){
  506. $sql = "update `project` set `monitorcount` = `monitorcount` - {$data['count']} WHERE `id`={$data['projectid']} and `monitorcount` >= {$data['count']}";
  507. $this->db->query($sql);
  508. if ($this->db->affected_rows() == 1) {
  509. return true;
  510. } else {
  511. return false;
  512. }
  513. }
  514. public function getNameById($id, $field = 'projectname'){
  515. $this->db->where('id',$id);
  516. $query = $this->db->get($this->table);
  517. $arr = $query->row_array();
  518. return !empty($arr) ? $arr[$field] : 0;
  519. }
  520. // 减少路灯故障数
  521. public function minus_fault_count($id){
  522. $sql = "update `project` set `faultcount` = `faultcount` - 1 WHERE `id`= {$id} and `faultcount` >= 1";
  523. $this->db->query($sql);
  524. if ($this->db->affected_rows() == 1) {
  525. return true;
  526. } else {
  527. return false;
  528. }
  529. }
  530. public function delData($condition) {
  531. if (!empty($condition)){
  532. foreach ($condition as $k => $v) {
  533. if (is_array($v)) {
  534. if (!empty($v)) {
  535. $this->db->where_in($k,$v);
  536. }else{
  537. $this->db->where_in($k,array(0));
  538. }
  539. }else{
  540. $this->db->where($k,$v);
  541. }
  542. }
  543. }else{
  544. return false;
  545. }
  546. $this->db->delete($this->table);
  547. if ($this->db->affected_rows() > 0) {
  548. return true;
  549. } else {
  550. return false;
  551. }
  552. }
  553. }
  554. ?>