update log service
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
|
|
||||||
type ConfigApi struct {
|
type ConfigApi struct {
|
||||||
configService service.ConfigService
|
configService service.ConfigService
|
||||||
|
logService service.LogService
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfigValue 根据参数键名查询参数值
|
// GetConfigValue 根据参数键名查询参数值
|
||||||
@@ -35,6 +36,9 @@ func (a ConfigApi) GetConfigValue(c *gin.Context) {
|
|||||||
|
|
||||||
// List 查询设置列表
|
// List 查询设置列表
|
||||||
func (a ConfigApi) List(c *gin.Context) {
|
func (a ConfigApi) List(c *gin.Context) {
|
||||||
|
a.logService.LogStart(c)
|
||||||
|
a.logService.LogToDB(c, "日志测试 start")
|
||||||
|
|
||||||
query := request.ConfigQuery{}
|
query := request.ConfigQuery{}
|
||||||
if c.Bind(&query) != nil {
|
if c.Bind(&query) != nil {
|
||||||
resp.ParamError(c)
|
resp.ParamError(c)
|
||||||
@@ -46,6 +50,7 @@ func (a ConfigApi) List(c *gin.Context) {
|
|||||||
Total: i,
|
Total: i,
|
||||||
Size: query.PageSize,
|
Size: query.PageSize,
|
||||||
})
|
})
|
||||||
|
a.logService.LogToDB(c, "日志测试 end")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add 添加数据
|
// Add 添加数据
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package request
|
||||||
|
|
||||||
|
import "cutego/pkg/base"
|
||||||
|
|
||||||
|
type LogQuery struct {
|
||||||
|
base.GlobalQuery
|
||||||
|
Uid string `form:"uid"`
|
||||||
|
Content string `form:"content"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package dao
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cutego/core/api/v1/request"
|
||||||
|
"cutego/core/entity"
|
||||||
|
"cutego/pkg/common"
|
||||||
|
"cutego/pkg/page"
|
||||||
|
"github.com/druidcaesa/gotool"
|
||||||
|
"github.com/go-xorm/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogDao struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d LogDao) sql(session *xorm.Session) *xorm.Session {
|
||||||
|
return session.Table("sys_log")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectPage 分页查询数据
|
||||||
|
func (d LogDao) SelectPage(query request.LogQuery) ([]entity.SysLog, int64) {
|
||||||
|
configs := make([]entity.SysLog, 0)
|
||||||
|
session := d.sql(SqlDB.NewSession())
|
||||||
|
if gotool.StrUtils.HasNotEmpty(query.Content) {
|
||||||
|
session.And("content like concat('%', ?, '%')", query.Content)
|
||||||
|
}
|
||||||
|
if gotool.StrUtils.HasNotEmpty(query.Uid) {
|
||||||
|
session.And("uid = ?", query.Uid)
|
||||||
|
}
|
||||||
|
total, _ := page.GetTotal(session.Clone())
|
||||||
|
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&configs)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorLog(err)
|
||||||
|
return nil, 0
|
||||||
|
}
|
||||||
|
return configs, total
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert 添加数据
|
||||||
|
func (d LogDao) Insert(config entity.SysLog) int64 {
|
||||||
|
session := SqlDB.NewSession()
|
||||||
|
session.Begin()
|
||||||
|
insert, err := session.Insert(&config)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorLog(err)
|
||||||
|
session.Rollback()
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
session.Commit()
|
||||||
|
return insert
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SysLog struct {
|
||||||
|
Id int64 `xorm:"pk autoincr" json:"id"` // 主键
|
||||||
|
Uid string `xorm:"uid" json:"uid"` // 唯一主键
|
||||||
|
Content string `xorm:"varchar(255)" json:"content"` // 记录内容
|
||||||
|
CreateTime time.Time `xorm:"created"` // 创建时间
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c SysLog) TableName() string {
|
||||||
|
return "sys_log"
|
||||||
|
}
|
||||||
@@ -7,35 +7,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CronJobService struct {
|
type CronJobService struct {
|
||||||
cronJobService dao.CronJobDao
|
cronJobDao dao.CronJobDao
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindPage 分页查询数据
|
// FindPage 分页查询数据
|
||||||
func (s CronJobService) FindPage(query request.CronJobQuery) ([]entity.SysCronJob, int64) {
|
func (s CronJobService) FindPage(query request.CronJobQuery) ([]entity.SysCronJob, int64) {
|
||||||
return s.cronJobService.SelectPage(query)
|
return s.cronJobDao.SelectPage(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save 添加数据
|
// Save 添加数据
|
||||||
func (s CronJobService) Save(config entity.SysCronJob) int64 {
|
func (s CronJobService) Save(config entity.SysCronJob) int64 {
|
||||||
return s.cronJobService.Insert(config)
|
return s.cronJobDao.Insert(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInfo 查询数据
|
// GetInfo 查询数据
|
||||||
func (s CronJobService) GetInfo(id int64) *entity.SysCronJob {
|
func (s CronJobService) GetInfo(id int64) *entity.SysCronJob {
|
||||||
return s.cronJobService.SelectById(id)
|
return s.cronJobDao.SelectById(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInfo 查询数据
|
// GetInfo 查询数据
|
||||||
func (s CronJobService) GetInfoByAlias(funcAlias string) *entity.SysCronJob {
|
func (s CronJobService) GetInfoByAlias(funcAlias string) *entity.SysCronJob {
|
||||||
return s.cronJobService.SelectByFuncAlias(funcAlias)
|
return s.cronJobDao.SelectByFuncAlias(funcAlias)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit 修改数据
|
// Edit 修改数据
|
||||||
func (s CronJobService) Edit(config entity.SysCronJob) int64 {
|
func (s CronJobService) Edit(config entity.SysCronJob) int64 {
|
||||||
return s.cronJobService.Update(config)
|
return s.cronJobDao.Update(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove 批量删除
|
// Remove 批量删除
|
||||||
func (s CronJobService) Remove(list []int64) bool {
|
func (s CronJobService) Remove(list []int64) bool {
|
||||||
return s.cronJobService.Delete(list)
|
return s.cronJobDao.Delete(list)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cutego/core/api/v1/request"
|
||||||
|
"cutego/core/dao"
|
||||||
|
"cutego/core/entity"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-basic/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const CURRENT_UID_KEY = "CURRENT_LOG_UID"
|
||||||
|
const DEFAULT_UID_KEY = "DEFAULT_LOG_UID"
|
||||||
|
|
||||||
|
type LogService struct {
|
||||||
|
logDao dao.LogDao
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindPage 分页查询数据
|
||||||
|
func (s LogService) FindPage(query request.LogQuery) ([]entity.SysLog, int64) {
|
||||||
|
return s.logDao.SelectPage(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save 添加数据
|
||||||
|
func (s LogService) save(config entity.SysLog) int64 {
|
||||||
|
return s.logDao.Insert(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogToDB 记录日志
|
||||||
|
func (s LogService) LogToDB(c *gin.Context, content string) {
|
||||||
|
uid, exists := c.Get(CURRENT_UID_KEY)
|
||||||
|
if !exists {
|
||||||
|
uid = DEFAULT_UID_KEY
|
||||||
|
}
|
||||||
|
uidStr := uid.(string)
|
||||||
|
s.save(entity.SysLog{Uid: uidStr, Content: content})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始记录日志前调用(只调用一次)
|
||||||
|
func (s LogService) LogStart(c *gin.Context) {
|
||||||
|
c.Set(CURRENT_UID_KEY, uuid.New())
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ require (
|
|||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/druidcaesa/gotool v0.0.0-20210722000450-b972ac6f3771
|
github.com/druidcaesa/gotool v0.0.0-20210722000450-b972ac6f3771
|
||||||
github.com/gin-gonic/gin v1.7.3
|
github.com/gin-gonic/gin v1.7.3
|
||||||
|
github.com/go-basic/uuid v1.0.0 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.6.0
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
github.com/go-xorm/xorm v0.7.9
|
github.com/go-xorm/xorm v0.7.9
|
||||||
github.com/gomodule/redigo v1.8.5
|
github.com/gomodule/redigo v1.8.5
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
|
|||||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
github.com/gin-gonic/gin v1.7.3 h1:aMBzLJ/GMEYmv1UWs2FFTcPISLrQH2mRgL9Glz8xows=
|
github.com/gin-gonic/gin v1.7.3 h1:aMBzLJ/GMEYmv1UWs2FFTcPISLrQH2mRgL9Glz8xows=
|
||||||
github.com/gin-gonic/gin v1.7.3/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
github.com/gin-gonic/gin v1.7.3/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||||
|
github.com/go-basic/uuid v1.0.0 h1:Faqtetcr8uwOzR2qp8RSpkahQiv4+BnJhrpuXPOo63M=
|
||||||
|
github.com/go-basic/uuid v1.0.0/go.mod h1:yVtVnsXcmaLc9F4Zw7hTV7R0+vtuQw00mdXi+F6tqco=
|
||||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||||
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||||
|
|||||||
Reference in New Issue
Block a user