package service

import (
	"pure-admin/global"
	"pure-admin/model"
	"pure-admin/model/request"
)

//@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, appid string) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.MG_DB.Model(&model.SysOperationRecord{}).Where("sys_operation_records.appid=?", appid).
		Joins("LEFT JOIN user 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 != "" {
		db = db.Where("sys_operation_records.created_at < ?", info.CreatedAtEnd)
	}
	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, appid)
		sysOperationRecords[i].ApiDescription = api.Description
	}
	return err, sysOperationRecords, total
}