batch_in.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package gate
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "gorm.io/gorm"
  11. "property-device/errors"
  12. dbmodel "property-device/model"
  13. pb_v1 "property-device/pb/v1"
  14. "property-device/utils/gate_utils"
  15. "strings"
  16. "time"
  17. )
  18. func checkGateBatchInParam(req *pb_v1.GateBatchInRequest) error {
  19. for _, v := range req.List {
  20. switch {
  21. case v.Manufactor == "":
  22. return status.Error(10003, "厂商不能为空")
  23. case gate_utils.GateProtocolNameMap[v.Protocol] == "":
  24. return status.Error(10003, "协议不能为空")
  25. case v.Sn == "":
  26. return status.Error(10003, "设备序列号不能为空")
  27. }
  28. }
  29. return nil
  30. }
  31. func gateBatchInHandle(db *gorm.DB, datas []*pb_v1.GateBatchInItem, now time.Time) error {
  32. array := make([]dbmodel.TGate, len(datas))
  33. for i, req := range datas {
  34. array[i].DeviceName = req.DeviceName
  35. array[i].Sn = req.Sn
  36. array[i].Manufactor = req.Manufactor
  37. array[i].AuthKey = req.AuthKey
  38. array[i].GardenId = 0
  39. array[i].OutUser = ""
  40. array[i].OutTime = 0
  41. array[i].Status = 2
  42. array[i].Enable = 1
  43. array[i].Location = ""
  44. array[i].Protocol = req.Protocol
  45. array[i].Direction = 0
  46. array[i].CreatedAt = now
  47. array[i].UpdatedAt = now
  48. array[i].QcodeSupport = gate_utils.GateProtocolFuntionMap[req.Protocol][0]
  49. array[i].PicSupport = gate_utils.GateProtocolFuntionMap[req.Protocol][1]
  50. array[i].CardSupport = gate_utils.GateProtocolFuntionMap[req.Protocol][2]
  51. }
  52. p := &dbmodel.TGate{}
  53. err := p.InsertMulti(db, &array)
  54. if err != nil {
  55. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  56. return status.Error(10003, "设备id或设备序列号重复")
  57. }
  58. return errors.DataBaseError
  59. }
  60. return nil
  61. }
  62. func GateBatchIn(ctx context.Context, req *pb_v1.GateBatchInRequest) (reply *pb_v1.GateBatchInReply, err error) {
  63. reply = &pb_v1.GateBatchInReply{}
  64. // 捕获各个task中的异常并返回给调用者
  65. defer func() {
  66. if r := recover(); r != nil {
  67. err = fmt.Errorf("%+v", r)
  68. e := &status.Status{}
  69. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  70. logger.Error("err",
  71. zap.String("system_err", err.Error()),
  72. zap.Stack("stacktrace"))
  73. }
  74. }
  75. }()
  76. err = checkGateBatchInParam(req)
  77. if err != nil {
  78. return nil, err
  79. }
  80. now := time.Now()
  81. length := len(req.List)
  82. gap := 50
  83. db := database.DB().Begin()
  84. // 分批入库
  85. for i := 0; i < length; i += gap {
  86. datas := req.List[i:length]
  87. if i+gap <= length {
  88. datas = req.List[i : i+gap]
  89. }
  90. if err = gateBatchInHandle(db, datas, now); err != nil {
  91. db.Rollback()
  92. return nil, err
  93. }
  94. }
  95. db.Commit()
  96. return reply, nil
  97. }