// Copyright 2019 getensh.com. All rights reserved.
// Use of this source code is governed by getensh.com.

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"
	"github.com/astaxie/beego"
	"go.uber.org/zap"
)

// Operations about provider
type DataSupplementController struct {
	metadata interface{} // 中继元数据
	beego.Controller
	LogID string
}

// @Title 获取补充数据库
// @Description 获取补充数据库
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Success 200 	{object} params.param_v1_0.ManagementGetSupplementDbResp "响应信息"
// @Failure 500 	服务器错误
// @router /db [get]
func (u *DataSupplementController) ManagementGetSupplementDb() {
	u.LogID = fmt.Sprintf("ManagementGetSuplementDb[%d]", time.Now().UnixNano())

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

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

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

// @Title 数据补充获取标题
// @Description 数据补充获取标题
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   db_code  query  string   false "数据类型"
// @Success 200 	{object} params.param_v1_0.ManagementGetSuplementTitleResp "响应信息"
// @Failure 500 	服务器错误
// @router /title [get]
func (u *DataSupplementController) ManagementGetSuplementTitle() {
	u.LogID = fmt.Sprintf("ManagementGetSuplementTitle[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_management.ManagementGetSuplementTitleReq{}
		mreq.DbCode = req.DbCode
		mreply, err := rpc_apis.Management.ManagementGetSuplementTitle(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementGetSuplementTitle"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementGetSuplementTitleResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data = mreply
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title 数据补充获取标题
// @Description 数据补充获取标题
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   body     body   rpc_apis.gd_management.ManagementGetDataSupplementReq true "参数"
// @Success 200 	{object} params.param_v1_0.ManagementGetDataSupplementResp "响应信息"
// @Failure 500 	服务器错误
// @router /data [put]
func (u *DataSupplementController) ManagementGetDataSupplement() {
	u.LogID = fmt.Sprintf("ManagementGetDataSupplement[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		//mreq := gd_management.ManagementGetDataSupplementReq{}
		//mreq.SearchCode = req.SearchCode
		//json.Unmarshal([]byte(req.Keys), &mreq.Keys)
		mreply, err := rpc_apis.Management.ManagementGetDataSupplement(utils.NewContextFromBeegoCtx(u.Ctx), &req.Body)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "ManagementGetDataSupplement"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.ManagementGetDataSupplementResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data = mreply
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title 数据补充获取标题
// @Description 数据补充获取标题
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   body     body   rpc_apis.gd_management.ManagementUpdateDataSupplementReq true "参数"
// @Success 200 	{object} params.param_v1_0.ManagementUpdateDataSupplementResp "响应信息"
// @Failure 500 	服务器错误
// @router /data [post]
func (u *DataSupplementController) ManagementUpdateDataSupplement() {
	u.LogID = fmt.Sprintf("ManagementUpdateDataSupplement[%d]", time.Now().UnixNano())

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

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

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