package services import ( "eta/eta_docs/models/crm" "eta/eta_docs/models/response" "eta/eta_docs/utils" "fmt" "strconv" "strings" ) // FormatVideosToVideoItems 格式化视频列表 func FormatVideosToVideoItems(list []*crm.EtaTrainingVideo) (items []*response.EtaTrainingVideoItem, err error) { items = make([]*response.EtaTrainingVideoItem, 0) if len(list) == 0 { return } // 获取分类 classifyMap := make(map[int]*crm.EtaTrainingVideoClassify) { classifyOB := new(crm.EtaTrainingVideoClassify) classifies, e := classifyOB.GetItemsByCondition(``, make([]interface{}, 0), "") if e != nil { err = fmt.Errorf("获取分类列表失败, Err: %s", e.Error()) return } for _, v := range classifies { classifyMap[v.EtaTrainingVideoClassifyId] = v } } // 获取标签 tagMap := make(map[int]*crm.EtaTrainingVideoTag) { tagOB := new(crm.EtaTrainingVideoTag) tags, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), "") 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(response.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([]*response.EtaTrainingVideoClassifyItem, 0) for _, s := range strClassifyIdArr { id, _ := strconv.Atoi(s) c := classifyMap[id] if c != nil { arr = append(arr, &response.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([]*response.EtaTrainingVideoTagItem, 0) for _, s := range strTagIdArr { id, _ := strconv.Atoi(s) t := tagMap[id] if t != nil { b.Tags = append(b.Tags, &response.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 }