| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Models\Manager;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\Manager\AdminUser;
- /**
- * 后台管理员操作历史
- *
- */
- class AdminHistory extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'admin_history';
- // 是否主动维护时间戳
- public $timestamps = false;
- protected $connection = 'mysql';
- // 模型日期的存储格式
- // protected $dateFormat = 'U';
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- // 操作表字段
- private $tableColumn = [
- 'admin' => [
- '_table_name' => '管理表',
- 'status' => '状态',
- ],
- 'need' => [
- '_table_name' => '需求表',
- 'baseline' => '基线名称',
- 'status' => '状态',
- 'project_id' => '项目ID',
- 'project_type' => '项目类型',
- 'project_title' => '项目类型',
- 'memory_size' => '硬件空间',
- 'template_id' => '项目平台',
- 'custom_uid' => '客户名称',
- ],
- ];
- /**
- * 添加数据
- *
- * @param Int $uid 用户ID
- * @param String $table 操作表
- * @param Int $type 操作类型
- * @param Array $oldData 旧数据
- * @param Array $oldData 新数据
- *
- *
- */
- public function addAll($uid, $table, $id, $type, $oldData, $newData)
- {
- // 有ID,删除
- if (isset($oldData['id'])) unset($oldData['id']);
- if (isset($newData['id'])) unset($newData['id']);
- // 取差集
- $diff = [];
- // 当前时间
- $time = time();
- // 循环新数据
- foreach ($newData as $key => $value) {
- // 如果存在字段,且字段相等。不做处理
- if (isset($oldData[$key]) && $oldData[$key] == $value) continue;
- // 获取结果
- $diff[] = [
- // 操作主键
- 'primary_id' => $id,
- // 操作类型
- 'notes_type' => $type,
- // 操作表名
- 'table_name' => $table,
- // 操作字段
- 'column_name' => $key,
- // 操作用户
- 'admin_uid' => $uid,
- // 操作之前
- 'before_value' => empty($oldData[$key]) ? '' : $oldData[$key],
- // 操作之后
- 'after_value' => $value,
- // 时间
- 'insert_time' => $time,
- // 时间
- 'update_time' => $time,
- ];
- }
- // 结果
- $result = $diff ? $this->query()->insert($diff) : 0;
- // 如果有数据
- return $result;
- }
- /**
- * 获取历史
- *
- * @param String $table 操作表
- * @param Int $id 操作ID
- * @param Bool $getHtml 是否获取Html
- *
- */
- public function getHistory($table, $id, $getHtml = true)
- {
- // 查询返回结果
- $data = $this->query()->where([['table_name', '=', $table], ['primary_id', '=', $id]])->orderByDesc('update_time')->limit($getHtml ? 1 : 3)->get()->toArray();
- // html
- $html = '';
- // 循环数据
- foreach ($data as $key => $value) {
- // 获取字段名称
- $value['table_name'] = $this->getTableName($table, $value['table_name']);
- // 获取字段名称
- $value['notes_type'] = $value['notes_type'] == 1 ? '添加' : ($value['notes_type'] == 2 ? '修改' : '删除');
- // 获取字段名称
- $value['column_name'] = $this->getTableColumn($table, $value['column_name']);
- // 获取字段名称
- $value['admin_name'] = (new AdminUser())->getOne($value['admin_uid'], 'nickname');
- // 返回结果
- $html = '<p>' . $value['admin_name'] . ' ' . date('m-d H:i', $value['update_time']) . ' ' . $value['notes_type'] . ' ' . $value['column_name'] . '</p>';
- // 重组
- $data[$key] = $value;
- }
- // 返回结果
- return $getHtml ? $html : $data;
- }
- /**
- * 获取表格字段名
- *
- * @param String $table 操作表
- * @param String $column 操作字段
- *
- */
- public function getTableColumn($table, $column)
- {
- // 结果
- return isset($this->tableColumn[$table][$column]) ? $this->tableColumn[$table][$column] : $column;
- }
- /**
- * 获取表格列表
- *
- *
- */
- public function getTableNameList()
- {
- // 结果
- return $this->tableColumn;
- }
- /**
- * 获取表格名
- *
- * @param String $table 操作表
- *
- */
- public function getTableName($table)
- {
- // 结果
- return isset($this->tableColumn[$table]['_table_name']) ? $this->tableColumn[$table]['_table_name'] : $table;
- }
- /**
- * 获取最后一次修改值的信息
- * @param String $table 操作表
- * @param Int $id 操作ID
- *
- */
- public function getLastByValue($table, $id, $column, $value)
- {
- // 查询返回结果
- $data = $this->query()->where([['table_name', '=', $table], ['primary_id', '=', $id], ['column_name', '=', $column], ['after_value', '=', $value]])->orderByDesc('update_time')->first(['admin_uid', 'update_time']);
- // 返回结果
- return $data ? $data->toArray() : [];
- }
- }
|