package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
)

func GetApiLogInfoForReq(req string) (error, model.ApiLog) {
	data := new(model.ApiLog)

	sqlStr := "SELECT id, create_time " +
		" FROM api_log" +
		" where req=?"

	global.GVA_DB.Raw(sqlStr, req).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, model.ApiLog{}
	}
	return nil, *data
}

func GetApiLogInfo(id uint) (error, model.ApiLog) {
	data := new(model.ApiLog)

	sqlStr := "SELECT id, create_time " +
		" FROM api_log" +
		" where id=?"

	global.GVA_DB.Raw(sqlStr, id).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, model.ApiLog{}
	}
	return nil, *data
}

func CreateApiLog(apiLog model.ApiLog) (uint, error) {
	err := global.GVA_DB.Table("api_log").Create(&apiLog).Error
	return apiLog.Id, err
}
