voice_broadcast.go 14 KB

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