publishLibrariesService.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "iot_manager_service/app/multimedia/dao"
  8. "iot_manager_service/app/multimedia/model"
  9. "iot_manager_service/app/system/service"
  10. "iot_manager_service/util/common"
  11. "iot_manager_service/util/logger"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var PublishLibrariesService = new(publishLibrariesService)
  17. type publishLibrariesService struct{}
  18. /**
  19. TODO: 此接口都未对接边缘端
  20. */
  21. func (s *publishLibrariesService) List(tenantId string, searchValue string, current, size int, sysType int) ([]dao.PublishLibraries, int64,
  22. *common.Errors) {
  23. program := &dao.PublishLibraries{
  24. TenantId: tenantId,
  25. SysType: sysType,
  26. }
  27. offset := (current - 1) * size
  28. limit := size
  29. if searchValue != "" {
  30. program.Name = searchValue
  31. }
  32. list, total, err := program.GetList(offset, limit)
  33. if err != nil {
  34. return nil, 0, common.FailResponse(err.Error(), nil)
  35. }
  36. return list, total, nil
  37. }
  38. // Audit 素材发布
  39. func (s *publishLibrariesService) Audit(req model.ReqProgramAudit) error {
  40. if !common.MultimediaEfficacyTime(req.StartTime, req.EndTime, req.KsTime) {
  41. panic("任务时间已过,请重新定义时间。")
  42. }
  43. program := dao.Program{ID: req.ProgramLibrariesId}
  44. err := program.Get()
  45. if err != nil {
  46. return err
  47. }
  48. update := PublishLibrariesService.Create(&program, req)
  49. fmt.Printf("update = %v", update)
  50. return nil
  51. }
  52. // SaveOrUpdate 保存
  53. func (s *publishLibrariesService) Create(program *dao.Program, req model.ReqProgramAudit) *common.Errors {
  54. fmt.Printf("program = %v", program)
  55. remarks := "多媒体系统"
  56. if program.SysType == 1 {
  57. remarks = "广播系统"
  58. }
  59. infoIds := req.InfoId
  60. infoIds = strings.Trim(infoIds, ",")
  61. infoIds = strings.ReplaceAll(infoIds, "-1", "")
  62. infoIds = strings.ReplaceAll(infoIds, ",-1", "") //新id
  63. libraries := &dao.PublishLibraries{
  64. Name: program.Name,
  65. Resolution: program.Resolution,
  66. Duration: program.Duration,
  67. FileSize: program.FileSize,
  68. InfoId: infoIds,
  69. ImgDuration: program.ImgDuration,
  70. StartTime: req.StartTime,
  71. EndTime: req.EndTime,
  72. NotificationType: "0",
  73. CreateTime: time.Now(),
  74. CreateUser: program.CreateUser,
  75. CreateName: "",
  76. UpdateTime: time.Now(),
  77. UpdateUser: "",
  78. IsDeleted: 0,
  79. Status: 1,
  80. TenantId: program.TenantId,
  81. Remarks: remarks,
  82. SysType: program.SysType,
  83. KsTime: req.KsTime,
  84. JsTime: req.JsTime,
  85. FileId: program.ID,
  86. PlayTime: req.PlayTime,
  87. PlayInterval: req.PlayInterval,
  88. }
  89. libraries.CreateTime = time.Now()
  90. if err := libraries.Create(); err != nil {
  91. logger.Logger.Errorf("Create err = %s \n", err.Error())
  92. return common.FailResponse(err.Error(), nil)
  93. }
  94. fmt.Printf("libraries = %v", libraries)
  95. //TODO: 日志保存
  96. return common.SuccessResponse(common.Succeeded, nil)
  97. }
  98. // GetProgram 取节目
  99. func (s *publishLibrariesService) GetProgram(id int) (int, error) {
  100. libraries := dao.PublishLibraries{ID: int64(id)}
  101. program, err := libraries.GetProgram()
  102. return program, err
  103. }
  104. func (s *publishLibrariesService) putPublishLibraries(programId int, publishLibraries dao.PublishLibraries) {
  105. }
  106. // UpdateStatus 状态变更
  107. func (s *publishLibrariesService) UpdateStatus(tenantId string, id int, status int, name string) (bool, error) {
  108. update := &dao.PublishLibraries{
  109. TenantId: tenantId,
  110. ID: int64(id),
  111. Status: status,
  112. Name: name,
  113. }
  114. err := update.Update()
  115. if err != nil {
  116. return false, err
  117. }
  118. return true, nil
  119. }
  120. func (s *publishLibrariesService) Remove(userId int64, tenantId string, id int) *common.Errors {
  121. // 创建查询实例
  122. device := &dao.PublishLibraries{
  123. ID: int64(id),
  124. IsDeleted: 1,
  125. UpdateUser: strconv.Itoa(int(userId)),
  126. UpdateTime: time.Now(),
  127. }
  128. err := device.Delete()
  129. if err != nil {
  130. return common.FailResponse(err.Error(), nil)
  131. }
  132. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.DeviceTypeInfoBoard,
  133. common.DeviceTypeBridge, common.GetDeviceObject(int(device.ID), device.Name), common.OperationSuccess)
  134. return nil
  135. }
  136. type CltLedLibraries struct {
  137. Type string `json:"type"` //文件类型 2:图片3:视频5:文本27:网页
  138. Name string `json:"name"` //节目名字
  139. Imageinfo Imageinfo `json:"imageinfo"` //图片或视频信息
  140. }
  141. type InEffect struct {
  142. Type string `json:"Type"` //翻页类型 2
  143. Time string `json:"Time"` ///翻页花费时间 1000为1秒
  144. }
  145. type Imageinfo struct {
  146. Urls []string `json:"urls"`
  147. Duration string `json:"duration"`
  148. InEffect InEffect `json:"inEffect"`
  149. }
  150. // LedPaying 组装当前led屏要播放的节目json
  151. func (s *publishLibrariesService) LedPaying(ledId int, playing string) string {
  152. libraries := dao.PublishLibraries{}
  153. librarie, err := libraries.GetProgramByLedId(ledId)
  154. if errors.Is(err, gorm.ErrRecordNotFound) {
  155. return ""
  156. }
  157. if err != nil {
  158. logger.Logger.Errorf("LedPaying err = %v \n", err)
  159. return ""
  160. }
  161. if librarie.ID < 1 {
  162. //没有新节目不更新
  163. return ""
  164. }
  165. //fmt.Printf("librarie = %v \n", librarie)
  166. program, _ := s.GetProgram(int(librarie.ID))
  167. relation := &dao.ProgramRelation{
  168. ProgramId: program,
  169. TenantId: librarie.TenantId,
  170. }
  171. relations, err := relation.GetByProgram()
  172. if errors.Is(err, gorm.ErrRecordNotFound) {
  173. return ""
  174. }
  175. if err != nil {
  176. logger.Logger.Errorf("LedPaying err = %s \n", err.Error())
  177. return ""
  178. }
  179. var libraryIds []int
  180. for _, relation := range relations {
  181. libraryIds = append(libraryIds, relation.LibraryId)
  182. }
  183. //fmt.Printf("libraryIds = %v \n", libraryIds)
  184. library := &dao.Library{TenantId: librarie.TenantId}
  185. getLibrariesByIds, _ := library.GetLibrariesByIds(libraryIds)
  186. if len(getLibrariesByIds) < 1 {
  187. logger.Logger.Errorf("无素材")
  188. return ""
  189. }
  190. var cltLedLibraries CltLedLibraries
  191. cltLedLibraries.Name = fmt.Sprintf("%v-%v", librarie.Name, librarie.ID)
  192. if strings.EqualFold(cltLedLibraries.Name, strings.ReplaceAll(playing, ".vsn", "")) {
  193. logger.Logger.Errorf("节目相同无需要更新")
  194. return ""
  195. }
  196. cltLedLibraries.Type = "2"
  197. if strings.Contains(getLibrariesByIds[0].MaterialAddress, "mp4") {
  198. cltLedLibraries.Type = "3"
  199. }
  200. var urls []string
  201. for _, getLibrariesById := range getLibrariesByIds {
  202. urls = append(urls, getLibrariesById.MaterialAddress)
  203. }
  204. cltLedLibraries.Imageinfo.Duration = strconv.Itoa(int(librarie.Duration))
  205. cltLedLibraries.Imageinfo.Urls = urls
  206. cltLedLibraries.Imageinfo.InEffect.Type = "2"
  207. //cltLedLibraries.Imageinfo.InEffect.Time = strconv.Itoa(relations[0].Duration)
  208. cltLedLibraries.Imageinfo.InEffect.Time = "1500"
  209. fmt.Printf("cltLedLibraries = %v \n", cltLedLibraries)
  210. marshal, err := json.Marshal(cltLedLibraries)
  211. if err != nil {
  212. fmt.Printf("LedPaying err = %v \n", err)
  213. return ""
  214. }
  215. fmt.Printf("cltLedLibraries = %v \n", string(marshal))
  216. str := string(marshal)
  217. //fmt.Printf("str = %v \n", str)
  218. return str
  219. }
  220. type iPCastPayingMp3Info struct {
  221. Type int `json:"Type"`
  222. PlayTimes int `json:"PlayTimes"`
  223. FileIds []int `json:"FileIds"`
  224. TermIds []int `json:"TermIds"`
  225. }
  226. type iPCastPayingContentInfo struct {
  227. Content string `json:"Content"`
  228. Playtime int `json:"Playtime"`
  229. TermIds []int `json:"TermIds"`
  230. PlayVol int `json:"PlayVol"`
  231. PromptTone int `json:"PromptTone"`
  232. }
  233. func (s *publishLibrariesService) IPCastPaying(id int) (string, int) {
  234. publishLibraries := dao.PublishLibraries{}
  235. libraries, err := publishLibraries.GetProgramByIpCastId(id)
  236. if err != nil {
  237. logger.Logger.Errorf("IPCastPaying err = %v \n", err)
  238. return "", 0
  239. }
  240. if len(libraries) < 1 {
  241. //没有新节目不更新
  242. return "", 0
  243. }
  244. //fmt.Printf("libraries = %v \n", libraries)
  245. var librarie dao.PublishLibraries
  246. //判断当前到底要放哪个节目
  247. for _, library := range libraries {
  248. library.Ptype = 1
  249. if library.JsTime == "" {
  250. library.Ptype = 0
  251. }
  252. if library.Ptype == 1 && library.JsTime != "" {
  253. currTime := time.Now()
  254. parse, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), library.JsTime))
  255. if err != nil {
  256. fmt.Printf("IPCastPaying parse err = %v \n", err)
  257. logger.Logger.Errorf("IPCastPaying parse err = %v \n", err)
  258. continue
  259. }
  260. if parse.Before(currTime) {
  261. continue
  262. }
  263. } else {
  264. cTime := time.Now()
  265. kTime, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), library.KsTime))
  266. if err != nil {
  267. fmt.Printf("IPCastPaying kTime err = %v \n", err)
  268. logger.Logger.Errorf("IPCastPaying kTime err = %v \n", err)
  269. }
  270. //这里要作限制 ,只有是当前时分 才操作
  271. if cTime.Minute() != kTime.Minute() || cTime.Hour() == kTime.Hour() {
  272. continue
  273. }
  274. }
  275. librarie = library
  276. }
  277. //fmt.Printf("librarie = %v \n", librarie)
  278. program, _ := s.GetProgram(int(librarie.ID))
  279. relation := &dao.ProgramRelation{
  280. ProgramId: program,
  281. TenantId: librarie.TenantId,
  282. }
  283. relations, err := relation.GetByProgram()
  284. if err != nil {
  285. logger.Logger.Errorf("IPCastPaying err = %s \n", err)
  286. return "", 0
  287. }
  288. var libraryIds []int
  289. for _, relation := range relations {
  290. libraryIds = append(libraryIds, relation.LibraryId)
  291. }
  292. library := &dao.Library{TenantId: librarie.TenantId}
  293. getLibrariesByIds, _ := library.GetLibrariesByIds(libraryIds)
  294. if len(getLibrariesByIds) < 1 {
  295. logger.Logger.Errorf("无素材")
  296. return "", 0
  297. }
  298. //fmt.Printf("getLibrariesByIds = %v \n", getLibrariesByIds)
  299. // 音频播放
  300. if librarie.Ptype == 1 {
  301. paytime := diffTime(librarie.KsTime, librarie.JsTime)
  302. if paytime < 0 {
  303. //logger.Logger.Errorf("过了播放结束时间啦 ")
  304. return "", 0
  305. }
  306. fileIds := []int{}
  307. for _, libs := range getLibrariesByIds {
  308. fid, _ := strconv.Atoi(libs.FileId)
  309. fileIds = append(fileIds, fid)
  310. }
  311. info := iPCastPayingMp3Info{
  312. Type: 1,
  313. PlayTimes: paytime,
  314. FileIds: fileIds,
  315. TermIds: []int{1, 2, 3, 4, 5, 6, 7, 8},
  316. }
  317. marshal, err := json.Marshal(info)
  318. if err != nil {
  319. logger.Logger.Errorf("IPCastPaying iPCastPayingMp3Info err = %v \n", err)
  320. return "", 0
  321. }
  322. return string(marshal), librarie.Ptype
  323. //文字播放
  324. } else {
  325. contentInfo := iPCastPayingContentInfo{
  326. Content: getLibrariesByIds[0].MaterialAddress,
  327. Playtime: librarie.PlayTime,
  328. TermIds: []int{1, 2, 3, 4, 5, 6, 7, 8},
  329. PlayVol: 50,
  330. PromptTone: librarie.PlayInterval,
  331. }
  332. marshal, err := json.Marshal(contentInfo)
  333. if err != nil {
  334. logger.Logger.Errorf("IPCastPaying contentInfo err = %v \n", err)
  335. return "", 0
  336. }
  337. return string(marshal), librarie.Ptype
  338. }
  339. }
  340. // 计算还有多久结束
  341. func diffTime(stime, etime string) int {
  342. //stimeT, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), stime))
  343. stimeT, err := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v", time.Now().Format("2006-01-02 15:04")))
  344. if err != nil {
  345. return -1
  346. }
  347. etimeT, err2 := time.Parse("2006-01-02 15:04", fmt.Sprintf("%v %v", time.Now().Format("2006-01-02"), etime))
  348. if err2 != nil {
  349. return -1
  350. }
  351. diff := int(etimeT.Sub(stimeT).Seconds())
  352. return diff
  353. }