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 LineStrategyTemplate struct {
	api.Api
}

// GetPage 获取波段策略列表
// @Summary 获取波段策略列表
// @Description 获取波段策略列表
// @Tags 波段策略
// @Param direction query int false "涨跌方向 1-涨 2-跌"
// @Param percentag query decimal.Decimal false "涨跌点数"
// @Param compareType query int false "比较类型 1-大于 2-大于等于 3-小于 4-小于等于 5等于 "
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.LineStrategyTemplate}} "{"code": 200, "data": [...]}"
// @Router /api/v1/line-strategy-template [get]
// @Security Bearer
func (e LineStrategyTemplate) GetPage(c *gin.Context) {
	req := dto.LineStrategyTemplateGetPageReq{}
	s := service.LineStrategyTemplate{}
	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.LineStrategyTemplate, 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.LineStrategyTemplate} "{"code": 200, "data": [...]}"
// @Router /api/v1/line-strategy-template/{id} [get]
// @Security Bearer
func (e LineStrategyTemplate) Get(c *gin.Context) {
	req := dto.LineStrategyTemplateGetReq{}
	s := service.LineStrategyTemplate{}
	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.LineStrategyTemplate

	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.LineStrategyTemplateInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/line-strategy-template [post]
// @Security Bearer
func (e LineStrategyTemplate) Insert(c *gin.Context) {
	req := dto.LineStrategyTemplateInsertReq{}
	s := service.LineStrategyTemplate{}
	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
	}

	if err := req.Valid(); err != nil {
		e.Error(500, err, "")
		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.LineStrategyTemplateUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/line-strategy-template/{id} [put]
// @Security Bearer
func (e LineStrategyTemplate) Update(c *gin.Context) {
	req := dto.LineStrategyTemplateUpdateReq{}
	s := service.LineStrategyTemplate{}
	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
	}

	if err := req.Valid(); err != nil {
		e.Error(500, err, "")
		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.LineStrategyTemplateDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/line-strategy-template [delete]
// @Security Bearer
func (e LineStrategyTemplate) Delete(c *gin.Context) {
	s := service.LineStrategyTemplate{}
	req := dto.LineStrategyTemplateDeleteReq{}
	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(), "删除成功")
}
