package services

import (
	"encoding/json"
	"eta/eta_mini_crm_ht/models"
	"github.com/google/uuid"
	"strconv"
	"strings"
)

func CreateMeta(authorName string, authorId int, mediaId int, publishTime string, sourceType models.SourceType) (id int64, err error) {
	ids, err := models.GetPostUser(authorId, publishTime)
	if err != nil {
		return
	}
	var idStrList []string
	for _, id := range ids {
		idStrList = append(idStrList, strconv.Itoa(id))
	}
	Meta := models.MetaData{
		AuthorName:    authorName,
		AuthorId:      authorId,
		SourceId:      mediaId,
		PublishedTime: publishTime,
	}
	metaStr, _ := json.Marshal(Meta)
	toStr := strings.Join(idStrList, ",")
	UUID := uuid.New()
	uuidStr := UUID.String()
	metaContent := models.MetaInfo{
		From:       "ADMIN",
		Uid:        "media:" + uuidStr,
		Meta:       string(metaStr),
		MetaType:   "USER_NOTICE",
		SourceType: sourceType,
		Status:     models.InitStatusType,
		To:         toStr,
	}
	id, err = metaContent.Insert()
	return
}
