You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

package service
import (
"bkb-seller/global"
"bkb-seller/model"
"bkb-seller/model/request"
"bkb-seller/utils"
"errors"
"time"
)
//@function: CreateSysOperationRecord
//@description: 创建记录
//@param: sysOperationRecord model.SysOperationRecord
//@return: err error
func CreateSysOperationRecord(sysOperationRecord model.SysOperationRecord) (err error) {
err = global.MG_DB.Create(&sysOperationRecord).Error
return err
}
//@function: DeleteSysOperationRecordByIds
//@description: 批量删除记录
//@param: ids request.IdsReq
//@return: err error
func DeleteSysOperationRecordByIds(ids request.IdsReq) (err error) {
err = global.MG_DB.Delete(&[]model.SysOperationRecord{}, "id in (?)", ids.Ids).Error
return err
}
//@function: DeleteSysOperationRecord
//@description: 删除操作记录
//@param: sysOperationRecord model.SysOperationRecord
//@return: err error
func DeleteSysOperationRecord(sysOperationRecord model.SysOperationRecord) (err error) {
err = global.MG_DB.Delete(&sysOperationRecord).Error
return err
}
//@function: DeleteSysOperationRecord
//@description: 根据id获取单条操作记录
//@param: id uint
//@return: err error, sysOperationRecord model.SysOperationRecord
func GetSysOperationRecord(id uint) (err error, sysOperationRecord model.SysOperationRecord) {
err = global.MG_DB.Where("id = ?", id).First(&sysOperationRecord).Error
return
}
//@function: GetSysOperationRecordInfoList
//@description: 分页获取操作记录列表
//@param: info request.SysOperationRecordSearch
//@return: err error, list interface{}, total int64
func GetSysOperationRecordInfoList(info request.SysOperationRecordSearch) (err error, list interface{}, total int64) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
// 创建db
db := global.MG_DB.Model(&model.SysOperationRecord{}).
Joins("LEFT JOIN sys_users as u ON u.id=sys_operation_records.user_id")
var sysOperationRecords []model.SysOperationRecord
// 如果有条件搜索 下方会自动创建搜索语句
if info.Method != "" {
db = db.Where("sys_operation_records.method = ?", info.Method)
}
if info.Path != "" {
db = db.Where("sys_operation_records.path LIKE ?", "%"+info.Path+"%")
}
if info.UserID != 0 {
db = db.Where("sys_operation_records.user_id = ?", info.UserID)
}
if info.NickName != "" {
db = db.Where("u.nick_name = ?", info.NickName)
}
if info.CreatedAtStart != "" {
db = db.Where("sys_operation_records.created_at >= ?", info.CreatedAtStart)
}
if info.CreatedAtEnd != "" {
var tmp time.Time
tmp, err = time.Parse(utils.DateTimeFormat, info.CreatedAtEnd)
if err != nil {
err = errors.New("创建截止时间格式错误")
return err, nil, 0
}
end := tmp.AddDate(0, 0, 1).Format(utils.DateTimeFormat)
db = db.Where("sys_operation_records.created_at < ?", end)
}
if info.Status != 0 {
db = db.Where("sys_operation_records.status = ?", info.Status)
}
err = db.Count(&total).Error
err = db.Select("sys_operation_records.id,sys_operation_records.ip,sys_operation_records.method,sys_operation_records.path,sys_operation_records.status,sys_operation_records.agent,sys_operation_records.user_id,sys_operation_records.created_at,sys_operation_records.updated_at").Order("sys_operation_records.id desc").Limit(limit).Offset(offset).Preload("User").Find(&sysOperationRecords).Error
// for i := 0; i < len(sysOperationRecords); i++ {
// var api model.SysApi
// _, api = GetApiByPathMethod(sysOperationRecords[i].Path, sysOperationRecords[i].Method)
// sysOperationRecords[i].ApiDescription = api.Description
// }
return err, sysOperationRecords, total
}