voice_broadcast.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/models/response"
  8. admin2 "hongze/hongze_yb/models/tables/admin"
  9. "hongze/hongze_yb/models/tables/company_product"
  10. "hongze/hongze_yb/models/tables/sys_role_admin"
  11. "hongze/hongze_yb/models/tables/voice_broadcast"
  12. "hongze/hongze_yb/models/tables/voice_broadcast_statistics"
  13. "hongze/hongze_yb/models/tables/voice_section"
  14. "hongze/hongze_yb/services/user"
  15. "hongze/hongze_yb/services/wechat"
  16. "hongze/hongze_yb/utils"
  17. "time"
  18. )
  19. func GetVoiceBroadcastList(pageindex, pagesize, sectionId, broadcastId int, userInfo user.UserInfo) (list []response.Broadcast, err error) {
  20. if broadcastId == 0 {
  21. if sectionId == 0 {
  22. broadList, e := voice_broadcast.GetBroadcast(pageindex, pagesize)
  23. if e != nil {
  24. e = errors.New("获取语音播报列表失败 Err:" + e.Error())
  25. }
  26. for _, item := range broadList {
  27. var respItem response.Broadcast
  28. respItem = response.Broadcast{
  29. BroadcastId: item.BroadcastId,
  30. BroadcastName: item.BroadcastName,
  31. SectionId: item.SectionId,
  32. SectionName: item.SectionName,
  33. VarietyId: item.VarietyId,
  34. VarietyName: item.VarietyName,
  35. AuthorId: item.AuthorId,
  36. Author: item.Author,
  37. ImgUrl: item.ImgUrl,
  38. VoiceUrl: item.VoiceUrl,
  39. VoicePlaySeconds: item.VoicePlaySeconds,
  40. VoiceSize: item.VoiceSize,
  41. CreateTime: item.CreateTime,
  42. IsAuthor: false,
  43. }
  44. if int(userInfo.UserID) == item.AuthorId {
  45. respItem.IsAuthor = true
  46. // 是否可以推送消息
  47. if item.MsgState == 0 {
  48. respItem.CouldSendMsg = true
  49. }
  50. }
  51. list = append(list, respItem)
  52. }
  53. err = e
  54. return
  55. }
  56. broadList, e := voice_broadcast.GetBroadcastByCondition(pageindex, pagesize, sectionId)
  57. if e != nil {
  58. e = errors.New("获取语音播报列表失败 Err:" + e.Error())
  59. }
  60. for _, item := range broadList {
  61. var respItem response.Broadcast
  62. respItem = response.Broadcast{
  63. BroadcastId: item.BroadcastId,
  64. BroadcastName: item.BroadcastName,
  65. SectionId: item.SectionId,
  66. SectionName: item.SectionName,
  67. VarietyId: item.VarietyId,
  68. VarietyName: item.VarietyName,
  69. AuthorId: item.AuthorId,
  70. Author: item.Author,
  71. ImgUrl: item.ImgUrl,
  72. VoiceUrl: item.VoiceUrl,
  73. VoicePlaySeconds: item.VoicePlaySeconds,
  74. VoiceSize: item.VoiceSize,
  75. CreateTime: item.CreateTime,
  76. IsAuthor: false,
  77. }
  78. if int(userInfo.UserID) == item.AuthorId {
  79. respItem.IsAuthor = true
  80. // 是否可以推送消息
  81. if item.MsgState == 0 {
  82. respItem.CouldSendMsg = true
  83. }
  84. }
  85. list = append(list, respItem)
  86. }
  87. err = e
  88. return
  89. } else {
  90. broadList, e := voice_broadcast.GetBroadcastByIdAndPage(pageindex, pagesize, broadcastId)
  91. if e != nil {
  92. e = errors.New("获取语音播报列表失败 Err:" + e.Error())
  93. }
  94. for _, item := range broadList {
  95. var respItem response.Broadcast
  96. respItem = response.Broadcast{
  97. BroadcastId: item.BroadcastId,
  98. BroadcastName: item.BroadcastName,
  99. SectionId: item.SectionId,
  100. SectionName: item.SectionName,
  101. VarietyId: item.VarietyId,
  102. VarietyName: item.VarietyName,
  103. AuthorId: item.AuthorId,
  104. Author: item.Author,
  105. ImgUrl: item.ImgUrl,
  106. VoiceUrl: item.VoiceUrl,
  107. VoicePlaySeconds: item.VoicePlaySeconds,
  108. VoiceSize: item.VoiceSize,
  109. CreateTime: item.CreateTime,
  110. IsAuthor: false,
  111. }
  112. if int(userInfo.UserID) == item.AuthorId {
  113. respItem.IsAuthor = true
  114. }
  115. list = append(list, respItem)
  116. }
  117. err = e
  118. return
  119. }
  120. }
  121. // GetVoiceAdminByUserInfo 判断当前用户是否为语音管理员
  122. func GetVoiceAdminByUserInfo(userInfo user.UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
  123. mobile := userInfo.Mobile
  124. var email string
  125. if mobile == "" {
  126. // 用户有可能是通过邮箱登录
  127. email = userInfo.Email
  128. if email == "" {
  129. return
  130. }
  131. }
  132. if userInfo.CompanyID != 16 {
  133. return
  134. }
  135. if mobile != "" {
  136. adminInfo, err = admin2.GetAdminByMobile(mobile)
  137. if err != nil {
  138. if err == utils.ErrNoRow {
  139. err = nil
  140. return
  141. }
  142. return
  143. }
  144. } else {
  145. adminInfo, err = admin2.GetAdminByEmail(email)
  146. if err != nil {
  147. if err == utils.ErrNoRow {
  148. err = nil
  149. return
  150. }
  151. return
  152. }
  153. }
  154. if adminInfo.Enabled != 1 {
  155. return
  156. }
  157. _, err = sys_role_admin.GetVoiceAdmin(int(adminInfo.AdminID))
  158. if err != nil && err != utils.ErrNoRow {
  159. return
  160. }
  161. if err == utils.ErrNoRow {
  162. ok = false
  163. return
  164. }
  165. ok = true
  166. return
  167. }
  168. func AddBroadcastRecord(userinfo user.UserInfo, source, broadcastId int) {
  169. var err error
  170. defer func() {
  171. if err != nil {
  172. global.LOG.Critical(fmt.Sprintf("AddBroadcastLog: userId=%d, err:%s", userinfo.UserID, err.Error()))
  173. }
  174. }()
  175. companyProduct, err := company_product.GetByCompany2ProductId(userinfo.CompanyID, 1)
  176. if err != nil {
  177. return
  178. }
  179. broadcast, err := voice_broadcast.GetBroadcastById(broadcastId)
  180. if err != nil {
  181. return
  182. }
  183. voiceBroadcastStatistics := voice_broadcast_statistics.VoiceBroadcastStatistics{
  184. CompanyId: companyProduct.CompanyID,
  185. CompanyName: companyProduct.CompanyName,
  186. UserId: int(userinfo.UserID),
  187. RealName: userinfo.RealName,
  188. Mobile: userinfo.Mobile,
  189. Email: userinfo.Email,
  190. CompanyStatus: companyProduct.Status,
  191. Source: source,
  192. BroadcastId: broadcastId,
  193. BroadcastName: broadcast.BroadcastName,
  194. SectionId: broadcast.SectionId,
  195. SectionName: broadcast.SectionName,
  196. VarietyId: broadcast.VarietyId,
  197. VarietyName: broadcast.VarietyName,
  198. AuthorId: broadcast.AuthorId,
  199. Author: broadcast.Author,
  200. PublishTime: broadcast.CreateTime,
  201. CreateTime: time.Now().Format(utils.FormatDateTime),
  202. }
  203. err = voiceBroadcastStatistics.AddBroadcastStatistics()
  204. if err != nil {
  205. return
  206. }
  207. return
  208. }
  209. // SendBroadcastMsg 推送语音播报消息
  210. func SendBroadcastMsg(broadcastId, userId int) (errMsg string, err error) {
  211. broadcast, e := voice_broadcast.GetBroadcastById(broadcastId)
  212. if e != nil {
  213. errMsg = "推送失败, 语音播报信息有误"
  214. err = errors.New("获取语音播报信息失败, Err: " + e.Error())
  215. return
  216. }
  217. if broadcast.AuthorId != userId {
  218. errMsg = "仅语音播报创建人可推送"
  219. err = errors.New("仅语音播报创建人可推送")
  220. return
  221. }
  222. if broadcast.MsgState != 0 {
  223. errMsg = "请勿重复推送"
  224. err = errors.New("请勿重复推送")
  225. return
  226. }
  227. // 更新语音播报信息
  228. updateCols := []string{"MsgState", "MsgTime"}
  229. broadcast.MsgState = 1
  230. broadcast.MsgTime = time.Now().Format(utils.FormatDateTime)
  231. if e = broadcast.Update(updateCols); e != nil {
  232. errMsg = "更新语音播报失败"
  233. err = errors.New("更新语音播报失败")
  234. return
  235. }
  236. // 推送模板消息
  237. go func() {
  238. _ = wechat.SendVoiceBroadcastWxMsg(broadcast.BroadcastId, broadcast.SectionName, broadcast.BroadcastName)
  239. }()
  240. // 推送客群消息
  241. go func() {
  242. _ = SendVoiceBroadcastToThs(broadcast)
  243. }()
  244. return
  245. }
  246. // VoiceBroadcastShareImgPars 语音播报分享图参数
  247. type VoiceBroadcastShareImgPars struct {
  248. BackgroundImg string `json:"background_img"`
  249. Title string `json:"title"`
  250. CreateTime string `json:"create_time"`
  251. }
  252. // UpdateVoiceBroadcastImgUrl 更新历史语音播报分享图
  253. func UpdateVoiceBroadcastImgUrl() (err error) {
  254. defer func() {
  255. if err != nil {
  256. fmt.Println(err.Error())
  257. }
  258. }()
  259. // 获取语音播报列表
  260. list, e := voice_broadcast.GetBroadcastList()
  261. if e != nil {
  262. err = errors.New("获取语音播报列表失败")
  263. return
  264. }
  265. // 获取语音播报板块分享图
  266. sectionList, e := voice_section.GetVoiceSection()
  267. if e != nil {
  268. err = errors.New("获取语音播报列表失败")
  269. return
  270. }
  271. sectionMap := make(map[int]string, 0)
  272. sectionNameMap := make(map[int]string, 0)
  273. for _, s := range sectionList {
  274. sectionMap[s.SectionId] = s.ImgUrl
  275. sectionNameMap[s.SectionId] = s.SectionName
  276. }
  277. // 生成语音分享图并更新
  278. listLen := len(list)
  279. fmt.Println("待更新数:", listLen)
  280. updateCols := []string{"ImgUrl"}
  281. for i := 0; i < listLen; i++ {
  282. item := list[i]
  283. fmt.Println("正在更新-Id:", item.BroadcastId)
  284. imgUrl := sectionMap[item.SectionId]
  285. if imgUrl == "" {
  286. fmt.Println("背景图为空-Id:", item.BroadcastId)
  287. continue
  288. }
  289. sectionName := sectionNameMap[item.SectionId]
  290. timeDate := item.CreateTime[0:10]
  291. pars := VoiceBroadcastShareImgPars{
  292. BackgroundImg: imgUrl,
  293. Title: sectionName,
  294. CreateTime: timeDate,
  295. }
  296. parsByte, e := json.Marshal(pars)
  297. if e != nil {
  298. err = errors.New("分享图参数有误")
  299. return
  300. }
  301. shareImg, e := GetDynamicShareImg(VoiceBroadcastShareImgSource, string(parsByte))
  302. //fmt.Println(shareImg)
  303. if e != nil {
  304. err = errors.New("生成分享图失败")
  305. return
  306. }
  307. item.ImgUrl = shareImg
  308. if e = item.Update(updateCols); e != nil {
  309. err = errors.New("更新语音播报失败")
  310. return
  311. }
  312. fmt.Println("更新成功-Id:", item.BroadcastId)
  313. }
  314. return
  315. }