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 merchant order
type MerchantOrderController struct {
	metadata interface{} // 中继元数据
	beego.Controller
	LogID string
}

// @Title add data api order
// @Description 添加订单
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   merchant_id    query uint64   true "商户id"
// @Param   combo_id    query uint64   true "套餐id"
// @Success 200 	{object} params.param_v1_0.ManagementAddOrderResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_order [post]
func (u *MerchantOrderController) ManagementAddOrder() {
	u.LogID = fmt.Sprintf("ManagementAddOrder[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementAddOrderReq{}
		mreq.MerchantId = req.MerchantId
		mreq.DataApiComboId = req.DataApiComboId

		reply, err := rpc_apis.Management.ManagementAddOrder(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementAddOrder"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementAddOrderResp{
			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,
	)
}

// @Title get merchant order list
// @Description 获取商户订单列表
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   merchant_id    query uint64   true "商户id"
// @Param   page_number    query int   true "当前页"
// @Param   data_api_name    query string   false "数据api名"
// @Param   status_code    query int   false "状态码"
// @Param   start_time    query string   false "开始时间格式（2022-08-04 10:40:00）"
// @Param   end_time    query string   false "结束时间格式（2022-08-04 10:40:00）"
// @Success 200 	{obj} params.param_v1_0.ManagementGetMerchantOrderListResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_order_list [get]
func (u *MerchantOrderController) ManagementGetMerchantOrderList() {
	u.LogID = fmt.Sprintf("ManagementGetMerchantOrderList[%d]", time.Now().UnixNano())
	req := &param_v1_0.ManagementGetMerchantOrderListReq{}
	getParamsTask := func() error {
		httper.FillRequireParams(u.Ctx, req, u.LogID)
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_management.ManagementGetMerchantOrderListReq{}
		mreq.MerchantId = req.MerchantId
		mreq.PageNumber = req.PageNumber
		mreq.DataApiName = req.DataApiName
		mreq.StatusCode = req.StatusCode
		mreq.StartTime = req.StartTime
		mreq.EndTime = req.EndTime

		reply, err := rpc_apis.Management.ManagementGetMerchantOrderList(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementGetMerchantOrderList"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementGetMerchantOrderListResp{
			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,
	)
}

// @Title set merchant order list
// @Description 修改订单价格
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   order_no    query string   true "订单号"
// @Param   price    query float64   true 订单价格""
// @Success 200 	{object} params.param_v1_0.ManagementSetMerchantOrderPriceResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_order_price [put]
func (u *MerchantOrderController) ManagementSetMerchantOrderPrice() {
	u.LogID = fmt.Sprintf("ManagementSetMerchantOrderPrice[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementChangeDataApiOrderPriceReq{}
		mreq.OrderNo = req.OrderNo
		mreq.Price = req.Price

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

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

// @Title pay order
// @Description 支付订单(未实际支付，仅添加数据库记录)
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   order_no    query string   true "订单号"
// @Param   is_pass    query int   true "是否通过 1 通过 2 不通过"
// @Success 200 	{object} params.param_v1_0.ManagementPayOrderResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_order_pay [post]
func (u *MerchantOrderController) ManagementPayOrder() {
	u.LogID = fmt.Sprintf("ManagementPayOrder[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		var err error
		if req.IsPass == 1 {
			mreq := gd_management.ManagementPayDataApiOrderReq{}
			mreq.OrderNo = req.OrderNo
			_, err = rpc_apis.Management.ManagementPayDataApiOrder(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		} else if req.IsPass == 2 {
			mreq := gd_management.ManagementCancelDataApiOrderReq{}
			mreq.OrderNo = req.OrderNo
			_, err = rpc_apis.Management.ManagementCancelDataApiOrder(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		} else {
			return errors.OrderStatusNoSupport
		}
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementPayOrder"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementPayOrderResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}

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

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