package service

import (
	"dsbqj-admin/model/mysql"
	"dsbqj-admin/pkg/cache"
	"errors"
	"fmt"
	"github.com/aliyun/credentials-go/credentials/utils"
	"github.com/goccy/go-json"
	"time"
)

/*
oss 层级规范

|- 主目录  [models]|[maps]|[musics]|[effects] ....
	|- 子目录 uuid为目录名
		|- 子目录 [source] 源数据
			|- 源数据结构
		|- 文件1 perviewImage.jpg // 源数据预览图
		|- 文件2 renderImage.jpg // 源数据渲染图
		|- 文件
*/

// UploadTokenService 获得上传oss token的服务
type UploadReportService struct {
	Message string `form:"message" json:"message"`
	Source  string `form:"source" json:"source"`
	Line    string `form:"line" json:"line"`
	Colno   string `form:"colno" json:"colno"`
	Err     string `form:"err" json:"err"`
	Aid     string `form:"aid" json:"aid"`
	Sid     string `form:"sid" json:"sid"`
	Uid     string `form:"uid" json:"uid"`
	Channel string `form:"channel" json:"channel"`
	Path    string `form:"path" json:"path"`
}

func (service *UploadReportService) Create() error {
	buf, _ := json.Marshal(service)
	str := utils.GetMD5Base64(buf)
	key := fmt.Sprintf("%s:%s", "ClientErrReport", service.Aid)

	// 检查数据是否冗余
	success, err := cache.RedisReport.SetNX(key, str, 60*time.Second).Result()
	if !success {
		return errors.New("请勿重复提交")
	}
	fmt.Println(str, success)
	var report = new(mysql.Report)
	report.Message = service.Message
	report.Err = service.Err
	report.Source = service.Source
	report.Line = service.Line
	report.Colno = service.Colno
	report.Aid = service.Aid
	report.Sid = service.Sid
	report.Uid = service.Uid
	report.Channel = service.Channel
	report.Path = service.Path

	err = report.Create()
	if err != nil {
		return err
	}

	return nil
}
