Battery_model.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Battery_model extends Base_model {
  5. protected $table = 'battery_cmd';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function queryData($start,$offset){
  10. $this->db->order_by("id","DESC");
  11. $query = $this->db->get($this->table);
  12. return $query->result_array();
  13. }
  14. public function getList($filter,$page = null,$limit = null){
  15. if (isset($filter['network'])){
  16. $sql_in = "";
  17. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  18. $lampIds = explode(",",$filter['keyword']);
  19. $arrIds = array();
  20. foreach ($lampIds as $id){
  21. if(is_numeric($id) && !empty($id)){
  22. $arrIds[] = trim($id);
  23. }
  24. }
  25. if(!empty($arrIds)){
  26. $sql_in_ids = implode(',',$arrIds);
  27. $sql_in =" AND L.number IN (".$sql_in_ids.")";
  28. }
  29. }
  30. if(isset($filter['status']) && $filter['status'] == 1){
  31. $sql_in .= " AND isfaulted = 1 ";
  32. }
  33. $sql_limit = "";
  34. if(is_numeric($page) && is_numeric($limit)){
  35. $sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
  36. }
  37. $sql = "SELECT
  38. L.*,
  39. cmd.*,
  40. L.`id` as lid,
  41. P.`projectname` as projectname,
  42. N.`networkname` as networkname FROM lampinfo AS L
  43. LEFT JOIN project AS P ON P.id = L.projectid
  44. LEFT JOIN network AS N ON N.id = L.networkid
  45. LEFT JOIN (SELECT * FROM ".$this->table." ORDER BY updatetime DESC) AS cmd ON L.id = cmd.id
  46. WHERE L.networkid=".intval($filter['network'])." {$sql_in} GROUP BY lid order by L.number asc, lid asc {$sql_limit}";
  47. $query = $this->db->query($sql);
  48. return $query->result_array();
  49. }
  50. if (!empty($filter)){
  51. foreach ($filter as $k => $v) {
  52. if($k == 'status' || $k == 'keyword' || $k == 'type'){
  53. continue;
  54. }
  55. $this->db->where($k,$v);
  56. }
  57. }
  58. if ($this->session->userdata('role') == COMPANY_ADMIN) {
  59. $companyid = $this->session->userdata('company_id');
  60. $this->db->where('company', $companyid);
  61. }
  62. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  63. $this->load->model('User_model');
  64. $user_project = $this->User_model->get_user_zone();
  65. if ($user_project) {
  66. $projects = explode(',', $user_project);
  67. $this->db->where_in('id',$projects);
  68. } else {
  69. $this->db->where_in('id', array(0));
  70. }
  71. }
  72. $this->db->select('*');
  73. $query = $this->db->get('project');
  74. $project = $query->result_array();
  75. if (empty($project)){
  76. return array();
  77. }
  78. $Ids = array();
  79. $projectIds = array();
  80. foreach ($project as $v){
  81. $Ids[] = $v['id'];
  82. $projectIds[$v['id']] = $v['projectid'];
  83. }
  84. $offset = ($page-1)*$limit;
  85. $condition = "L.projectid in (".implode(',', $Ids).")";
  86. $sql_in = "";
  87. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  88. $lampIds = explode(",",$filter['keyword']);
  89. $arrIds = array(0);
  90. foreach ($lampIds as $id){
  91. if(is_numeric($id) && !empty($id)){
  92. $arrIds[] = trim($id);
  93. }
  94. }
  95. if(!empty($arrIds)){
  96. $sql_in_ids = implode(',',$arrIds);
  97. $fieldMap = array(0=>'projectid', 1=>'networkid', 2=>'number');
  98. $sql_in .=" AND L.".$fieldMap[$filter['type']]." IN (".$sql_in_ids.")";
  99. }
  100. }
  101. if(isset($filter['status']) && $filter['status'] == 1){
  102. $sql_in .= " AND isfaulted = 1 ";
  103. }
  104. $sql = "SELECT
  105. L.*,
  106. cmd.*,
  107. L.`id` as lid,
  108. P.`projectname` as projectname,
  109. N.`networkname` as networkname FROM lampinfo AS L
  110. LEFT JOIN project AS P ON P.id = L.projectid
  111. LEFT JOIN network AS N ON N.id = L.networkid
  112. LEFT JOIN (SELECT * FROM ".$this->table." ORDER BY updatetime DESC) AS cmd ON L.id = cmd.id
  113. WHERE {$condition} {$sql_in} GROUP BY lid order by L.number asc, lid asc limit {$offset}, {$limit}";
  114. $query = $this->db->query($sql);
  115. $arrRet = $query->result_array();
  116. return $arrRet;
  117. }
  118. public function getTotal($filter){
  119. if(isset($filter['network'])){
  120. $this->db->select('count(id) as total');
  121. $this->db->where_in('networkid',array(intval($filter['network'])));
  122. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  123. $lampIds = explode(",",$filter['keyword']);
  124. $arrIds = array();
  125. foreach ($lampIds as $id){
  126. if(is_numeric($id)){
  127. $arrIds[] = intval(trim($id));
  128. }
  129. }
  130. if (!empty($arrIds)){
  131. $this->db->where_in('number', $arrIds);
  132. }
  133. }
  134. if(isset($filter['status']) && $filter['status'] == 1){
  135. $this->db->where('isfaulted',1);
  136. }
  137. $query = $this->db->get('lampinfo');
  138. $arr = $query->row_array();
  139. return $arr['total'];
  140. }
  141. if (!empty($filter)){
  142. foreach ($filter as $k => $v) {
  143. if($k == 'status' || $k == 'keyword' || $k == 'type'){
  144. continue;
  145. }
  146. $this->db->where($k,$v);
  147. }
  148. }
  149. if ($this->session->userdata('role') == COMPANY_ADMIN) {
  150. $companyid = $this->session->userdata('company_id');
  151. $this->db->where('company', $companyid);
  152. }
  153. if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
  154. $this->load->model('User_model');
  155. $user_project = $this->User_model->get_user_zone();
  156. if ($user_project) {
  157. $projects = explode(',', $user_project);
  158. $this->db->where_in('id',$projects);
  159. } else {
  160. $this->db->where_in('id', array(0));
  161. }
  162. }
  163. $this->db->select('id');
  164. $this->db->order_by("id","DESC");
  165. $query = $this->db->get('project');
  166. $project = $query->result_array();
  167. if (empty($project)){
  168. return 0;
  169. }
  170. foreach ($project as $v){
  171. $Ids[] = $v['id'];
  172. }
  173. $this->db->select('count(id) as total');
  174. $this->db->where_in('projectid',$Ids);
  175. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  176. $lampIds = explode(",",$filter['keyword']);
  177. $arrIds = array(0);
  178. foreach ($lampIds as $id){
  179. if(is_numeric($id)){
  180. $arrIds[] = intval(trim($id));
  181. }
  182. }
  183. if (!empty($arrIds)){
  184. $fieldMap = array(0=>'projectid', 1=>'networkid', 2=>'number');
  185. $this->db->where_in($fieldMap[$filter['type']], $arrIds);
  186. }
  187. }
  188. if(isset($filter['status']) && $filter['status'] == 1){
  189. $this->db->where('isfaulted',1);
  190. }
  191. $query = $this->db->get('lampinfo');
  192. $arr = $query->row_array();
  193. return $arr['total'];
  194. }
  195. }
  196. ?>