AdminHistory.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Models\Manager;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Models\Manager\AdminUser;
  6. /**
  7. * 后台管理员操作历史
  8. *
  9. */
  10. class AdminHistory extends Model
  11. {
  12. use HasFactory;
  13. // 与模型关联的表名
  14. protected $table = 'admin_history';
  15. // 是否主动维护时间戳
  16. public $timestamps = false;
  17. protected $connection = 'mysql';
  18. // 模型日期的存储格式
  19. // protected $dateFormat = 'U';
  20. // 定义时间戳字段名
  21. // const CREATED_AT = 'insert_time';
  22. // const UPDATED_AT = 'update_time';
  23. // 操作表字段
  24. private $tableColumn = [
  25. 'admin' => [
  26. '_table_name' => '管理表',
  27. 'status' => '状态',
  28. ],
  29. 'need' => [
  30. '_table_name' => '需求表',
  31. 'baseline' => '基线名称',
  32. 'status' => '状态',
  33. 'project_id' => '项目ID',
  34. 'project_type' => '项目类型',
  35. 'project_title' => '项目类型',
  36. 'memory_size' => '硬件空间',
  37. 'template_id' => '项目平台',
  38. 'custom_uid' => '客户名称',
  39. ],
  40. ];
  41. /**
  42. * 添加数据
  43. *
  44. * @param Int $uid 用户ID
  45. * @param String $table 操作表
  46. * @param Int $type 操作类型
  47. * @param Array $oldData 旧数据
  48. * @param Array $oldData 新数据
  49. *
  50. *
  51. */
  52. public function addAll($uid, $table, $id, $type, $oldData, $newData)
  53. {
  54. // 有ID,删除
  55. if (isset($oldData['id'])) unset($oldData['id']);
  56. if (isset($newData['id'])) unset($newData['id']);
  57. // 取差集
  58. $diff = [];
  59. // 当前时间
  60. $time = time();
  61. // 循环新数据
  62. foreach ($newData as $key => $value) {
  63. // 如果存在字段,且字段相等。不做处理
  64. if (isset($oldData[$key]) && $oldData[$key] == $value) continue;
  65. // 获取结果
  66. $diff[] = [
  67. // 操作主键
  68. 'primary_id' => $id,
  69. // 操作类型
  70. 'notes_type' => $type,
  71. // 操作表名
  72. 'table_name' => $table,
  73. // 操作字段
  74. 'column_name' => $key,
  75. // 操作用户
  76. 'admin_uid' => $uid,
  77. // 操作之前
  78. 'before_value' => empty($oldData[$key]) ? '' : $oldData[$key],
  79. // 操作之后
  80. 'after_value' => $value,
  81. // 时间
  82. 'insert_time' => $time,
  83. // 时间
  84. 'update_time' => $time,
  85. ];
  86. }
  87. // 结果
  88. $result = $diff ? $this->query()->insert($diff) : 0;
  89. // 如果有数据
  90. return $result;
  91. }
  92. /**
  93. * 获取历史
  94. *
  95. * @param String $table 操作表
  96. * @param Int $id 操作ID
  97. * @param Bool $getHtml 是否获取Html
  98. *
  99. */
  100. public function getHistory($table, $id, $getHtml = true)
  101. {
  102. // 查询返回结果
  103. $data = $this->query()->where([['table_name', '=', $table], ['primary_id', '=', $id]])->orderByDesc('update_time')->limit($getHtml ? 1 : 3)->get()->toArray();
  104. // html
  105. $html = '';
  106. // 循环数据
  107. foreach ($data as $key => $value) {
  108. // 获取字段名称
  109. $value['table_name'] = $this->getTableName($table, $value['table_name']);
  110. // 获取字段名称
  111. $value['notes_type'] = $value['notes_type'] == 1 ? '添加' : ($value['notes_type'] == 2 ? '修改' : '删除');
  112. // 获取字段名称
  113. $value['column_name'] = $this->getTableColumn($table, $value['column_name']);
  114. // 获取字段名称
  115. $value['admin_name'] = (new AdminUser())->getOne($value['admin_uid'], 'nickname');
  116. // 返回结果
  117. $html = '<p>' . $value['admin_name'] . ' ' . date('m-d H:i', $value['update_time']) . ' ' . $value['notes_type'] . ' ' . $value['column_name'] . '</p>';
  118. // 重组
  119. $data[$key] = $value;
  120. }
  121. // 返回结果
  122. return $getHtml ? $html : $data;
  123. }
  124. /**
  125. * 获取表格字段名
  126. *
  127. * @param String $table 操作表
  128. * @param String $column 操作字段
  129. *
  130. */
  131. public function getTableColumn($table, $column)
  132. {
  133. // 结果
  134. return isset($this->tableColumn[$table][$column]) ? $this->tableColumn[$table][$column] : $column;
  135. }
  136. /**
  137. * 获取表格列表
  138. *
  139. *
  140. */
  141. public function getTableNameList()
  142. {
  143. // 结果
  144. return $this->tableColumn;
  145. }
  146. /**
  147. * 获取表格名
  148. *
  149. * @param String $table 操作表
  150. *
  151. */
  152. public function getTableName($table)
  153. {
  154. // 结果
  155. return isset($this->tableColumn[$table]['_table_name']) ? $this->tableColumn[$table]['_table_name'] : $table;
  156. }
  157. /**
  158. * 获取最后一次修改值的信息
  159. * @param String $table 操作表
  160. * @param Int $id 操作ID
  161. *
  162. */
  163. public function getLastByValue($table, $id, $column, $value)
  164. {
  165. // 查询返回结果
  166. $data = $this->query()->where([['table_name', '=', $table], ['primary_id', '=', $id], ['column_name', '=', $column], ['after_value', '=', $value]])->orderByDesc('update_time')->first(['admin_uid', 'update_time']);
  167. // 返回结果
  168. return $data ? $data->toArray() : [];
  169. }
  170. }