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

// @Title merchant add h5 service
// @Description 商户添加h5 服务
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   merchant_id    query uint64   true "商户id"
// @Param   combo_id    query int   true "套餐id"
// @Param   theme_color    query string   true "主题颜色"
// @Success 200 	{object} params.param_v1_0.ManagementAddMerchantH5ServiceResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_h5_service [post]
func (u *MerchantH5Controller) ManagementAddMerchantH5Service() {
	u.LogID = fmt.Sprintf("ManagementAddMerchantH5Service[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementAddMerchantH5ServiceReq{}
		mreq.MerchantId = req.MerchantId
		mreq.ComboId = req.ComboId
		fmt.Printf("!!!!!!!:%v\n", req.ComboId)
		mreq.ThemeColor = req.ThemeColor

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

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

// @Title merchant get h5 service list
// @Description 商户获取h5 服务列表
// @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   is_all    query string   true "是否返回所有"
// @Success 200 	{object} params.param_v1_0.ManagementGetMerchantH5ServiceResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_h5_service_list [get]
func (u *MerchantH5Controller) ManagementGetMerchantH5Service() {
	u.LogID = fmt.Sprintf("ManagementGetMerchantH5Service[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementGetMerchantH5ServiceReq{}
		mreq.MerchantId = req.MerchantId
		mreq.PageNumber = req.PageNumber
		mreq.IsAll = req.IsAll

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

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

// @Title merchant get h5 combo
// @Description 商户获取h5 套餐信息
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   merchant_h5_service_id    query uint64   true "商户h5服务id"
// @Success 200 	{object} params.param_v1_0.ManagementGetMerchantH5ComboResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_h5_service_combo [get]
func (u *MerchantH5Controller) ManagementGetMerchantH5Combo() {
	u.LogID = fmt.Sprintf("ManagementGetMerchantH5Combo[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementGetMerchantH5ComboReq{}
		mreq.MerchantH5ServiceId = req.MerchantH5ServiceId

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

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

// @Title merchant set h5 combo
// @Description 商户设置h5 套餐信息
// @Param   token    header   string true "token"
// @Param   uid    header   string true "管理员id"
// @Param   merchant_h5_service_id    query uint64   true "商户h5服务id"
// @Param day_count query int true "单天次数"
// @Param count query int true "总调用次数"
// @Param day_number query int true "有效天数"
// @Success 200 	{object} params.param_v1_0.ManagementSetMerchantH5ComboResp "响应信息"
// @Failure 500 	服务器错误
// @router /merchant_h5_service_combo [put]
func (u *MerchantH5Controller) ManagementSetMerchantH5Combo() {
	u.LogID = fmt.Sprintf("ManagementSetMerchantH5Combo[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementSetMerchantH5ComboReq{}
		mreq.MerchantH5ServiceId = req.MerchantH5ServiceId
		mreq.DayNumber = req.DayNumber
		mreq.DayCount = req.DayCount
		mreq.Count = req.Count

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