[ '_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 = '
' . $value['admin_name'] . ' ' . date('m-d H:i', $value['update_time']) . ' ' . $value['notes_type'] . ' ' . $value['column_name'] . '
'; // 重组 $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() : []; } }