programService.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package service
  2. import (
  3. deviceModel "iot_manager_service/app/device/model"
  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. TenantId: tenantId,
  93. CreateTime: time.Now(),
  94. CreateUser: userId,
  95. UpdateTime: time.Now(),
  96. UpdateUser: userId,
  97. IsDeleted: 0,
  98. }
  99. if err := program.Create(); err != nil {
  100. logger.Logger.Errorf("Create err = %s \n", err.Error())
  101. return common.FailResponse(err.Error(), nil)
  102. }
  103. libraryIds := common.StringToIntArray(req.LibraryIds)
  104. for index, libraryId := range libraryIds {
  105. relation := &dao.ProgramRelation{
  106. ProgramId: program.ID,
  107. LibraryId: libraryId,
  108. OrderNo: index + 1,
  109. TenantId: tenantId,
  110. IsDeleted: 0,
  111. }
  112. err := relation.Save()
  113. if err != nil {
  114. logger.Logger.Errorf("relation save err = %s \n", err.Error())
  115. _ = program.Remove()
  116. _ = relation.Remove()
  117. return common.FailResponse(err.Error(), nil)
  118. }
  119. }
  120. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeInfoBar,
  121. common.DeviceTypeDefault, program.Name, common.OperationSuccess)
  122. return common.SuccessResponse(common.Succeeded, nil)
  123. }
  124. func (s *programService) GetLibraryList(tenantId, programId int) ([]dao.Library, *common.Errors) {
  125. relation := &dao.ProgramRelation{
  126. ProgramId: programId,
  127. TenantId: tenantId,
  128. }
  129. relations, err := relation.GetByProgram()
  130. if err != nil {
  131. logger.Logger.Errorf("relation BatchCreate err = %s \n", err.Error())
  132. return nil, common.FailResponse(err.Error(), nil)
  133. }
  134. var libraryIds []int
  135. for _, relation := range relations {
  136. libraryIds = append(libraryIds, relation.LibraryId)
  137. }
  138. library := &dao.Library{TenantId: tenantId}
  139. libraries, err := library.GetLibrariesByIds(libraryIds)
  140. if err != nil {
  141. logger.Logger.Errorf("GetLibrariesByIds err = %s \n", err.Error())
  142. return nil, common.FailResponse(err.Error(), nil)
  143. }
  144. return libraries, nil
  145. }
  146. func (s *programService) RelationDeviceList(tenantId, resolution int) ([]deviceModel.LampPoleGroupDetail,
  147. *common.Errors) {
  148. return nil, nil
  149. }