voice_broadcast.go 14 KB

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