voice_broadcast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. fmt.Println("发布失败,err:" + e.Error())
  210. response.FailMsg("发布失败", e.Error(), c)
  211. return
  212. }
  213. response.Ok("操作成功", c)
  214. }
  215. // SectionList
  216. // @Description 语音播报板块列表
  217. // @Success 200 {object} []voiceResp.VarietyList
  218. // @failure 400 {string} string "获取失败"
  219. // @Router /section/list [get]
  220. func SectionList(c *gin.Context) {
  221. sList, err := voice_section.GetVoiceSection()
  222. if err != nil {
  223. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  224. }
  225. vList, err := voice_section.GetVoiceVariety()
  226. if err != nil {
  227. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  228. }
  229. var sectionList []voiceResp.SectionList
  230. var varietyList []voiceResp.VarietyList
  231. var resp []voiceResp.VarietyList
  232. //var resp voiceResp.SectionListResp
  233. //for _, s := range sList {
  234. // section := voiceResp.SectionList{
  235. // SectionId: s.SectionId,
  236. // SectionName: s.SectionName,
  237. // Status: s.Status,
  238. // }
  239. // sectionList = append(sectionList, section)
  240. //}
  241. var newsList []*voice_section.VoiceSection
  242. //var bannedSectionList []*voice_section.VoiceSection
  243. //查找被禁用的板块ids
  244. var bannedIds []int
  245. for _, section := range sList {
  246. if section.Status == 0 {
  247. //bannedSectionList = append(bannedSectionList, section)
  248. bannedIds = append(bannedIds, section.SectionId)
  249. } else {
  250. newsList = append(newsList, section)
  251. }
  252. }
  253. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  254. var lists []*voice_broadcast.VoiceBroadcast
  255. if len(bannedIds) > 0 {
  256. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  257. if err != nil {
  258. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  259. }
  260. }
  261. //被禁用板块有语音,依然显示该板块
  262. if len(lists) > 0 {
  263. //清空切片,用新的
  264. newsList = newsList[0:0]
  265. bannedMap := make(map[int]int)
  266. for _, broadcast := range lists {
  267. bannedMap[broadcast.SectionId] = broadcast.SectionId
  268. }
  269. for _, section := range sList {
  270. _, ok := bannedMap[section.SectionId]
  271. if section.Status != 0 || ok {
  272. newsList = append(newsList, section)
  273. }
  274. }
  275. }
  276. for _, v := range vList {
  277. variety := voiceResp.VarietyList{
  278. VarietyId: v.VarietyId,
  279. VarietyName: v.VarietyName,
  280. }
  281. varietyList = append(varietyList, variety)
  282. }
  283. for _, v := range varietyList {
  284. for _, s := range newsList {
  285. if v.VarietyId == s.VarietyId {
  286. section := voiceResp.SectionList{
  287. ImgUrl: s.ImgUrl,
  288. SectionId: s.SectionId,
  289. SectionName: s.SectionName,
  290. Status: s.Status,
  291. }
  292. sectionList = append(sectionList, section)
  293. }
  294. }
  295. if len(sectionList) == 0 {
  296. continue
  297. }
  298. v.Children = sectionList
  299. resp = append(resp, v)
  300. sectionList = []voiceResp.SectionList{}
  301. }
  302. response.OkData("上传成功", resp, c)
  303. }
  304. // DelBroadcast
  305. // @Description 删除语音播报
  306. // @Param broadcast_id query int false "语音播报id"
  307. // @Success 200 {string} string "删除成功"
  308. // @failure 400 {string} string "删除失败"
  309. // @Router /delete [get]
  310. func DelBroadcast(c *gin.Context) {
  311. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  312. broadcastId, err := strconv.Atoi(sbroadcastId)
  313. if err != nil {
  314. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  315. return
  316. }
  317. if broadcastId <= 0 {
  318. response.FailMsg("参数错误", "参数有误", c)
  319. return
  320. }
  321. userInfo := user.GetInfoByClaims(c)
  322. // 是否为内部员工
  323. ok, _, err := user.GetAdminByUserInfo(userInfo)
  324. if err != nil {
  325. response.FailMsg("您无权操作", "获取系统用户信息失败"+err.Error(), c)
  326. return
  327. }
  328. if !ok {
  329. response.FailMsg("您无权操作", "非公司内部员工", c)
  330. return
  331. }
  332. var item voice_broadcast.VoiceBroadcast
  333. item.BroadcastId = broadcastId
  334. err = item.DelVoiceBroadcast()
  335. if err != nil {
  336. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  337. return
  338. }
  339. response.Ok("删除成功", c)
  340. }
  341. // AddStatistics
  342. // @Description 新增语音播报记录
  343. // @Param file query string true "音频文件"
  344. // @Success 200 {string} string "新增成功"
  345. // @failure 400 {string} string "新增失败"
  346. // @Router /statistics/add [post]
  347. func AddStatistics(c *gin.Context) {
  348. var req request.AddBroadcastStatisticsReq
  349. if err := c.Bind(&req); err != nil {
  350. response.Fail("参数有误", c)
  351. return
  352. }
  353. if req.BroadcastId <= 0 {
  354. response.Fail("参数有误", c)
  355. }
  356. userinfo := user.GetInfoByClaims(c)
  357. newId, err := services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  358. if err != nil {
  359. response.FailMsg("新增播放记录失败", "AddStatistics ErrMsg:"+err.Error(), c)
  360. return
  361. }
  362. response.OkData("新增记录成功", newId, c)
  363. }
  364. // BroadcastDetail 获取语音播报详情
  365. // @Tags 语音播报模块
  366. // @Description 获取语音播报详情
  367. // @Param broadcast_id query int true "语音播报ID"
  368. // @Success 200 {object} voiceResp.Broadcast
  369. // @failure 400 {string} string "获取失败"
  370. // @Router /detail [get]
  371. func BroadcastDetail(c *gin.Context) {
  372. var req request.BroadcastDetailReq
  373. if err := c.Bind(&req); err != nil {
  374. response.Fail("参数有误", c)
  375. return
  376. }
  377. if req.BroadcastId <= 0 {
  378. response.Fail("参数有误", c)
  379. return
  380. }
  381. userInfo := user.GetInfoByClaims(c)
  382. resp, e := services.GetVoiceBroadcastDetail(req.BroadcastId, userInfo)
  383. if e != nil {
  384. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  385. return
  386. }
  387. response.OkData("获取成功", resp, c)
  388. }
  389. // MsgSend 语音播报消息推送
  390. // @Tags 语音播报模块
  391. // @Description 语音播报消息推送
  392. // @Param broadcast_id query int true "语音播报ID"
  393. // @Success 200 {string} string "操作成功"
  394. // @failure 400 {string} string "操作失败"
  395. // @Router /msg_send [post]
  396. func MsgSend(c *gin.Context) {
  397. var req request.BroadcastMsgSendReq
  398. if err := c.Bind(&req); err != nil {
  399. response.Fail("参数有误", c)
  400. return
  401. }
  402. if req.BroadcastId <= 0 {
  403. response.Fail("参数有误", c)
  404. return
  405. }
  406. userInfo := user.GetInfoByClaims(c)
  407. errMsg, err := services.SendBroadcastMsg(req.BroadcastId, int(userInfo.UserID))
  408. if err != nil {
  409. response.FailMsg(errMsg, "MsgSend ErrMsg:"+err.Error(), c)
  410. fmt.Println("SendBroadcastMsg Err:" + err.Error())
  411. return
  412. }
  413. response.Ok("操作成功", c)
  414. }
  415. // BroadcastDetail 获取语音播报详情
  416. // @Tags 语音播报模块
  417. // @Description 获取语音播报详情
  418. // @Param broadcast_id query int true "语音播报ID"
  419. // @Success 200 {object} voiceResp.Broadcast
  420. // @failure 400 {string} string "获取失败"
  421. // @Router /detail [get]
  422. func MyVoiceBroadcastListCount(c *gin.Context) {
  423. var req request.BroadcastListCountReq
  424. if err := c.Bind(&req); err != nil {
  425. response.Fail("参数有误", c)
  426. return
  427. }
  428. if req.AuthorId <= 0 {
  429. response.Fail("参数有误", c)
  430. return
  431. }
  432. resp, e := services.GetMyVoiceBroadcastListCount(req.AuthorId, req.SectionId)
  433. if e != nil {
  434. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  435. return
  436. }
  437. response.OkData("获取成功", resp, c)
  438. }