// 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_statistics"
	"fmt"
	"strconv"
	"strings"
	"time"

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

// @Title bi dashbord
// @Description 获取bi dashboard
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Success 200 	{object} params.param_v1_0.BiGetDashResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_dashboard [get]
func (u *BaseAccessLogController) BiGetDash() {
	u.LogID = fmt.Sprintf("BiGetDash[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetDashReq{}
		reply, err := rpc_apis.Statistics.BiGetDash(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetDash"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetDashResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		dashInfo := param_v1_0.BiDash{}
		dashInfo.VehicleInfo.Count = reply.DashInfo.VehicleInfo.Count
		dashInfo.VehicleInfo.Increase = reply.DashInfo.VehicleInfo.Increase
		dashInfo.ChargeInfo.Charge = reply.DashInfo.ChargeInfo.Charge
		rateString := strconv.FormatFloat(reply.DashInfo.ChargeInfo.ChargeRate*100, 'f', 2, 64)
		dashInfo.ChargeInfo.ChargeRate, _ = strconv.ParseFloat(rateString, 64)
		dashInfo.ChargeInfo.Increase = strconv.FormatFloat(100.0*reply.DashInfo.ChargeInfo.Increase, 'f', 2, 64) + "%"

		rateString = strconv.FormatFloat(reply.DashInfo.FailInfo.FailRate*100, 'f', 2, 64)
		dashInfo.FailInfo.Fail = reply.DashInfo.FailInfo.Fail
		dashInfo.FailInfo.FailRate, _ = strconv.ParseFloat(rateString, 64)
		dashInfo.FailInfo.Increase = strconv.FormatFloat(100.0*reply.DashInfo.FailInfo.Increase, 'f', 2, 64) + "%"
		resp.DashInfo = dashInfo
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi api trend
// @Description 获取api 走势
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   api_id    query   string true ""
// @Param   merchant_id    query   int64 false ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   time_type    query   int false ""
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetTrendResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_api_trend [get]
func (u *BaseAccessLogController) BiGetTrend() {
	u.LogID = fmt.Sprintf("BiGetTrend[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetTrendReq{}
		mreq.MerchantId = req.MerchantId
		array := strings.Split(req.ApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ApiId = append(mreq.ApiId, int64(id))
			}
		}
		mreq.EndTimestamp = req.EndTimestamp
		mreq.StartTimestamp = req.StartTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		mreq.TimeType = req.TimeType
		reply, err := rpc_apis.Statistics.BiGetTrend(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetTrend"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetTrendResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		if len(reply.Infos) == 0 {
			reply.Infos = []gd_statistics.Trend{}
		}
		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, handleTokenCheck, handleDataTask,
	)
}

// @Title bi provider trend
// @Description 获取数据源走势
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   provider_api_id    query   string true ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   time_type    query   int false ""
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderTrendResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_provider_trend [get]
func (u *BaseAccessLogController) BiGetProviderTrend() {
	u.LogID = fmt.Sprintf("BiGetProviderTrend[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderTrendReq{}
		array := strings.Split(req.ProviderApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ProviderApiId = append(mreq.ProviderApiId, int64(id))
			}
		}
		mreq.EndTimestamp = req.EndTimestamp
		mreq.StartTimestamp = req.StartTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		mreq.TimeType = req.TimeType
		reply, err := rpc_apis.Statistics.BiGetProviderTrend(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderTrend"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderTrendResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		if len(reply.Infos) == 0 {
			reply.Infos = []gd_statistics.Trend{}
		}
		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, handleTokenCheck, handleDataTask,
	)
}

// formatFloat64 格式化data小数位数为两位
func formatFloat64(data float64) float64 {
	s := strconv.FormatFloat(data, 'f', 2, 64)
	ret, _ := strconv.ParseFloat(s, 64)
	return ret
}

// @Title bi api status
// @Description 获取api 服务性能
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   api_id    query   string true ""
// @Param   merchant_id    query   int64 false ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   time_type    query   int false ""
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetServiceStatusResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_api_status [get]
func (u *BaseAccessLogController) BiGetServiceStatus() {
	u.LogID = fmt.Sprintf("BiGetServiceStatus[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetServiceStatusReq{}
		mreq.MerchantId = req.MerchantId
		array := strings.Split(req.ApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ApiId = append(mreq.ApiId, int64(id))
			}
		}
		mreq.EndTimestamp = req.EndTimestamp
		mreq.StartTimestamp = req.StartTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		mreq.TimeType = req.TimeType
		reply, err := rpc_apis.Statistics.BiGetServiceStatus(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetServiceStatus"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetServiceStatusResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}

		if len(reply.Infos) == 0 {
			reply.Infos = []gd_statistics.ServiceStatus{}
		}
		resp.Infos = reply.Infos
		for i, v := range resp.Infos {
			resp.Infos[i].AvgElapsed = formatFloat64(v.AvgElapsed)
		}
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi provider status
// @Description 获取 数据源api 服务性能
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   provider_api_id    query   string true ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   time_type    query   int false ""
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderServiceStatusResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_provider_status [get]
func (u *BaseAccessLogController) BiGetProviderServiceStatus() {
	u.LogID = fmt.Sprintf("BiGetProviderServiceStatus[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderServiceStatusReq{}
		array := strings.Split(req.ProviderApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ProviderApiId = append(mreq.ProviderApiId, int64(id))
			}
		}
		mreq.EndTimestamp = req.EndTimestamp
		mreq.StartTimestamp = req.StartTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		mreq.TimeType = req.TimeType
		reply, err := rpc_apis.Statistics.BiGetProviderServiceStatus(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderServiceStatus"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderServiceStatusResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		if len(reply.Infos) == 0 {
			reply.Infos = []gd_statistics.ServiceStatus{}
		}
		resp.Infos = reply.Infos
		for i, v := range resp.Infos {
			resp.Infos[i].AvgElapsed = formatFloat64(v.AvgElapsed)
		}
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi api report
// @Description 获取api 报表
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   api_id    query   string false ""
// @Param   merchant_id    query   int64 false ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   today_end_timestamp query int64 false ""
// @Param   page_size    query   int false ""
// @Param   page_number    query   int false ""
// @Success 200 	{object} params.param_v1_0.BiGetReportResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_api_report [get]
func (u *BaseAccessLogController) BiGetReport() {
	u.LogID = fmt.Sprintf("BiGetReport[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetReportReq{}
		mreq.MerchantId = req.MerchantId
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		array := strings.Split(req.ApiId, ",")
		for _, v := range array {
			id, _ := strconv.ParseInt(v, 10, 64)
			mreq.ApiId = append(mreq.ApiId, id)
		}
		if req.ApiId == "" {
			mreq.ApiId = []int64{}
		}
		for i := req.StartTimestamp; i < req.EndTimestamp; i = i + 24*3600 {
			date := time.Unix(i, 0).Format("2006-01-02")
			mreq.Date = append(mreq.Date, date)
		}

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

		if len(reply.Infos) == 0 {
			reply.Infos = []gd_statistics.BiReportInfo{}
		}
		resp.Data.PageSize = reply.PageSize
		resp.Data.Infos = reply.Infos
		resp.Data.Total = reply.Total
		resp.Data.Sum = reply.Sum
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi provider report
// @Description 获取数据源 报表
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   provider_api_id    query   string true ""
// @Param   page_number    query   int false ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   today_end_timestamp query int64 false ""
// @Param   page_size    query   int false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderReportResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_provider_report [get]
func (u *BaseAccessLogController) BiGetProviderReport() {
	u.LogID = fmt.Sprintf("BiGetProviderReport[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderReportReq{}
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		array := strings.Split(req.ProviderApiId, ",")
		for _, v := range array {
			id, _ := strconv.ParseInt(v, 10, 64)
			mreq.ProviderApiId = append(mreq.ProviderApiId, id)
		}
		if req.ProviderApiId == "" {
			mreq.ProviderApiId = []int64{}
		}
		for i := req.StartTimestamp; i < req.EndTimestamp; i = i + 24*3600 {
			date := time.Unix(i, 0).Format("2006-01-02")
			mreq.Date = append(mreq.Date, date)
		}
		reply, err := rpc_apis.Statistics.BiGetProviderReport(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderReport"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderReportResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		if len(reply.Infos) == 0 {
			reply.Infos = []gd_statistics.BiProviderReportInfo{}
		}
		resp.Data.PageSize = reply.PageSize
		resp.Data.PageNumber = reply.PageNumber
		resp.Data.Infos = reply.Infos
		resp.Data.Total = reply.Total
		resp.Data.Sum = reply.Sum
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi api distribution
// @Description 获取api 错误码分布
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   api_id    query   string true ""
// @Param   merchant_id    query   int64 false ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   today_end_timestamp query int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetDistributionResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_api_distribution [get]
func (u *BaseAccessLogController) BiGetDistribution() {
	u.LogID = fmt.Sprintf("BiGetDistribution[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetDistributionReq{}
		mreq.MerchantId = req.MerchantId
		array := strings.Split(req.ApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ApiId = append(mreq.ApiId, int64(id))
			}
		}
		mreq.StartTimestamp = req.StartTimestamp
		mreq.EndTimestamp = req.EndTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetDistribution(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetDistribution"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetDistributionResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		if len(reply.Info.ApiCodeList) == 0 {
			reply.Info.ApiCodeList = []gd_statistics.CodeCount{}
		}
		resp.Info = reply.Info
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi provider distribution
// @Description 获取数据源错误码分布
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   provider_api_id    query string   int64 true ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   today_end_timestamp query int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderDistributionResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_provider_distribution [get]
func (u *BaseAccessLogController) BiGetProviderDistribution() {
	u.LogID = fmt.Sprintf("BiGetProviderDistribution[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderDistributionReq{}
		array := strings.Split(req.ProviderApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ProviderApiId = append(mreq.ProviderApiId, int64(id))
			}
		}
		mreq.StartTimestamp = req.StartTimestamp
		mreq.EndTimestamp = req.EndTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetProviderDistribution(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderDistribution"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderDistributionResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		if len(reply.Info.ProviderCodeList) == 0 {
			reply.Info.ProviderCodeList = []gd_statistics.CodeCount{}
		}
		resp.Info = reply.Info
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi data info
// @Description 获取数据概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   api_id    query   int64 true ""
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetApiServiceDataInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /api_service_data_info [get]
func (u *BaseAccessLogController) BiGetApiServiceDataInfo() {
	u.LogID = fmt.Sprintf("BiGetApiServiceDataInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetApiServiceDataInfoReq{}
		mreq.ApiId = req.ApiId
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetApiServiceDataInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetApiServiceDataInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetApiServiceDataInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Info = reply.Info
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi consume info
// @Description 获取消费概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   page_number    query   int false ""
// @Param   page_size    query   int false ""
// @Param   time_type    query   int false "0 今天，1 昨天 2 本周 3 本月"
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetApiServiceConsumeInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /api_service_consume_info [get]
func (u *BaseAccessLogController) BiGetApiServiceConsumeInfo() {
	u.LogID = fmt.Sprintf("BiGetApiServiceConsumeInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetApiServiceConsumeInfoReq{}
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TimeType = req.TimeType
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetApiServiceConsumeInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetApiServiceConsumeInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetApiServiceConsumeInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data.Infos = reply.Infos
		if resp.Data.Infos == nil {
			resp.Data.Infos = []gd_statistics.ApiServiceConsumeInfo{}
		}
		resp.Data.Total = reply.Total
		resp.Data.PageNumber = reply.PageNumber
		resp.Data.PageSize = reply.PageSize
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi income info
// @Description 获取营利概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   page_number    query   int false ""
// @Param   page_size    query   int false ""
// @Param   time_type    query   int false "0 今天，1 昨天 2 本周 3 本月"
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetApiServiceIncomeInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /api_service_income_info [get]
func (u *BaseAccessLogController) BiGetApiServiceIncomeInfo() {
	u.LogID = fmt.Sprintf("BiGetApiServiceIncomeInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetApiServiceIncomeInfoReq{}
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TimeType = req.TimeType
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetApiServiceIncomeInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetApiServiceIncomeInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetApiServiceIncomeInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data.Infos = reply.Infos
		if resp.Data.Infos == nil {
			resp.Data.Infos = []gd_statistics.ApiServiceIncomeInfo{}
		}
		resp.Data.Total = reply.Total
		resp.Data.PageSize = reply.PageSize
		resp.Data.PageNumber = reply.PageNumber
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi fail info
// @Description 获取异常概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   page_number    query   int false ""
// @Param   page_size    query   int false ""
// @Param   time_type    query   int false "0 今天，1 昨天 2 本周 3 本月"
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetApiServiceConsumeInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /api_service_fail_info [get]
func (u *BaseAccessLogController) BiGetApiServiceFailInfo() {
	u.LogID = fmt.Sprintf("BiGetApiServiceFailInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetApiServiceFailInfoReq{}
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TimeType = req.TimeType
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetApiServiceFailInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetApiServiceFailInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetApiServiceFailInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data.Infos = reply.Infos
		if resp.Data.Infos == nil {
			resp.Data.Infos = []gd_statistics.ApiServiceFailInfo{}
		}
		resp.Data.Total = reply.Total
		resp.Data.PageNumber = reply.PageNumber
		resp.Data.PageSize = reply.PageSize
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi provider data info
// @Description 获取数据源数据概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   provider_api_id    query   int64 true ""
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderServiceDataInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /provider_service_data_info [get]
func (u *BaseAccessLogController) BiGetProviderServiceDataInfo() {
	u.LogID = fmt.Sprintf("BiGetProviderServiceDataInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderServiceDataInfoReq{}
		mreq.ProviderApiId = req.ProviderApiId
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetProviderServiceDataInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderServiceDataInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderServiceDataInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Info = reply.Info
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi provider fail info
// @Description 获取异常概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   page_number    query   int false ""
// @Param   page_size    query   int false ""
// @Param   time_type    query   int false "0 今天，1 昨天 2 本周 3 本月"
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderServiceFailInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /provider_service_fail_info [get]
func (u *BaseAccessLogController) BiGetProviderServiceFailInfo() {
	u.LogID = fmt.Sprintf("BiGetProviderServiceFailInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderServiceFailInfoReq{}
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TimeType = req.TimeType
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetProviderServiceFailInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderServiceFailInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderServiceFailInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data.Infos = reply.Infos
		if resp.Data.Infos == nil {
			resp.Data.Infos = []gd_statistics.ProviderServiceFailInfo{}
		}
		resp.Data.Total = reply.Total
		resp.Data.PageSize = reply.PageSize
		resp.Data.PageNumber = reply.PageNumber
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi provider pay info
// @Description 获取数据源成本概况
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   page_number    query   int false ""
// @Param   page_size    query   int false ""
// @Param   time_type    query   int false "0 今天，1 昨天 2 本周 3 本月"
// @Param   today_end_timestamp    query   int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetProviderServicePayInfoResp "响应信息"
// @Failure 500 	服务器错误
// @router /provider_service_pay_info [get]
func (u *BaseAccessLogController) BiGetProviderServicePayInfo() {
	u.LogID = fmt.Sprintf("BiGetProviderServicePayInfo[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetProviderServicePayInfoReq{}
		mreq.PageNumber = req.PageNumber
		mreq.PageSize = req.PageSize
		mreq.TimeType = req.TimeType
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetProviderServicePayInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetProviderServicePayInfo"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetProviderServicePayInfoResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data.Infos = reply.Infos
		if resp.Data.Infos == nil {
			resp.Data.Infos = []gd_statistics.ProviderServicePayInfo{}
		}
		resp.Data.Total = reply.Total
		resp.Data.PageSize = reply.PageSize
		resp.Data.PageNumber = reply.PageNumber
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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

// @Title bi api pay distribution
// @Description 获取api 成本分布
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   api_id    query   string true ""
// @Param   merchant_id    query   int64 false ""
// @Param   start_timestamp    query   int64 true ""
// @Param   end_timestamp    query   int64 true ""
// @Param   today_end_timestamp query int64 false ""
// @Success 200 	{object} params.param_v1_0.BiGetPayDistributionResp "响应信息"
// @Failure 500 	服务器错误
// @router /bi_pay_distribution [get]
func (u *BaseAccessLogController) BiGetPayDistribution() {
	u.LogID = fmt.Sprintf("BiGetPayDistribution[%d]", time.Now().UnixNano())

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

	handleTokenCheck := func() error {
		uid, _, err := utils.ParseToken(req.Token)
		if err != nil {
			return err
		}

		if uid != req.Uid {
			return errors.UserTokenillegality
		}
		return nil
	}

	handleDataTask := func() error {
		mreq := gd_statistics.BiGetPayDistributionReq{}
		mreq.MerchantId = req.MerchantId
		array := strings.Split(req.ApiId, ",")
		for _, v := range array {
			id, _ := strconv.Atoi(v)
			if id > 0 {
				mreq.ApiId = append(mreq.ApiId, int64(id))
			}
		}
		mreq.StartTimestamp = req.StartTimestamp
		mreq.EndTimestamp = req.EndTimestamp
		mreq.TodayEndTimestamp = req.TodayEndTimestamp
		reply, err := rpc_apis.Statistics.BiGetPayDistribution(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
		if err != nil {
			l.Error("rpc",
				zap.String("call", "BiGetPayDistribution"),
				zap.String("args", utils.MarshalJsonString(req)),
				zap.String("error", err.Error()))
			return errors.ErrorTransform(err)
		}
		resp := param_v1_0.BiGetPayDistributionResp{
			SimpleResp: param_base.SimpleResp{
				Code: 0,
				Msg:  "SUCCESS",
			},
		}
		resp.Data.Infos = reply.PayInfos
		if resp.Data.Infos == nil {
			resp.Data.Infos = []gd_statistics.ProviderCount{}
		}
		resp.Data.MerchantSumCharge = reply.MerchantSumCharge
		resp.Data.ProviderSumCharge = reply.ProviderSumCharge
		httper.JSON(u.Ctx, resp, u.LogID)
		return nil
	}

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