eta_training_video.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package eta_training_video
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. VideoUnPublish = 0
  11. VideoPublished = 1
  12. )
  13. // EtaTrainingVideo ETA培训视频
  14. type EtaTrainingVideo struct {
  15. EtaTrainingVideoId int `orm:"column(eta_training_video_id);pk"`
  16. VideoCode string `description:"视频唯一编码"`
  17. Title string `description:"视频标题"`
  18. Introduce string `description:"视频简介"`
  19. ClassifyIds string `description:"视频分类IDs, 英文逗号拼接"`
  20. TagIds string `description:"标签IDs, 英文逗号拼接"`
  21. CoverImg string `description:"封面图"`
  22. VideoUrl string `description:"视频地址"`
  23. PublishState int `description:"发布状态:0-未发布; 1-已发布"`
  24. PublishTime time.Time `description:"发布时间"`
  25. ViewTotal int `description:"访问量"`
  26. CreateTime time.Time `description:"创建时间"`
  27. ModifyTime time.Time `description:"更新时间"`
  28. }
  29. func (m *EtaTrainingVideo) TableName() string {
  30. return "eta_training_video"
  31. }
  32. func (m *EtaTrainingVideo) PrimaryId() string {
  33. return VideoColumns.EtaTrainingVideoId
  34. }
  35. var VideoColumns = struct {
  36. EtaTrainingVideoId string
  37. VideoCode string
  38. Title string
  39. Introduce string
  40. ClassifyIds string
  41. TagIds string
  42. CoverImg string
  43. VideoUrl string
  44. PublishState string
  45. PublishTime string
  46. ViewTotal string
  47. CreateTime string
  48. ModifyTime string
  49. }{
  50. EtaTrainingVideoId: "eta_training_video_id",
  51. VideoCode: "video_code",
  52. Title: "title",
  53. Introduce: "introduce",
  54. ClassifyIds: "classify_ids",
  55. TagIds: "tag_ids",
  56. CoverImg: "cover_img",
  57. VideoUrl: "video_url",
  58. PublishState: "publish_state",
  59. PublishTime: "publish_time",
  60. ViewTotal: "view_total",
  61. CreateTime: "create_time",
  62. ModifyTime: "modify_time",
  63. }
  64. func (m *EtaTrainingVideo) Create() (err error) {
  65. o := orm.NewOrm()
  66. id, err := o.Insert(m)
  67. if err != nil {
  68. return
  69. }
  70. m.EtaTrainingVideoId = int(id)
  71. return
  72. }
  73. func (m *EtaTrainingVideo) CreateMulti(items []*EtaTrainingVideo) (err error) {
  74. if len(items) == 0 {
  75. return
  76. }
  77. o := orm.NewOrm()
  78. _, err = o.InsertMulti(len(items), items)
  79. return
  80. }
  81. func (m *EtaTrainingVideo) Update(cols []string) (err error) {
  82. o := orm.NewOrm()
  83. _, err = o.Update(m, cols...)
  84. return
  85. }
  86. func (m *EtaTrainingVideo) Del() (err error) {
  87. o := orm.NewOrm()
  88. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  89. _, err = o.Raw(sql, m.EtaTrainingVideoId).Exec()
  90. return
  91. }
  92. func (m *EtaTrainingVideo) GetItemById(id int) (item *EtaTrainingVideo, err error) {
  93. o := orm.NewOrm()
  94. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  95. err = o.Raw(sql, id).QueryRow(&item)
  96. return
  97. }
  98. func (m *EtaTrainingVideo) GetItemByCondition(condition string, pars []interface{}) (item *EtaTrainingVideo, err error) {
  99. o := orm.NewOrm()
  100. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  101. err = o.Raw(sql, pars).QueryRow(&item)
  102. return
  103. }
  104. func (m *EtaTrainingVideo) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  105. o := orm.NewOrm()
  106. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  107. err = o.Raw(sql, pars).QueryRow(&count)
  108. return
  109. }
  110. func (m *EtaTrainingVideo) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaTrainingVideo, err error) {
  111. o := orm.NewOrm()
  112. fields := strings.Join(fieldArr, ",")
  113. if len(fieldArr) == 0 {
  114. fields = `*`
  115. }
  116. order := `ORDER BY create_time DESC`
  117. if orderRule != "" {
  118. order = ` ORDER BY ` + orderRule
  119. }
  120. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  121. _, err = o.Raw(sql, pars).QueryRows(&items)
  122. return
  123. }
  124. func (m *EtaTrainingVideo) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaTrainingVideo, err error) {
  125. o := orm.NewOrm()
  126. fields := strings.Join(fieldArr, ",")
  127. if len(fieldArr) == 0 {
  128. fields = `*`
  129. }
  130. order := `ORDER BY create_time DESC`
  131. if orderRule != "" {
  132. order = ` ORDER BY ` + orderRule
  133. }
  134. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  135. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  136. return
  137. }
  138. func (m *EtaTrainingVideo) CreateVideoAndRelates(videoItem *EtaTrainingVideo, classifyRelates []*EtaTrainingVideoClassifyRelate, tagRelates []*EtaTrainingVideoTagRelate) (err error) {
  139. o := orm.NewOrm()
  140. tx, err := o.Begin()
  141. if err != nil {
  142. return
  143. }
  144. defer func() {
  145. if err != nil {
  146. _ = tx.Rollback()
  147. return
  148. }
  149. _ = tx.Commit()
  150. }()
  151. id, e := tx.Insert(videoItem)
  152. if e != nil {
  153. err = fmt.Errorf("insert video err: %s", e.Error())
  154. return
  155. }
  156. videoItem.EtaTrainingVideoId = int(id)
  157. if len(classifyRelates) > 0 {
  158. for _, cr := range classifyRelates {
  159. cr.EtaTrainingVideoId = videoItem.EtaTrainingVideoId
  160. }
  161. if _, e = tx.InsertMulti(len(classifyRelates), classifyRelates); e != nil {
  162. err = fmt.Errorf("insert multi classify relates err: %s", e.Error())
  163. return
  164. }
  165. }
  166. if len(tagRelates) > 0 {
  167. for _, tr := range tagRelates {
  168. tr.EtaTrainingVideoId = videoItem.EtaTrainingVideoId
  169. }
  170. if _, e = tx.InsertMulti(len(tagRelates), tagRelates); e != nil {
  171. err = fmt.Errorf("insert multi tag relates err: %s", e.Error())
  172. return
  173. }
  174. }
  175. return
  176. }
  177. func (m *EtaTrainingVideo) UpdateVideoAndRelates(videoItem *EtaTrainingVideo, updateCols []string, classifyRelates []*EtaTrainingVideoClassifyRelate, tagRelates []*EtaTrainingVideoTagRelate) (err error) {
  178. if videoItem.EtaTrainingVideoId <= 0 {
  179. return
  180. }
  181. o := orm.NewOrm()
  182. tx, err := o.Begin()
  183. if err != nil {
  184. return
  185. }
  186. defer func() {
  187. if err != nil {
  188. _ = tx.Rollback()
  189. return
  190. }
  191. _ = tx.Commit()
  192. }()
  193. if len(updateCols) > 0 {
  194. _, e := tx.Update(videoItem, updateCols...)
  195. if e != nil {
  196. err = fmt.Errorf("update video err: %s", e.Error())
  197. return
  198. }
  199. }
  200. // 分类
  201. sql := `DELETE FROM eta_training_video_classify_relate WHERE eta_training_video_id = ?`
  202. if _, e := tx.Raw(sql, videoItem.EtaTrainingVideoId).Exec(); e != nil {
  203. err = fmt.Errorf("clear classify relates err: %s", e.Error())
  204. return
  205. }
  206. if len(classifyRelates) > 0 {
  207. for _, cr := range classifyRelates {
  208. cr.EtaTrainingVideoId = videoItem.EtaTrainingVideoId
  209. }
  210. if _, e := tx.InsertMulti(len(classifyRelates), classifyRelates); e != nil {
  211. err = fmt.Errorf("insert multi classify relates err: %s", e.Error())
  212. return
  213. }
  214. }
  215. // 标签
  216. sql = `DELETE FROM eta_training_video_tag_relate WHERE eta_training_video_id = ?`
  217. if _, e := tx.Raw(sql, videoItem.EtaTrainingVideoId).Exec(); e != nil {
  218. err = fmt.Errorf("clear tag relates err: %s", e.Error())
  219. return
  220. }
  221. if len(tagRelates) > 0 {
  222. for _, tr := range tagRelates {
  223. tr.EtaTrainingVideoId = videoItem.EtaTrainingVideoId
  224. }
  225. if _, e := tx.InsertMulti(len(tagRelates), tagRelates); e != nil {
  226. err = fmt.Errorf("insert multi tag relates err: %s", e.Error())
  227. return
  228. }
  229. }
  230. return
  231. }
  232. // EtaTrainingVideoAddReq 新增视频请求体
  233. type EtaTrainingVideoAddReq struct {
  234. Title string `description:"视频标题"`
  235. Introduce string `description:"视频简介"`
  236. CoverImg string `description:"封面图"`
  237. VideoUrl string `description:"视频地址"`
  238. ClassifyId int `description:"分类ID"`
  239. TagIds []int `description:"标签IDs"`
  240. }
  241. // EtaTrainingVideoEditReq 编辑视频请求体
  242. type EtaTrainingVideoEditReq struct {
  243. VideoId int `description:"视频ID"`
  244. EtaTrainingVideoAddReq
  245. }
  246. // EtaTrainingVideoPublishReq 发布/取消发布视频请求体
  247. type EtaTrainingVideoPublishReq struct {
  248. VideoId int `description:"视频ID"`
  249. PublishState int `description:"发布状态:0-取消发布;1-发布"`
  250. }
  251. // EtaTrainingVideoRemoveReq 删除视频请求体
  252. type EtaTrainingVideoRemoveReq struct {
  253. VideoId int `description:"视频ID"`
  254. }
  255. // EtaTrainingVideoListResp 视频分页列表响应体
  256. type EtaTrainingVideoListResp struct {
  257. List []*EtaTrainingVideoItem `description:"视频列表数据"`
  258. Paging *paging.PagingItem `description:"分页数据"`
  259. }
  260. // EtaTrainingVideoItem ETA视频信息
  261. type EtaTrainingVideoItem struct {
  262. VideoId int `description:"视频ID"`
  263. VideoCode string `description:"视频唯一编码"`
  264. Title string `description:"视频标题"`
  265. Introduce string `description:"视频简介"`
  266. Classify *EtaTrainingVideoClassifyItem `description:"视频分类"`
  267. Tags []*EtaTrainingVideoTagItem `description:"视频标签"`
  268. CoverImg string `description:"封面图"`
  269. VideoUrl string `description:"视频地址"`
  270. PublishState int `description:"发布状态:0-未发布;1-已发布"`
  271. PublishTime string `description:"发布时间"`
  272. ViewTotal int `description:"访问量"`
  273. CreateTime string `description:"创建时间"`
  274. ModifyTime string `description:"更新时间"`
  275. }