micro_roadshow.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "math"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. )
  12. // GetMicroRoadShowPageList 获取微路演列表
  13. func GetMicroRoadShowPageList(pageSize, currentIndex, audioId, videoId int, keywords string) (respList []*models.MicroRoadShowPageList, total int, err error) {
  14. var e error
  15. // 根据每页数据量获取音视频配比
  16. audioRatio, videoRatio, audioPageNum, videoPageNum, sliceNum, e := getMicroRoadShowDataRatio(pageSize)
  17. if e != nil {
  18. err = errors.New("获取微路演列表数据音视频配比失败, Err: " + e.Error())
  19. return
  20. }
  21. audioTotal := 0
  22. videoTotal := 0
  23. audioList := make([]*models.MicroRoadShowPageList, 0)
  24. videoList := make([]*models.MicroRoadShowPageList, 0)
  25. if keywords != "" {
  26. keywords = "%" + keywords + "%"
  27. }
  28. // 查询指定音频/视频时, 调整比例为1方便后面组合数据
  29. if audioId > 0 || videoId > 0 {
  30. audioRatio = 1
  31. videoRatio = 1
  32. }
  33. wg := sync.WaitGroup{}
  34. wg.Add(2)
  35. // 分页查询音频
  36. go func() {
  37. defer wg.Done()
  38. // 如果筛选条件为指定视频ID则不做音频查询
  39. if videoId > 0 {
  40. return
  41. }
  42. var audioCond string
  43. var audioPars []interface{}
  44. if keywords != "" {
  45. audioCond += ` AND a.voice_name LIKE ? OR b.label LIKE ?`
  46. audioPars = append(audioPars, keywords, keywords)
  47. }
  48. if audioId > 0 {
  49. audioCond += ` AND a.activity_voice_id = ?`
  50. audioPars = append(audioPars, audioId)
  51. }
  52. audioStartSize := 0
  53. if currentIndex > 1 {
  54. audioStartSize = (currentIndex - 1) * audioPageNum
  55. }
  56. audioTotal, audioList, e = models.GetMicroRoadShowAudioPageList(audioStartSize, audioPageNum, audioCond, audioPars)
  57. }()
  58. // 分页查询视频
  59. go func() {
  60. defer wg.Done()
  61. // 如果筛选条件为指定音频ID则不做视频查询
  62. if audioId > 0 {
  63. return
  64. }
  65. var videoCond string
  66. var videoPars []interface{}
  67. if keywords != "" {
  68. videoCond += ` AND video_name LIKE ?`
  69. videoPars = append(videoPars, keywords)
  70. }
  71. if videoId > 0 {
  72. videoCond += ` AND video_id = ?`
  73. videoPars = append(videoPars, videoId)
  74. }
  75. videoStartSize := 0
  76. if currentIndex > 1 {
  77. videoStartSize = (currentIndex - 1) * videoPageNum
  78. }
  79. videoTotal, videoList, e = models.GetMicroRoadShowVideoPageList(videoStartSize, audioPageNum, videoCond, videoPars)
  80. }()
  81. wg.Wait()
  82. if e != nil {
  83. err = errors.New("获取微路演音视频列表失败, Err: " + e.Error())
  84. return
  85. }
  86. // 按比例组合列表
  87. audioLen := len(audioList)
  88. videoLen := len(videoList)
  89. for i := 0; i < sliceNum; i++ {
  90. // 音频
  91. a := i * audioRatio // 0 4 8 12
  92. oa := a + audioRatio // 4 8 12 16
  93. if oa <= audioLen {
  94. for k1 := a; k1 < oa; k1++ {
  95. respList = append(respList, audioList[k1])
  96. }
  97. }
  98. // 视频
  99. b := i * videoRatio // 0 1 2 3
  100. ob := b + videoRatio // 1 2 3 4
  101. if ob <= videoLen {
  102. for k2 := b; k2 < ob; k2++ {
  103. respList = append(respList, videoList[k2])
  104. }
  105. }
  106. }
  107. total = audioTotal + videoTotal
  108. return
  109. }
  110. // getMicroRoadShowDataRatio 获取微路演列表数据音视频配比
  111. func getMicroRoadShowDataRatio(pageSize int) (audioRatio, videoRatio, audioPageNum, videoPageNum, sliceNum int, err error) {
  112. if pageSize <= 0 {
  113. pageSize = utils.PageSize20
  114. }
  115. key := models.MicroRoadShowListDataRatioConfigKey
  116. config, e := models.GetConfigByCode(key)
  117. if e != nil && e.Error() != utils.ErrNoRow() {
  118. err = errors.New("获取微路演列表数据量配置失败, Err: " + e.Error())
  119. return
  120. }
  121. // 默认音频视频展示比例为4:1
  122. ratio := "4:1"
  123. if config != nil {
  124. ratio = config.ConfigValue
  125. }
  126. ratioArr := strings.Split(ratio, ":")
  127. if len(ratioArr) != 2 {
  128. err = errors.New("微路演列表数据量配比有误")
  129. return
  130. }
  131. audioRatio, e = strconv.Atoi(ratioArr[0])
  132. if e != nil {
  133. err = errors.New("微路演列表数据量配比有误")
  134. return
  135. }
  136. videoRatio, e = strconv.Atoi(ratioArr[1])
  137. if e != nil {
  138. err = errors.New("微路演列表数据量配比有误")
  139. return
  140. }
  141. totalRatio := audioRatio + videoRatio
  142. if totalRatio == 0 {
  143. err = errors.New("微路演列表数据量配比有误")
  144. return
  145. }
  146. // 每比率对应数量(向上取整)
  147. sliceNum = int(math.Ceil(float64(pageSize) / float64(totalRatio)))
  148. audioPageNum = audioRatio * sliceNum
  149. videoPageNum = videoRatio * sliceNum
  150. return
  151. }
  152. // GetMicroRoadShowDefaultImgConfig 获取微路演默认图配置
  153. func GetMicroRoadShowDefaultImgConfig() (audioMap, videoMap map[int]string, err error) {
  154. audioMap = make(map[int]string, 0)
  155. videoMap = make(map[int]string, 0)
  156. key := models.MicroRoadshowDefaultImgKey
  157. conf, e := models.GetConfigByCode(key)
  158. if e != nil {
  159. err = errors.New("获取微路演默认图配置失败, Err: " + e.Error())
  160. return
  161. }
  162. if conf.ConfigValue == "" {
  163. err = errors.New("获取微路演默认图配置有误")
  164. return
  165. }
  166. list := new(models.MicroRoadShowDefaultImgList)
  167. if e = json.Unmarshal([]byte(conf.ConfigValue), &list); e != nil {
  168. err = errors.New("微路演默认图配置配置值解析失败, Err: " + e.Error())
  169. return
  170. }
  171. audioList := list.Audio
  172. for i := range audioList {
  173. audioMap[audioList[i].ChartPermissionId] = audioList[i].ImgUrl
  174. }
  175. videoList := list.Video
  176. for i := range videoList {
  177. videoMap[videoList[i].ChartPermissionId] = videoList[i].ImgUrl
  178. }
  179. return
  180. }
  181. // GetHomeNewestList 获取首页最新列表
  182. func GetHomeNewestList(userId, companyId int, condition string, pars []interface{}) (resp []*models.HomeArticle, total int, err error) {
  183. resp = make([]*models.HomeArticle, 0)
  184. unionList, e := models.GetHomeNewestListUnionList(condition, pars, 0, 20)
  185. if e != nil {
  186. err = errors.New("获取首页最新列表失败")
  187. return
  188. }
  189. unionTotal, e := models.GetHomeNewestListUnionCount(condition, pars)
  190. if e != nil {
  191. err = errors.New("获取首页最新列表总数失败")
  192. return
  193. }
  194. total = unionTotal
  195. // 用户权限
  196. authInfo, permissionArr, e := GetUserRaiPermissionInfo(userId, companyId)
  197. if e != nil {
  198. err = errors.New("获取用户权限失败, Err: " + e.Error())
  199. return
  200. }
  201. // 获取默认图配置
  202. audioMap, videoMap, e := GetMicroRoadShowDefaultImgConfig()
  203. if e != nil {
  204. err = errors.New("获取微路演默认图配置失败, Err: " + e.Error())
  205. return
  206. }
  207. // 此处没有直接使用HomeArticle结构体而是多加了一层, 纯粹是为了方便前端区分纪要和微路演音频=_=!
  208. for _, v := range unionList {
  209. item := new(models.HomeArticle)
  210. item.HomeType = v.HomeType
  211. // 纪要
  212. if item.HomeType == 0 {
  213. item.ArticleId = v.ArticleId
  214. item.Title = v.Title
  215. item.TitleEn = v.TitleEn
  216. item.UpdateFrequency = v.UpdateFrequency
  217. item.CreateDate = v.CreateDate
  218. item.PublishDate = v.PublishDate
  219. item.Body = v.Body
  220. item.BodyHtml = v.BodyHtml
  221. item.Abstract = v.Abstract
  222. item.CategoryName = v.CategoryName
  223. item.SubCategoryName = v.SubCategoryName
  224. item.ExpertBackground = v.ExpertBackground
  225. item.IsResearch = v.IsResearch
  226. item.Pv = v.Pv
  227. item.ImgUrlPc = v.ImgUrlPc
  228. item.CategoryId = v.CategoryId
  229. item.HttpUrl = v.HttpUrl
  230. item.IsNeedJump = v.IsNeedJump
  231. item.Source = v.Source
  232. item.Annotation = v.Annotation
  233. }
  234. // 音频
  235. if v.HomeType == 1 {
  236. ad := new(models.MicroAudioUnionList)
  237. ad.Id = v.Id
  238. ad.AudioTitle = v.AudioTitle
  239. ad.AudioResourceUrl = v.AudioResourceUrl
  240. ad.AudioType = v.AudioType
  241. ad.AudioPublishTime = v.AudioPublishTime
  242. ad.AudioImgUrl = v.AudioImgUrl
  243. ad.AudioChartPermissionId = v.AudioChartPermissionId
  244. ad.AudioChartPermissionName = v.AudioChartPermissionName
  245. ad.AudioPlaySeconds = v.AudioPlaySeconds
  246. ad.AudioPlaySeconds = v.AudioPlaySeconds
  247. ad.AudioActivityId = v.AudioActivityId
  248. item.MicroAudio = ad
  249. // 默认图
  250. if ad.AudioImgUrl == "" {
  251. if ad.AudioType == 1 {
  252. ad.AudioImgUrl = audioMap[ad.AudioChartPermissionId]
  253. } else {
  254. ad.AudioImgUrl = videoMap[ad.AudioChartPermissionId]
  255. }
  256. }
  257. // 权限
  258. au := new(models.UserPermissionAuthInfo)
  259. au.SellerName = authInfo.SellerName
  260. au.SellerMobile = authInfo.SellerMobile
  261. au.HasPermission = authInfo.HasPermission
  262. if authInfo.HasPermission == 1 {
  263. if !utils.InArrayByStr(permissionArr, ad.AudioChartPermissionName) {
  264. au.HasPermission = 2
  265. }
  266. }
  267. if authInfo.HasPermission == 2 {
  268. if ad.AudioType == 1 {
  269. au.PopupMsg = UserPermissionPopupMsgCallActivity
  270. } else {
  271. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  272. }
  273. }
  274. if authInfo.HasPermission == 3 {
  275. if ad.AudioType == 1 {
  276. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  277. } else {
  278. au.PopupMsg = UserPermissionPopupMsgApplyMicroVideo
  279. }
  280. }
  281. ad.AuthInfo = au
  282. }
  283. resp = append(resp, item)
  284. }
  285. return
  286. }