captureUintService.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. detail.EndLineTime = ""
  57. detail.NetworkState = util.GetDeviceState(device.CaptureSn)
  58. return detail, nil
  59. }
  60. func (s *captureUintService) CaptureList(searchValue string, current, size int) ([]model.CaptureDetail, *util.Errors) {
  61. var details []model.CaptureDetail
  62. device := dao.CaptureUnit{}
  63. if searchValue != "" {
  64. device.CaptureSn = searchValue
  65. }
  66. offset := (current - 1) * size
  67. limit := size
  68. devices, err := device.GetDevices(offset, limit)
  69. if err != nil {
  70. return nil, util.FailResponse(err.Error(), nil)
  71. }
  72. for _, d := range devices {
  73. details = append(details, model.CaptureDetail{
  74. CaptureUnit: d,
  75. EndLineTime: "",
  76. NetworkState: util.GetDeviceState(device.CaptureSn),
  77. })
  78. }
  79. return details, nil
  80. }
  81. func (s *captureUintService) CaptureGetList(tenantId int) ([]*dao.CaptureUnit, *util.Errors) {
  82. device := &dao.CaptureUnit{
  83. TenantId: tenantId,
  84. IsDeleted: 0,
  85. }
  86. devices, err := device.GetAllDevices()
  87. if err != nil {
  88. return nil, util.FailResponse(err.Error(), nil)
  89. }
  90. for _, d := range devices {
  91. d.CaptureName = d.CaptureName + "(" + d.CaptureSn + ")"
  92. }
  93. return devices, nil
  94. }
  95. func (s *captureUintService) GetPoint(id int) (*dao.CheckPoint, *util.Errors) {
  96. // 创建查询实例
  97. device := &dao.CheckPoint{
  98. ID: id,
  99. }
  100. err := device.GetDevice()
  101. if err != nil {
  102. return nil, util.FailResponse(err.Error(), nil)
  103. }
  104. return device, nil
  105. }
  106. func (s *captureUintService) PointSubmit(userId int64, tenantId int, req *dao.CheckPoint) *util.Errors {
  107. device := req
  108. device.TenantId = tenantId
  109. device.UpdateUser = userId
  110. device.UpdateTime = time.Now()
  111. if device.ID == 0 {
  112. device.CreateTime = time.Now()
  113. device.CreateUser = userId
  114. if device.IsExistedBySN() {
  115. fmt.Printf("Create IsExistedBySN \n")
  116. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  117. }
  118. fmt.Printf("device = %+v \n", device)
  119. if err := device.Create(); err != nil {
  120. fmt.Printf("Create err = %s \n", err.Error())
  121. return util.FailResponse(err.Error(), nil)
  122. }
  123. service.OperationHisService.Save(userId, tenantId, util.OperationCreate, util.ModuleTypeDevice,
  124. util.DeviceTypePoint, util.GetDeviceObject(device.ID, device.PointName), util.OperationSuccess)
  125. return util.SuccessResponse(util.Succeeded, nil)
  126. }
  127. if err := device.Update(); err != nil {
  128. fmt.Printf("Update err = %s \n", err.Error())
  129. return util.FailResponse(err.Error(), nil)
  130. }
  131. service.OperationHisService.Save(userId, tenantId, util.OperationUpdate, util.ModuleTypeDevice,
  132. util.DeviceTypePoint, util.GetDeviceObject(device.ID, device.PointName), util.OperationSuccess)
  133. return util.SuccessResponse(util.Succeeded, nil)
  134. }
  135. func (s *captureUintService) PointList(searchValue string, current, size int) ([]dao.CheckPoint, *util.Errors) {
  136. device := dao.CheckPoint{}
  137. if searchValue != "" {
  138. device.PointSN = searchValue
  139. }
  140. offset := (current - 1) * size
  141. limit := size
  142. devices, err := device.GetDevices(offset, limit)
  143. if err != nil {
  144. return nil, util.FailResponse(err.Error(), nil)
  145. }
  146. return devices, nil
  147. }
  148. func (s *captureUintService) PointGetList(tenantId int) ([]*dao.CheckPoint, *util.Errors) {
  149. device := &dao.CheckPoint{
  150. TenantId: tenantId,
  151. IsDeleted: 0,
  152. }
  153. devices, err := device.GetAllDevices()
  154. if err != nil {
  155. return nil, util.FailResponse(err.Error(), nil)
  156. }
  157. for _, d := range devices {
  158. d.PointName = d.PointName + "(" + d.PointSN + ")"
  159. }
  160. return devices, nil
  161. }
  162. func (s *captureUintService) RemoveCapture(userId int64, tenantId int, id int) *util.Errors {
  163. // 创建查询实例
  164. device := &dao.CaptureUnit{
  165. ID: id,
  166. IsDeleted: 1,
  167. UpdateUser: userId,
  168. UpdateTime: time.Now(),
  169. }
  170. err := device.Delete()
  171. if err != nil {
  172. return util.FailResponse(err.Error(), nil)
  173. }
  174. service.OperationHisService.Save(userId, tenantId, util.OperationRemove, util.ModuleTypeDevice,
  175. util.DeviceTypeCaptureUnit, util.GetDeviceObject(device.ID, device.CaptureName), util.OperationSuccess)
  176. return nil
  177. }
  178. func (s *captureUintService) RemovePoint(userId int64, tenantId int, id int) *util.Errors {
  179. // 创建查询实例
  180. device := &dao.CheckPoint{
  181. ID: id,
  182. IsDeleted: 1,
  183. UpdateUser: userId,
  184. UpdateTime: time.Now(),
  185. }
  186. err := device.Delete()
  187. if err != nil {
  188. return util.FailResponse(err.Error(), nil)
  189. }
  190. service.OperationHisService.Save(userId, tenantId, util.OperationRemove, util.ModuleTypeDevice,
  191. util.DeviceTypePoint, util.GetDeviceObject(device.ID, device.PointName), util.OperationSuccess)
  192. return nil
  193. }
  194. func (s *captureUintService) GetCaptureByGateway(id int) []dao.CaptureUnit {
  195. // 创建查询实例
  196. device := &dao.CaptureUnit{
  197. GatewayId: id,
  198. }
  199. return device.GetDevicesByGateway()
  200. }
  201. func (s *captureUintService) GetByLampPole(id int) []dao.CaptureUnit {
  202. // 创建查询实例
  203. device := &dao.CaptureUnit{
  204. LampPoleId: id,
  205. }
  206. return device.GetDevicesByLampPole()
  207. }