浏览代码

Merge branch 'eta/1.0.8' into debug

hsun 1 年之前
父节点
当前提交
601ea1a169

+ 244 - 0
controller/eta_training_video.go

@@ -0,0 +1,244 @@
+package controller
+
+import (
+	"eta/eta_docs/controller/resp"
+	"eta/eta_docs/global"
+	"eta/eta_docs/models/base"
+	"eta/eta_docs/models/crm"
+	"eta/eta_docs/models/request"
+	"eta/eta_docs/models/response"
+	"eta/eta_docs/services"
+	"eta/eta_docs/utils"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"github.com/go-playground/validator/v10"
+	"strings"
+	"text/template"
+	"time"
+)
+
+// EtaTrainingVideoController 培训视频
+type EtaTrainingVideoController struct{}
+
+// ClassifyTree
+// @Description 分类树
+// @Success 200 {string} string "获取成功"
+// @Router /classify/tree [get]
+func (a *EtaTrainingVideoController) ClassifyTree(c *gin.Context) {
+	result := new(response.EtaTrainingVideoClassifyResp)
+	result.List = make([]*response.EtaTrainingVideoClassifyItem, 0)
+
+	// 获取所有分类
+	ob := new(crm.EtaTrainingVideoClassify)
+	classifies, e := ob.GetItemsByCondition(``, make([]interface{}, 0), ``)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取分类列表失败, Err: "+e.Error(), c)
+		return
+	}
+
+	items := make([]*response.EtaTrainingVideoClassifyItem, 0)
+	for _, v := range classifies {
+		t := &response.EtaTrainingVideoClassifyItem{
+			ClassifyId:   v.EtaTrainingVideoClassifyId,
+			ClassifyName: v.ClassifyName,
+			ParentId:     v.ParentId,
+			Sort:         v.Sort,
+			Children:     make([]*response.EtaTrainingVideoClassifyItem, 0),
+		}
+		items = append(items, t)
+	}
+
+	// 递归返回树形结构
+	items = services.GetClassifyTreeRecursive(items, 0)
+
+	result.List = items
+	resp.OkData("获取成功", result, c)
+}
+
+// TagList
+// @Description 标签列表
+// @Success 200 {string} string "获取成功"
+// @Router /tag/list [get]
+func (a *EtaTrainingVideoController) TagList(c *gin.Context) {
+	// 获取列表数据
+	tagOB := new(crm.EtaTrainingVideoTag)
+	list, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), "")
+	if e != nil {
+		resp.FailMsg("获取失败", "获取标签列表失败, Err: "+e.Error(), c)
+		return
+	}
+
+	result := make([]*response.EtaTrainingVideoTagItem, 0)
+	for _, v := range list {
+		t := new(response.EtaTrainingVideoTagItem)
+		t.TagId = v.EtaTrainingVideoTagId
+		t.TagName = v.TagName
+		t.VideoTotal = v.VideoTotal
+		t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
+		t.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
+		result = append(result, t)
+	}
+
+	resp.OkData("获取成功", result, c)
+}
+
+// VideoPageList
+// @Description 视频列表-分页
+// @Success 200 {string} string "获取成功"
+// @Router /video/page_list [get]
+func (a *EtaTrainingVideoController) VideoPageList(c *gin.Context) {
+	var req request.TrainingVideoPageListReq
+	if e := c.BindQuery(&req); e != nil {
+		err, ok := e.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+e.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", err.Translate(global.Trans), c)
+		return
+	}
+
+	// 筛选项
+	cond := fmt.Sprintf(`%s = ?`, crm.EtaTrainingVideoColumns.PublishState)
+	pars := make([]interface{}, 0)
+	pars = append(pars, 1)
+	{
+		// 关键词
+		keyword := strings.TrimSpace(req.Keyword)
+		keyword = template.HTMLEscapeString(keyword)
+		if keyword != "" {
+			kw := fmt.Sprint("%", keyword, "%")
+			cond += fmt.Sprintf(` AND (%s LIKE ?)`, crm.EtaTrainingVideoColumns.Title)
+			pars = append(pars, kw)
+		}
+
+		// 分类ID
+		classifyId := req.ClassifyId
+		if classifyId > 0 {
+			cond += fmt.Sprintf(` AND FIND_IN_SET(?, %s)`, crm.EtaTrainingVideoColumns.ClassifyIds)
+			pars = append(pars, classifyId)
+		}
+
+		// 标签IDs
+		strTagIds := req.TagIds
+		if strTagIds != "" {
+			strTags := strings.Split(strTagIds, ",")
+			if len(strTags) > 0 {
+				joinArr := make([]string, 0)
+				cond += ` AND (`
+				for _, s := range strTags {
+					joinArr = append(joinArr, fmt.Sprintf(`FIND_IN_SET(?, %s)`, crm.EtaTrainingVideoColumns.TagIds))
+					pars = append(pars, s)
+				}
+				cond += strings.Join(joinArr, ` OR `)
+				cond += `)`
+			} else {
+				cond += ` AND 1=2`
+			}
+		}
+	}
+
+	page := new(base.Page)
+	page.SetPageSize(req.PageSize)
+	page.SetCurrent(req.CurrentIndex)
+	// 排序
+	if req.IsHot {
+		page.AddOrderItem(base.OrderItem{Column: crm.EtaTrainingVideoColumns.ViewTotal, Asc: false})
+	} else {
+		// 默认和最新均为发布时间排序
+		page.AddOrderItem(base.OrderItem{Column: crm.EtaTrainingVideoColumns.PublishTime, Asc: false})
+	}
+
+	// 获取列表
+	videoOb := new(crm.EtaTrainingVideo)
+	total, list, e := videoOb.GetPageItems(page, cond, pars)
+	if e != nil {
+		resp.FailData("获取失败", "获取视频列表失败, Err: "+e.Error(), c)
+		return
+	}
+	items, e := services.FormatVideosToVideoItems(list)
+	if e != nil {
+		resp.FailData("获取失败", "格式化视频列表失败, Err: "+e.Error(), c)
+		return
+	}
+
+	page.SetTotal(total)
+	baseData := new(base.BaseData)
+	baseData.SetPage(page)
+	baseData.SetList(items)
+	resp.OkData("获取成功", baseData, c)
+}
+
+// VideoDetail
+// @Description 视频详情
+// @Success 200 {string} string "获取成功"
+// @Router /video/detail [get]
+func (a *EtaTrainingVideoController) VideoDetail(c *gin.Context) {
+	var req request.TrainingVideoDetailReq
+	if e := c.BindQuery(&req); e != nil {
+		err, ok := e.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+e.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", err.Translate(global.Trans), c)
+		return
+	}
+	req.VideoCode = strings.TrimSpace(req.VideoCode)
+	if req.VideoCode == "" {
+		resp.Fail("参数有误", c)
+		return
+	}
+	req.BusinessCode = strings.TrimSpace(req.BusinessCode)
+
+	// 获取视频
+	ob := new(crm.EtaTrainingVideo)
+	cond := fmt.Sprintf(`%s = ? AND %s = ?`, crm.EtaTrainingVideoColumns.VideoCode, crm.EtaTrainingVideoColumns.PublishState)
+	pars := make([]interface{}, 0)
+	pars = append(pars, req.VideoCode, 1)
+	item, e := ob.GetItem(cond, pars)
+	if e != nil {
+		if e == utils.ErrNoRow {
+			resp.Fail("视频已下架", c)
+			return
+		}
+		resp.FailMsg("获取失败", "获取视频失败, Err: "+e.Error(), c)
+		return
+	}
+
+	list := make([]*crm.EtaTrainingVideo, 0)
+	list = append(list, item)
+	formats, e := services.FormatVideosToVideoItems(list)
+	if e != nil {
+		resp.FailMsg("获取失败", "格式化视频信息失败, Err: "+e.Error(), c)
+		return
+	}
+	result := new(response.EtaTrainingVideoItem)
+	if len(formats) > 0 {
+		result = formats[0]
+	}
+
+	// 更新访问量、访问记录
+	go func() {
+		_ = ob.UpdateViewTotal(item.EtaTrainingVideoId)
+
+		if req.BusinessCode != "" {
+			businessOB := new(crm.EtaBusiness)
+			businessCond := `code_encrypt = ?`
+			businessPars := make([]interface{}, 0)
+			businessPars = append(businessPars, req.BusinessCode)
+			business, _ := businessOB.GetItemByCondition(businessCond, businessPars, "")
+			if business == nil {
+				return
+			}
+			newLog := new(crm.EtaTrainingVideoViewLog)
+			newLog.EtaTrainingVideoId = item.EtaTrainingVideoId
+			newLog.EtaBusinessId = business.EtaBusinessId
+			newLog.EtaBusinessCodeEncrypt = business.CodeEncrypt
+			newLog.CreateTime = time.Now().Local()
+			_ = newLog.Create()
+		}
+	}()
+
+	resp.OkData("获取成功", result, c)
+}

