2022-07-14 11:39:12 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-07-14 11:39:12 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-01-14 16:29:52 +08:00
|
|
|
|
"mayfly-go/pkg/req"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
2022-07-14 11:39:12 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Syslog interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2022-07-14 11:39:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 从请求上下文的参数保存系统日志
|
2023-01-14 16:29:52 +08:00
|
|
|
|
SaveFromReq(req *req.Ctx)
|
2022-07-14 11:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
func newSyslogApp(syslogRepo repository.Syslog) Syslog {
|
|
|
|
|
|
return &syslogAppImpl{
|
|
|
|
|
|
syslogRepo: syslogRepo,
|
|
|
|
|
|
}
|
2022-07-14 11:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type syslogAppImpl struct {
|
|
|
|
|
|
syslogRepo repository.Syslog
|
2022-07-14 11:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (m *syslogAppImpl) GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
2022-07-14 11:39:12 +08:00
|
|
|
|
return m.syslogRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 16:29:52 +08:00
|
|
|
|
func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) {
|
2022-07-14 11:39:12 +08:00
|
|
|
|
lg := req.LoginAccount
|
|
|
|
|
|
if lg == nil {
|
2023-07-03 21:42:04 +08:00
|
|
|
|
lg = &model.LoginAccount{Id: 0, Username: "-"}
|
2022-07-14 11:39:12 +08:00
|
|
|
|
}
|
2023-07-08 20:05:55 +08:00
|
|
|
|
syslog := new(entity.SysLog)
|
2022-07-14 11:39:12 +08:00
|
|
|
|
syslog.CreateTime = time.Now()
|
|
|
|
|
|
syslog.Creator = lg.Username
|
|
|
|
|
|
syslog.CreatorId = lg.Id
|
2023-07-08 20:05:55 +08:00
|
|
|
|
syslog.Description = req.GetLogInfo().Description
|
2022-07-14 11:39:12 +08:00
|
|
|
|
|
2023-07-08 20:05:55 +08:00
|
|
|
|
if req.GetLogInfo().LogResp {
|
2022-07-14 11:39:12 +08:00
|
|
|
|
respB, _ := json.Marshal(req.ResData)
|
|
|
|
|
|
syslog.Resp = string(respB)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
reqParam := req.ReqParam
|
2023-07-21 17:07:04 +08:00
|
|
|
|
if !anyx.IsBlank(reqParam) {
|
2022-07-14 11:39:12 +08:00
|
|
|
|
// 如果是字符串类型,则不使用json序列化
|
|
|
|
|
|
if reqStr, ok := reqParam.(string); ok {
|
|
|
|
|
|
syslog.ReqParam = reqStr
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reqB, _ := json.Marshal(reqParam)
|
|
|
|
|
|
syslog.ReqParam = string(reqB)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := req.Err; err != nil {
|
|
|
|
|
|
syslog.Type = entity.SyslogTypeError
|
|
|
|
|
|
var errMsg string
|
|
|
|
|
|
switch t := err.(type) {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
case errorx.BizError:
|
2022-07-14 11:39:12 +08:00
|
|
|
|
errMsg = fmt.Sprintf("errCode: %d, errMsg: %s", t.Code(), t.Error())
|
|
|
|
|
|
case error:
|
|
|
|
|
|
errMsg = t.Error()
|
|
|
|
|
|
}
|
|
|
|
|
|
syslog.Resp = errMsg
|
|
|
|
|
|
} else {
|
|
|
|
|
|
syslog.Type = entity.SyslogTypeNorman
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m.syslogRepo.Insert(syslog)
|
|
|
|
|
|
}
|