english_video.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/system"
  7. "hongze/hz_crm_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // GetEnglishVideoCoverList 封面库列表
  13. func GetEnglishVideoCoverList(condition string, pars []interface{}, orderRule string, startSize, pageSize int) (total int, resp *models.EnglishVideoCoverListResp, err error) {
  14. resp = new(models.EnglishVideoCoverListResp)
  15. total, list, e := models.GetEnglishVideoCoverPageList(condition, pars, orderRule, startSize, pageSize)
  16. if e != nil {
  17. err = errors.New("获取封面库列表失败, Err:" + e.Error())
  18. return
  19. }
  20. if len(list) == 0 {
  21. return
  22. }
  23. respList := make([]*models.EnglishVideoCoverItem, 0)
  24. for _, v := range list {
  25. item := &models.EnglishVideoCoverItem{
  26. Id: v.Id,
  27. CoverName: v.CoverName,
  28. CoverUrl: v.CoverUrl,
  29. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  30. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  31. }
  32. respList = append(respList, item)
  33. }
  34. resp.List = respList
  35. return
  36. }
  37. // SaveEnglishVideoCover 保存视频封面
  38. func SaveEnglishVideoCover(coverId int, coverName, coverUrl string) (resp *models.EnglishVideoCoverItem, errMsg string, err error) {
  39. resp = new(models.EnglishVideoCoverItem)
  40. nowTime := time.Now().Local()
  41. // 更新
  42. if coverId > 0 {
  43. item, e := models.GetEnglishVideoCoverById(coverId)
  44. if e != nil {
  45. if e.Error() == utils.ErrNoRow() {
  46. errMsg = "封面不存在"
  47. err = errors.New(errMsg)
  48. return
  49. }
  50. errMsg = "封面信息有误"
  51. err = errors.New("获取封面图失败, Err:" + e.Error())
  52. return
  53. }
  54. updateCols := make([]string, 0)
  55. updateCols = append(updateCols, "CoverName", "CoverUrl", "ModifyTime")
  56. item.CoverName = coverName
  57. item.CoverUrl = coverUrl
  58. item.ModifyTime = nowTime
  59. if e := item.Update(updateCols); e != nil {
  60. errMsg = "更新失败"
  61. err = errors.New("更新封面图失败, Err:" + e.Error())
  62. }
  63. resp = formatEnglishVideoCoverItem(item)
  64. return
  65. }
  66. // 新增
  67. newCover := &models.EnglishVideoCover{
  68. CoverName: coverName,
  69. CoverUrl: coverUrl,
  70. CreateTime: nowTime,
  71. ModifyTime: nowTime,
  72. }
  73. if e := newCover.Add(); e != nil {
  74. errMsg = "新增失败"
  75. err = errors.New("新增封面图失败, Err:" + e.Error())
  76. }
  77. resp = formatEnglishVideoCoverItem(newCover)
  78. return
  79. }
  80. func formatEnglishVideoCoverItem(item *models.EnglishVideoCover) (resp *models.EnglishVideoCoverItem) {
  81. resp = new(models.EnglishVideoCoverItem)
  82. if item != nil {
  83. resp.Id = item.Id
  84. resp.CoverName = item.CoverName
  85. resp.CoverUrl = item.CoverUrl
  86. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  87. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  88. }
  89. return
  90. }
  91. // SoftDeleteEnglishVideoCover 删除封面图
  92. func SoftDeleteEnglishVideoCover(coverId int) (errMsg string, err error) {
  93. if _, e := models.GetEnglishVideoCoverById(coverId); e != nil {
  94. errMsg = "封面图信息有误"
  95. err = errors.New("获取封面图失败, Err:" + e.Error())
  96. return
  97. }
  98. if e := models.DeleteEnglishVideoCover(coverId); e != nil {
  99. errMsg = "删除失败"
  100. err = errors.New("删除封面图失败, Err:" + e.Error())
  101. }
  102. return
  103. }
  104. // SaveEnglishVideo 创建线上路演
  105. func SaveEnglishVideo(req models.SaveEnglishVideoReq, adminInfo *system.Admin) (newReportId int, videoCode string, err error, errMsg string) {
  106. // 查询分类是否存在
  107. req.Overview = strings.Replace(req.Overview, "Powered by Froala Editor", "", -1)
  108. var classifyIds []int
  109. classifyIds = append(classifyIds, req.ClassifyIdFirst)
  110. if req.ClassifyIdSecond > 0 {
  111. classifyIds = append(classifyIds, req.ClassifyIdSecond)
  112. }
  113. classifyList, err := models.GetEnglishReportClassifyByIds(classifyIds)
  114. if err != nil {
  115. err = fmt.Errorf("查询线上路演分类出错")
  116. errMsg = fmt.Sprintf("查询线上路演分类出错, Err: %v", err)
  117. return
  118. }
  119. firstClassify := new(models.EnglishClassify)
  120. secondClassify := new(models.EnglishClassify)
  121. for _, v := range classifyList {
  122. if v.ParentId == v.RootId {
  123. firstClassify = v
  124. } else if v.ParentId > 0 {
  125. secondClassify = v
  126. }
  127. }
  128. if firstClassify == nil {
  129. err = fmt.Errorf("一级分类不存在")
  130. return
  131. }
  132. if req.ClassifyIdSecond > 0 && secondClassify == nil {
  133. err = fmt.Errorf("二级分类不存在")
  134. return
  135. }
  136. if req.ClassifyIdSecond > 0 && secondClassify.ParentId != firstClassify.Id {
  137. err = fmt.Errorf("二级分类错误")
  138. return
  139. }
  140. if req.Id > 0 {
  141. item, e := models.GetEnglishVideoItemById(req.Id)
  142. if e != nil {
  143. if e.Error() == utils.ErrNoRow() {
  144. err = fmt.Errorf("路演视频不存在")
  145. return
  146. }
  147. err = fmt.Errorf("查询线上路演出错")
  148. errMsg = fmt.Sprintf("查询线上路演出错, Err: %v", e)
  149. return
  150. }
  151. item.ClassifyIdFirst = req.ClassifyIdFirst
  152. item.ClassifyNameFirst = firstClassify.ClassifyName
  153. item.ClassifyIdSecond = req.ClassifyIdSecond
  154. if secondClassify != nil {
  155. item.ClassifyNameSecond = secondClassify.ClassifyName
  156. }
  157. item.Title = req.Title
  158. item.Abstract = req.Abstract
  159. //item.Author = req.Author
  160. item.State = req.State
  161. item.VideoUrl = req.VideoUrl
  162. item.VideoCoverUrl = req.VideoCoverUrl
  163. item.VideoSeconds = req.VideoSeconds
  164. item.Overview = req.Overview
  165. e = models.EditEnglishVideo(item, req.Id)
  166. if e != nil {
  167. err = errors.New("更新报告失败")
  168. errMsg = fmt.Sprintf("更新报告失败, Err: %v", e)
  169. return
  170. }
  171. newReportId = item.Id
  172. videoCode = item.VideoCode
  173. return
  174. }
  175. item := new(models.EnglishVideo)
  176. item.ClassifyIdFirst = req.ClassifyIdFirst
  177. item.ClassifyNameFirst = firstClassify.ClassifyName
  178. item.ClassifyIdSecond = req.ClassifyIdSecond
  179. if secondClassify != nil {
  180. item.ClassifyNameSecond = secondClassify.ClassifyName
  181. }
  182. item.VideoUrl = req.VideoUrl
  183. item.VideoCoverUrl = req.VideoCoverUrl
  184. item.VideoSeconds = req.VideoSeconds
  185. item.Title = req.Title
  186. item.Abstract = req.Abstract
  187. //item.Author = req.Author
  188. item.State = req.State
  189. if req.State == 2 {
  190. item.PublishTime = time.Now()
  191. }
  192. item.Overview = req.Overview
  193. item.AdminId = adminInfo.AdminId
  194. item.AdminRealName = adminInfo.RealName
  195. item.CreateTime = time.Now()
  196. item.ModifyTime = time.Now()
  197. e := models.AddEnglishVideo(item)
  198. if e != nil {
  199. err = errors.New("新增路演视频失败")
  200. errMsg = fmt.Sprintf("新增路演视频失败, Err: %v", e)
  201. return
  202. }
  203. newReportId = item.Id
  204. videoCode = utils.MD5(strconv.Itoa(item.Id))
  205. //修改唯一编码
  206. {
  207. go models.ModifyEnglishVideoCode(item.Id, videoCode)
  208. }
  209. if req.State == 2 {
  210. go UpdateEnglishVideoEs(newReportId, 2)
  211. }
  212. return
  213. }