package ctrl_v1

import (
	"gd_management_gateway/errors"
	"gd_management_gateway/params/param_base"
	"gd_management_gateway/params/param_v1_0"
	"gd_management_gateway/rpc_apis"
	"gd_management_gateway/rpc_apis/gd_management"
	"fmt"
	"time"

	"gd_management_gateway/common.in/httper"
	"gd_management_gateway/common.in/task"
	"gd_management_gateway/common.in/utils"
	"go.uber.org/zap"

	"github.com/astaxie/beego"
)

// Operations about error code
type ErrorController struct {
	metadata interface{} // 中继元数据
	beego.Controller
	LogID string
}

// @Title add error code
// @Description 添加错误码
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   code    query   string true "错误码"
// @Param   msg    query   string true "错误消息"
// @Success 200 	{object} params.param_v1_0.ManagementAddErrorResp "响应信息"
// @Failure 500 	服务器错误
// @router /error_code [post]
func (u *ErrorController) ManagementAddError() {
	u.LogID = fmt.Sprintf("ManagementAddError[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementAddErrorReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementAddErrorReq{}
		mreq.Code = req.Code
		mreq.Msg = req.Msg

		reply, err := rpc_apis.Management.ManagementAddError(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementAddError"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementAddErrorResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data = reply

		httper.JSON(u.Ctx, resp, u.LogID)
		SetErrorCodeMap()
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title del error code
// @Description 删除错误码
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   id    query   int true "错误码id"
// @Success 200 	{object} params.param_v1_0.ManagementDelErrorResp "响应信息"
// @Failure 500 	服务器错误
// @router /error_code [delete]
func (u *ErrorController) ManagementDelError() {
	u.LogID = fmt.Sprintf("ManagementDelError[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementDelErrorReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		fmt.Printf("del request:%v\n", req)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementDelErrorReq{}
		mreq.Id = req.Id
		_, err := rpc_apis.Management.ManagementDelError(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementDelError"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementDelErrorResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}

		httper.JSON(u.Ctx, resp, u.LogID)
		SetErrorCodeMap()
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title update error code
// @Description 修改错误码
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   id    query   int true "错误码id"
// @Param   code    query   string true "错误码"
// @Param   msg    query   string true "错误消息"
// @Success 200 	{object} params.param_v1_0.ManagementUpdateErrorResp "响应信息"
// @Failure 500 	服务器错误
// @router /error_code [put]
func (u *ErrorController) ManagementUpdateError() {
	u.LogID = fmt.Sprintf("ManagementUpdateError[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementUpdateErrorReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementUpdateErrorReq{}
		mreq.Code = req.Code
		mreq.Msg = req.Msg
		mreq.Id = req.Id
		_, err := rpc_apis.Management.ManagementUpdateError(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementUpdateError"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementUpdateErrorResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}

		httper.JSON(u.Ctx, resp, u.LogID)
		SetErrorCodeMap()
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}

// @Title get error code list
// @Description 获取错误码列表
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   page_number    query   int true "当前页"
// @Param   is_all    query   bool false "是否全量返回"
// @Success 200 	{object} params.param_v1_0.ManagementGetErrorListResp "响应信息"
// @Failure 500 	服务器错误
// @router /error_code_list [get]
func (u *ErrorController) ManagementGetErrorList() {
	u.LogID = fmt.Sprintf("ManagementGetErrorList[%d]", time.Now().UnixNano())

	req := &param_v1_0.ManagementGetErrorListReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementGetErrorListReq{}
		mreq.PageNumber = req.PageNumber
		mreq.IsAll = req.IsAll
		reply, err := rpc_apis.Management.ManagementGetErrorList(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementGetErrorList"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementGetErrorListResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data = reply

		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

	// 执行以上定义的任务
	task.Do(&task.PanicRecover{
		Ctx:   u.Ctx,
		LogID: u.LogID}, u.Ctx,
		getParamsTask, handleDataTask,
	)
}
