Videomonitor_model.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Videomonitor_model extends Base_model{
  5. protected $table = 'video_monitor';
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. public function get_project_company($condition,$field){
  11. $this->db->select($field);
  12. if (!empty($condition)) {
  13. foreach ($condition as $key => $value) {
  14. if (is_array($value)) {
  15. if (!empty($value)) {
  16. $this->db->where_in($key,$value);
  17. }else{
  18. $this->db->where_in($key,array(0));
  19. }
  20. }else{
  21. $this->db->where($key,$value);
  22. }
  23. }
  24. }else{
  25. return array();
  26. }
  27. $this->db->join('project as P','P.id = V.projectid','left');
  28. return $this->db->get($this->table.' as V')->row_array();
  29. }
  30. public function get_data_by_fiter($fiter=array(),$field="*"){
  31. $this->db->select($field);
  32. foreach ($fiter as $key => $value) {
  33. if (is_array($value)) {
  34. $this->db->where_in($key,$value);
  35. }else{
  36. $this->db->where($key,$value);
  37. }
  38. }
  39. return $this->db->get($this->table)->row_array();
  40. }
  41. // 地图页获取监控列表
  42. public function get_data_by_location($role, $companyid, $projectid = 0, $userid = 0,$fields='*',$filter=array()){
  43. $this->db->select($fields, false);
  44. $projectIds = array();
  45. if ($projectid > 0) {
  46. $projectIds[] = $projectid;
  47. $this->db->where_in('projectid',$projectIds);
  48. } elseif ($role != SYSTEM_ADMIN) {
  49. $idStr = $this->get_projectid_by_role($role,$userid,$companyid);
  50. if (!empty($idStr)) {
  51. $projectIds = explode(',', $idStr);
  52. }else{
  53. $projectIds = array(0);
  54. }
  55. $this->db->where_in('projectid',$projectIds);
  56. }
  57. // 经纬度筛选
  58. 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']) {
  59. $this->db->group_start();
  60. $this->db->group_start();
  61. $this->db->where('longitude >=', $filter['lng_low']);
  62. $this->db->where('longitude <=', 180);
  63. $this->db->group_end();
  64. $this->db->or_group_start();
  65. $this->db->where('longitude <=', $filter['lng_high']);
  66. $this->db->where('longitude >=', -180);
  67. $this->db->group_end();
  68. $this->db->group_end();
  69. unset($filter['lng_high']);
  70. unset($filter['lng_low']);
  71. }else{
  72. if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
  73. $this->db->where('longitude >=', $filter['lng_low']);
  74. unset($filter['lng_low']);
  75. }
  76. if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
  77. $this->db->where('longitude <=', $filter['lng_high']);
  78. unset($filter['lng_high']);
  79. }
  80. }
  81. if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
  82. $this->db->where('latitude >=', $filter['lat_low']);
  83. unset($filter['lat_low']);
  84. }
  85. if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
  86. $this->db->where('latitude <=', $filter['lat_high']);
  87. unset($filter['lat_high']);
  88. }
  89. if(isset($filter['keyword']) && !empty($filter['keyword'])){
  90. $this->db->like('name', $filter['keyword']);
  91. }
  92. // if(empty($type) && !empty($filter['page']) && !empty($filter['count'])){
  93. // $this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
  94. // unset($filter['page']);
  95. // unset($filter['count']);
  96. // }
  97. // 根据路灯状态来筛选
  98. // foreach ($filter as $key => $value) {
  99. // $this->db->where($key,$value);
  100. // }
  101. $this->db->from($this->table);
  102. // $this->db->join('project as P', 'L.projectid = P.id', 'left');
  103. // $this->db->join('network as N', 'L.networkid = N.id', 'left');
  104. // $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');
  105. // $this->db->group_by('L.id');
  106. // $this->db->order_by("L.number ASC,N.networkname ASC");
  107. $query = $this->db->get();
  108. // if (empty($type)) {
  109. // }else{
  110. // return $query->num_rows();
  111. // }
  112. return $query->result_array();
  113. }
  114. // 获取视屏详情
  115. public function get_video_by_id($id,$field='*'){
  116. $sql = "select {$field} from {$this->table} as V
  117. where V.id = {$id}";
  118. return $this->db->query($sql)->row_array();
  119. }
  120. public function get_one_by_role($role, $companyid, $projectid = 0, $userid = 0,$fields='*',$keyword){
  121. $this->db->select($fields, false);
  122. $projectIds = array();
  123. if ($projectid > 0) {
  124. $projectIds[] = $projectid;
  125. $this->db->where_in('projectid',$projectIds);
  126. } elseif ($role != SYSTEM_ADMIN) {
  127. $idStr = $this->get_projectid_by_role($role,$userid,$companyid);
  128. if (!empty($idStr)) {
  129. $projectIds = explode(',', $idStr);
  130. }else{
  131. $projectIds = array(0);
  132. }
  133. $this->db->where_in('projectid',$projectIds);
  134. }
  135. if (!empty($keyword)) {
  136. $this->db->like('name',$keyword);
  137. }
  138. $this->db->from($this->table);
  139. $query = $this->db->get();
  140. return $query->row_array();
  141. }
  142. // (首页)获取首页视屏监控列表
  143. public function getList($role, $companyid, $projectid, $userid,$page=null,$limit = null){
  144. if ($projectid > 0) { // 有项目id
  145. $condition = "projectid = {$projectid}";
  146. } elseif ($role == SYSTEM_ADMIN) { // 系统管理员,所有项目
  147. $condition = "1=1";
  148. } else {
  149. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  150. if (empty($projectIds)) {
  151. $condition = "projectid in (0)";
  152. }else{
  153. $condition = "projectid in ({$projectIds})";
  154. }
  155. }
  156. $sql = "select address,id,devid,password,type,channel,image from video_monitor WHERE {$condition} order by createtime desc";
  157. if ($page != null && $limit != null){
  158. $sql .= " LIMIT ".($page-1)*$limit.",".$limit;
  159. }
  160. $res = $this->db->query($sql);
  161. return $res->result_array();
  162. }
  163. // 根据条件获取监控数
  164. public function getTotal($filter=array(),$role,$user_id,$companyid,$project_id=0){
  165. $sql = "SELECT count(*) as total from {$this->table} ";
  166. if (isset($filter['keyword'])) {
  167. $implode[] = "name like '%{$filter['keyword']}%'";
  168. unset($filter['keyword']);
  169. }
  170. foreach ($filter as $key => $value) {
  171. $implode[] = "$key=$value";
  172. }
  173. if ($project_id > 0) {
  174. $projectIds = $project_id;
  175. }else{
  176. $projectIds = $this->get_projectid_by_role($role,$user_id,$companyid);
  177. }
  178. if(empty($projectIds)){
  179. $implode[] = "projectid in (0)";
  180. }else{
  181. $implode[] = "projectid in ({$projectIds})";
  182. }
  183. $where = $implode ? " WHERE " . implode(" AND ", $implode) : '';
  184. $sql .= $where;
  185. $res = $this->db->query($sql);
  186. $data = $res->row_array();
  187. return $data["total"];
  188. }
  189. // 根据用户权限获取视屏监控列表
  190. public function get_list_by_role($role,$userid,$companyid,$field="*",$page=null,$limit = null,$projectid=0,$fiter=array()){
  191. if ($projectid > 0) {
  192. $idStr = $projectid;
  193. }else{
  194. $idStr = $this->get_projectid_by_role($role,$userid,$companyid);
  195. $idStr = empty($idStr) ? '0' : $idStr;
  196. }
  197. $sql = "select {$field}
  198. from video_monitor as V
  199. where V.projectid in ({$idStr})";
  200. if (isset($fiter['keyword'])) {
  201. $sql .= " AND V.name like '%{$fiter['keyword']}%'";
  202. unset($fiter['keyword']);
  203. }
  204. if ($page != null && $limit != null){
  205. $sql .= " LIMIT ".($page-1)*$limit.",".$limit;
  206. }
  207. $res = $this->db->query($sql);
  208. $data = $res->result_array();
  209. return $data;
  210. }
  211. // 根据刷选条件获取视屏监控列表
  212. public function get_list_by_filter($filter,$field='*'){
  213. $temp = [];
  214. $sql = "select {$field}
  215. from video_monitor as V ";
  216. foreach ($filter as $key => $value) {
  217. if (is_array($value)) {
  218. if (!empty($value)) {
  219. $v = implode(',', $value);
  220. $temp[] = "V.{$key} in ($v)";
  221. }else{
  222. $temp[] = "V.{$key} in (0)";
  223. }
  224. }else{
  225. $temp[] = "V.{$key} = '{$value}'";
  226. }
  227. }
  228. if (!empty($temp)) {
  229. $where = implode(' AND ', $temp);
  230. $sql .= "where ".$where;
  231. }
  232. $data = $this->db->query($sql)->result_array();
  233. return $data;
  234. }
  235. // 通过筛选条件删除监控
  236. public function delData($condition){
  237. if (!empty($condition)) {
  238. foreach ($condition as $key => $value) {
  239. if (is_array($value)) {
  240. if (!empty($value)) {
  241. $this->db->where_in($key,$value);
  242. }else{
  243. $this->db->where_in($key,array(0));
  244. }
  245. }else{
  246. $this->db->where($key,$value);
  247. }
  248. }
  249. }else{
  250. return false;
  251. }
  252. $this->db->delete($this->table);
  253. if ($this->db->affected_rows()) {
  254. return true;
  255. } else {
  256. return false;
  257. }
  258. }
  259. // 通过筛选条件删除监控日志
  260. public function del_video_info_log($fiter=array()){
  261. if (empty($fiter)) {
  262. return array();
  263. }
  264. $implode = array();
  265. foreach ($fiter as $key => $value) {
  266. if (is_array($value)) {
  267. if (!empty($value)) {
  268. $v = implode(',', $value);
  269. $implode[] = "$key in ($v)";
  270. }
  271. }else{
  272. $implode[] = "$key = $value";
  273. }
  274. }
  275. $where = " where ".implode(' AND ', $implode);
  276. // 删除视屏文件
  277. $sql1 = "select video_path as path from video_info_log {$where}";
  278. $data = $this->db->query($sql1)->result_array();
  279. foreach ($data as $v) {
  280. @unlink($v['path']);
  281. }
  282. // 删除数据记录
  283. $sql2 = "delete from video_info_log {$where}";
  284. $this->db->query($sql2);
  285. if ($this->db->affected_rows()) {
  286. return true;
  287. }else{
  288. return false;
  289. }
  290. }
  291. // 添加NVR设备
  292. public function insertNVR($data){
  293. $this->db->insert('nvrdevice', $data);
  294. return $this->db->insert_id();
  295. }
  296. // 获取NVR设备id
  297. public function getNVRCount($condition){
  298. $this->db->select('id');
  299. if (!empty($condition)){
  300. foreach ($condition as $k => $v) {
  301. $this->db->where($k,$v);
  302. }
  303. }
  304. $query = $this->db->get('nvrdevice');
  305. $data = $query->row_array();
  306. if (empty($data)) {
  307. return 0;
  308. } else {
  309. return $data['id'];
  310. }
  311. }
  312. public function getDataCount($condition, $id = 0) {
  313. if (!empty($condition)){
  314. foreach ($condition as $k => $v) {
  315. $this->db->where($k,$v);
  316. }
  317. }
  318. if (!empty($id)) {
  319. $this->db->where('id !=',$id);
  320. }
  321. $query = $this->db->get($this->table);
  322. $data = $query->row_array();
  323. if (empty($data)) {
  324. return 0;
  325. } else {
  326. return $id == $data['id'] ? 0 : 1;
  327. }
  328. }
  329. }
  330. ?>