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

// @Title 手动生成日报
// @Description 手动生成日报
// @Param   token    header   string true "token"
// @Param   uid    header   int64 true "admin id"
// @Param   merchant_id    query   int64 false "商户id"
// @Param   api_id    query   int64 false "基础api id"
// @Param   provider_api_id query int64 false "数据源api id"
// @Param   dates query string false "导出日期以分号分隔，默认为前一天"
// @Param   api_report query bool false "是否统计商户日报"
// @Param   provider_report query bool false "是否统计数据源日报"
// @Param   business_report query bool false "是否统计商务日报"
// @Param   is_insert query bool false "统计的数据是否插入日报表中"
// @Param   only_mail query bool false "是否仅发送邮件，不统计"
// @Param   only_report query bool false "是否仅统计， 不发送邮件"
// @Param   file_name query string false "指定文件名"
// @Success 200 	{object} params.param_v1_0.LogGenerateReportResp "响应信息"
// @Failure 500 	服务器错误
// @router /report [get]
func (u *CrontabController) LogGenerateReport() {
	u.LogID = fmt.Sprintf("LogQueryAccessLog[%d]", time.Now().UnixNano())

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

	handleDataTask := func() error {
		mreq := gd_crontab.LogGenerateReportReq{}
		mreq.ApiId = req.ApiId
		mreq.MerchantId = req.MerchantId
		mreq.ProviderApiId = req.ProviderApiId
		mreq.IsInsert = req.IsInsert
		mreq.OnlyMail = req.OnlyMail
		mreq.OnlyReport = req.OnlyReport
		mreq.Dates = req.Dates
		mreq.ApiReport = req.ApiReport
		mreq.ProviderReport = req.ProviderReport
		mreq.FileName = req.FileName
		mreq.BusinessReport = req.BusinessReport
		mreq.ApiWeekStart = req.ApiWeekStart
		mreq.ApiMonthStart = req.ApiMonthStart
		mreq.ProviderMonthStart = req.ProviderMonthStart
		mreq.ProviderWeekStart = req.ProviderWeekStart
		mreq.BusinessWeekStart = req.BusinessWeekStart
		mreq.BusinessMonthStart = req.BusinessMonthStart

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