produdct.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package controllers
  2. import (
  3. "context"
  4. "sparrow/pkg/models"
  5. "sparrow/pkg/rpcs"
  6. "sparrow/pkg/server"
  7. "sparrow/services/knowoapi/services"
  8. "strings"
  9. "github.com/kataras/iris"
  10. opentracing "github.com/opentracing/opentracing-go"
  11. "github.com/opentracing/opentracing-go/ext"
  12. )
  13. // ProductController 产品API
  14. type ProductController struct {
  15. Ctx iris.Context
  16. Service services.ProductService
  17. Token Token
  18. }
  19. // Post /product 添加产品
  20. func (a *ProductController) Post() {
  21. product := new(models.Product)
  22. if err := parseBody(a.Ctx, product); err != nil {
  23. badRequest(a.Ctx, err)
  24. return
  25. }
  26. product.VendorID = int32(a.Token.getVendorID(a.Ctx))
  27. // if upload a image file
  28. if product.ProductImage != "" {
  29. //move image
  30. args := &rpcs.ArgsMoveFile{
  31. Source: product.ProductImage,
  32. Target: "product",
  33. }
  34. args.SpanCarrier = make(map[string]string)
  35. reply := &rpcs.ReplyMoveFile{}
  36. span, ctx := opentracing.StartSpanFromContext(context.Background(), a.Ctx.Path())
  37. defer span.Finish()
  38. ext.SpanKindRPCClient.Set(span)
  39. ext.HTTPMethod.Set(span, a.Ctx.Method())
  40. err := server.RPCCallByName(ctx, "fileaccess", "FileAccess.MoveFile", args, reply)
  41. if err != nil {
  42. server.Log.Error(err)
  43. product.ProductImage = ""
  44. } else {
  45. product.ProductImage = reply.FilePath
  46. }
  47. }
  48. err := a.Service.Create(product)
  49. if err != nil {
  50. responseError(a.Ctx, ErrDatabase, err.Error())
  51. return
  52. }
  53. done(a.Ctx, product)
  54. }
  55. // Delete /product 删除
  56. func (a *ProductController) Delete() {
  57. product := new(models.Product)
  58. if err := parseBody(a.Ctx, product); err != nil {
  59. badRequest(a.Ctx, err)
  60. return
  61. }
  62. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  63. responseError(a.Ctx, ErrNormal, "非法操作")
  64. return
  65. }
  66. err := a.Service.Delete(product)
  67. if err != nil {
  68. responseError(a.Ctx, ErrDatabase, err.Error())
  69. return
  70. }
  71. done(a.Ctx, "删除成功")
  72. }
  73. // Put /produdct 更新产品信息
  74. func (a *ProductController) Put() {
  75. product := new(models.Product)
  76. if err := parseBody(a.Ctx, product); err != nil {
  77. badRequest(a.Ctx, err)
  78. return
  79. }
  80. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  81. responseError(a.Ctx, ErrNormal, "非法操作")
  82. return
  83. }
  84. // check update image
  85. if strings.Contains(product.ProductImage, "tmp") {
  86. //move image
  87. args := &rpcs.ArgsMoveFile{
  88. Source: product.ProductImage,
  89. Target: "product",
  90. }
  91. reply := &rpcs.ReplyMoveFile{}
  92. err := server.RPCCallByName(nil, "fileaccess", "FileAccess.MoveFile", args, reply)
  93. if err != nil {
  94. server.Log.Error(err)
  95. product.ProductImage = ""
  96. } else {
  97. product.ProductImage = reply.FilePath
  98. }
  99. }
  100. if product.ProductImage == "" {
  101. old, _ := a.Service.QueryOne(a.Token.getVendorID(a.Ctx), product.ProductKey)
  102. arg := &rpcs.ArgsDeleteFile{
  103. FileName: old.ProductImage,
  104. }
  105. reply := &rpcs.ReplyEmptyResult{}
  106. server.RPCCallByName(nil, "fileaccess", "FileAccess.DeleteFile", arg, reply)
  107. }
  108. pro, err := a.Service.Update(product)
  109. if err != nil {
  110. responseError(a.Ctx, ErrDatabase, err.Error())
  111. return
  112. }
  113. done(a.Ctx, pro)
  114. }
  115. // Get /product 查询我的产品
  116. func (a *ProductController) Get() {
  117. pi, err := a.Ctx.URLParamInt("pi")
  118. if err != nil {
  119. badRequest(a.Ctx, err)
  120. return
  121. }
  122. ps, err := a.Ctx.URLParamInt("ps")
  123. if err != nil {
  124. badRequest(a.Ctx, err)
  125. return
  126. }
  127. name := a.Ctx.URLParam("name")
  128. ds, total, err := a.Service.GetVendorProducts(a.Token.getVendorID(a.Ctx),
  129. pi,
  130. ps,
  131. name)
  132. if err != nil {
  133. responseError(a.Ctx, ErrDatabase, err.Error())
  134. return
  135. }
  136. done(a.Ctx, map[string]interface{}{
  137. "list": ds,
  138. "total": total,
  139. })
  140. }
  141. // GetBy 获取产品信息
  142. // GET /product/{key}
  143. func (a *ProductController) GetBy(key string) {
  144. pro, err := a.Service.QueryOne(a.Token.getVendorID(a.Ctx), key)
  145. if err != nil {
  146. responseError(a.Ctx, ErrDatabase, err.Error())
  147. return
  148. }
  149. done(a.Ctx, pro)
  150. }