+ 1 - 0
init_serve/router.go

@@ -19,5 +19,6 @@ func InitRouter() (r *gin.Engine) {
 	rBase := r.Group("api/")
 	routers.InitVersionUpdateLog(rBase) // 版本更新日志
 	routers.InitHelpDoc(rBase)          // 帮助文档
+	routers.InitEtaTrainingVideo(rBase) // 培训视频
 	return
 }

+ 7 - 7
models/base/page.go

@@ -20,12 +20,12 @@ type IPage interface {
 }
 
 type OrderItem struct {
-	column string // 需要排序的字段
-	asc    bool   // 是否正序排列,默认true
+	Column string // 需要排序的字段
+	Asc    bool   // 是否正序排列,默认true
 }
 
 type BaseData struct {
-	Page *Page        `json:"page"`
+	Page *Page       `json:"page"`
 	List interface{} `json:"list"` // 查询数据列表
 }
 
@@ -94,12 +94,12 @@ func (page *Page) GetOrderItemsString() string {
 	var order string
 
 	for _, val := range page.orders {
-		if val.asc {
+		if val.Asc {
 			order = ""
 		} else {
 			order = "desc"
 		}
-		arr = append(arr, fmt.Sprintf("%s %s", val.column, order))
+		arr = append(arr, fmt.Sprintf("%s %s", val.Column, order))
 	}
 	return strings.Join(arr, ",")
 }
@@ -137,11 +137,11 @@ func (page *Page) SetPages() {
 }
 
 func BuildAsc(column string) OrderItem {
-	return OrderItem{column: column, asc: true}
+	return OrderItem{Column: column, Asc: true}
 }
 
 func BuildDesc(column string) OrderItem {
-	return OrderItem{column: column, asc: false}
+	return OrderItem{Column: column, Asc: false}
 }
 
 func BuildAscs(columns ...string) []OrderItem {

+ 91 - 0
models/crm/eta_training_video.go

@@ -0,0 +1,91 @@
+package crm
+
+import (
+	"eta/eta_docs/global"
+	"eta/eta_docs/models/base"
+	"fmt"
+	"time"
+)
+
+// EtaTrainingVideo ETA培训视频表
+type EtaTrainingVideo struct {
+	EtaTrainingVideoId int       `gorm:"primaryKey;column:eta_training_video_id;type:int(10) unsigned;not null" json:"eta_training_video_id"`
+	VideoCode          string    `gorm:"unique;column:video_code;type:varchar(128);not null;default:''" json:"video_code"`                    // 视频唯一编码
+	Title              string    `gorm:"column:title;type:varchar(255);not null;default:''" json:"title"`                                     // 视频标题
+	Introduce          string    `gorm:"column:introduce;type:varchar(255);not null;default:''" json:"introduce"`                             // 视频简介
+	ClassifyIds        string    `gorm:"index:idx_classify_id;column:classify_ids;type:varchar(255);not null;default:''" json:"classify_ids"` // 视频分类IDs,英文逗号拼接
+	TagIds             string    `gorm:"column:tag_ids;type:varchar(255);not null;default:''" json:"tag_ids"`                                 // 标签IDs,英文逗号拼接
+	CoverImg           string    `gorm:"column:cover_img;type:varchar(255);not null;default:''" json:"cover_img"`                             // 封面图
+	VideoUrl           string    `gorm:"column:video_url;type:varchar(255);not null;default:''" json:"video_url"`                             // 视频地址
+	PublishState       int       `gorm:"column:publish_state;type:tinyint(4) unsigned;not null;default:0" json:"publish_state"`               // 发布状态:0-未发布;1-已发布
+	PublishTime        time.Time `gorm:"column:publish_time;type:datetime" json:"publish_time"`                                               // 发布时间
+	ViewTotal          int       `gorm:"column:view_total;type:int(10) unsigned;not null;default:0" json:"view_total"`                        // 访问量(包含非商家访问)
+	CreateTime         time.Time `gorm:"column:create_time;type:datetime" json:"create_time"`                                                 // 创建时间
+	ModifyTime         time.Time `gorm:"column:modify_time;type:datetime" json:"modify_time"`                                                 // 更新时间
+}
+
+func (m *EtaTrainingVideo) TableName() string {
+	return "eta_training_video"
+}
+
+var EtaTrainingVideoColumns = struct {
+	EtaTrainingVideoId string
+	VideoCode          string
+	Title              string
+	Introduce          string
+	ClassifyIds        string
+	TagIds             string
+	CoverImg           string
+	VideoUrl           string
+	PublishState       string
+	PublishTime        string
+	ViewTotal          string
+	CreateTime         string
+	ModifyTime         string
+}{
+	EtaTrainingVideoId: "eta_training_video_id",
+	VideoCode:          "video_code",
+	Title:              "title",
+	Introduce:          "introduce",
+	ClassifyIds:        "classify_ids",
+	TagIds:             "tag_ids",
+	CoverImg:           "cover_img",
+	VideoUrl:           "video_url",
+	PublishState:       "publish_state",
+	PublishTime:        "publish_time",
+	ViewTotal:          "view_total",
+	CreateTime:         "create_time",
+	ModifyTime:         "modify_time",
+}
+
+// Update 更新
+func (m *EtaTrainingVideo) Update(cols []string) (err error) {
+	err = global.MYSQL["hz_crm"].Model(m).Select(cols).Updates(m).Error
+	return
+}
+
+// GetItem 获取视频
+func (m *EtaTrainingVideo) GetItem(condition string, pars []interface{}) (item *EtaTrainingVideo, err error) {
+	err = global.MYSQL["hz_crm"].Where(condition, pars...).First(&item).Error
+	return
+}
+
+// GetPageItems 获取列表-分页
+func (m *EtaTrainingVideo) GetPageItems(page base.IPage, condition string, pars []interface{}) (count int64, items []*EtaTrainingVideo, err error) {
+	query := global.MYSQL["hz_crm"].Model(m).Where(condition, pars...)
+	query.Count(&count)
+	if len(page.GetOrderItemsString()) > 0 {
+		query = query.Order(page.GetOrderItemsString())
+	}
+	err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&items).Error
+	if err != nil {
+		return
+	}
+	return
+}
+
+func (m *EtaTrainingVideo) UpdateViewTotal(videoId int) (err error) {
+	sql := fmt.Sprintf(`UPDATE %s SET %s = %s + 1 WHERE %s = ?`, m.TableName(), EtaTrainingVideoColumns.ViewTotal, EtaTrainingVideoColumns.ViewTotal, EtaTrainingVideoColumns.EtaTrainingVideoId)
+	err = global.MYSQL["hz_crm"].Exec(sql, videoId).Error
+	return
+}

+ 31 - 0
models/crm/eta_training_video_classify.go

@@ -0,0 +1,31 @@
+package crm
+
+import (
+	"eta/eta_docs/global"
+	"time"
+)
+
+// EtaTrainingVideoClassify ETA培训视频分类
+type EtaTrainingVideoClassify struct {
+	EtaTrainingVideoClassifyId int       `gorm:"primaryKey;column:eta_training_video_classify_id;type:int(10) unsigned;not null" json:"eta_training_video_classify_id"`
+	ClassifyName               string    `gorm:"column:classify_name;type:varchar(128);not null;default:''" json:"classify_name"` // 分类名称
+	ParentId                   int       `gorm:"column:parent_id;type:int(10) unsigned;not null;default:0" json:"parent_id"`      // 父级ID
+	Sort                       int       `gorm:"column:sort;type:int(10) unsigned;not null;default:0" json:"sort"`                // 排序
+	SysUserId                  int       `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0" json:"sys_user_id"`  // 创建人ID
+	SysRealName                string    `gorm:"column:sys_real_name;type:varchar(128);not null;default:''" json:"sys_real_name"` // 创建人姓名
+	CreateTime                 time.Time `gorm:"column:create_time;type:datetime" json:"create_time"`                             // 创建时间
+	ModifyTime                 time.Time `gorm:"column:modify_time;type:datetime" json:"modify_time"`                             // 更新时间
+}
+
+func (m *EtaTrainingVideoClassify) TableName() string {
+	return "eta_training_video_classify"
+}
+
+// GetItemsByCondition 获取分类列表
+func (m *EtaTrainingVideoClassify) GetItemsByCondition(condition string, pars []interface{}, orderRule string) (items []*EtaTrainingVideoClassify, err error) {
+	if orderRule == "" {
+		orderRule = "sort ASC, create_time DESC"
+	}
+	err = global.MYSQL["hz_crm"].Where(condition, pars...).Order(orderRule).Find(&items).Error
+	return
+}

+ 30 - 0
models/crm/eta_training_video_tag.go

@@ -0,0 +1,30 @@
+package crm
+
+import (
+	"eta/eta_docs/global"
+	"time"
+)
+
+// EtaTrainingVideoTag ETA培训视频标签表
+type EtaTrainingVideoTag struct {
+	EtaTrainingVideoTagId int       `gorm:"primaryKey;column:eta_training_video_tag_id;type:int(10) unsigned;not null" json:"eta_training_video_tag_id"`
+	TagName               string    `gorm:"column:tag_name;type:varchar(128);not null;default:''" json:"tag_name"`           // 标签名称
+	SysUserId             int       `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0" json:"sys_user_id"`  // 创建人ID
+	SysRealName           string    `gorm:"column:sys_real_name;type:varchar(128);not null;default:''" json:"sys_real_name"` // 创建人姓名
+	VideoTotal            int       `gorm:"column:video_total;type:int(10) unsigned;not null;default:0" json:"video_total"`  // 关联视频数
+	CreateTime            time.Time `gorm:"column:create_time;type:datetime" json:"create_time"`                             // 创建时间
+	ModifyTime            time.Time `gorm:"column:modify_time;type:datetime" json:"modify_time"`                             // 更新时间
+}
+
+func (m *EtaTrainingVideoTag) TableName() string {
+	return "eta_training_video_tag"
+}
+
+// GetItemsByCondition 获取标签列表
+func (m *EtaTrainingVideoTag) GetItemsByCondition(condition string, pars []interface{}, orderRule string) (items []*EtaTrainingVideoTag, err error) {
+	if orderRule == "" {
+		orderRule = "create_time DESC"
+	}
+	err = global.MYSQL["hz_crm"].Where(condition, pars...).Order(orderRule).Find(&items).Error
+	return
+}

+ 24 - 0
models/crm/eta_training_video_view_log.go

@@ -0,0 +1,24 @@
+package crm
+
+import (
+	"eta/eta_docs/global"
+	"time"
+)
+
+// EtaTrainingVideoViewLog ETA培训视频访问日志
+type EtaTrainingVideoViewLog struct {
+	Id                     int       `gorm:"primaryKey;column:id;type:int(10) unsigned;not null" json:"-"`
+	EtaTrainingVideoId     int       `gorm:"index:idx_video_id;column:eta_training_video_id;type:int(10) unsigned;not null;default:0" json:"etaTrainingVideoId"` // 培训视频ID
+	EtaBusinessId          int       `gorm:"column:eta_business_id;type:int(10) unsigned;not null;default:0" json:"etaBusinessId"`                               // 商家ID
+	EtaBusinessCodeEncrypt string    `gorm:"column:eta_business_code_encrypt;type:varchar(128);not null;default:''" json:"etaBusinessCodeEncrypt"`               // 商家编码encrypt
+	CreateTime             time.Time `gorm:"column:create_time;type:datetime" json:"createTime"`                                                                 // 创建时间
+}
+
+func (m *EtaTrainingVideoViewLog) TableName() string {
+	return "eta_training_video_view_log"
+}
+
+func (m *EtaTrainingVideoViewLog) Create() (err error) {
+	err = global.MYSQL["hz_crm"].Create(m).Error
+	return
+}

+ 18 - 0
models/request/eta_training_video.go

@@ -0,0 +1,18 @@
+package request
+
+// TrainingVideoPageListReq 培训视频分页列表请求体
+type TrainingVideoPageListReq struct {
+	PageSize     int64  `json:"page_size" form:"page_size"`
+	CurrentIndex int64  `json:"current_index" form:"current_index"`
+	Keyword      string `json:"keyword" form:"keyword"`
+	ClassifyId   int    `json:"classify_id" form:"classify_id"`
+	TagIds       string `json:"tag_ids" form:"tag_ids"`
+	IsNew        bool   `json:"is_new" form:"is_new"`
+	IsHot        bool   `json:"is_hot" form:"is_hot"`
+}
+
+// TrainingVideoDetailReq 培训视频详情请求体
+type TrainingVideoDetailReq struct {
+	VideoCode    string `json:"video_code" form:"video_code"`
+	BusinessCode string `json:"business_code" form:"business_code" description:"商家编码"`
+}

+ 41 - 0
models/response/eta_training_video.go

@@ -0,0 +1,41 @@
+package response
+
+// EtaTrainingVideoClassifyResp 视频分类响应体
+type EtaTrainingVideoClassifyResp struct {
+	List []*EtaTrainingVideoClassifyItem
+}
+
+// EtaTrainingVideoClassifyItem 视频分类信息
+type EtaTrainingVideoClassifyItem struct {
+	ClassifyId   int    `description:"视频分类ID"`
+	ClassifyName string `description:"视频分类名称"`
+	ParentId     int    `description:"父级ID"`
+	Sort         int    `description:"排序"`
+	Children     []*EtaTrainingVideoClassifyItem
+}
+
+// EtaTrainingVideoTagItem 标签信息
+type EtaTrainingVideoTagItem struct {
+	TagId      int    `description:"标签ID"`
+	TagName    string `description:"标签名称"`
+	VideoTotal int    `description:"关联视频数"`
+	CreateTime string `description:"创建时间"`
+	ModifyTime string `description:"更新时间"`
+}
+
+// EtaTrainingVideoItem ETA视频信息
+type EtaTrainingVideoItem struct {
+	VideoId      int                           `description:"视频ID"`
+	VideoCode    string                        `description:"视频唯一编码"`
+	Title        string                        `description:"视频标题"`
+	Introduce    string                        `description:"视频简介"`
+	Classify     *EtaTrainingVideoClassifyItem `description:"视频分类"`
+	Tags         []*EtaTrainingVideoTagItem    `description:"视频标签"`
+	CoverImg     string                        `description:"封面图"`
+	VideoUrl     string                        `description:"视频地址"`
+	PublishState int                           `description:"发布状态:0-未发布;1-已发布"`
+	PublishTime  string                        `description:"发布时间"`
+	ViewTotal    int                           `description:"访问量"`
+	CreateTime   string                        `description:"创建时间"`
+	ModifyTime   string                        `description:"更新时间"`
+}

+ 16 - 0
routers/eta_training_video.go

@@ -0,0 +1,16 @@
+package routers
+
+import (
+	"eta/eta_docs/controller"
+	"github.com/gin-gonic/gin"
+)
+
+func InitEtaTrainingVideo(r *gin.RouterGroup) {
+	c := new(controller.EtaTrainingVideoController)
+	authGroup := r.Group("training_video/")
+
+	authGroup.GET("classify/tree", c.ClassifyTree)
+	authGroup.GET("tag/list", c.TagList)
+	authGroup.GET("video/page_list", c.VideoPageList)
+	authGroup.GET("video/detail", c.VideoDetail)
+}

+ 109 - 0
services/eta_training_video.go

@@ -0,0 +1,109 @@
+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
+}

