voice_broadcast.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package voice_broadcast
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/models/request"
  6. voiceResp "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/voice_broadcast"
  8. "hongze/hongze_yb/models/tables/voice_section"
  9. "hongze/hongze_yb/services"
  10. "hongze/hongze_yb/services/user"
  11. "hongze/hongze_yb/utils"
  12. "strconv"
  13. "strings"
  14. )
  15. // BroadcastList
  16. // @Description 语音播报列表
  17. // @Param page_index query int false "页码"
  18. // @Param page_size query int false "每页数量"
  19. // @Param broadcast_id query int false "语音播报ID(分享进来的时候筛选用)"
  20. // @Param section_id query int false "板块ID"
  21. // @Param author_id query int false "作者ID(我的语音播报列表)"
  22. // @Param mine_status query int false "语音播报状态:0-未发布 1-已发布 2-全部(我的语音播报列表)"
  23. // @Success 200 {object} []voiceResp.BroadcastListResp
  24. // @failure 400 {string} string "获取失败"
  25. // @Router /list [Post]
  26. func BroadcastList(c *gin.Context) {
  27. var req request.BroadcastListReq
  28. if err := c.Bind(&req); err != nil {
  29. response.Fail("参数有误", c)
  30. return
  31. }
  32. if req.PageIndex == 0 {
  33. req.PageIndex = 1
  34. }
  35. if req.PageSize == 0 {
  36. req.PageSize = utils.PageSize20
  37. }
  38. userinfo := user.GetInfoByClaims(c)
  39. list, err := services.GetVoiceBroadcastList(req.PageIndex, req.PageSize, req.SectionId, req.BroadcastId, req.AuthorId, req.MineStatus, userinfo)
  40. if err != nil {
  41. response.FailMsg("获取语音播报列表失败,"+err.Error(), "QuestionList ErrMsg:"+err.Error(), c)
  42. return
  43. }
  44. isVoiceAdmin, _, err := services.GetVoiceAdminByUserInfo(userinfo)
  45. if err != nil && err != utils.ErrNoRow {
  46. response.FailMsg("获取语音管理员信息失败", "QuestionList ErrMsg:"+err.Error(), c)
  47. return
  48. }
  49. var resp voiceResp.BroadcastListResp
  50. resp.List = list
  51. resp.IsVoiceAdmin = isVoiceAdmin
  52. response.OkData("获取成功", resp, c)
  53. }
  54. // AddBroadcast
  55. // @Description 新增语音播报
  56. // @Param broadcast_name query string true "语音标题"
  57. // @Param section_id query int true "板块ID"
  58. // @Param section_name query string true "板块名称"
  59. // @Param variety_id query int true "品种ID"
  60. // @Param variety_name query string true "品种名称"
  61. // @Param author_id query int true "作者ID"
  62. // @Param author query string true "作者名称"
  63. // @Param Imgs query string false "图片,英文逗号拼接"
  64. // @Param voice_seconds query string true "音频时长"
  65. // @Param voice_size query string true "音频大小"
  66. // @Param voice_url query string true "音频文件地址"
  67. // @Success 200 {string} string "发布成功"
  68. // @failure 400 {string} string "发布失败"
  69. // @Router /add [post]
  70. func AddBroadcast(c *gin.Context) {
  71. var req request.SaveBroadcastReq
  72. if err := c.ShouldBind(&req); err != nil {
  73. response.Fail("参数有误", c)
  74. return
  75. }
  76. // 参数校验
  77. if req.BroadcastName == "" {
  78. response.Fail("请输入标题", c)
  79. return
  80. }
  81. if req.SectionId <= 0 || req.SectionName == "" {
  82. response.Fail("请选择品种", c)
  83. return
  84. }
  85. if req.SectionId <= 0 || req.SectionName == "" {
  86. response.Fail("请选择板块", c)
  87. return
  88. }
  89. if req.VoiceUrl == "" || req.VoiceSeconds == "" || req.VoiceSize == "" {
  90. response.Fail("请上传音频", c)
  91. return
  92. }
  93. if req.Imgs != "" {
  94. imgList := strings.Split(req.Imgs, ",")
  95. if len(imgList) > 5 {
  96. response.Fail("最多插入五张图片", c)
  97. return
  98. }
  99. }
  100. userInfo := user.GetInfoByClaims(c)
  101. // 新增
  102. resp, e := services.CreateVoiceBroadcast(req.SectionId, req.VarietyId, req.AuthorId, int(userInfo.UserID), req.BroadcastName, req.SectionName, req.VarietyName,
  103. req.Author, req.VoiceSeconds, req.VoiceSize, req.VoiceUrl, req.Imgs)
  104. if e != nil {
  105. response.FailMsg("新增失败", e.Error(), c)
  106. return
  107. }
  108. response.OkData("操作成功", resp, c)
  109. }
  110. // EditBroadcast
  111. // @Description 编辑语音播报
  112. // @Param broadcast_id query int true "语音播报ID"
  113. // @Param broadcast_name query string true "语音标题"
  114. // @Param section_id query int true "板块ID"
  115. // @Param section_name query string true "板块名称"
  116. // @Param variety_id query int true "品种ID"
  117. // @Param variety_name query string true "品种名称"
  118. // @Param author_id query int true "作者ID"
  119. // @Param author query string true "作者名称"
  120. // @Param Imgs query string false "图片,英文逗号拼接"
  121. // @Param voice_seconds query string true "音频时长"
  122. // @Param voice_size query string true "音频大小"
  123. // @Param voice_url query string true "音频文件地址"
  124. // @Success 200 {string} string "发布成功"
  125. // @failure 400 {string} string "发布失败"
  126. // @Router /edit [post]
  127. func EditBroadcast(c *gin.Context) {
  128. var req request.SaveBroadcastReq
  129. if err := c.ShouldBind(&req); err != nil {
  130. response.Fail("参数有误", c)
  131. return
  132. }
  133. // 参数校验
  134. if req.BroadcastId <= 0 {
  135. response.Fail("参数有误", c)
  136. return
  137. }
  138. if req.BroadcastName == "" {
  139. response.Fail("请输入标题", c)
  140. return
  141. }
  142. if req.SectionId <= 0 || req.SectionName == "" {
  143. response.Fail("请选择品种", c)
  144. return
  145. }
  146. if req.SectionId <= 0 || req.SectionName == "" {
  147. response.Fail("请选择板块", c)
  148. return
  149. }
  150. if req.VoiceUrl == "" || req.VoiceSeconds == "" || req.VoiceSize == "" {
  151. response.Fail("请上传音频", c)
  152. return
  153. }
  154. if req.Imgs != "" {
  155. imgList := strings.Split(req.Imgs, ",")
  156. if len(imgList) > 5 {
  157. response.Fail("最多插入五张图片", c)
  158. return
  159. }
  160. }
  161. userInfo := user.GetInfoByClaims(c)
  162. // 编辑
  163. resp, e := services.EditVoiceBroadcast(req.BroadcastId, req.SectionId, req.VarietyId, req.AuthorId, int(userInfo.UserID), req.BroadcastName, req.SectionName, req.VarietyName,
  164. req.Author, req.VoiceSeconds, req.VoiceSize, req.VoiceUrl, req.Imgs)
  165. if e != nil {
  166. response.FailMsg("新增失败", e.Error(), c)
  167. return
  168. }
  169. response.OkData("操作成功", resp, c)
  170. }
  171. // PublishBroadcast
  172. // @Description 发布语音播报
  173. // @Param broadcast_id query int true "语音播报ID"
  174. // @Param publish_type query int true "发布类型: 1-发布 2-定时发布"
  175. // @Param pre_publish_time query string false "预发布时间"
  176. // @Success 200 {string} string "发布成功"
  177. // @failure 400 {string} string "发布失败"
  178. // @Router /publish [post]
  179. func PublishBroadcast(c *gin.Context) {
  180. var req request.PublishBroadcastReq
  181. if err := c.ShouldBind(&req); err != nil {
  182. response.Fail("参数有误", c)
  183. return
  184. }
  185. // 参数校验
  186. if req.BroadcastId <= 0 {
  187. response.Fail("参数有误", c)
  188. return
  189. }
  190. if req.PublishType <= 0 {
  191. response.Fail("请选择发布类型", c)
  192. return
  193. }
  194. if req.PublishType == 2 && req.PrePublishTime == "" {
  195. response.Fail("定时发布请选择发布时间", c)
  196. return
  197. }
  198. // 发布
  199. if e := services.PublishVoiceBroadcast(req.BroadcastId, req.PublishType, req.PrePublishTime); e != nil {
  200. response.FailMsg("发布失败", e.Error(), c)
  201. return
  202. }
  203. response.Ok("操作成功", c)
  204. }
  205. // SectionList
  206. // @Description 语音播报板块列表
  207. // @Success 200 {object} []voiceResp.VarietyList
  208. // @failure 400 {string} string "获取失败"
  209. // @Router /section/list [get]
  210. func SectionList(c *gin.Context) {
  211. sList, err := voice_section.GetVoiceSection()
  212. if err != nil {
  213. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  214. }
  215. vList, err := voice_section.GetVoiceVariety()
  216. if err != nil {
  217. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  218. }
  219. var sectionList []voiceResp.SectionList
  220. var varietyList []voiceResp.VarietyList
  221. var resp []voiceResp.VarietyList
  222. //var resp voiceResp.SectionListResp
  223. //for _, s := range sList {
  224. // section := voiceResp.SectionList{
  225. // SectionId: s.SectionId,
  226. // SectionName: s.SectionName,
  227. // Status: s.Status,
  228. // }
  229. // sectionList = append(sectionList, section)
  230. //}
  231. var newsList []*voice_section.VoiceSection
  232. //var bannedSectionList []*voice_section.VoiceSection
  233. //查找被禁用的板块ids
  234. var bannedIds []int
  235. for _, section := range sList {
  236. if section.Status == 0 {
  237. //bannedSectionList = append(bannedSectionList, section)
  238. bannedIds = append(bannedIds, section.SectionId)
  239. } else {
  240. newsList = append(newsList, section)
  241. }
  242. }
  243. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  244. var lists []*voice_broadcast.VoiceBroadcast
  245. if len(bannedIds) > 0 {
  246. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  247. if err != nil {
  248. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  249. }
  250. }
  251. //被禁用板块有语音,依然显示该板块
  252. if len(lists) > 0 {
  253. //清空切片,用新的
  254. newsList = newsList[0:0]
  255. bannedMap := make(map[int]int)
  256. for _, broadcast := range lists {
  257. bannedMap[broadcast.SectionId] = broadcast.SectionId
  258. }
  259. for _, section := range sList {
  260. _, ok := bannedMap[section.SectionId]
  261. if section.Status != 0 || ok {
  262. newsList = append(newsList, section)
  263. }
  264. }
  265. }
  266. for _, v := range vList {
  267. variety := voiceResp.VarietyList{
  268. VarietyId: v.VarietyId,
  269. VarietyName: v.VarietyName,
  270. }
  271. varietyList = append(varietyList, variety)
  272. }
  273. for _, v := range varietyList {
  274. for _, s := range newsList {
  275. if v.VarietyId == s.VarietyId {
  276. section := voiceResp.SectionList{
  277. ImgUrl: s.ImgUrl,
  278. SectionId: s.SectionId,
  279. SectionName: s.SectionName,
  280. Status: s.Status,
  281. }
  282. sectionList = append(sectionList, section)
  283. }
  284. }
  285. if len(sectionList) == 0 {
  286. continue
  287. }
  288. v.Children = sectionList
  289. resp = append(resp, v)
  290. sectionList = []voiceResp.SectionList{}
  291. }
  292. response.OkData("上传成功", resp, c)
  293. }
  294. // DelBroadcast
  295. // @Description 删除语音播报
  296. // @Param broadcast_id query int false "语音播报id"
  297. // @Success 200 {string} string "删除成功"
  298. // @failure 400 {string} string "删除失败"
  299. // @Router /delete [get]
  300. func DelBroadcast(c *gin.Context) {
  301. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  302. broadcastId, err := strconv.Atoi(sbroadcastId)
  303. if err != nil {
  304. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  305. }
  306. if broadcastId <= 0 {
  307. response.FailMsg("参数错误", "参数有误", c)
  308. return
  309. }
  310. var item voice_broadcast.VoiceBroadcast
  311. item.BroadcastId = broadcastId
  312. err = item.DelVoiceBroadcast()
  313. if err != nil {
  314. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  315. }
  316. response.Ok("删除成功", c)
  317. }
  318. // AddStatistics
  319. // @Description 新增语音播报记录
  320. // @Param file query string true "音频文件"
  321. // @Success 200 {string} string "新增成功"
  322. // @failure 400 {string} string "新增失败"
  323. // @Router /statistics/add [post]
  324. func AddStatistics(c *gin.Context) {
  325. var req request.AddBroadcastStatisticsReq
  326. if err := c.Bind(&req); err != nil {
  327. response.Fail("参数有误", c)
  328. return
  329. }
  330. if req.BroadcastId <= 0 {
  331. response.Fail("参数有误", c)
  332. }
  333. userinfo := user.GetInfoByClaims(c)
  334. go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  335. response.Ok("新增记录成功", c)
  336. }
  337. // BroadcastDetail 获取语音播报详情
  338. // @Tags 语音播报模块
  339. // @Description 获取语音播报详情
  340. // @Param broadcast_id query int true "语音播报ID"
  341. // @Success 200 {object} voiceResp.Broadcast
  342. // @failure 400 {string} string "获取失败"
  343. // @Router /detail [get]
  344. func BroadcastDetail(c *gin.Context) {
  345. var req request.BroadcastDetailReq
  346. if err := c.Bind(&req); err != nil {
  347. response.Fail("参数有误", c)
  348. return
  349. }
  350. if req.BroadcastId <= 0 {
  351. response.Fail("参数有误", c)
  352. return
  353. }
  354. userInfo := user.GetInfoByClaims(c)
  355. resp, e := services.GetVoiceBroadcastDetail(req.BroadcastId, int(userInfo.UserID))
  356. if e != nil {
  357. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  358. return
  359. }
  360. response.OkData("获取成功", resp, c)
  361. }
  362. // MsgSend 语音播报消息推送
  363. // @Tags 语音播报模块
  364. // @Description 语音播报消息推送
  365. // @Param broadcast_id query int true "语音播报ID"
  366. // @Success 200 {string} string "操作成功"
  367. // @failure 400 {string} string "操作失败"
  368. // @Router /msg_send [post]
  369. func MsgSend(c *gin.Context) {
  370. var req request.BroadcastMsgSendReq
  371. if err := c.Bind(&req); err != nil {
  372. response.Fail("参数有误", c)
  373. return
  374. }
  375. if req.BroadcastId <= 0 {
  376. response.Fail("参数有误", c)
  377. return
  378. }
  379. userInfo := user.GetInfoByClaims(c)
  380. errMsg, err := services.SendBroadcastMsg(req.BroadcastId, int(userInfo.UserID))
  381. if err != nil {
  382. response.FailMsg(errMsg, "MsgSend ErrMsg:"+err.Error(), c)
  383. return
  384. }
  385. response.Ok("操作成功", c)
  386. }
  387. // BroadcastDetail 获取语音播报详情
  388. // @Tags 语音播报模块
  389. // @Description 获取语音播报详情
  390. // @Param broadcast_id query int true "语音播报ID"
  391. // @Success 200 {object} voiceResp.Broadcast
  392. // @failure 400 {string} string "获取失败"
  393. // @Router /detail [get]
  394. func MyVoiceBroadcastListCount(c *gin.Context) {
  395. var req request.BroadcastListCountReq
  396. if err := c.Bind(&req); err != nil {
  397. response.Fail("参数有误", c)
  398. return
  399. }
  400. if req.AuthorId <= 0 {
  401. response.Fail("参数有误", c)
  402. return
  403. }
  404. resp, e := services.GetMyVoiceBroadcastListCount(req.AuthorId, req.SectionId)
  405. if e != nil {
  406. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  407. return
  408. }
  409. response.OkData("获取成功", resp, c)
  410. }