new_api.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package api
  2. import (
  3. "net/http"
  4. "strconv"
  5. "tmr-watch/middleware"
  6. "tmr-watch/service/group"
  7. "tmr-watch/service/sheep"
  8. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  9. feedPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/pasture"
  10. "gitee.com/xuyiping_admin/pkg/apierr"
  11. "gitee.com/xuyiping_admin/pkg/ginutil"
  12. "gitee.com/xuyiping_admin/pkg/valid"
  13. "github.com/gin-gonic/gin"
  14. )
  15. // SearchFeetTemplateList 配方列表
  16. func SearchFeetTemplateList(c *gin.Context) {
  17. var req feedPb.SearchFeedTemplateRequest
  18. if err := ginutil.BindProto(c, &req); err != nil {
  19. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  20. return
  21. }
  22. req.Pagination = &feedPb.PaginationModel{
  23. Page: int32(c.GetInt(middleware.Page)),
  24. PageSize: int32(c.GetInt(middleware.PageSize)),
  25. PageOffset: int32(c.GetInt(middleware.PageOffset)),
  26. }
  27. list, err := group.SearchFeedTemplateList(c, &req)
  28. if err != nil {
  29. apierr.ClassifiedAbort(c, err)
  30. return
  31. }
  32. ginutil.JSONResp(c, list)
  33. }
  34. // GetFeetTemplateVersion 获取配方最新版本详情
  35. func GetFeetTemplateVersion(c *gin.Context) {
  36. var req feedPb.FeetTemplateVersionRequest
  37. if err := ginutil.BindProto(c, &req); err != nil {
  38. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  39. return
  40. }
  41. if err := valid.ValidateStruct(&req,
  42. valid.Field(&req.FeetTemplateId, valid.Required, valid.Length(1, 100)),
  43. valid.Field(&req.PastureId, valid.Required, valid.Min(1)),
  44. ); err != nil {
  45. apierr.ClassifiedAbort(c, err)
  46. return
  47. }
  48. feetTemplateId, _ := strconv.Atoi(req.GetFeetTemplateId())
  49. list, err := group.GetFeetTemplateVersion(c, int(req.GetPastureId()), feetTemplateId)
  50. if err != nil {
  51. apierr.ClassifiedAbort(c, err)
  52. return
  53. }
  54. ginutil.JSONResp(c, list)
  55. }
  56. // FeetTemplateUpgradeNewVersion 配方更新至最新版本
  57. func FeetTemplateUpgradeNewVersion(c *gin.Context) {
  58. var req feedPb.FeetTemplateVersionRequest
  59. if err := ginutil.BindProto(c, &req); err != nil {
  60. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  61. return
  62. }
  63. if err := valid.ValidateStruct(&req,
  64. valid.Field(&req.FeetTemplateId, valid.Required, valid.Length(1, 100)),
  65. valid.Field(&req.PastureId, valid.Required, valid.Min(1)),
  66. ); err != nil {
  67. apierr.ClassifiedAbort(c, err)
  68. return
  69. }
  70. feetTemplateId, _ := strconv.Atoi(req.GetFeetTemplateId())
  71. if err := group.FeetTemplateUpgradeNewVersion(c, int(req.GetPastureId()), feetTemplateId); err != nil {
  72. apierr.ClassifiedAbort(c, err)
  73. return
  74. }
  75. ginutil.JSONResp(c, &operationPb.CommonOK{
  76. Code: http.StatusOK,
  77. Msg: "ok",
  78. Data: &operationPb.Success{Success: true},
  79. })
  80. }
  81. // UpdateTmrGear 更新tmr_gear表数据
  82. func UpdateTmrGear(c *gin.Context) {
  83. var req operationPb.UpdateOrCreateTmrGearRequest
  84. if err := ginutil.BindProto(c, &req); err != nil {
  85. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  86. return
  87. }
  88. if err := valid.ValidateStruct(&req,
  89. valid.Field(&req.TmrId, valid.Required),
  90. valid.Field(&req.Params, valid.Required),
  91. ); err != nil {
  92. apierr.ClassifiedAbort(c, err)
  93. return
  94. }
  95. if err := sheep.UpdateOrCreateTmrGear(c, &req); err != nil {
  96. apierr.ClassifiedAbort(c, err)
  97. return
  98. }
  99. ginutil.JSONResp(c, &operationPb.CommonOK{
  100. Code: http.StatusOK,
  101. Msg: "ok",
  102. Data: &operationPb.Success{Success: true},
  103. })
  104. }
  105. func UseGear(c *gin.Context) {
  106. var req operationPb.UseGearRequest
  107. if err := ginutil.BindProto(c, &req); err != nil {
  108. apierr.AbortBadRequest(c, http.StatusBadRequest, err)
  109. return
  110. }
  111. if err := valid.ValidateStruct(&req,
  112. valid.Field(&req.TmrId, valid.Required),
  113. valid.Field(&req.Id, valid.Required),
  114. valid.Field(&req.TmrId, valid.Required),
  115. ); err != nil {
  116. apierr.ClassifiedAbort(c, err)
  117. return
  118. }
  119. data, err := sheep.UseGear(c, &req)
  120. if err != nil {
  121. apierr.ClassifiedAbort(c, err)
  122. return
  123. }
  124. ginutil.JSONResp(c, data)
  125. }