package controllers

import (
	"encoding/json"
	"fmt"
	quanshiReq "hongze/hongze_open_api/models/request/quanshi"
	"hongze/hongze_open_api/services/quanshi"
	"hongze/hongze_open_api/services/yb"
	"hongze/hongze_open_api/utils"
	"strconv"
)

// QuanShiControllerCommon 报告模块
type QuanShiControllerCommon struct {
	BaseCommon
}

// CallBack
// @Title 全时回调接口
// @Description 全时回调接口
// @router /callback [post]
func (c *QuanShiControllerCommon) CallBack() {
	//回调中url参数要做签名（get请求中的参数，按照自己的方式签名，避免链接被别人拿去随意请求）
	utils.FileLog.Info(fmt.Sprintf("全时回调参数：%s", string(c.Ctx.Input.RequestBody)))
	//c.OkDetailed("ok", "获取成功")

	activityId, _ := c.GetInt("activity_id")
	timeInt, _ := c.GetInt("time")
	paramStr := fmt.Sprintf(`activity_id=%d&time=%d`, activityId, timeInt)
	signStr := quanshi.GetSign(paramStr)
	ownSign := c.GetString("sign")
	if ownSign != signStr {
		c.FailWithMessage("请求异常")
		return
	}

	var req1 quanshiReq.QsCallBackReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req1)
	if err != nil {
		c.FailWithMessage("参数解析异常")
		return
	}
	switch req1.Method {
	case "report":
		//报告
		var req quanshiReq.QsCallBackReportReq
		err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
		if err != nil {
			c.FailWithMessage("参数解析异常")
			return
		}
		eventId, err := strconv.Atoi(req.Report.EventID)
		if err != nil {
			c.FailWithMessage("会议id异常：" + req.Report.EventID)
			return
		}
		err = yb.SyncUser(eventId)
	case "record":
		//录制
		var req quanshiReq.QsCallBackRecordReq
		err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
		if err != nil {
			c.FailWithMessage("参数解析异常")
			return
		}
		eventId, err := strconv.Atoi(req.Record.EventID)
		if err != nil {
			c.FailWithMessage("会议id异常：" + req.Record.EventID)
			return
		}
		err = yb.SyncVideo(eventId, req.Record.VideoURL)
	default:
		c.OkWithMessage("ok")
		return
	}
	if err != nil {
		c.FailWithMessage("同步数据失败")
		return
	}
	c.Ok()
}
