package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
)

type LinePriceLimit struct {
	api.Api
}

// GetPage 获取涨跌幅列表
// @Summary 获取涨跌幅列表
// @Description 获取涨跌幅列表
// @Tags 涨跌幅
// @Param symbol query string false "交易对"
// @Param type query string false "类型:1=现货,2=合约"
// @Param directionStatus query string false "方向:1=涨,2=跌"
// @Param range query string false "幅度"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.LinePriceLimit}} "{"code": 200, "data": [...]}"
// @Router /api/v1/line-price-limit [get]
// @Security Bearer
func (e LinePriceLimit) GetPage(c *gin.Context) {
	req := dto.LinePriceLimitGetPageReq{}
	s := service.LinePriceLimit{}
	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)
	list := make([]models.LinePriceLimit, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取涨跌幅失败，\r\n失败信息 %s", err.Error()))
		return
	}

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

// Get 获取涨跌幅
// @Summary 获取涨跌幅
// @Description 获取涨跌幅
// @Tags 涨跌幅
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.LinePriceLimit} "{"code": 200, "data": [...]}"
// @Router /api/v1/line-price-limit/{id} [get]
// @Security Bearer
func (e LinePriceLimit) Get(c *gin.Context) {
	req := dto.LinePriceLimitGetReq{}
	s := service.LinePriceLimit{}
	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
	}
	var object models.LinePriceLimit

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取涨跌幅失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建涨跌幅
// @Summary 创建涨跌幅
// @Description 创建涨跌幅
// @Tags 涨跌幅
// @Accept application/json
// @Product application/json
// @Param data body dto.LinePriceLimitInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/line-price-limit [post]
// @Security Bearer
func (e LinePriceLimit) Insert(c *gin.Context) {
	req := dto.LinePriceLimitInsertReq{}
	s := service.LinePriceLimit{}
	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
	}
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建涨跌幅失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改涨跌幅
// @Summary 修改涨跌幅
// @Description 修改涨跌幅
// @Tags 涨跌幅
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.LinePriceLimitUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/line-price-limit/{id} [put]
// @Security Bearer
func (e LinePriceLimit) Update(c *gin.Context) {
	req := dto.LinePriceLimitUpdateReq{}
	s := service.LinePriceLimit{}
	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
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改涨跌幅失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除涨跌幅
// @Summary 删除涨跌幅
// @Description 删除涨跌幅
// @Tags 涨跌幅
// @Param data body dto.LinePriceLimitDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/line-price-limit [delete]
// @Security Bearer
func (e LinePriceLimit) Delete(c *gin.Context) {
	s := service.LinePriceLimit{}
	req := dto.LinePriceLimitDeleteReq{}
	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
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除涨跌幅失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e LinePriceLimit) UpRange(c *gin.Context) {
	s := service.LinePriceLimit{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	err = s.UpRange()
	if err != nil {
		e.Error(500, err, fmt.Sprintf("更新涨跌幅失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "操作成功")
}
