voice_broadcast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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_img"
  13. "hongze/hongze_yb/models/tables/voice_broadcast_statistics"
  14. "hongze/hongze_yb/models/tables/voice_section"
  15. "hongze/hongze_yb/services/user"
  16. "hongze/hongze_yb/services/wechat"
  17. "hongze/hongze_yb/utils"
  18. "strings"
  19. "time"
  20. )
  21. // GetVoiceBroadcastList 获取语音播报列表
  22. func GetVoiceBroadcastList(pageIndex, pageSize, sectionId, broadcastId, authorId, mineStatus int, userInfo user.UserInfo) (resp []response.Broadcast, err error) {
  23. condition := ` 1=1`
  24. var pars []interface{}
  25. // 分享进来的指定语音播报
  26. if broadcastId > 0 {
  27. condition += ` AND broadcast_id = ? AND publish_state = 1`
  28. pars = append(pars, broadcastId)
  29. } else {
  30. // 我的-非我的只能看到已发布
  31. if authorId > 0 {
  32. condition += ` AND author_id = ?`
  33. pars = append(pars, authorId)
  34. // 我的语音播报状态: 0-未发布 1-已发布 2-全部
  35. if mineStatus != 2 {
  36. condition += ` AND publish_state = ?`
  37. pars = append(pars, mineStatus)
  38. }
  39. } else {
  40. condition += ` AND publish_state = 1`
  41. }
  42. // 板块
  43. if sectionId > 0 {
  44. condition += ` AND section_id = ?`
  45. pars = append(pars, sectionId)
  46. }
  47. }
  48. voiceList, e := voice_broadcast.GetPageListByCondition(condition, pars, pageIndex, pageSize)
  49. if e != nil {
  50. err = errors.New("获取语音播报列表失败, Err: " + e.Error())
  51. return
  52. }
  53. listLen := len(voiceList)
  54. if listLen == 0 {
  55. return
  56. }
  57. // 图片
  58. voiceIds := make([]int, 0)
  59. for i := 0; i < listLen; i++ {
  60. voiceIds = append(voiceIds, voiceList[i].BroadcastId)
  61. }
  62. imgList, e := voice_broadcast_img.GetVoiceImgListByVoiceIds(voiceIds)
  63. if e != nil {
  64. err = errors.New("获取语音播报列表图片失败, Err: " + e.Error())
  65. return
  66. }
  67. imgMap := make(map[int][]*voice_broadcast_img.YbVoiceBroadcastImg, 0)
  68. imgListLen := len(imgList)
  69. for i := 0; i < imgListLen; i++ {
  70. imgMap[imgList[i].BroadcastId] = append(imgMap[imgList[i].BroadcastId], imgList[i])
  71. }
  72. // 响应数据
  73. for i := 0; i < listLen; i++ {
  74. r, e := handleBroadcastItem(userInfo, voiceList[i], imgMap[voiceList[i].BroadcastId])
  75. if e != nil {
  76. err = errors.New("语音播报响应数据处理失败, Err: " + e.Error())
  77. return
  78. }
  79. resp = append(resp, r)
  80. }
  81. return
  82. }
  83. // GetVoiceAdminByUserInfo 判断当前用户是否为语音管理员
  84. func GetVoiceAdminByUserInfo(userInfo user.UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
  85. mobile := userInfo.Mobile
  86. var email string
  87. if mobile == "" {
  88. // 用户有可能是通过邮箱登录
  89. email = userInfo.Email
  90. if email == "" {
  91. return
  92. }
  93. }
  94. if userInfo.CompanyID != 16 {
  95. return
  96. }
  97. if mobile != "" {
  98. adminInfo, err = admin2.GetAdminByMobile(mobile)
  99. if err != nil {
  100. if err == utils.ErrNoRow {
  101. err = nil
  102. return
  103. }
  104. return
  105. }
  106. } else {
  107. adminInfo, err = admin2.GetAdminByEmail(email)
  108. if err != nil {
  109. if err == utils.ErrNoRow {
  110. err = nil
  111. return
  112. }
  113. return
  114. }
  115. }
  116. if adminInfo.Enabled != 1 {
  117. return
  118. }
  119. _, err = sys_role_admin.GetVoiceAdmin(int(adminInfo.AdminID))
  120. if err != nil && err != utils.ErrNoRow {
  121. return
  122. }
  123. if err == utils.ErrNoRow {
  124. ok = false
  125. return
  126. }
  127. ok = true
  128. return
  129. }
  130. func AddBroadcastRecord(userinfo user.UserInfo, source, broadcastId int) (newId int, err error) {
  131. defer func() {
  132. if err != nil {
  133. global.LOG.Critical(fmt.Sprintf("AddBroadcastLog: userId=%d, err:%s", userinfo.UserID, err.Error()))
  134. }
  135. }()
  136. companyProduct, err := company_product.GetByCompany2ProductId(userinfo.CompanyID, 1)
  137. if err != nil {
  138. return
  139. }
  140. broadcast, err := voice_broadcast.GetBroadcastById(broadcastId)
  141. if err != nil {
  142. return
  143. }
  144. voiceBroadcastStatistics := voice_broadcast_statistics.VoiceBroadcastStatistics{
  145. CompanyId: companyProduct.CompanyID,
  146. CompanyName: companyProduct.CompanyName,
  147. UserId: int(userinfo.UserID),
  148. RealName: userinfo.RealName,
  149. Mobile: userinfo.Mobile,
  150. Email: userinfo.Email,
  151. CompanyStatus: companyProduct.Status,
  152. SellerId: companyProduct.SellerID,
  153. Source: source,
  154. BroadcastId: broadcastId,
  155. BroadcastName: broadcast.BroadcastName,
  156. SectionId: broadcast.SectionId,
  157. SectionName: broadcast.SectionName,
  158. VarietyId: broadcast.VarietyId,
  159. VarietyName: broadcast.VarietyName,
  160. AuthorId: broadcast.AuthorId,
  161. Author: broadcast.Author,
  162. PublishTime: broadcast.PublishTime,
  163. CreateTime: time.Now().Format(utils.FormatDateTime),
  164. }
  165. err = voiceBroadcastStatistics.AddBroadcastStatistics()
  166. if err != nil {
  167. return
  168. }
  169. newId = voiceBroadcastStatistics.Id
  170. return
  171. }
  172. // SendBroadcastMsg 推送语音播报消息
  173. func SendBroadcastMsg(broadcastId, userId int) (errMsg string, err error) {
  174. broadcast, e := voice_broadcast.GetBroadcastById(broadcastId)
  175. if e != nil {
  176. errMsg = "推送失败, 语音播报信息有误"
  177. err = errors.New("获取语音播报信息失败, Err: " + e.Error())
  178. return
  179. }
  180. if broadcast.PublishState != 1 {
  181. errMsg = "报告未发布, 不可推送"
  182. err = errors.New("报告未发布, 不可推送")
  183. return
  184. }
  185. if broadcast.AuthorId != userId {
  186. errMsg = "仅语音播报创建人可推送"
  187. err = errors.New("仅语音播报创建人可推送")
  188. return
  189. }
  190. if broadcast.MsgState != 0 {
  191. errMsg = "请勿重复推送"
  192. err = errors.New("请勿重复推送")
  193. return
  194. }
  195. // 更新语音播报信息
  196. updateCols := []string{"MsgState", "MsgTime"}
  197. broadcast.MsgState = 1
  198. broadcast.MsgTime = time.Now().Format(utils.FormatDateTime)
  199. if e = broadcast.Update(updateCols); e != nil {
  200. errMsg = "更新语音播报失败"
  201. err = errors.New("更新语音播报失败")
  202. return
  203. }
  204. // 推送模板消息
  205. go func() {
  206. _ = wechat.SendVoiceBroadcastWxMsg(broadcast.BroadcastId,broadcast.VarietyId, broadcast.SectionName, broadcast.BroadcastName)
  207. }()
  208. // 推送客群消息
  209. //go func() {
  210. // _ = SendVoiceBroadcastToThs(broadcast)
  211. //}()
  212. return
  213. }
  214. // CreateVoiceBroadcast 新增语音播报
  215. func CreateVoiceBroadcast(sectionId, varietyId, authorId int, broadcastName, sectionName, varietyName, author, voiceSeconds, voiceSize, voiceUrl, imgs string, userInfo user.UserInfo) (resp response.Broadcast, err error) {
  216. nowTime := time.Now().Local()
  217. item := &voice_broadcast.VoiceBroadcast{
  218. BroadcastName: broadcastName,
  219. SectionId: sectionId,
  220. SectionName: sectionName,
  221. VarietyId: varietyId,
  222. VarietyName: varietyName,
  223. AuthorId: authorId,
  224. Author: author,
  225. VoiceUrl: voiceUrl,
  226. VoicePlaySeconds: voiceSeconds,
  227. VoiceSize: voiceSize,
  228. CreateTime: nowTime.Format(utils.FormatDateTime),
  229. ModifyTime: nowTime.Format(utils.FormatDateTime),
  230. }
  231. // 图片
  232. imgList := make([]*voice_broadcast_img.YbVoiceBroadcastImg, 0)
  233. if imgs != "" {
  234. imgArr := strings.Split(imgs, ",")
  235. imgLen := len(imgArr)
  236. for i := 0; i < imgLen; i++ {
  237. if imgArr[i] == "" {
  238. continue
  239. }
  240. imgList = append(imgList, &voice_broadcast_img.YbVoiceBroadcastImg{
  241. BroadcastId: item.BroadcastId,
  242. ImgUrl: imgArr[i],
  243. CreateTime: nowTime,
  244. })
  245. }
  246. }
  247. if e := voice_broadcast.CreateVoiceBroadcastAndImgs(item, imgList); e != nil {
  248. err = errors.New("新增语音播报及图片失败, Err: " + e.Error())
  249. return
  250. }
  251. respItem, e := handleBroadcastItem(userInfo, item, imgList)
  252. if e != nil {
  253. err = errors.New("语音播报响应数据处理失败, Err: " + e.Error())
  254. return
  255. }
  256. resp = respItem
  257. return
  258. }
  259. // EditVoiceBroadcast 编辑语音播报
  260. func EditVoiceBroadcast(broadcastId, sectionId, varietyId, authorId int, broadcastName, sectionName, varietyName, author, voiceSeconds, voiceSize, voiceUrl, imgs string, userInfo user.UserInfo) (resp response.Broadcast, err error) {
  261. if broadcastId <= 0 {
  262. return
  263. }
  264. item, e := voice_broadcast.GetBroadcastById(broadcastId)
  265. if e != nil {
  266. err = errors.New("语音播报信息有误")
  267. return
  268. }
  269. nowTime := time.Now().Local()
  270. updateCols := []string{"BroadcastName", "SectionId", "SectionName", "VarietyId", "VarietyName", "AuthorId", "Author", "VoiceUrl",
  271. "VoicePlaySeconds", "VoiceSize", "ModifyTime"}
  272. item.BroadcastName = broadcastName
  273. item.SectionId = sectionId
  274. item.SectionName = sectionName
  275. item.VarietyId = varietyId
  276. item.VarietyName = varietyName
  277. item.AuthorId = authorId
  278. item.Author = author
  279. item.VoiceUrl = voiceUrl
  280. item.VoicePlaySeconds = voiceSeconds
  281. item.VoiceSize = voiceSize
  282. item.ModifyTime = nowTime.Format(utils.FormatDateTime)
  283. // 图片
  284. imgList := make([]*voice_broadcast_img.YbVoiceBroadcastImg, 0)
  285. if imgs != "" {
  286. imgArr := strings.Split(imgs, ",")
  287. imgLen := len(imgArr)
  288. for i := 0; i < imgLen; i++ {
  289. if imgArr[i] == "" {
  290. continue
  291. }
  292. imgList = append(imgList, &voice_broadcast_img.YbVoiceBroadcastImg{
  293. BroadcastId: item.BroadcastId,
  294. ImgUrl: imgArr[i],
  295. CreateTime: nowTime,
  296. })
  297. }
  298. }
  299. if e := voice_broadcast.UpdateVoiceBroadcastAndImgs(item, updateCols, imgList); e != nil {
  300. err = errors.New("更新语音播报及图片失败, Err: " + e.Error())
  301. return
  302. }
  303. resp, e = handleBroadcastItem(userInfo, item, imgList)
  304. if e != nil {
  305. err = errors.New("语音播报响应数据处理失败, Err: " + e.Error())
  306. return
  307. }
  308. return
  309. }
  310. // PublishVoiceBroadcast 发布语音播报
  311. func PublishVoiceBroadcast(broadcastId, publishType int, prePublishTime string) (err error) {
  312. item, e := voice_broadcast.GetBroadcastById(broadcastId)
  313. if e != nil {
  314. err = errors.New("语音播报信息有误")
  315. return
  316. }
  317. if item.PublishState == 1 {
  318. err = errors.New("不可重复发布")
  319. return
  320. }
  321. // publishType: 0-仅发布 1-发布并推送 2-定时发布; 发布类型为定时发送时, 分享图时间取预发布时间
  322. updateCols := []string{"ImgUrl", "PublishState", "PublishTime", "PrePublishTime", "ModifyTime"}
  323. publishTime := time.Now().Local()
  324. if publishType == 2 {
  325. item.PublishState = 0
  326. item.PrePublishTime = prePublishTime
  327. publishTime, e = time.ParseInLocation(utils.FormatDateTime, prePublishTime, time.Local)
  328. if e != nil {
  329. err = errors.New("预发布时间有误, Err: " + e.Error())
  330. return
  331. }
  332. } else {
  333. // 非定时发布时重置预发布时间
  334. item.PublishState = 1
  335. item.PublishTime = publishTime.Format(utils.FormatDateTime)
  336. item.PrePublishTime = ""
  337. }
  338. // 分享背景图-取板块的图
  339. section, e := voice_section.GetVoiceSectionById(item.SectionId)
  340. if e != nil {
  341. err = errors.New("获取板块信息失败, Err: " + e.Error())
  342. return
  343. }
  344. shareTime := publishTime.Format(utils.FormatDate)
  345. shareImg, e := createVoiceBroadcastShareImg(section.ImgUrl, item.SectionName, shareTime)
  346. if e != nil {
  347. err = errors.New("生成分享图失败, Err: " + e.Error())
  348. return
  349. }
  350. item.ImgUrl = shareImg
  351. // 发布
  352. if e = item.Update(updateCols); e != nil {
  353. err = errors.New("发布语音播报失败, Err: " + e.Error())
  354. return
  355. }
  356. return
  357. }
  358. // VoiceBroadcastShareImgPars 语音播报分享图参数
  359. type VoiceBroadcastShareImgPars struct {
  360. BackgroundImg string `json:"background_img"`
  361. Title string `json:"title"`
  362. CreateTime string `json:"create_time"`
  363. }
  364. // createVoiceBroadcastShareImg 生成动态分享图
  365. func createVoiceBroadcastShareImg(baseImg, sectionName, createTime string) (shareImg string, err error) {
  366. pars := VoiceBroadcastShareImgPars{
  367. BackgroundImg: baseImg,
  368. Title: sectionName,
  369. CreateTime: createTime,
  370. }
  371. parsByte, e := json.Marshal(pars)
  372. if e != nil {
  373. err = e
  374. return
  375. }
  376. shareImg, e = GetDynamicShareImg(VoiceBroadcastShareImgSource, string(parsByte))
  377. if e != nil {
  378. err = e
  379. return
  380. }
  381. return
  382. }
  383. // GetVoiceBroadcastDetail 获取语音播报详情
  384. func GetVoiceBroadcastDetail(broadcastId int, userInfo user.UserInfo) (detail response.Broadcast, err error) {
  385. item, e := voice_broadcast.GetBroadcastById(broadcastId)
  386. if e != nil {
  387. err = errors.New("获取语音播报详情失败, Err: " + e.Error())
  388. return
  389. }
  390. // 语音播报图片
  391. imgList, e := voice_broadcast_img.GetVoiceImgListByVoiceId(broadcastId)
  392. if e != nil {
  393. err = errors.New("获取语音播报图片失败, Err: " + e.Error())
  394. return
  395. }
  396. detail, e = handleBroadcastItem(userInfo, item, imgList)
  397. if e != nil {
  398. err = errors.New("语音播报响应数据处理失败, Err: " + e.Error())
  399. return
  400. }
  401. return
  402. }
  403. // handleBroadcastItem 语音播报响应数据处理
  404. func handleBroadcastItem(userInfo user.UserInfo, item *voice_broadcast.VoiceBroadcast, imgs []*voice_broadcast_img.YbVoiceBroadcastImg) (resp response.Broadcast, err error) {
  405. if item == nil {
  406. return
  407. }
  408. resp.BroadcastId = item.BroadcastId
  409. resp.BroadcastName = item.BroadcastName
  410. resp.SectionId = item.SectionId
  411. resp.SectionName = item.SectionName
  412. resp.VarietyId = item.VarietyId
  413. resp.VarietyName = item.VarietyName
  414. resp.AuthorId = item.AuthorId
  415. resp.Author = item.Author
  416. resp.ImgUrl = item.ImgUrl
  417. resp.VoiceUrl = item.VoiceUrl
  418. resp.VoicePlaySeconds = item.VoicePlaySeconds
  419. resp.VoiceSize = item.VoiceSize
  420. resp.CreateTime = item.CreateTime
  421. resp.ModifyTime = item.ModifyTime
  422. resp.PublishState = item.PublishState
  423. resp.PublishTime = item.PublishTime
  424. resp.PrePublishTime = item.PrePublishTime
  425. // 是否为作者、是否可推送消息
  426. ok, _, err := user.GetAdminByUserInfo(userInfo)
  427. if err != nil {
  428. return
  429. }
  430. if int(userInfo.UserID) == item.AuthorId && ok {
  431. resp.IsAuthor = true
  432. if item.MsgState == 0 {
  433. resp.CouldSendMsg = true
  434. }
  435. }
  436. imgLen := len(imgs)
  437. imgArr := make([]string, 0)
  438. for i := 0; i < imgLen; i++ {
  439. imgArr = append(imgArr, imgs[i].ImgUrl)
  440. }
  441. resp.Imgs = imgArr
  442. return
  443. }
  444. // GetMyVoiceBroadcastListCount 获取我的语音播报列表计数
  445. func GetMyVoiceBroadcastListCount(authorId, sectionId int) (resp response.BroadcastListStatusCount, err error) {
  446. condition := ` 1=1 `
  447. var pars []interface{}
  448. // 我的-非我的只能看到已发布
  449. if authorId > 0 {
  450. condition += ` AND author_id = ?`
  451. pars = append(pars, authorId)
  452. }
  453. // 板块
  454. if sectionId > 0 {
  455. condition += ` AND section_id = ?`
  456. pars = append(pars, sectionId)
  457. }
  458. counts, e := voice_broadcast.GetVoiceBroadcastListStatusCount(condition, pars)
  459. if e != nil {
  460. err = errors.New("获取语音播报列表计数失败, Err: " + e.Error())
  461. return
  462. }
  463. for _, v := range counts {
  464. if v.State == 0 {
  465. resp.Unpublished = v.Num
  466. continue
  467. }
  468. if v.State == 1 {
  469. resp.Published = v.Num
  470. }
  471. }
  472. resp.All = resp.Unpublished + resp.Published
  473. return
  474. }