+ 44 - 0
services/eta_training_video_classify.go

@@ -0,0 +1,44 @@
+package services
+
+import "eta/eta_docs/models/response"
+
+// GetClassifyTreeRecursive 递归菜单树
+func GetClassifyTreeRecursive(list []*response.EtaTrainingVideoClassifyItem, parentId int) []*response.EtaTrainingVideoClassifyItem {
+	res := make([]*response.EtaTrainingVideoClassifyItem, 0)
+	for _, v := range list {
+		if v.ParentId == parentId {
+			v.Children = GetClassifyTreeRecursive(list, v.ClassifyId)
+			res = append(res, v)
+		}
+	}
+	return res
+}
+
+// GetClassifyChildrenIdsRecursive 遍历子菜单IDs
+//func GetClassifyChildrenIdsRecursive(list []*eta_training_video.EtaTrainingVideoClassify, parentId int) []int {
+//	res := make([]int, 0)
+//	for _, v := range list {
+//		if v.ParentId == parentId {
+//			ids := GetClassifyChildrenIdsRecursive(list, v.EtaTrainingVideoClassifyId)
+//			res = append(res, v.EtaTrainingVideoClassifyId)
+//			res = append(res, ids...)
+//		}
+//	}
+//	return res
+//}
+//
+//// GetClassifyParentsRecursive 遍历子分类的所有父级分类
+//func GetClassifyParentsRecursive(list []*eta_training_video.EtaTrainingVideoClassify, parentId int) []*eta_training_video.EtaTrainingVideoClassify {
+//	res := make([]*eta_training_video.EtaTrainingVideoClassify, 0)
+//	for _, v := range list {
+//		if v.EtaTrainingVideoClassifyId == parentId {
+//			res = append(res, v)
+//			parents := make([]*eta_training_video.EtaTrainingVideoClassify, 0)
+//			if v.ParentId > 0 {
+//				parents = GetClassifyParentsRecursive(list, v.ParentId)
+//			}
+//			res = append(res, parents...)
+//		}
+//	}
+//	return res
+//}