sys_operation_record.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // 自动生成模板SysOperationRecord
  2. package dao
  3. import (
  4. "time"
  5. "server/global"
  6. )
  7. // 如果含有time.Time 请自行import time包
  8. type SysOperationRecord struct {
  9. global.GVA_MODEL
  10. Ip string `json:"ip" form:"ip" gorm:"column:ip;comment:请求ip"` // 请求ip
  11. Method string `json:"method" form:"method" gorm:"column:method;comment:请求方法"` // 请求方法
  12. Path string `json:"path" form:"path" gorm:"column:path;comment:请求路径"` // 请求路径
  13. Status int `json:"status" form:"status" gorm:"column:status;comment:请求状态"` // 请求状态
  14. Latency time.Duration `json:"latency" form:"latency" gorm:"column:latency;comment:延迟" swaggertype:"string"` // 延迟
  15. Agent string `json:"agent" form:"agent" gorm:"type:text;column:agent;comment:代理"` // 代理
  16. ErrorMessage string `json:"error_message" form:"error_message" gorm:"column:error_message;comment:错误信息"` // 错误信息
  17. Body string `json:"body" form:"body" gorm:"type:text;column:body;comment:请求Body"` // 请求Body
  18. Resp string `json:"resp" form:"resp" gorm:"type:text;column:resp;comment:响应Body"` // 响应Body
  19. UserID int `json:"user_id" form:"user_id" gorm:"column:user_id;comment:用户id"` // 用户id
  20. User SysUser `json:"user"`
  21. }
  22. // TODO:操作查询
  23. // GetSysOperationRecord 根据id获取单条操作记录
  24. func GetSysOperationRecord(id uint) (sysOperationRecord SysOperationRecord, err error) {
  25. err = global.GVA_DB.Where("id = ?", id).First(&sysOperationRecord).Error
  26. return
  27. }
  28. // GetSysOperationRecordInfoList 分页获取操作记录列表
  29. func (sor SysOperationRecord) GetSysOperationRecordInfoList(limit, offset int) (sysOperationRecords []SysOperationRecord, total int64, err error) {
  30. // 创建db
  31. db := global.GVA_DB.Model(&SysOperationRecord{})
  32. // 如果有条件搜索 下方会自动创建搜索语句
  33. if sor.Method != "" {
  34. db = db.Where("method = ?", sor.Method)
  35. }
  36. if sor.Path != "" {
  37. db = db.Where("path LIKE ?", "%"+sor.Path+"%")
  38. }
  39. if sor.Status != 0 {
  40. db = db.Where("status = ?", sor.Status)
  41. }
  42. err = db.Count(&total).Error
  43. if err != nil {
  44. return
  45. }
  46. err = db.Order("id desc").Limit(limit).Offset(offset).Preload("User").Find(&sysOperationRecords).Error
  47. return sysOperationRecords, total, err
  48. }
  49. // TODO:操作新增
  50. func (sor SysOperationRecord) CreateSysOperationRecord() error {
  51. return global.GVA_DB.Create(&sor).Error
  52. }
  53. // TODO:操作修改
  54. // TODO:操作删除
  55. // DeleteSysOperationRecordByIds 删除操作记录按id
  56. func DeleteSysOperationRecordByIds(ids []int) (err error) {
  57. err = global.GVA_DB.Delete(&[]SysOperationRecord{}, "id in (?)", ids).Error
  58. return err
  59. }
  60. // DeleteSysOperationRecord 删除操作记录
  61. func (sor SysOperationRecord) DeleteSysOperationRecord() (err error) {
  62. err = global.GVA_DB.Delete(&sor).Error
  63. return err
  64. }