programService.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package service
  2. import (
  3. deviceService "iot_manager_service/app/device/service"
  4. "iot_manager_service/app/multimedia/dao"
  5. "iot_manager_service/app/multimedia/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "time"
  10. )
  11. var ProgramService = new(programService)
  12. type programService struct{}
  13. func (s *programService) Get(id int) (*dao.Program, *common.Errors) {
  14. Program := &dao.Program{
  15. ID: id,
  16. }
  17. err := Program.Get()
  18. if err != nil {
  19. return nil, common.FailResponse(err.Error(), nil)
  20. }
  21. return Program, nil
  22. }
  23. func (s *programService) List(tenantId int, searchValue string, current, size, sysType int) ([]model.ProgramDetail,
  24. *common.Errors) {
  25. program := &dao.Program{
  26. TenantId: tenantId,
  27. }
  28. offset := (current - 1) * size
  29. limit := size
  30. if searchValue != "" {
  31. program.Name = searchValue
  32. }
  33. if sysType != -1 {
  34. program.SysType = sysType
  35. }
  36. programs, err := program.GetPrograms(offset, limit)
  37. if err != nil {
  38. return nil, common.FailResponse(err.Error(), nil)
  39. }
  40. var rsp []model.ProgramDetail
  41. for _, pro := range programs {
  42. rsp = append(rsp, model.ProgramDetail{
  43. Program: pro,
  44. ResolutionName: service.DictService.GetResolutionName(tenantId, pro.Resolution),
  45. })
  46. }
  47. return rsp, nil
  48. }
  49. func (s *programService) Remove(userId int64, tenantId int, id int) *common.Errors {
  50. relation := &dao.ProgramRelation{
  51. ProgramId: id,
  52. TenantId: tenantId,
  53. IsDeleted: 1,
  54. }
  55. err := relation.Delete()
  56. if err != nil {
  57. return common.FailResponse(err.Error(), nil)
  58. }
  59. program := &dao.Program{
  60. ID: id,
  61. IsDeleted: 1,
  62. UpdateUser: userId,
  63. UpdateTime: time.Now(),
  64. }
  65. err = program.Delete()
  66. if err != nil {
  67. return common.FailResponse(err.Error(), nil)
  68. }
  69. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeInfoBar,
  70. common.DeviceTypeDefault, "", common.OperationSuccess)
  71. return nil
  72. }
  73. func (s *programService) GetList(tenantId int, name string) ([]dao.Program, *common.Errors) {
  74. Program := &dao.Program{
  75. TenantId: tenantId,
  76. Name: name,
  77. }
  78. libraries, err := Program.GetAll()
  79. if err != nil {
  80. return nil, common.FailResponse(err.Error(), nil)
  81. }
  82. return libraries, nil
  83. }
  84. func (s *programService) Submit(tenantId int, userId int64, req model.ReqProgramSubmit) *common.Errors {
  85. program := &dao.Program{
  86. Name: req.Name,
  87. Resolution: req.Resolution,
  88. Duration: req.Duration,
  89. FileSize: req.FileSize,
  90. ImgDuration: req.ImgDuration,
  91. Remarks: req.Remarks,
  92. SysType: req.SysType,
  93. TenantId: tenantId,
  94. CreateTime: time.Now(),
  95. CreateUser: userId,
  96. UpdateTime: time.Now(),
  97. UpdateUser: userId,
  98. IsDeleted: 0,
  99. }
  100. if err := program.Create(); err != nil {
  101. logger.Logger.Errorf("Create err = %s \n", err.Error())
  102. return common.FailResponse(err.Error(), nil)
  103. }
  104. libraryIds := common.StringToIntArray(req.LibraryIds)
  105. for index, libraryId := range libraryIds {
  106. relation := &dao.ProgramRelation{
  107. ProgramId: program.ID,
  108. LibraryId: libraryId,
  109. OrderNo: index + 1,
  110. TenantId: tenantId,
  111. IsDeleted: 0,
  112. }
  113. err := relation.Save()
  114. if err != nil {
  115. logger.Logger.Errorf("relation save err = %s \n", err.Error())
  116. _ = program.Remove()
  117. _ = relation.Remove()
  118. return common.FailResponse(err.Error(), nil)
  119. }
  120. }
  121. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeInfoBar,
  122. common.DeviceTypeDefault, program.Name, common.OperationSuccess)
  123. return common.SuccessResponse(common.Succeeded, nil)
  124. }
  125. func (s *programService) GetLibraryList(tenantId, programId int) ([]dao.Library, *common.Errors) {
  126. relation := &dao.ProgramRelation{
  127. ProgramId: programId,
  128. TenantId: tenantId,
  129. }
  130. relations, err := relation.GetByProgram()
  131. if err != nil {
  132. logger.Logger.Errorf("relation BatchCreate err = %s \n", err.Error())
  133. return nil, common.FailResponse(err.Error(), nil)
  134. }
  135. var libraryIds []int
  136. for _, relation := range relations {
  137. libraryIds = append(libraryIds, relation.LibraryId)
  138. }
  139. //fmt.Printf("libraryIds = %v", libraryIds)
  140. library := &dao.Library{TenantId: tenantId}
  141. libraries, err := library.GetLibrariesByIds(libraryIds)
  142. for i, librarie := range libraries {
  143. if librarie.LibDuration == 0 {
  144. libraries[i].LibDuration = 3000 //图片默认给个3秒
  145. }
  146. }
  147. if err != nil {
  148. logger.Logger.Errorf("GetLibrariesByIds err = %s \n", err.Error())
  149. return nil, common.FailResponse(err.Error(), nil)
  150. }
  151. return libraries, nil
  152. }
  153. func (s *programService) RelationDeviceList(tenantId, sysType, resolution int) ([]model.ProgramDeviceDetail,
  154. *common.Errors) {
  155. var lampGroupIds []int
  156. infoBoardMap := make(map[int][]model.ProgramDevice)
  157. if sysType == model.SysTypeInfoBar {
  158. infoBoardDevices := deviceService.InfoBoardService.GetByResolution(tenantId, resolution)
  159. for _, device := range infoBoardDevices {
  160. lampGroupIds = append(lampGroupIds, device.GroupId)
  161. programDevice := model.ProgramDevice{
  162. Id: device.ID,
  163. PublicName: device.InfoName,
  164. }
  165. if devices, isExist := infoBoardMap[device.GroupId]; isExist {
  166. devices = append(devices, programDevice)
  167. infoBoardMap[device.GroupId] = devices
  168. } else {
  169. infoBoardMap[device.GroupId] = []model.ProgramDevice{programDevice}
  170. }
  171. }
  172. } else if sysType == model.SysTypeBroadcast {
  173. ipBroadcastDevices := deviceService.IpBroadcastService.GetAll(tenantId)
  174. for _, device := range ipBroadcastDevices {
  175. lampGroupIds = append(lampGroupIds, device.GroupId)
  176. programDevice := model.ProgramDevice{
  177. Id: device.ID,
  178. PublicName: device.CastName,
  179. }
  180. if devices, isExist := infoBoardMap[device.GroupId]; isExist {
  181. devices = append(devices, programDevice)
  182. infoBoardMap[device.GroupId] = devices
  183. } else {
  184. infoBoardMap[device.GroupId] = []model.ProgramDevice{programDevice}
  185. }
  186. }
  187. } else {
  188. return nil, nil
  189. }
  190. lampPoleGroups := deviceService.LampPoleGroupService.GetByIds(tenantId, lampGroupIds)
  191. var details []model.ProgramDeviceDetail
  192. for _, lampPoleGroup := range lampPoleGroups {
  193. details = append(details, model.ProgramDeviceDetail{
  194. PublicName: lampPoleGroup.PoleGroupName,
  195. InfoBoardList: infoBoardMap[lampPoleGroup.ID],
  196. })
  197. }
  198. return details, nil
  199. }