Project_model.php 20 KB

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