package eta_training_video

import (
	"fmt"
	"hongze/hz_crm_api/models/eta_training_video"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
)

// FormatVideosToVideoItems 格式化视频列表
func FormatVideosToVideoItems(list []*eta_training_video.EtaTrainingVideo) (items []*eta_training_video.EtaTrainingVideoItem, err error) {
	items = make([]*eta_training_video.EtaTrainingVideoItem, 0)
	if len(list) == 0 {
		return
	}

	// 获取分类
	classifyMap := make(map[int]*eta_training_video.EtaTrainingVideoClassify)
	{
		classifyOB := new(eta_training_video.EtaTrainingVideoClassify)
		classifies, e := classifyOB.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
		if e != nil {
			err = fmt.Errorf("获取分类列表失败, Err: %s", e.Error())
			return
		}
		for _, v := range classifies {
			classifyMap[v.EtaTrainingVideoClassifyId] = v
		}
	}

	// 获取标签
	tagMap := make(map[int]*eta_training_video.EtaTrainingVideoTag)
	{
		tagOB := new(eta_training_video.EtaTrainingVideoTag)
		tags, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
		if e != nil {
			err = fmt.Errorf("获取标签列表失败, Err: %s", e.Error())
			return
		}
		for _, v := range tags {
			tagMap[v.EtaTrainingVideoTagId] = v
		}
	}

	for _, v := range list {
		b := new(eta_training_video.EtaTrainingVideoItem)
		b.VideoId = v.EtaTrainingVideoId
		b.VideoCode = v.VideoCode
		b.Title = v.Title
		b.Introduce = v.Introduce
		b.CoverImg = v.CoverImg
		b.VideoUrl = v.VideoUrl
		b.PublishState = v.PublishState
		b.PublishTime = utils.TimeTransferString(utils.FormatDateTime, v.PublishTime)
		b.ViewTotal = v.ViewTotal
		b.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
		b.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)

		// 分类
		if v.ClassifyIds != "" {
			strClassifyIdArr := strings.Split(v.ClassifyIds, ",")
			if len(strClassifyIdArr) > 0 {
				arr := make([]*eta_training_video.EtaTrainingVideoClassifyItem, 0)
				for _, s := range strClassifyIdArr {
					id, _ := strconv.Atoi(s)
					c := classifyMap[id]
					if c != nil {
						arr = append(arr, &eta_training_video.EtaTrainingVideoClassifyItem{
							ClassifyId:   c.EtaTrainingVideoClassifyId,
							ClassifyName: c.ClassifyName,
							ParentId:     c.ParentId,
							Sort:         c.Sort,
						})
					}
				}
				tree := GetClassifyTreeRecursive(arr, 0)
				if len(tree) > 0 {
					b.Classify = tree[0]
				}
			}
		}

		// 标签
		if v.TagIds != "" {
			strTagIdArr := strings.Split(v.TagIds, ",")
			if len(strTagIdArr) > 0 {
				b.Tags = make([]*eta_training_video.EtaTrainingVideoTagItem, 0)
				for _, s := range strTagIdArr {
					id, _ := strconv.Atoi(s)
					t := tagMap[id]
					if t != nil {
						b.Tags = append(b.Tags, &eta_training_video.EtaTrainingVideoTagItem{
							TagId:      t.EtaTrainingVideoTagId,
							TagName:    t.TagName,
							VideoTotal: t.VideoTotal,
							CreateTime: utils.TimeTransferString(utils.FormatDateTime, t.CreateTime),
							ModifyTime: utils.TimeTransferString(utils.FormatDateTime, t.ModifyTime),
						})
					}
				}
			}
		}

		items = append(items, b)
	}
	return
}
