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"
	"gd_management_gateway/rpc_apis/gd_service"
	"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 base api
type ServiceOrderController struct {
	metadata interface{} // 中继元数据
	beego.Controller
	LogID string
}

// @Title get service order list
// @Description 获取服务订单
// @Param   token    header   string true "token"
// @Param   uid    header   string true "uid"
// @Param   page_number    query   string false ""
// @Param   order_status    query   int false "0 获取所有状态订单 1 待支付，2 进行中 3已完成 4 已取消"
// @Param   order_type    query   int false "0 所有服务类型 311上线年审，312 免检年审，314免检年审在线办理， 315 上线年审自行预约"
// @Param   start_time    query   string false "uid"
// @Param   end_time    query   string false "uid"
// @Success 200 	{object} params.param_v1_0.ServiceManagementOrderListResp "响应信息"
// @Failure 500 	服务器错误
// @router /service_order_list [get]
func (u *ServiceOrderController) ServiceManagementOrderList() {
	u.LogID = fmt.Sprintf("ServiceManagementOrderList[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		sreq := gd_service.ServiceManagementOrderListReq{}
		sreq.PageNumber = req.PageNumber
		sreq.StartTime = req.StartTime
		sreq.EndTime = req.EndTime
		sreq.OrderType = req.OrderType
		sreq.OrderStatus = req.OrderStatus

		reply, err := rpc_apis.Service.ServiceManagementOrderList(utils.NewContextFromBeegoCtx(u.Ctx), &sreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ServiceManagementOrderList"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		appKeys := []string{}
		for _, v := range reply.Infos {
			appKeys = append(appKeys, v.AppKey)
		}
		mreq := gd_management.ManagementGetUserMerchantNameReq{}
		mreq.AppKeys = appKeys

		mreply, err := rpc_apis.Management.ManagementGetUserMerchantName(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			return errors.ErrorTransform(err)
		}
		appKeyMap := map[string]gd_management.ManagementGetUserMerchantNameItem{}
		for _, v := range mreply.Infos {
			appKeyMap[v.AppKey] = v
		}
		for k, _ := range reply.Infos {
			reply.Infos[k].MerchantName = appKeyMap[reply.Infos[k].AppKey].MerchantName
			reply.Infos[k].UserName = appKeyMap[reply.Infos[k].AppKey].UserName
		}
		resp := param_v1_0.ServiceManagementOrderListResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Infos = reply.Infos

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

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