captureUintService.go 5.5 KB

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