dust_device_add.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package provincial_department
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/tidwall/gjson"
  6. "go.uber.org/zap"
  7. "smart-provincial/errors"
  8. "smart-provincial/pb/v1"
  9. "smart-provincial/thirdparty"
  10. "strings"
  11. "smart-provincial/parser"
  12. "github.com/jaryhe/gopkgs/logger"
  13. )
  14. func ProvincialPostDustDevice(ctx context.Context, req *v1.ProvincialPostDustDeviceRequest)(reply *v1.ProvincialPostDustDeviceReply, err error) {
  15. return &v1.ProvincialPostDustDeviceReply{}, nil
  16. token, err := Login()
  17. if err != nil {
  18. return nil, err
  19. }
  20. param := map[string]interface{}{
  21. "Code":req.Code,
  22. "Name":req.Name,
  23. "DeviceName":req.DeviceName,
  24. "DeviceID":req.DeviceId,
  25. "Manufacturer":req.Manufacturer,
  26. "Batch":req.Batch,
  27. "Unit":req.Unit,
  28. "DeviceModel":req.DeviceModel,
  29. }
  30. bytes, err := thirdparty.ProvincialPost(parser.Conf.ThirdParty.Provincial.DustDeviceUploadApi, token, param)
  31. if err != nil {
  32. s, _ := json.Marshal(param)
  33. logger.Error("thirdpary",
  34. zap.String("call", parser.Conf.ThirdParty.Provincial.DustDeviceUploadApi),
  35. zap.String("param", string(s)),
  36. zap.String("error", err.Error()))
  37. return nil, errors.ProvincialDustDeviceUploadError
  38. }
  39. ack := gjson.GetBytes(bytes, "ack_code").String()
  40. if strings.ToUpper(ack) != "SUCCESS" {
  41. s, _ := json.Marshal(param)
  42. logger.Error("thirdpary",
  43. zap.String("call", parser.Conf.ThirdParty.Provincial.DustDeviceUploadApi),
  44. zap.String("param", string(s)),
  45. zap.String("error",string(bytes)))
  46. return nil, errors.ProvincialDustDeviceUploadError
  47. }
  48. id := gjson.GetBytes(bytes, "ID").String()
  49. reply.Id = id
  50. reply = &v1.ProvincialPostDustDeviceReply{}
  51. return reply, nil
  52. }