voice_broadcast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 !strings.Contains(req.VoiceUrl, utils.ALIYUN_OSS_HOST) {
  94. response.Fail("音频地址有误, 请重新上传", c)
  95. return
  96. }
  97. if req.Imgs != "" {
  98. imgList := strings.Split(req.Imgs, ",")
  99. if len(imgList) > 5 {
  100. response.Fail("最多插入五张图片", c)
  101. return
  102. }
  103. }
  104. userInfo := user.GetInfoByClaims(c)
  105. // 新增
  106. resp, e := services.CreateVoiceBroadcast(req.SectionId, req.VarietyId, req.AuthorId, req.BroadcastName, req.SectionName, req.VarietyName,
  107. req.Author, req.VoiceSeconds, req.VoiceSize, req.VoiceUrl, req.Imgs, userInfo)
  108. if e != nil {
  109. response.FailMsg("新增失败", e.Error(), c)
  110. return
  111. }
  112. response.OkData("操作成功", resp, c)
  113. }
  114. // EditBroadcast
  115. // @Description 编辑语音播报
  116. // @Param broadcast_id query int true "语音播报ID"
  117. // @Param broadcast_name query string true "语音标题"
  118. // @Param section_id query int true "板块ID"
  119. // @Param section_name query string true "板块名称"
  120. // @Param variety_id query int true "品种ID"
  121. // @Param variety_name query string true "品种名称"
  122. // @Param author_id query int true "作者ID"
  123. // @Param author query string true "作者名称"
  124. // @Param Imgs query string false "图片,英文逗号拼接"
  125. // @Param voice_seconds query string true "音频时长"
  126. // @Param voice_size query string true "音频大小"
  127. // @Param voice_url query string true "音频文件地址"
  128. // @Success 200 {string} string "发布成功"
  129. // @failure 400 {string} string "发布失败"
  130. // @Router /edit [post]
  131. func EditBroadcast(c *gin.Context) {
  132. var req request.SaveBroadcastReq
  133. if err := c.ShouldBind(&req); err != nil {
  134. response.Fail("参数有误", c)
  135. return
  136. }
  137. // 参数校验
  138. if req.BroadcastId <= 0 {
  139. response.Fail("参数有误", c)
  140. return
  141. }
  142. if req.BroadcastName == "" {
  143. response.Fail("请输入标题", c)
  144. return
  145. }
  146. if req.SectionId <= 0 || req.SectionName == "" {
  147. response.Fail("请选择品种", c)
  148. return
  149. }
  150. if req.SectionId <= 0 || req.SectionName == "" {
  151. response.Fail("请选择板块", c)
  152. return
  153. }
  154. if req.VoiceUrl == "" || req.VoiceSeconds == "" || req.VoiceSize == "" {
  155. response.Fail("请上传音频", c)
  156. return
  157. }
  158. if !strings.Contains(req.VoiceUrl, utils.ALIYUN_OSS_HOST) {
  159. response.Fail("音频地址有误, 请重新上传", c)
  160. return
  161. }
  162. if req.Imgs != "" {
  163. imgList := strings.Split(req.Imgs, ",")
  164. if len(imgList) > 5 {
  165. response.Fail("最多插入五张图片", c)
  166. return
  167. }
  168. }
  169. userInfo := user.GetInfoByClaims(c)
  170. // 编辑
  171. resp, e := services.EditVoiceBroadcast(req.BroadcastId, req.SectionId, req.VarietyId, req.AuthorId, req.BroadcastName, req.SectionName, req.VarietyName,
  172. req.Author, req.VoiceSeconds, req.VoiceSize, req.VoiceUrl, req.Imgs, userInfo)
  173. if e != nil {
  174. response.FailMsg("新增失败", e.Error(), c)
  175. return
  176. }
  177. response.OkData("操作成功", resp, c)
  178. }
  179. // PublishBroadcast
  180. // @Description 发布语音播报
  181. // @Param broadcast_id query int true "语音播报ID"
  182. // @Param publish_type query int true "发布类型: 1-发布 2-定时发布"
  183. // @Param pre_publish_time query string false "预发布时间"
  184. // @Success 200 {string} string "发布成功"
  185. // @failure 400 {string} string "发布失败"
  186. // @Router /publish [post]
  187. func PublishBroadcast(c *gin.Context) {
  188. var req request.PublishBroadcastReq
  189. if err := c.ShouldBind(&req); err != nil {
  190. response.Fail("参数有误", c)
  191. return
  192. }
  193. // 参数校验
  194. if req.BroadcastId <= 0 {
  195. response.Fail("参数有误", c)
  196. return
  197. }
  198. if req.PublishType <= 0 {
  199. response.Fail("请选择发布类型", c)
  200. return
  201. }
  202. if req.PublishType == 2 && req.PrePublishTime == "" {
  203. response.Fail("定时发布请选择发布时间", c)
  204. return
  205. }
  206. // 发布
  207. if e := services.PublishVoiceBroadcast(req.BroadcastId, req.PublishType, req.PrePublishTime); e != nil {
  208. response.FailMsg("发布失败", e.Error(), c)
  209. return
  210. }
  211. response.Ok("操作成功", c)
  212. }
  213. // SectionList
  214. // @Description 语音播报板块列表
  215. // @Success 200 {object} []voiceResp.VarietyList
  216. // @failure 400 {string} string "获取失败"
  217. // @Router /section/list [get]
  218. func SectionList(c *gin.Context) {
  219. sList, err := voice_section.GetVoiceSection()
  220. if err != nil {
  221. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  222. }
  223. vList, err := voice_section.GetVoiceVariety()
  224. if err != nil {
  225. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  226. }
  227. var sectionList []voiceResp.SectionList
  228. var varietyList []voiceResp.VarietyList
  229. var resp []voiceResp.VarietyList
  230. //var resp voiceResp.SectionListResp
  231. //for _, s := range sList {
  232. // section := voiceResp.SectionList{
  233. // SectionId: s.SectionId,
  234. // SectionName: s.SectionName,
  235. // Status: s.Status,
  236. // }
  237. // sectionList = append(sectionList, section)
  238. //}
  239. var newsList []*voice_section.VoiceSection
  240. //var bannedSectionList []*voice_section.VoiceSection
  241. //查找被禁用的板块ids
  242. var bannedIds []int
  243. for _, section := range sList {
  244. if section.Status == 0 {
  245. //bannedSectionList = append(bannedSectionList, section)
  246. bannedIds = append(bannedIds, section.SectionId)
  247. } else {
  248. newsList = append(newsList, section)
  249. }
  250. }
  251. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  252. var lists []*voice_broadcast.VoiceBroadcast
  253. if len(bannedIds) > 0 {
  254. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  255. if err != nil {
  256. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  257. }
  258. }
  259. //被禁用板块有语音,依然显示该板块
  260. if len(lists) > 0 {
  261. //清空切片,用新的
  262. newsList = newsList[0:0]
  263. bannedMap := make(map[int]int)
  264. for _, broadcast := range lists {
  265. bannedMap[broadcast.SectionId] = broadcast.SectionId
  266. }
  267. for _, section := range sList {
  268. _, ok := bannedMap[section.SectionId]
  269. if section.Status != 0 || ok {
  270. newsList = append(newsList, section)
  271. }
  272. }
  273. }
  274. for _, v := range vList {
  275. variety := voiceResp.VarietyList{
  276. VarietyId: v.VarietyId,
  277. VarietyName: v.VarietyName,
  278. }
  279. varietyList = append(varietyList, variety)
  280. }
  281. for _, v := range varietyList {
  282. for _, s := range newsList {
  283. if v.VarietyId == s.VarietyId {
  284. section := voiceResp.SectionList{
  285. ImgUrl: s.ImgUrl,
  286. SectionId: s.SectionId,
  287. SectionName: s.SectionName,
  288. Status: s.Status,
  289. }
  290. sectionList = append(sectionList, section)
  291. }
  292. }
  293. if len(sectionList) == 0 {
  294. continue
  295. }
  296. v.Children = sectionList
  297. resp = append(resp, v)
  298. sectionList = []voiceResp.SectionList{}
  299. }
  300. response.OkData("上传成功", resp, c)
  301. }
  302. // DelBroadcast
  303. // @Description 删除语音播报
  304. // @Param broadcast_id query int false "语音播报id"
  305. // @Success 200 {string} string "删除成功"
  306. // @failure 400 {string} string "删除失败"
  307. // @Router /delete [get]
  308. func DelBroadcast(c *gin.Context) {
  309. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  310. broadcastId, err := strconv.Atoi(sbroadcastId)
  311. if err != nil {
  312. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  313. return
  314. }
  315. if broadcastId <= 0 {
  316. response.FailMsg("参数错误", "参数有误", c)
  317. return
  318. }
  319. userInfo := user.GetInfoByClaims(c)
  320. // 是否为内部员工
  321. ok, _, err := user.GetAdminByUserInfo(userInfo)
  322. if err != nil {
  323. response.FailMsg("您无权操作", "获取系统用户信息失败"+err.Error(), c)
  324. return
  325. }
  326. if !ok {
  327. response.FailMsg("您无权操作", "非公司内部员工", c)
  328. return
  329. }
  330. var item voice_broadcast.VoiceBroadcast
  331. item.BroadcastId = broadcastId
  332. err = item.DelVoiceBroadcast()
  333. if err != nil {
  334. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  335. return
  336. }
  337. response.Ok("删除成功", c)
  338. }
  339. // AddStatistics
  340. // @Description 新增语音播报记录
  341. // @Param file query string true "音频文件"
  342. // @Success 200 {string} string "新增成功"
  343. // @failure 400 {string} string "新增失败"
  344. // @Router /statistics/add [post]
  345. func AddStatistics(c *gin.Context) {
  346. var req request.AddBroadcastStatisticsReq
  347. if err := c.Bind(&req); err != nil {
  348. response.Fail("参数有误", c)
  349. return
  350. }
  351. if req.BroadcastId <= 0 {
  352. response.Fail("参数有误", c)
  353. }
  354. userinfo := user.GetInfoByClaims(c)
  355. newId, err := services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  356. if err != nil {
  357. response.FailMsg("新增播放记录失败", "AddStatistics ErrMsg:"+err.Error(), c)
  358. return
  359. }
  360. response.OkData("新增记录成功", newId, c)
  361. }
  362. // BroadcastDetail 获取语音播报详情
  363. // @Tags 语音播报模块
  364. // @Description 获取语音播报详情
  365. // @Param broadcast_id query int true "语音播报ID"
  366. // @Success 200 {object} voiceResp.Broadcast
  367. // @failure 400 {string} string "获取失败"
  368. // @Router /detail [get]
  369. func BroadcastDetail(c *gin.Context) {
  370. var req request.BroadcastDetailReq
  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. resp, e := services.GetVoiceBroadcastDetail(req.BroadcastId, userInfo)
  381. if e != nil {
  382. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  383. return
  384. }
  385. response.OkData("获取成功", resp, c)
  386. }
  387. // MsgSend 语音播报消息推送
  388. // @Tags 语音播报模块
  389. // @Description 语音播报消息推送
  390. // @Param broadcast_id query int true "语音播报ID"
  391. // @Success 200 {string} string "操作成功"
  392. // @failure 400 {string} string "操作失败"
  393. // @Router /msg_send [post]
  394. func MsgSend(c *gin.Context) {
  395. var req request.BroadcastMsgSendReq
  396. if err := c.Bind(&req); err != nil {
  397. response.Fail("参数有误", c)
  398. return
  399. }
  400. if req.BroadcastId <= 0 {
  401. response.Fail("参数有误", c)
  402. return
  403. }
  404. userInfo := user.GetInfoByClaims(c)
  405. errMsg, err := services.SendBroadcastMsg(req.BroadcastId, int(userInfo.UserID))
  406. if err != nil {
  407. response.FailMsg(errMsg, "MsgSend ErrMsg:"+err.Error(), c)
  408. return
  409. }
  410. response.Ok("操作成功", c)
  411. }
  412. // BroadcastDetail 获取语音播报详情
  413. // @Tags 语音播报模块
  414. // @Description 获取语音播报详情
  415. // @Param broadcast_id query int true "语音播报ID"
  416. // @Success 200 {object} voiceResp.Broadcast
  417. // @failure 400 {string} string "获取失败"
  418. // @Router /detail [get]
  419. func MyVoiceBroadcastListCount(c *gin.Context) {
  420. var req request.BroadcastListCountReq
  421. if err := c.Bind(&req); err != nil {
  422. response.Fail("参数有误", c)
  423. return
  424. }
  425. if req.AuthorId <= 0 {
  426. response.Fail("参数有误", c)
  427. return
  428. }
  429. resp, e := services.GetMyVoiceBroadcastListCount(req.AuthorId, req.SectionId)
  430. if e != nil {
  431. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  432. return
  433. }
  434. response.OkData("获取成功", resp, c)
  435. }