get_project_code.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package provincial_department
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/tidwall/gjson"
  6. "go.uber.org/zap"
  7. "smart-provincial/errors"
  8. "smart-provincial/pb/v1"
  9. "smart-provincial/thirdparty"
  10. "strconv"
  11. "strings"
  12. "smart-provincial/parser"
  13. "github.com/jaryhe/gopkgs/logger"
  14. )
  15. func ProvincialGetProjectCode(ctx context.Context, req *v1.ProvincialGetProjectCodeRequest)(reply *v1.ProvincialGetProjectCodeReply, err error) {
  16. token, err := Login()
  17. if err != nil {
  18. return nil, err
  19. }
  20. areaCode, _ := strconv.Atoi(req.AreaCode)
  21. data := map[string]interface{}{
  22. "Name":req.Name,
  23. "Category":req.Category,
  24. "AreaCode":areaCode,
  25. }
  26. bytes, err := thirdparty.ProvincialPost(parser.Conf.ThirdParty.Provincial.ProjectCodeApi, token, data)
  27. fmt.Printf("provincial get project code: %s,%v\n", bytes, err)
  28. if err != nil {
  29. logger.Error("thirdpary get project code",
  30. zap.String("call", parser.Conf.ThirdParty.Provincial.ProjectCodeApi),
  31. zap.String("error", err.Error()))
  32. return nil, errors.ProvincialProjectCodeError
  33. }
  34. ack := gjson.GetBytes(bytes, "ack_code").String()
  35. if strings.ToUpper(ack) != "SUCCESS" {
  36. logger.Error("thirdpary get project code",
  37. zap.String("call", parser.Conf.ThirdParty.Provincial.ProjectCodeApi),
  38. zap.String("error",string(bytes)))
  39. return nil, errors.ProvincialProjectCodeError
  40. }
  41. projectCode := gjson.GetBytes(bytes, "Code").String()
  42. if projectCode == "" {
  43. logger.Error("thirdpary get project code",
  44. zap.String("call", parser.Conf.ThirdParty.Provincial.ProjectCodeApi),
  45. zap.String("error",string(bytes)))
  46. return nil, errors.ProvincialProjectCodeError
  47. }
  48. reply = &v1.ProvincialGetProjectCodeReply{
  49. Code:projectCode,
  50. }
  51. return reply, nil
  52. }