Lamp_model.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Lamp_model extends Base_model {
  5. protected $table = 'lampinfo';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function queryData($where, $fields='*', $table='lampinfo', $order_by='updatetime desc') {
  10. $this->db->select($fields)->from($table)->where($where)->order_by($order_by)->limit(1);
  11. $query = $this->db->get();
  12. return $query->row_array();
  13. }
  14. public function get_list_not_in($where_in_field, $where_in,$fields='*',$filter=array()){
  15. if(!empty($where_in_field) && !empty($where_in)){
  16. $this->db->where_not_in($where_in_field, $where_in);
  17. }
  18. return $this->get_list_in(null,null,$fields,$filter);
  19. }
  20. // 项目管理页路灯详情数据
  21. public function get_list_in($where_in_field, $where_in, $fields='*', $filter=array(), $lite = false) {
  22. $this->db->select($fields);
  23. if(!empty($where_in_field) && !empty($where_in)){
  24. if (is_array($where_in)) {
  25. $this->db->where_in($where_in_field, $where_in);
  26. }else{
  27. $this->db->where($where_in_field, $where_in);
  28. }
  29. }
  30. if(isset($filter['networkid']) && !empty($filter['networkid'])){
  31. $this->db->where('L.networkid', $filter['networkid']);
  32. }
  33. if(isset($filter['offline']) && !empty($filter['offline'])){
  34. $this->db->where('L.netstatus', 0);
  35. }
  36. if(isset($filter['online']) && !empty($filter['online'])){
  37. $this->db->where('L.netstatus', 1);
  38. }
  39. if(isset($filter['section']) && !empty($filter['section'])){
  40. $this->db->where('L.section', $filter['section']);
  41. }
  42. if(isset($filter['lampid']) && !empty($filter['lampid'])){
  43. $this->db->where('L.id', $filter['lampid']);
  44. }
  45. // if(isset($filter['status']) && is_numeric($filter['status'])){
  46. // $this->db->where('L.isfaulted', $filter['status']);
  47. // $this->db->where('AI.stralarmtype !=', '');
  48. // $this->db->where('AI.status', 0);
  49. // }
  50. if(isset($filter['lightStatus']) && is_numeric($filter['lightStatus'])){
  51. $this->db->where('L.status', $filter['lightStatus']);
  52. }
  53. if(isset($filter['controllerstatus']) && is_numeric($filter['controllerstatus'])){
  54. $this->db->group_start();
  55. $this->db->where('L.controllerstatus', $filter['controllerstatus']);
  56. $this->db->or_where('L.netstatus', 0);
  57. $this->db->group_end();
  58. }
  59. // 关键字搜索
  60. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  61. // $this->db->like('L.number', $filter['keyword']);
  62. $this->db->group_start();
  63. $this->db->or_like('L.number', $filter['keyword']);
  64. $this->db->or_like('L.address', $filter['keyword']);
  65. $this->db->or_like('L.section', $filter['keyword']);
  66. $this->db->group_end();
  67. }
  68. if(isset($filter['projectid']) && !empty($filter['projectid'])){
  69. $this->db->where('L.projectid', $filter['projectid']);
  70. }
  71. if (isset($filter['lng_low']) && is_numeric($filter['lng_low']) && isset($filter['lng_high']) && is_numeric($filter['lng_high']) && $filter['lng_low'] > $filter['lng_high']) {
  72. $this->db->group_start();
  73. $this->db->group_start();
  74. $this->db->where('L.longitude >=', $filter['lng_low']);
  75. $this->db->where('L.longitude <=', 180);
  76. $this->db->group_end();
  77. $this->db->or_group_start();
  78. $this->db->where('L.longitude <=', $filter['lng_high']);
  79. $this->db->where('L.longitude >=', -180);
  80. $this->db->group_end();
  81. $this->db->group_end();
  82. }else{
  83. if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
  84. $this->db->where('L.longitude >=', $filter['lng_low']);
  85. }
  86. if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
  87. $this->db->where('L.longitude <=', $filter['lng_high']);
  88. }
  89. }
  90. if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
  91. $this->db->where('L.latitude >=', $filter['lat_low']);
  92. }
  93. if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
  94. $this->db->where('L.latitude <=', $filter['lat_high']);
  95. }
  96. $this->db->from($this->table.' as L');
  97. if ($lite != 1) {
  98. // $fieldArr = explode(',',$fields);
  99. // $temp = array();
  100. // foreach ($fieldArr as $value) {
  101. // $a = explode('.',$value)[0];
  102. // if (!in_array($a, $temp)) {
  103. // $temp[] = $a;
  104. // }
  105. // }
  106. // $this->db->join('project as P', 'L.projectid = P.id', 'left');
  107. // $this->db->join('network as N', 'L.networkid = N.id', 'left');
  108. // $this->db->join('(select t1.* from lamp_info_log t1, (select lampid, max(updatetime) as maxtime from lamp_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as LI', 'L.id = LI.lampid', 'left');
  109. // $this->db->join('(select t1.* from battery_info_log t1, (select lampid, max(updatetime) as maxtime from battery_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as BI', 'L.id = BI.lampid', 'left');
  110. // $this->db->join('(select t1.* from history_info_log t1, (select lampid, max(updatetime) as maxtime from history_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as HI', 'L.id = HI.lampid', 'left');
  111. // $this->db->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', 'L.id = AI.lampid', 'left');
  112. // $this->db->join('(select t1.* from solar_info_log t1, (select lampid, max(updatetime) as maxtime from solar_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as SI', 'L.id = SI.lampid', 'left');
  113. // $this->db->join('(select t1.* from electric_info_log t1, (select lampid, max(updatetime) as maxtime from electric_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as EI', 'L.id = EI.lampid', 'left');
  114. // $this->db->join('(select t1.* from system_info_log t1, (select lampid, max(updatetime) as maxtime from system_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as SIL', 'L.id = SIL.lampid', 'left');
  115. $this->db->join('project as P', 'L.projectid = P.id', 'left');
  116. // $this->db->join('network as N', 'L.networkid = N.id', 'left');
  117. // $this->db->join('msisdn as M', 'N.simid = M.imsi', 'left');
  118. // $this->db->join('(select lampid,max(updatetime) as time from history_info_log group by lampid) as T', 'L.id = T.lampid', 'left');
  119. // $this->db->join('lamp_info_log as LI', 'L.id = LI.lampid AND LI.lampid=T.lampid AND LI.updatetime = T.time', 'left');
  120. // $this->db->join('battery_info_log as BI', 'L.id = BI.lampid AND BI.lampid=T.lampid AND BI.updatetime = T.time', 'left');
  121. // $this->db->join('history_info_log as HI', 'L.id = HI.lampid AND HI.lampid=T.lampid AND HI.updatetime = T.time', 'left');
  122. // $this->db->join('alarm_info_log as AI', 'L.id = AI.lampid AND AI.lampid=T.lampid AND AI.updatetime = T.time', 'left');
  123. $this->db->join('(select t1.* from warning_info_log t1, (select lampid, max(updatetime) as maxtime from warning_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'L.id = AI.lampid', 'left');
  124. // $this->db->join('solar_info_log as SI', 'L.id = SI.lampid AND SI.lampid=T.lampid AND SI.updatetime = T.time', 'left');
  125. // $this->db->join('electric_info_log as EI', 'L.id = EI.lampid AND EI.lampid=T.lampid AND EI.updatetime = T.time', 'left');
  126. // $this->db->join('system_info_log as SIL', 'L.id = SIL.lampid AND SIL.lampid=T.lampid AND SIL.updatetime = T.time', 'left');
  127. $this->db->order_by("L.number ASC");
  128. }else{
  129. $this->db->order_by("L.number ASC");
  130. }
  131. if(!empty($filter['page']) && !empty($filter['count'])){
  132. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  133. }
  134. $query = $this->db->get();
  135. // echo $this->db->last_query();die;
  136. return $query->result_array();
  137. }
  138. public function report_get_list_in($where_in_field, $where_in, $fields='*', $filter=array()) {
  139. $this->db->select($fields);
  140. if(!empty($where_in_field) && !empty($where_in)){
  141. if (is_array($where_in)) {
  142. $this->db->where_in($where_in_field, $where_in);
  143. }else{
  144. $this->db->where($where_in_field, $where_in);
  145. }
  146. }
  147. // if(isset($filter['networkid']) && !empty($filter['networkid'])){
  148. // $this->db->where('L.networkid', $filter['networkid']);
  149. // }
  150. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  151. $this->db->like('L.number', $filter['keyword']);
  152. }
  153. if(isset($filter['projectid']) && !empty($filter['projectid'])){
  154. $this->db->where('L.projectid', $filter['projectid']);
  155. }
  156. if(isset($filter['section']) && !empty($filter['section'])){
  157. $this->db->where('L.section', $filter['section']);
  158. }
  159. $this->db->from($this->table.' as L');
  160. $this->db->join('project as P', 'L.projectid = P.id', 'left');
  161. // $this->db->join('network as N', 'L.networkid = N.id', 'left');
  162. // $this->db->join('(select lampid,max(updatetime) as updatetime from lamp_info_log group by lampid) as LI', 'L.id = LI.lampid', 'left');
  163. $this->db->order_by("L.number ASC");
  164. if(!empty($filter['page']) && !empty($filter['count'])){
  165. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  166. }
  167. $query = $this->db->get();
  168. return $query->result_array();
  169. }
  170. // 获取项目下路灯列表
  171. public function get_list_by_project($projectid,$fields = '*'){
  172. if (empty($projectid)) {
  173. return array();
  174. }
  175. $sql = "SELECT {$fields} from {$this->table} as L
  176. left join network as N on L.networkid = N.id
  177. where L.projectid = {$projectid}";
  178. return $this->db->query($sql)->result_array();
  179. }
  180. // 项目管理页路灯数量
  181. public function get_total($where_in_field, $where_in, $fields='*', $filter=array()) {
  182. // $this->db->select($fields);
  183. $this->db->select('count(*) as total');
  184. if(!empty($where_in_field) && !empty($where_in)){
  185. if (is_array($where_in)) {
  186. $this->db->where_in($where_in_field, $where_in);
  187. }else{
  188. $this->db->where($where_in_field, $where_in);
  189. }
  190. }
  191. if(isset($filter['networkid']) && !empty($filter['networkid'])){
  192. $this->db->where('L.networkid', $filter['networkid']);
  193. }
  194. // if(isset($filter['alarmtype']) && !empty($filter['alarmtype'])){
  195. // $this->db->where('AI.alarmtype', $filter['alarmtype']);
  196. // }
  197. // if(isset($filter['status']) && is_numeric($filter['status'])){
  198. // $this->db->where('L.isfaulted', $filter['status']);
  199. // $this->db->where('AI.stralarmtype !=', '');
  200. // $this->db->where('AI.status', 0);
  201. // }
  202. if(isset($filter['section']) && !empty($filter['section'])){
  203. $this->db->where('L.section', $filter['section']);
  204. }
  205. if(isset($filter['offline']) && !empty($filter['offline'])){
  206. $this->db->where('L.netstatus', 0);
  207. }
  208. if(isset($filter['online']) && !empty($filter['online'])){
  209. $this->db->where('L.netstatus', 1);
  210. }
  211. if(isset($filter['controllerstatus']) && !empty($filter['controllerstatus'])){
  212. $this->db->group_start();
  213. $this->db->where('L.controllerstatus', 5);
  214. $this->db->or_where('L.netstatus', 0);
  215. $this->db->group_end();
  216. }
  217. if(isset($filter['lightStatus']) && is_numeric($filter['lightStatus'])){
  218. $this->db->group_start();
  219. $this->db->where('L.lighteness >', 0);
  220. $this->db->or_where('L.netstatus', 1);
  221. $this->db->group_end();
  222. // $this->db->where('L.status', $filter['lightStatus']);
  223. }
  224. // 关键字搜索
  225. if (uri_string() == 'report/home') { // 数据报表页
  226. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  227. // $this->db->group_start();
  228. $this->db->like('L.number', $filter['keyword']);
  229. // $this->db->group_end();
  230. }
  231. }else{ // 项目管理页
  232. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  233. $this->db->group_start();
  234. $this->db->like('L.number', $filter['keyword']);
  235. // $this->db->or_like('BI.updatetime', $filter['keyword']);
  236. $this->db->group_end();
  237. }
  238. }
  239. if(isset($filter['projectid']) && !empty($filter['projectid'])){
  240. $this->db->where('L.projectid', $filter['projectid']);
  241. }
  242. if (isset($filter['lng_low']) && is_numeric($filter['lng_low']) && isset($filter['lng_high']) && is_numeric($filter['lng_high']) && $filter['lng_low'] > $filter['lng_high']) {
  243. $this->db->group_start();
  244. $this->db->group_start();
  245. $this->db->where('L.longitude >=', $filter['lng_low']);
  246. $this->db->where('L.longitude <=', 180);
  247. $this->db->group_end();
  248. $this->db->or_group_start();
  249. $this->db->where('L.longitude <=', $filter['lng_high']);
  250. $this->db->where('L.longitude >=', -180);
  251. $this->db->group_end();
  252. $this->db->group_end();
  253. }else{
  254. if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
  255. $this->db->where('L.longitude >=', $filter['lng_low']);
  256. }
  257. if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
  258. $this->db->where('L.longitude <=', $filter['lng_high']);
  259. }
  260. }
  261. if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
  262. $this->db->where('L.latitude >=', $filter['lat_low']);
  263. }
  264. if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
  265. $this->db->where('L.latitude <=', $filter['lat_high']);
  266. }
  267. $this->db->from($this->table.' as L');
  268. // $this->db->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', 'L.id = AI.lampid', 'left');
  269. // $this->db->join('network as N', 'N.id = L.networkid', 'left');
  270. if(!empty($filter['page']) && !empty($filter['count'])){
  271. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  272. }
  273. // $this->db->group_by('L.id');
  274. // $this->db->order_by("L.number","ASC");
  275. $query = $this->db->get()->row_array();
  276. return $query['total'];
  277. }
  278. public function get_projectid_by_role($role,$userid,$companyid){
  279. if ($role == SYSTEM_ADMIN) {
  280. $list = $this->db->query('select id from project')->result_array();
  281. }else {
  282. $list = $this->db->query('select id from project where company = '.$companyid)->result_array();
  283. }
  284. return array_column($list, 'id');
  285. }
  286. // gis地图页路灯列表
  287. public function get_list_by_role($role, $companyid, $projectid = 0, $userid = 0,$fields='*',$filter=array(),$type = 0,$is_map = 0){
  288. $this->db->select($fields, false);
  289. $projectIds = array();
  290. if ($projectid > 0) {
  291. $projectIds[] = $projectid;
  292. $this->db->where_in('L.projectid',$projectIds);
  293. } elseif ($role != SYSTEM_ADMIN) {
  294. $idStr = $this->get_projectid_by_role($role,$userid,$companyid);
  295. if (!empty($idStr)) {
  296. $projectIds = explode(',', $idStr);
  297. }else{
  298. $projectIds = array(0);
  299. }
  300. $this->db->where_in('L.projectid',$projectIds);
  301. }
  302. // 筛选带有监控的路灯
  303. // if (isset($filter['monitor']) && !empty($filter['monitor'])) {
  304. // $this->db->group_start();
  305. // $this->db->or_where('V.id >','0');
  306. // $this->db->group_end();
  307. // unset($filter['monitor']);
  308. // $this->db->join('video_monitor as V', 'L.id = V.lampid', 'left');
  309. // }
  310. // 筛选带有监控的路灯
  311. if (isset($filter['keyword']) && !empty($filter['keyword'])) {
  312. $this->db->like('L.number',$filter['keyword']);
  313. unset($filter['keyword']);
  314. }
  315. // 筛选故障设备
  316. if (isset($filter['L.controllerstatus']) && !empty($filter['L.controllerstatus'])) {
  317. $this->db->like('L.controllerstatus',$filter['keyword']);
  318. $this->db->group_start();
  319. $this->db->where('L.controllerstatus', $filter['L.controllerstatus']);
  320. $this->db->or_where('L.netstatus', 0);
  321. $this->db->group_end();
  322. unset($filter['L.controllerstatus']);
  323. }
  324. // 经纬度筛选
  325. if (isset($filter['lng_low']) && is_numeric($filter['lng_low']) && isset($filter['lng_high']) && is_numeric($filter['lng_high']) && $filter['lng_low'] > $filter['lng_high']) {
  326. $this->db->group_start();
  327. $this->db->group_start();
  328. $this->db->where('L.longitude >=', $filter['lng_low']);
  329. $this->db->where('L.longitude <=', 180);
  330. $this->db->group_end();
  331. $this->db->or_group_start();
  332. $this->db->where('L.longitude <=', $filter['lng_high']);
  333. $this->db->where('L.longitude >=', -180);
  334. $this->db->group_end();
  335. $this->db->group_end();
  336. unset($filter['lng_high']);
  337. unset($filter['lng_low']);
  338. }else{
  339. if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
  340. $this->db->where('L.longitude >=', $filter['lng_low']);
  341. unset($filter['lng_low']);
  342. }
  343. if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
  344. $this->db->where('L.longitude <=', $filter['lng_high']);
  345. unset($filter['lng_high']);
  346. }
  347. }
  348. if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
  349. $this->db->where('L.latitude >=', $filter['lat_low']);
  350. unset($filter['lat_low']);
  351. }
  352. if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
  353. $this->db->where('L.latitude <=', $filter['lat_high']);
  354. unset($filter['lat_high']);
  355. }
  356. if(isset($filter['section']) && !empty($filter['section'])){
  357. $this->db->where('L.section', $filter['section']);
  358. unset($filter['section']);
  359. }
  360. if(empty($type) && !empty($filter['page']) && !empty($filter['count'])){
  361. $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  362. unset($filter['page']);
  363. unset($filter['count']);
  364. }
  365. // 根据路灯状态来筛选
  366. foreach ($filter as $key => $value) {
  367. $this->db->where($key,$value);
  368. }
  369. $this->db->from($this->table.' as L');
  370. // $this->db->join('project as P', 'L.projectid = P.id', 'left');
  371. // $this->db->join('network as N', 'L.networkid = N.id', 'left');
  372. // $this->db->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', 'L.id = AI.lampid', 'left');
  373. $this->db->group_by('L.id');
  374. if ($is_map) {
  375. $this->db->order_by("L.latitude DESC");
  376. }else{
  377. $this->db->order_by("L.number ASC,L.id DESC");
  378. }
  379. $query = $this->db->get();
  380. if (empty($type)) {
  381. return $query->result_array();
  382. }else{
  383. return $query->num_rows();
  384. }
  385. }
  386. public function getTotalLampInfo($role, $companyid, $projectid = 0, $userid = 0,$type = 0){
  387. if ($projectid > 0) {
  388. $condition = "id = {$projectid}";
  389. } elseif ($role == SYSTEM_ADMIN) {
  390. $condition = "1=1";
  391. } else {
  392. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  393. if (empty($projectIds)) {
  394. $condition = "id in (0)";
  395. }else{
  396. $condition = "id in ({$projectIds})";
  397. }
  398. }
  399. $toDate = date('Y-m-d H:i:s',time());
  400. $year = date('Y',time())-1;
  401. $fromDate = date("{$year}-m-d H:i:s",time());
  402. // $sql = "SELECT COUNT(*) AS num, SUM(boardpower) AS install, SUM(epower) AS power from
  403. // (SELECT
  404. // L.boardpower as boardpower,
  405. // L.totalgeneration AS epower
  406. // FROM lampinfo AS L
  407. // WHERE {$condition} GROUP BY L.id) Temp";
  408. // if (empty($type)) {
  409. // $sql = "SELECT SUM(lampcount) AS num, SUM(totalinstall) AS install, SUM(totalgeneration) AS power,SUM(totalconsumption) as totalconsumption,avg(online) as online,avg(light) as light
  410. // FROM project
  411. // WHERE {$condition} ";
  412. // }else{
  413. if ($projectid > 0) {
  414. $condition = "projectid = {$projectid}";
  415. } elseif ($role == SYSTEM_ADMIN) {
  416. $condition = "1=1";
  417. } else {
  418. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  419. if (empty($projectIds)) {
  420. $condition = "projectid in (0)";
  421. }else{
  422. $condition = "projectid in ({$projectIds})";
  423. }
  424. }
  425. // var_dump($condition);
  426. $sql = "SELECT COUNT(*) AS num, SUM(boardpower) AS install, SUM(totalgeneration) AS power,SUM(totalconsumption) as totalconsumption FROM lampinfo WHERE {$condition}";
  427. // }
  428. $query = $this->db->query($sql);
  429. $arrRet = $query->row_array();
  430. // var_dump($arrRet);die;
  431. return $arrRet;
  432. }
  433. // 获取项目下面的故障数
  434. public function get_fault_count($filter, $role, $companyid, $userid,$projectIds=0){
  435. $Ids = empty($projectIds) ? 0 : $projectIds;
  436. $temp = array();
  437. $temp[] = "L.projectid in ({$Ids})";
  438. if (empty($filter['L.projectid'])) {
  439. unset($filter['L.projectid']);
  440. }
  441. foreach ($filter as $key => $value) {
  442. if (is_array($value)) {
  443. if (!empty($value)) {
  444. $temp[] = "{$key} in ({implode(',', $value)})";
  445. }else{
  446. $temp[] = "{$key} in (0)";
  447. }
  448. }else{
  449. $temp[] = "{$key}={$value}";
  450. }
  451. }
  452. $temp[] = 'AI.status = 0';
  453. $temp[] = 'AI.stralarmtype != ""';
  454. $where = '';
  455. if (!empty($temp)) {
  456. $where = 'where '.implode(' and ', $temp);
  457. }
  458. $sql = "select count(*) as total from (select L.id from lampinfo as L LEFT JOIN (select lampid,max(updatetime) as time from alarm_info_log group by lampid) as T on T.lampid = L.id left join alarm_info_log AS AI on AI.lampid = T.lampid and AI.updatetime = T.time {$where} group by L.id) as t";
  459. // $sql = "select count(*) as total from lampinfo as L {$where}";
  460. // echo $sql;
  461. $data = $this->db->query($sql)->row_array();
  462. return $data['total'];
  463. }
  464. public function getTotal($filter, $role, $companyid, $userid = 0){
  465. if(isset($filter['network'])){
  466. $this->db->select('count(id) as total');
  467. $this->db->where_in('networkid',array(intval($filter['network'])));
  468. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  469. $lampIds = explode(",",$filter['keyword']);
  470. $arrIds = array();
  471. foreach ($lampIds as $id){
  472. if(is_numeric($id)){
  473. $arrIds[] = intval(trim($id));
  474. }
  475. }
  476. if (!empty($arrIds)){
  477. $this->db->where_in('number', $arrIds);
  478. }
  479. }
  480. if(isset($filter['status']) && $filter['status'] == 1){
  481. $this->db->where('isfaulted',1);
  482. }
  483. $query = $this->db->get($this->table);
  484. $arr = $query->row_array();
  485. return $arr['total'];
  486. }
  487. if (isset($filter['ids']) && is_array($filter['ids'])) {
  488. $this->db->select('count(id) as total');
  489. $this->db->where_in('id', $filter['ids']);
  490. $query = $this->db->get($this->table);
  491. $arr = $query->row_array();
  492. return $arr['total'];
  493. }
  494. if (!empty($filter)){
  495. foreach ($filter as $k => $v) {
  496. if($k == 'status' || $k == 'keyword' || $k == 'project_name_like' || $k == 'network_name_like'){
  497. continue;
  498. }
  499. $this->db->where($k,$v);
  500. }
  501. }
  502. if (empty($filter['projectid'])) {
  503. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  504. $Ids = empty($projectIds) ? array(0) : explode(',', $projectIds);
  505. $this->db->where_in('projectid',$Ids);
  506. }
  507. $this->db->select('count(id) as total');
  508. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  509. $lampIds = explode(",",$filter['keyword']);
  510. $arrIds = array();
  511. foreach ($lampIds as $id){
  512. if(is_numeric($id)){
  513. $arrIds[] = intval(trim($id));
  514. }
  515. }
  516. if (!empty($arrIds)){
  517. $this->db->where_in('number', $arrIds);
  518. }
  519. }
  520. if(isset($filter['status']) && $filter['status'] == 1){
  521. $this->db->where('isfaulted',1);
  522. }
  523. $query = $this->db->get($this->table);
  524. $arr = $query->row_array();
  525. return $arr['total'];
  526. }
  527. public function get_light_count_by_project($projectid){
  528. $this->db->select('count(id) as total');
  529. $this->db->where('projectid',$projectid);
  530. $this->db->where('status',1);
  531. $query = $this->db->get($this->table);
  532. $arr = $query->row_array();
  533. return $arr['total'];
  534. }
  535. public function get_online_count_by_project($projectid){
  536. $sql = "select count(*) as total from lampinfo as L left join network as N on N.id = L.networkid where N.status = 1 AND L.projectid = ".$projectid;
  537. $data = $this->db->query($sql)->row_array();
  538. return $data['total'];
  539. }
  540. public function get_lamp_count_by_project($projectid, $role, $companyid, $userid = 0){
  541. $this->db->select('projectid,count(*) as total');
  542. if (empty($projectid)) {
  543. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  544. if (empty($projectIds)) {
  545. $this->db->where_in('projectid',array(0));
  546. }else{
  547. $Ids = explode(',', $projectIds);
  548. $this->db->where_in('projectid',$Ids);
  549. }
  550. }else{
  551. if (is_array($projectid)) {
  552. $this->db->where_in('projectid',$projectid);
  553. }else{
  554. $this->db->where('projectid',$projectid);
  555. }
  556. }
  557. return $this->db->group_by('projectid')->get($this->table)->result_array();
  558. }
  559. // 获取单个路灯数据
  560. public function getData($condition, $fields = '*') {
  561. $this->db->select($fields);
  562. if (!empty($condition)){
  563. foreach ($condition as $k => $v) {
  564. if (is_array($v)) {
  565. if (empty($v)) {
  566. $this->db->where_in($k,$v);
  567. }else{
  568. $this->db->where_in($k,array(0));
  569. }
  570. }else{
  571. $this->db->where($k,$v);
  572. }
  573. }
  574. }
  575. $query = $this->db->get($this->table);
  576. $data = $query->row_array();
  577. return $data;
  578. }
  579. // 获取路灯详情
  580. public function getOne($id,$field="*"){
  581. // $sql = "SELECT {$field}, L.id as id
  582. // FROM ( select * from lampinfo where id = {$id} limit 1 ) AS L
  583. // LEFT JOIN network as N on N.id = L.networkid
  584. // LEFT JOIN ( select * from lamp_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS log ON L.id = log.lampid
  585. // LEFT JOIN ( select * from system_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS sylog ON L.id = sylog.lampid
  586. // LEFT JOIN ( select * from solar_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS solog ON L.id = solog.lampid
  587. // LEFT JOIN ( select * from battery_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS blog ON L.id = blog.lampid
  588. // LEFT JOIN ( select * from electric_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS elog ON L.id = elog.lampid
  589. // LEFT JOIN ( select * from history_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS hlog ON L.id = hlog.lampid
  590. // LEFT JOIN ( select * from alarm_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS alog ON L.id = alog.lampid
  591. // LEFT JOIN ( select * from battery_cmd where id = {$id} order by updatetime desc limit 1 ) AS BC ON L.id = BC.id
  592. // WHERE L.id = {$id} limit 1";
  593. $sql = "SELECT {$field}, L.id as id
  594. FROM ( select * from lampinfo where id = {$id} limit 1 ) AS L
  595. -- LEFT JOIN ( select * from battery_cmd where id = {$id} order by updatetime desc limit 1 ) AS BC ON L.id = BC.id
  596. WHERE L.id = {$id} limit 1";
  597. $query = $this->db->query($sql);
  598. return $query->row_array();
  599. }
  600. public function getOneLastInfo($filter){
  601. if (!empty($filter)){
  602. foreach ($filter as $k => $v) {
  603. $this->db->where($k,$v);
  604. }
  605. }
  606. $this->db->order_by("createtime","DESC");
  607. $this->db->limit('1');
  608. $query = $this->db->get($this->table);
  609. return $query->row_array();
  610. }
  611. public function getOneForInfoLog($lampid, $table){
  612. $this->db->where('lampid',$lampid);
  613. $this->db->order_by("updatetime","DESC");
  614. $this->db->limit('1');
  615. $query = $this->db->get($table);
  616. $ret = $query->result_array();
  617. $info = ($ret)? $ret[0] : '';
  618. return $info;
  619. }
  620. // 获取历史信息的数量
  621. public function getCountForInfoLog($lampid, $table,$beginDate = '',$endData = ''){
  622. $this->db->select('count(*) as total');
  623. $this->db->where('lampid',$lampid);
  624. if (!empty($beginDate) && !empty($endData)) {
  625. $this->db->where('updatetime >=',$beginDate);
  626. $this->db->where('updatetime <=',$endData);
  627. }
  628. $query = $this->db->get($table);
  629. $ret = $query->row_array();
  630. // var_dump($this->db->last_query());
  631. return $ret['total'];
  632. }
  633. // 获取历史信息列表
  634. public function getListForInfoLog($lampid, $table, $offset = null, $limit = null,$beginDate = '',$endData = ''){
  635. if ($table == 'lamp_info_log') {
  636. $this->db->select('lamp_info_log.*,BI.daydischarmaxpow,BI.daychargemincurrent,L.number');
  637. $this->db->join('battery_info_log as BI','lamp_info_log.lampid = BI.lampid AND lamp_info_log.updatetime = BI.updatetime','left');
  638. $this->db->join('lampinfo as L','lamp_info_log.lampid = L.id','left');
  639. }elseif ($table == 'solar_info_log') {
  640. $this->db->select('solar_info_log.*,BI.daychargemaxpow,BI.daydischargemincurrent');
  641. $this->db->join('battery_info_log as BI','solar_info_log.lampid = BI.lampid AND solar_info_log.updatetime = BI.updatetime','left');
  642. }elseif($table == 'system_info_log'){
  643. $this->db->select('system_info_log.*,L.number');
  644. $this->db->join('lampinfo as L','system_info_log.lampid = L.id','left');
  645. }elseif($table == 'battery_info_log'){
  646. $this->db->select('battery_info_log.*,HI.overtimes,system_info_log.sysvoltage,system_info_log.syscurrent,system_info_log.temper');
  647. $this->db->join('history_info_log as HI','battery_info_log.lampid = HI.lampid AND battery_info_log.updatetime = HI.updatetime','left');
  648. $this->db->join('system_info_log','system_info_log.lampid = battery_info_log.lampid AND system_info_log.updatetime = battery_info_log.updatetime','left');
  649. }
  650. $this->db->where($table.'.lampid',$lampid);
  651. if (!empty($beginDate) && !empty($endData)) {
  652. $this->db->where($table.'.updatetime >=',$beginDate);
  653. $this->db->where($table.'.updatetime <=',$endData);
  654. }
  655. $this->db->order_by("updatetime","DESC");
  656. if ($limit !== null && $offset !== null) {
  657. $this->db->limit($limit, $offset);
  658. }
  659. $query = $this->db->get($table);
  660. $ret = $query->result_array();
  661. return $ret;
  662. }
  663. // 获取最新历史信息
  664. public function getForInfoLog($lampid, $table){
  665. if ($table == 'lamp_info_log') {
  666. $this->db->where('id',$lampid);
  667. // $this->db->order_by("LI.updatetime","DESC");
  668. $this->db->limit(1);
  669. $this->db->select('lighteness,status as lampstatus,lampvoltage,lampcurrent,lamppower,temper,updatetime');
  670. // $this->db->join('lampinfo as L','lamp_info_log.lampid = L.id','left');
  671. $query = $this->db->get('lampinfo');
  672. $ret = $query->row_array();
  673. return $ret;
  674. }else{
  675. $this->db->where('lampid',$lampid);
  676. $this->db->order_by("updatetime","DESC");
  677. $this->db->limit(1);
  678. $query = $this->db->get($table);
  679. $ret = $query->row_array();
  680. return $ret;
  681. }
  682. }
  683. public function delData($condition) {
  684. if (!empty($condition)){
  685. foreach ($condition as $k => $v) {
  686. if (is_array($v)) {
  687. if (!empty($v)) {
  688. $this->db->where_in($k,$v);
  689. }else{
  690. $this->db->where_in($k,array(0));
  691. }
  692. }else{
  693. $this->db->where($k,$v);
  694. }
  695. }
  696. }else{
  697. return false;
  698. }
  699. $this->db->delete($this->table);
  700. if ($this->db->affected_rows() > 0) {
  701. return true;
  702. } else {
  703. return false;
  704. }
  705. }
  706. public function delBatch($data, $field = 'id'){
  707. $this->db->where_in($field, $data);
  708. $this->db->delete($this->table);
  709. if ($this->db->affected_rows()) {
  710. return true;
  711. } else {
  712. return false;
  713. }
  714. }
  715. public function getBatch($ids){
  716. $this->db->where_in('id',$ids);
  717. $query = $this->db->get($this->table);
  718. $ret = $query->result_array();
  719. if (empty($ret)){
  720. return array();
  721. }
  722. return $ret;
  723. }
  724. public function getDataCount($condition, $id = 0) {
  725. if (!empty($condition)){
  726. foreach ($condition as $k => $v) {
  727. $this->db->where($k,$v);
  728. }
  729. }
  730. if (!empty($id)) {
  731. $this->db->where('id !=',$id);
  732. }
  733. $query = $this->db->get($this->table);
  734. $data = $query->row_array();
  735. if (empty($data)) {
  736. return 0;
  737. } else {
  738. return $id == $data['id'] ? 0 : 1;
  739. }
  740. }
  741. public function getLampIds($filter){
  742. if (isset($filter['project']) && is_numeric($filter['project'])) {
  743. $query = "SELECT id FROM lampinfo WHERE projectid=".$filter['project'];
  744. }elseif (isset($filter['network']) && is_numeric($filter['network'])){
  745. $query = "SELECT id FROM lampinfo WHERE networkid=".$filter['network'];
  746. }elseif (isset($filter['lamp']) && is_numeric($filter['lamp'])){
  747. $query = "SELECT id FROM lampinfo WHERE id=".$filter['lamp'];
  748. }else {
  749. return array();
  750. }
  751. $query = $this->db->query($query);
  752. $data = $query->result_array();
  753. if (empty($data)) {
  754. return array();
  755. }
  756. $ids = array();
  757. foreach ($data as $k => $v){
  758. $ids[] = $v['id'];
  759. }
  760. return $ids;
  761. }
  762. public function getIdByLikeName($name){
  763. $sql = "SELECT id FROM lampinfo WHERE number LIKE '%".$name."%'";
  764. $query = $this->db->query($sql);
  765. $arr = $query->row_array();
  766. if (!empty($arr)){
  767. return $arr['id'];
  768. }
  769. return false;
  770. }
  771. public function getCountByFilter($role, $companyid, $projectid = 0, $userid = 0, $filter = array()){
  772. if ($projectid > 0) {
  773. $condition = "L.projectid = {$projectid}";
  774. } elseif ($role == SYSTEM_ADMIN) {
  775. $condition = "1=1";
  776. } else {
  777. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  778. if (empty($projectIds)) {
  779. $condition = "L.projectid in (0)";
  780. }else{
  781. $condition = "L.projectid in ({$projectIds})";
  782. }
  783. }
  784. if (!empty($filter)) {
  785. $andCondition = ' AND '.key($filter).' = '.current($filter);
  786. } else {
  787. $andCondition = '';
  788. }
  789. // $sql = "SELECT COUNT(L.id) as num
  790. // FROM lampinfo AS L
  791. // LEFT JOIN (select lampid, max(updatetime) as maxtime from lamp_info_log group by lampid) t2 on L.id = t2.lampid
  792. // LEFT JOIN lamp_info_log AS log ON log.lampid = t2.lampid and log.updatetime = t2.maxtime
  793. // WHERE {$condition} {$andCondition}";
  794. $sql = "SELECT COUNT(L.id) as num
  795. FROM lampinfo AS L
  796. WHERE {$condition} {$andCondition}";
  797. $query = $this->db->query($sql);
  798. $arrRet = $query->row_array();
  799. return $arrRet;
  800. }
  801. public function getOnlineCount($role, $companyid, $projectid = 0, $userid = 0, $nw_status = false){
  802. if ($projectid > 0) {
  803. $condition = "L.projectid = {$projectid}";
  804. } elseif ($role == SYSTEM_ADMIN) {
  805. $condition = "1=1";
  806. } else {
  807. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  808. if (empty($projectIds)) {
  809. $condition = "L.projectid in (0)";
  810. }else{
  811. $condition = "L.projectid in ({$projectIds})";
  812. }
  813. }
  814. if ($nw_status !== false) {
  815. $andCondition = 'AND L.netstatus = '.$nw_status;
  816. } else {
  817. $andCondition = '';
  818. }
  819. $sql = "SELECT COUNT(L.id) AS num FROM lampinfo as L WHERE {$condition} {$andCondition}";
  820. $query = $this->db->query($sql);
  821. $arrRet = $query->row_array();
  822. return $arrRet;
  823. }
  824. public function get_project_company($condition,$field){
  825. $this->db->select($field);
  826. if (!empty($condition)) {
  827. foreach ($condition as $key => $value) {
  828. if (is_array($value)) {
  829. if (!empty($value)) {
  830. $this->db->where_in($key,$value);
  831. }else{
  832. $this->db->where_in($key,array(0));
  833. }
  834. }else{
  835. $this->db->where($key,$value);
  836. }
  837. }
  838. }else{
  839. return array();
  840. }
  841. $this->db->join('project as P','P.id = L.projectid','left');
  842. // $this->db->join('network as N','N.id = L.networkid','left');
  843. return $this->db->get($this->table.' as L')->row_array();
  844. }
  845. public function markNormal($ids){
  846. $ids = implode(',', $ids);
  847. $sql = "UPDATE lampinfo set isfaulted = 0 WHERE id IN ($ids)";
  848. $query = $this->db->query($sql);
  849. if ($this->db->affected_rows() > 0) {
  850. return true;
  851. } else {
  852. return false;
  853. }
  854. }
  855. // 地图页获取路段均值数据
  856. public function get_section_avg($project,$section){
  857. $sql = "SELECT avg(daychargemincurrent) as daychargemincurrent,avg(daydischargemincurrent) as daydischargemincurrent,avg(totalgeneration) as totalgeneration,avg(totalconsumption) as totalconsumption,avg(electricleft) as electricleft,avg(battvoltage) as battvoltage from lampinfo WHERE projectid = {$project} AND section = '{$section}' group by section";
  858. return $this->db->query($sql)->row_array();
  859. }
  860. }