captureUintService.go 6.8 KB

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