You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
2.7 KiB

  1. package controller
  2. import (
  3. "strconv"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/model"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // GetAllPricingTags 获取所有定价标签
  9. func GetAllPricingTags(c *gin.Context) {
  10. list, err := model.GetAllPricingTags()
  11. if err != nil {
  12. common.ApiError(c, err)
  13. return
  14. }
  15. common.ApiSuccess(c, list)
  16. }
  17. // CreatePricingTagRequest 创建定价标签请求
  18. type CreatePricingTagRequest struct {
  19. Name string `json:"name" binding:"required"`
  20. Color string `json:"color"`
  21. Description string `json:"description"`
  22. SortOrder int `json:"sort_order"`
  23. }
  24. // CreatePricingTag 创建定价标签
  25. func CreatePricingTag(c *gin.Context) {
  26. var req CreatePricingTagRequest
  27. if err := c.ShouldBindJSON(&req); err != nil {
  28. common.ApiError(c, err)
  29. return
  30. }
  31. // 检查名称是否重复
  32. existing, _ := model.GetPricingTagByName(req.Name)
  33. if existing != nil {
  34. common.ApiErrorMsg(c, "tag name already exists")
  35. return
  36. }
  37. pt := &model.PricingTag{
  38. Name: req.Name,
  39. Color: req.Color,
  40. Description: req.Description,
  41. SortOrder: req.SortOrder,
  42. }
  43. if pt.Color == "" {
  44. pt.Color = "#1890ff"
  45. }
  46. if err := pt.Insert(); err != nil {
  47. common.ApiError(c, err)
  48. return
  49. }
  50. common.ApiSuccess(c, pt)
  51. }
  52. // UpdatePricingTagRequest 更新定价标签请求
  53. type UpdatePricingTagRequest struct {
  54. Name string `json:"name"`
  55. Color string `json:"color"`
  56. Description string `json:"description"`
  57. SortOrder int `json:"sort_order"`
  58. }
  59. // UpdatePricingTag 更新定价标签
  60. func UpdatePricingTag(c *gin.Context) {
  61. idStr := c.Param("id")
  62. id, err := strconv.Atoi(idStr)
  63. if err != nil {
  64. common.ApiError(c, err)
  65. return
  66. }
  67. var req UpdatePricingTagRequest
  68. if err := c.ShouldBindJSON(&req); err != nil {
  69. common.ApiError(c, err)
  70. return
  71. }
  72. pt, err := model.GetPricingTagById(id)
  73. if err != nil {
  74. common.ApiError(c, err)
  75. return
  76. }
  77. // 检查名称是否与其他标签重复
  78. if req.Name != "" && req.Name != pt.Name {
  79. existing, _ := model.GetPricingTagByName(req.Name)
  80. if existing != nil && existing.Id != id {
  81. common.ApiErrorMsg(c, "tag name already exists")
  82. return
  83. }
  84. pt.Name = req.Name
  85. }
  86. if req.Color != "" {
  87. pt.Color = req.Color
  88. }
  89. pt.Description = req.Description
  90. pt.SortOrder = req.SortOrder
  91. if err := pt.Update(); err != nil {
  92. common.ApiError(c, err)
  93. return
  94. }
  95. common.ApiSuccess(c, pt)
  96. }
  97. // DeletePricingTag 删除定价标签
  98. func DeletePricingTag(c *gin.Context) {
  99. idStr := c.Param("id")
  100. id, err := strconv.Atoi(idStr)
  101. if err != nil {
  102. common.ApiError(c, err)
  103. return
  104. }
  105. pt := &model.PricingTag{Id: id}
  106. if err := pt.Delete(); err != nil {
  107. common.ApiError(c, err)
  108. return
  109. }
  110. common.ApiSuccess(c, nil)
  111. }