package controller

import (
	"cold-delivery/common/actions"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"gogs.baozhida.cn/zoie/OAuth-core/api"
	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"

	"cold-delivery/app/admin/model"
	"cold-delivery/app/admin/service"
	"cold-delivery/app/admin/service/dto"
)

type SysOperaLogController struct {
	api.Api
}

// GetPage 操作日志列表
// @Summary 操作日志列表
// @Description 操作日志列表
// @Tags 操作日志
// @Param title query string false "title"
// @Param method query string false "method"
// @Param requestMethod  query string false "requestMethod"
// @Param operaUrl query string false "operaUrl"
// @Param operaIp query string false "operaIp"
// @Param status query string false "status"
// @Param beginTime query string false "beginTime"
// @Param endTime query string false "endTime"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.SysOperaLog}} "{"code": 200, "data": [...]}"
// @Router /api/sys-opera-log [get]
// @Security Bearer
func (e SysOperaLogController) GetPage(c *gin.Context) {
	s := service.SysOperaLog{}
	req := new(dto.SysOperaLogGetPageReq)
	err := e.MakeContext(c).
		MakeOrm().
		Bind(req, binding.Form).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	list := make([]model.SysOperaLog, 0)
	var count int64
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	err = s.GetPage(req, &list, &count, p)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "查询失败")
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 通过id获取操作日志
// @Summary 通过id获取操作日志
// @Description 通过id获取操作日志
// @Tags 操作日志
// @Param id path string true "操作日志id"
// @Success 200 {object} response.Response{data=model.SysOperaLog} "{"code": 200, "data": [...]}"
// @Router /api/sys-opera-log/{id} [get]
// @Security Bearer
func (e SysOperaLogController) Get(c *gin.Context) {
	s := new(service.SysOperaLog)
	req := dto.SysOperaLogGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	var object model.SysOperaLog
	err = s.Get(&req, &object, p)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "查询失败")
		return
	}
	e.OK(object, "查询成功")
}

// Delete 删除操作日志
// @Summary 删除操作日志
// @Description 删除操作日志
// @Tags 操作日志
// @Param data body dto.SysOperaLogDeleteReq true "body"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/sys-opera-log [delete]
// @Security Bearer
func (e SysOperaLogController) Delete(c *gin.Context) {
	s := new(service.SysOperaLog)
	req := dto.SysOperaLogDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	err = s.Remove(&req, p)
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, fmt.Sprintf("删除失败！错误详情：%s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}
