captureUintService.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/util"
  7. "time"
  8. )
  9. // 中间件管理服务
  10. var CaptureUintService = new(captureUintService)
  11. type captureUintService struct{}
  12. func (s *captureUintService) CaptureSubmit(userId int64, tenantId int, req *model.CaptureDetail) *util.Errors {
  13. device := req.CaptureUnit
  14. device.TenantId = tenantId
  15. device.UpdateUser = userId
  16. device.UpdateTime = time.Now()
  17. if gateway, err := GatewayService.GetOne(device.GatewayId); err == nil {
  18. device.GatewayName = gateway.GatewayName
  19. device.GatewaySN = gateway.GatewaySN
  20. }
  21. if device.ID == 0 {
  22. device.CreateTime = time.Now()
  23. device.CreateUser = userId
  24. if device.IsExistedBySN() {
  25. fmt.Printf("Create IsExistedBySN \n")
  26. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  27. }
  28. fmt.Printf("device = %+v \n", device)
  29. if err := device.Create(); err != nil {
  30. fmt.Printf("Create err = %s \n", err.Error())
  31. return util.FailResponse(err.Error(), nil)
  32. }
  33. return util.SuccessResponse(util.Succeeded, nil)
  34. }
  35. if err := device.Update(); err != nil {
  36. fmt.Printf("Update err = %s \n", err.Error())
  37. return util.FailResponse(err.Error(), nil)
  38. }
  39. //todo operation record
  40. return util.SuccessResponse(util.Succeeded, nil)
  41. }
  42. func (s *captureUintService) GetCapture(id int) (*model.CaptureDetail, *util.Errors) {
  43. // 创建查询实例
  44. device := &dao.CaptureUnit{
  45. ID: id,
  46. }
  47. err := device.GetDevice()
  48. if err != nil {
  49. return nil, util.FailResponse(err.Error(), nil)
  50. }
  51. detail := &model.CaptureDetail{CaptureUnit: *device}
  52. //todo 获取实时在线最新数据
  53. // detail.EndLineTime = *
  54. // detail.NetworkState = *
  55. return detail, nil
  56. }
  57. func (s *captureUintService) CaptureList(searchValue string, current, size int) ([]model.CaptureDetail, *util.Errors) {
  58. var details []model.CaptureDetail
  59. device := dao.CaptureUnit{}
  60. if searchValue != "" {
  61. device.CaptureSN = searchValue
  62. }
  63. offset := (current - 1) * size
  64. limit := size
  65. devices, err := device.GetDevices(offset, limit)
  66. if err != nil {
  67. return nil, util.FailResponse(err.Error(), nil)
  68. }
  69. for _, d := range devices {
  70. details = append(details, model.CaptureDetail{
  71. CaptureUnit: d,
  72. //todo 获取实时在线最新数据
  73. // detail.EndLineTime = *
  74. // detail.NetworkState = *
  75. })
  76. }
  77. return details, nil
  78. }
  79. func (s *captureUintService) CaptureGetList(tenantId int) ([]*dao.CaptureUnit, *util.Errors) {
  80. device := &dao.CaptureUnit{
  81. TenantId: tenantId,
  82. IsDeleted: 0,
  83. }
  84. devices, err := device.GetAllDevices()
  85. if err != nil {
  86. return nil, util.FailResponse(err.Error(), nil)
  87. }
  88. for _, d := range devices {
  89. d.CaptureName = d.CaptureName + "(" + d.CaptureSN + ")"
  90. }
  91. return devices, nil
  92. }
  93. func (s *captureUintService) GetPoint(id int) (*dao.CheckPoint, *util.Errors) {
  94. // 创建查询实例
  95. device := &dao.CheckPoint{
  96. ID: id,
  97. }
  98. err := device.GetDevice()
  99. if err != nil {
  100. return nil, util.FailResponse(err.Error(), nil)
  101. }
  102. return device, nil
  103. }
  104. func (s *captureUintService) PointSubmit(userId int64, tenantId int, req *dao.CheckPoint) *util.Errors {
  105. device := req
  106. device.TenantId = tenantId
  107. device.UpdateUser = userId
  108. device.UpdateTime = time.Now()
  109. if device.ID == 0 {
  110. device.CreateTime = time.Now()
  111. device.CreateUser = userId
  112. if device.IsExistedBySN() {
  113. fmt.Printf("Create IsExistedBySN \n")
  114. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  115. }
  116. fmt.Printf("device = %+v \n", device)
  117. if err := device.Create(); err != nil {
  118. fmt.Printf("Create err = %s \n", err.Error())
  119. return util.FailResponse(err.Error(), nil)
  120. }
  121. return util.SuccessResponse(util.Succeeded, nil)
  122. }
  123. if err := device.Update(); err != nil {
  124. fmt.Printf("Update err = %s \n", err.Error())
  125. return util.FailResponse(err.Error(), nil)
  126. }
  127. //todo operation record
  128. return util.SuccessResponse(util.Succeeded, nil)
  129. }
  130. func (s *captureUintService) PointList(searchValue string, current, size int) ([]dao.CheckPoint, *util.Errors) {
  131. device := dao.CheckPoint{}
  132. if searchValue != "" {
  133. device.PointSN = searchValue
  134. }
  135. offset := (current - 1) * size
  136. limit := size
  137. devices, err := device.GetDevices(offset, limit)
  138. if err != nil {
  139. return nil, util.FailResponse(err.Error(), nil)
  140. }
  141. return devices, nil
  142. }
  143. func (s *captureUintService) PointGetList(tenantId int) ([]*dao.CheckPoint, *util.Errors) {
  144. device := &dao.CheckPoint{
  145. TenantId: tenantId,
  146. IsDeleted: 0,
  147. }
  148. devices, err := device.GetAllDevices()
  149. if err != nil {
  150. return nil, util.FailResponse(err.Error(), nil)
  151. }
  152. for _, d := range devices {
  153. d.PointName = d.PointName + "(" + d.PointSN + ")"
  154. }
  155. return devices, nil
  156. }
  157. func (s *captureUintService) RemoveCapture(userId int64, id int) *util.Errors {
  158. // 创建查询实例
  159. device := &dao.CaptureUnit{
  160. ID: id,
  161. IsDeleted: 1,
  162. UpdateUser: userId,
  163. UpdateTime: time.Now(),
  164. }
  165. //todo operation record
  166. err := device.Delete()
  167. if err != nil {
  168. return util.FailResponse(err.Error(), nil)
  169. }
  170. return nil
  171. }
  172. func (s *captureUintService) RemovePoint(userId int64, id int) *util.Errors {
  173. // 创建查询实例
  174. device := &dao.CheckPoint{
  175. ID: id,
  176. IsDeleted: 1,
  177. UpdateUser: userId,
  178. UpdateTime: time.Now(),
  179. }
  180. //todo operation record
  181. err := device.Delete()
  182. if err != nil {
  183. return util.FailResponse(err.Error(), nil)
  184. }
  185. return nil
  186. }