package controller

import (
	"cold-delivery/common/actions"
	"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/jwtauth/user"
	_ "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 SysLoginLogController struct {
	api.Api
}

// GetPage 登录日志列表
// @Summary 登录日志列表
// @Description 登录日志列表
// @Tags 登录日志
// @Param username query string false "用户名"
// @Param ipaddr query string false "ip地址"
// @Param loginLocation  query string false "归属地"
// @Param status query string false "状态"
// @Param beginTime query string false "开始时间"
// @Param endTime query string false "结束时间"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.SysLoginLog}} "{"code": 200, "data": [...]}"
// @Router /api/sys-login-log [get]
// @Security Bearer
func (e SysLoginLogController) GetPage(c *gin.Context) {
	s := service.SysLoginLogService{}
	req := dto.SysLoginLogGetPageReq{}
	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
	}
	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	list := make([]model.SysLoginLog, 0)
	var count int64
	err = s.GetPage(&req, &list, &count, p)
	if err != nil {
		e.Error(500, err, err.Error())
		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.SysLoginLog} "{"code": 200, "data": [...]}"
// @Router /api/sys-login-log/{id} [get]
// @Security Bearer
func (e SysLoginLogController) Get(c *gin.Context) {
	s := service.SysLoginLogService{}
	req := dto.SysLoginLogGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		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.SysLoginLog
	err = s.Get(&req, &object, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(object, "查询成功")
}

// Delete 删除登录日志
// @Summary 删除登录日志
// @Description 删除登录日志
// @Tags 登录日志
// @Param data body dto.SysLoginLogDeleteReq true "body"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/sys-login-log [delete]
// @Security Bearer
func (e SysLoginLogController) Delete(c *gin.Context) {
	s := service.SysLoginLogService{}
	req := dto.SysLoginLogDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	req.SetUpdateBy(user.GetUserId(c))
	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.GetId(), "删除成功")
}
