Syslog_model.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Syslog_model extends Base_model {
  5. protected $table = 'syslog';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function set_read($id){
  10. return $this->db->where('id',$id)->update($this->table,array('is_read'=>1));
  11. }
  12. // 获取日志列表
  13. public function getList($filter,$page = null,$limit = null,$field="S.*",$type = 0){
  14. $where_sql = "";
  15. if (isset($filter['id']) || isset($filter['realname']) || isset($filter['username'])){
  16. $arrUserIds = $this->getUserIds($filter);
  17. if (empty($arrUserIds)){
  18. $arrUserIds[] = 0;
  19. }
  20. $where_sql .= " AND S.userid IN(".implode(',', $arrUserIds).") ";
  21. }
  22. if(isset($filter['starttime']) && isset($filter['endtime'])){
  23. $starttime = date('Y-m-d 00:00:00',strtotime($filter['starttime']));
  24. $endtime = date('Y-m-d 23:59:59',strtotime($filter['endtime']));
  25. $where_sql .= " AND S.time >= '".$starttime."' AND S.time <='".$endtime."' ";
  26. }
  27. if (isset($filter['optype']) && !empty($filter['optype'])){
  28. if($filter['optype'] == 'login'){
  29. $where_sql .= " AND S.optype IN('login','logout') ";
  30. }else{
  31. $where_sql .= " AND S.optype='".$filter['optype']."'";
  32. }
  33. }
  34. // 获取未读日志
  35. // if (isset($filter['is_read'])){
  36. // $filter['is_read'] = empty($filter['is_read']) ? 0 : $filter['is_read'];
  37. // $where_sql .= " AND S.is_read = ".$filter['is_read'];
  38. // }
  39. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  40. $where_sql .= " AND (S.time LIKE binary \"%{$filter['keyword']}%\"";
  41. $where_sql .= " OR U.username LIKE binary \"%{$filter['keyword']}%\"";
  42. $where_sql .= " OR U.realname LIKE binary \"%{$filter['keyword']}%\"";
  43. $where_sql .= " OR S.content LIKE binary \"%{$filter['keyword']}%\")";
  44. }
  45. if (!empty($filter['startDate']) && !empty($filter['endDate'])) {
  46. $where_sql .= " AND S.time >= '{$filter['startDate']}'";
  47. $where_sql .= " AND S.time <= '{$filter['endDate']}'";
  48. }
  49. if (!empty($filter['typeArr']) && is_array($filter['typeArr'])) {
  50. $typeStr = implode(',', $filter['typeArr']);
  51. $where_sql .= " AND S.optype in({$typeStr})";
  52. }
  53. if (isset($filter['language'])) {
  54. $where_sql .= " AND S.language = {$filter['language']}";
  55. }
  56. if (!empty($filter['companyid'])) {
  57. $where_sql .= " AND S.companyid = {$filter['companyid']}";
  58. }
  59. if (!empty($filter['userid'])) {
  60. $where_sql .= " AND S.userid = {$filter['userid']}";
  61. }
  62. $sql = "SELECT {$field},
  63. U.username AS username,
  64. U.realname AS realname
  65. FROM syslog AS S JOIN user AS U ON S.userid=U.id
  66. WHERE 1=1 ";
  67. $limit_sql = "";
  68. if (empty($type) && $limit < 1000) {
  69. if ($page != null && $limit != null){
  70. $limit_sql = " LIMIT ".($page-1)*$limit.",".$limit;
  71. }
  72. }else{
  73. if (isset($page) && isset($limit)){
  74. $limit_sql = " LIMIT ".$page.",".$limit;
  75. }
  76. }
  77. $query = $sql.$where_sql." ORDER BY time DESC ".$limit_sql;
  78. $query = $this->db->query($query);
  79. return $query->result_array();
  80. }
  81. // 获取日志总数
  82. public function getTotal($filter){
  83. $where_sql = "";
  84. if (isset($filter['id']) || isset($filter['realname']) || isset($filter['username'])){
  85. $arrUserIds = $this->getUserIds($filter);
  86. if (empty($arrUserIds)){
  87. $arrUserIds[] = 0;
  88. }
  89. $where_sql .= " AND S.userid IN(".implode(',', $arrUserIds).") ";
  90. }
  91. if(isset($filter['starttime']) && isset($filter['endtime'])){
  92. $starttime = date('Y-m-d 00:00:00',strtotime($filter['starttime']));
  93. $endtime = date('Y-m-d 23:59:59',strtotime($filter['endtime']));
  94. $where_sql .= " AND S.time >= '".$starttime."' AND S.time <='".$endtime."' ";
  95. }
  96. if (isset($filter['optype']) && !empty($filter['optype'])){
  97. if($filter['optype'] == 'login'){
  98. $where_sql .= " AND S.optype IN('login','logout') ";
  99. }else{
  100. $where_sql .= " AND S.optype='".$filter['optype']."'";
  101. }
  102. }
  103. // 获取未读日志
  104. // if (isset($filter['is_read'])){
  105. // $filter['is_read'] = empty($filter['is_read']) ? 0 : $filter['is_read'];
  106. // $where_sql .= " AND S.is_read = ".$filter['is_read'];
  107. // }
  108. if (isset($filter['keyword']) && !empty($filter['keyword'])){
  109. $where_sql .= " AND (S.time LIKE binary \"%{$filter['keyword']}%\"";
  110. $where_sql .= " OR U.username LIKE binary \"%{$filter['keyword']}%\"";
  111. $where_sql .= " OR U.realname LIKE binary \"%{$filter['keyword']}%\"";
  112. $where_sql .= " OR S.content LIKE binary \"%{$filter['keyword']}%\")";
  113. }
  114. if (!empty($filter['startDate']) && !empty($filter['endDate'])) {
  115. $where_sql .= " AND S.time >= '{$filter['startDate']}'";
  116. $where_sql .= " AND S.time <= '{$filter['endDate']}'";
  117. }
  118. if (!empty($filter['typeArr']) && is_array($filter['typeArr'])) {
  119. $typeStr = implode(',', $filter['typeArr']);
  120. $where_sql .= " AND S.optype in({$typeStr})";
  121. }
  122. if (isset($filter['language'])) {
  123. $where_sql .= " AND S.language = {$filter['language']}";
  124. }
  125. if (!empty($filter['companyid'])) {
  126. $where_sql .= " AND S.companyid = {$filter['companyid']}";
  127. }
  128. if (!empty($filter['userid'])) {
  129. $where_sql .= " AND S.userid = {$filter['userid']}";
  130. }
  131. $sql = "SELECT count(*) as total
  132. FROM syslog AS S JOIN user AS U ON S.userid=U.id
  133. WHERE 1=1 ";
  134. // $limit_sql = "";
  135. $query = $sql.$where_sql;
  136. $query = $this->db->query($query);
  137. $row = $query->row_array();
  138. return $row['total'];
  139. }
  140. public function getUserIds($filter){
  141. $arrIds = array();
  142. if (isset($filter['realname']) && !empty($filter['realname'])){
  143. $sql = "SELECT id FROM user WHERE realname LIKE '%".$filter['realname']."%' ";
  144. $query = $this->db->query($sql);
  145. $userList = $query->result_array();
  146. if (empty($userList)){
  147. return $arrIds;
  148. }
  149. foreach ($userList as $v){
  150. if (!in_array($v['id'], $arrIds)){
  151. $arrIds[] = $v['id'];
  152. }
  153. }
  154. return $arrIds;
  155. }
  156. if (isset($filter['username']) && !empty($filter['username'])){
  157. $sql = "SELECT id FROM user WHERE username LIKE '%".$filter['username']."%' ";
  158. $query = $this->db->query($sql);
  159. $userList = $query->result_array();
  160. if (empty($userList)){
  161. return $arrIds;
  162. }
  163. foreach ($userList as $v){
  164. if (!in_array($v['id'], $arrIds)){
  165. $arrIds[] = $v['id'];
  166. }
  167. }
  168. return $arrIds;
  169. }
  170. if (isset($filter['id']) && !empty($filter['id'])){
  171. $sql = "SELECT id,role FROM user WHERE id={$filter['id']}";
  172. $query = $this->db->query($sql);
  173. $user = $query->row_array();
  174. if (empty($user)){
  175. return $arrIds;
  176. }
  177. $arrIds[] = $filter['id'];
  178. // 超级管理员
  179. if($user['role'] == 1){
  180. $sql = "SELECT id FROM user WHERE parentid={$filter['id']}";
  181. $query = $this->db->query($sql);
  182. $userList = $query->result_array();
  183. if (empty($userList)){
  184. return $arrIds;
  185. }
  186. $arrIds2 = array();
  187. foreach ($userList as $v){
  188. $arrIds2[] = $v['id'];
  189. if (!in_array($v['id'], $arrIds)){
  190. $arrIds[] = $v['id'];
  191. }
  192. }
  193. $sql = "SELECT id FROM user WHERE parentid IN (".implode(',', $arrIds2).")";
  194. $query = $this->db->query($sql);
  195. $userList = $query->result_array();
  196. if (empty($userList)){
  197. return $arrIds;
  198. }
  199. foreach ($userList as $v){
  200. if (!in_array($v['id'], $arrIds)){
  201. $arrIds[] = $v['id'];
  202. }
  203. }
  204. return $arrIds;
  205. }elseif ($user['role'] == 2){
  206. $sql = "SELECT id FROM user WHERE parentid={$filter['id']}";
  207. $query = $this->db->query($sql);
  208. $userList = $query->result_array();
  209. if (empty($userList)){
  210. return $arrIds;
  211. }
  212. foreach ($userList as $v){
  213. if (!in_array($v['id'], $arrIds)){
  214. $arrIds[] = $v['id'];
  215. }
  216. }
  217. $arrIds[] = $v['id'];
  218. }else {
  219. return $arrIds;
  220. }
  221. }
  222. }
  223. }