p09.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package query
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-data/errors"
  7. "adm-data/model"
  8. v1 "adm-data/pb/v1"
  9. jsoniter "github.com/json-iterator/go"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. )
  15. type p09Reqeust struct {
  16. StyleId string `json:"style_id"`
  17. }
  18. type p09Config struct {
  19. Useage string `json:"useage"`
  20. Detail string `json:"detail"`
  21. AttributeRule string `json:"attribute_rule"`
  22. }
  23. type p09Data struct {
  24. Config p09Config `json:"config"`
  25. C2Name string `json:"c2_name"`
  26. C2Id int64 `json:"c2_id"`
  27. }
  28. type p09Response struct {
  29. List []p09Data `json:"list"`
  30. }
  31. func p09(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  32. reply = &v1.QueryResponse{}
  33. // 捕获各个task中的异常并返回给调用者
  34. defer func() {
  35. if r := recover(); r != nil {
  36. err = fmt.Errorf("%+v", r)
  37. e := &status.Status{}
  38. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  39. logger.Error("err",
  40. zap.String("system_err", err.Error()),
  41. zap.Stack("stacktrace"))
  42. }
  43. }
  44. }()
  45. var req p09Reqeust
  46. err = jsoniter.UnmarshalFromString(params, &req)
  47. if err != nil && req.StyleId == "" {
  48. return nil, errors.ParamsError
  49. }
  50. list, err := model.NewAds14Model().List(database.DB().Where("style_id = ?", req.StyleId))
  51. if err != nil {
  52. return reply, errors.DataNotExistError
  53. }
  54. res := p09Response{
  55. List: make([]p09Data, 0, len(list)),
  56. }
  57. for _, v := range list {
  58. res.List = append(res.List, p09Data{
  59. C2Name: v.C2Name,
  60. C2Id: v.C2Id,
  61. Config: p09Config{
  62. Useage: v.Useage,
  63. Detail: v.Detail,
  64. AttributeRule: v.AttributeRule,
  65. },
  66. })
  67. }
  68. reply.Data, _ = jsoniter.MarshalToString(res)
  69. return reply, nil
  70. }