Lamp_model.php 35 KB

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