136 lines
3.9 KiB
Go
136 lines
3.9 KiB
Go
|
|
package data
|
||
|
|
|
||
|
|
import (
|
||
|
|
consts "cid/consts/public"
|
||
|
|
dto "cid/model/dto/data"
|
||
|
|
entity "cid/model/entity/data"
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"gitea.com/red-future/common/db/gfdb"
|
||
|
|
"github.com/gogf/gf/v2/database/gdb"
|
||
|
|
)
|
||
|
|
|
||
|
|
var DataFetchLog = new(dataFetchLogDao)
|
||
|
|
|
||
|
|
type dataFetchLogDao struct{}
|
||
|
|
|
||
|
|
// Insert 插入数据获取日志
|
||
|
|
func (d *dataFetchLogDao) Insert(ctx context.Context, log *entity.DataFetchLog) (id int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).Data(&log).Insert()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.LastInsertId()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update 更新数据获取日志
|
||
|
|
func (d *dataFetchLogDao) Update(ctx context.Context, id int64, data map[string]interface{}) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||
|
|
Data(data).
|
||
|
|
OmitEmpty().
|
||
|
|
Where(entity.DataFetchLogCols.Id, id).
|
||
|
|
Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetOne 获取单个数据获取日志
|
||
|
|
func (d *dataFetchLogDao) GetOne(ctx context.Context, req *dto.GetDataFetchLogReq) (res *entity.DataFetchLog, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).Where(entity.DataFetchLogCols.Id, req.Id).One()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Struct(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// Count 获取数据获取日志数量
|
||
|
|
func (d *dataFetchLogDao) Count(ctx context.Context, req *dto.ListDataFetchLogReq) (count int, err error) {
|
||
|
|
return d.buildListFilter(ctx, req).Count()
|
||
|
|
}
|
||
|
|
|
||
|
|
// List 获取数据获取日志列表
|
||
|
|
func (d *dataFetchLogDao) List(ctx context.Context, req *dto.ListDataFetchLogReq) (res []entity.DataFetchLog, total int, err error) {
|
||
|
|
model := d.buildListFilter(ctx, req)
|
||
|
|
model.OrderDesc(entity.DataFetchLogCols.CreatedAt)
|
||
|
|
if req.Page != nil {
|
||
|
|
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||
|
|
}
|
||
|
|
r, total, err := model.AllAndCount(false)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Structs(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// buildListFilter 构建列表查询的过滤条件
|
||
|
|
func (d *dataFetchLogDao) buildListFilter(ctx context.Context, req *dto.ListDataFetchLogReq) *gdb.Model {
|
||
|
|
model := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).Model
|
||
|
|
model.Where(entity.DataFetchLogCols.PlatformId, req.PlatformId)
|
||
|
|
model.Where(entity.DataFetchLogCols.InterfaceId, req.InterfaceId)
|
||
|
|
model.Where(entity.DataFetchLogCols.RequestId, req.RequestId)
|
||
|
|
model.Where(entity.DataFetchLogCols.Status, req.Status)
|
||
|
|
if req.StartTime > 0 {
|
||
|
|
model.WhereGTE(entity.DataFetchLogCols.StartTime, req.StartTime)
|
||
|
|
}
|
||
|
|
if req.EndTime > 0 {
|
||
|
|
model.WhereLTE(entity.DataFetchLogCols.StartTime, req.EndTime)
|
||
|
|
}
|
||
|
|
model.OmitEmptyWhere()
|
||
|
|
return model
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetByRequestId 根据请求ID获取日志
|
||
|
|
func (d *dataFetchLogDao) GetByRequestId(ctx context.Context, requestId string) (res *entity.DataFetchLog, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||
|
|
Where(entity.DataFetchLogCols.RequestId, requestId).
|
||
|
|
One()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Struct(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateStatus 更新日志状态
|
||
|
|
func (d *dataFetchLogDao) UpdateStatus(ctx context.Context, id int64, status string, endTime int64, duration int, responseData, errorMessage string) (rows int64, err error) {
|
||
|
|
updateData := map[string]interface{}{
|
||
|
|
"status": status,
|
||
|
|
}
|
||
|
|
if endTime > 0 {
|
||
|
|
updateData["end_time"] = endTime
|
||
|
|
}
|
||
|
|
if duration > 0 {
|
||
|
|
updateData["duration"] = duration
|
||
|
|
}
|
||
|
|
if responseData != "" {
|
||
|
|
updateData["response_data"] = responseData
|
||
|
|
}
|
||
|
|
if errorMessage != "" {
|
||
|
|
updateData["error_message"] = errorMessage
|
||
|
|
}
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||
|
|
Data(updateData).
|
||
|
|
Where(entity.DataFetchLogCols.Id, id).
|
||
|
|
Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// IncrementRetryCount 增加重试次数
|
||
|
|
func (d *dataFetchLogDao) IncrementRetryCount(ctx context.Context, id int64) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.DataFetchLogTable).
|
||
|
|
Where(entity.DataFetchLogCols.Id, id).
|
||
|
|
Data(gdb.Raw(entity.DataFetchLogCols.RetryCount + " + 1")).
|
||
|
|
Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|