brand.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package material
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/form"
  7. "zhiyuan/services/material/brand"
  8. )
  9. func BrandList(c *gin.Context) {
  10. isSelect := utils.ToInt(c.Query("is_select"))
  11. if isSelect == 1 {
  12. BrandListSelect(c)
  13. return
  14. }
  15. page := app.HandlePageNum(c)
  16. where := map[string]interface{}{
  17. "deleted_at": 0,
  18. }
  19. total, err := brand.Count(where)
  20. if err != nil {
  21. app.Error(c, err.Error())
  22. return
  23. }
  24. type BrandList struct {
  25. ID int `json:"id"`
  26. BrandName string `json:"brand_name"`
  27. CreatedAt string `json:"created_at"`
  28. }
  29. brandList := make([]BrandList, 0)
  30. if _, err := brand.GetList(where, nil, page, &brandList); err != nil {
  31. app.Error(c, err.Error())
  32. return
  33. }
  34. for k, v := range brandList {
  35. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  36. brandList[k] = v
  37. }
  38. data := gin.H{
  39. "list": brandList,
  40. "total": total,
  41. "limit": page.PageSize,
  42. }
  43. app.Success(c, data)
  44. }
  45. func BrandListSelect(c *gin.Context) {
  46. type BrandList struct {
  47. ID int `json:"id"`
  48. BrandName string `json:"brand_name"`
  49. }
  50. where := map[string]interface{}{
  51. "_orderby": "CONVERT(brand_name USING gbk) COLLATE gbk_chinese_ci ASC",
  52. }
  53. if name := c.Query("name"); name != "" {
  54. where["brand_name like"] = "%" + name + "%"
  55. }
  56. if c.Query("show_all") == "" {
  57. where["deleted_at"] = 0
  58. }
  59. if id := c.Query("id"); id != "" {
  60. where["id"] = id
  61. }
  62. page := app.Page{}
  63. if c.Query("limit") != "" {
  64. page = app.HandlePageNum(c)
  65. }
  66. brandList := make([]BrandList, 0)
  67. _, err := brand.GetList(where, []string{"id", "brand_name"}, page, &brandList)
  68. if err != nil {
  69. app.Error(c, err.Error())
  70. }
  71. app.Success(c, brandList)
  72. }
  73. func BrandAdd(c *gin.Context) {
  74. var form form.MaterialBrandAdd
  75. if app.Bind(c, &form) != nil {
  76. return
  77. }
  78. if id, err := brand.Add(form); err != nil {
  79. app.Error(c, err.Error())
  80. return
  81. } else {
  82. app.Success(c, gin.H{"id": id})
  83. }
  84. }
  85. func BrandEdit(c *gin.Context) {
  86. id := utils.StrTo(c.Param("id")).MustInt()
  87. if id <= 0 {
  88. app.Error(c, "套餐id有误")
  89. return
  90. }
  91. var form form.MaterialBrandAdd
  92. if app.Bind(c, &form) != nil {
  93. return
  94. }
  95. if _, err := brand.Edit(form, id); err != nil {
  96. app.ErrorMsg(c, err.Error(), nil)
  97. return
  98. }
  99. app.Success(c, nil)
  100. }