brand.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package material
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/material/brand"
  7. "zhiyuan/services/material/item"
  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 := make(map[string]interface{})
  17. total, err := brand.Count(where)
  18. if err != nil {
  19. app.Error(c, err.Error())
  20. return
  21. }
  22. type BrandList struct {
  23. ID int `json:"id"`
  24. BrandName string `json:"brand_name"`
  25. CreatedAt string `json:"created_at"`
  26. }
  27. brandList := make([]BrandList, 0)
  28. if _, err := brand.GetList(where, nil, page, &brandList); err != nil {
  29. app.Error(c, err.Error())
  30. return
  31. }
  32. for k, v := range brandList {
  33. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  34. brandList[k] = v
  35. }
  36. data := gin.H{
  37. "list": brandList,
  38. "total": total,
  39. "limit": page.PageSize,
  40. }
  41. app.Success(c, data)
  42. }
  43. func BrandListSelect(c *gin.Context) {
  44. where := map[string]interface{}{}
  45. if typeID := utils.ToInt(c.Query("type_id")); typeID > 0 {
  46. brandIds := make([]int, 0)
  47. if brandList, err := item.GetList(map[string]interface{}{"type_id": typeID, "_groupby": "brand_id"}, []string{"brand_id"}, app.Page{}, nil); err == nil {
  48. for _, v := range brandList {
  49. brandIds = append(brandIds, v.BrandID)
  50. }
  51. }
  52. if len(brandIds) > 0 {
  53. where["id in"] = brandIds
  54. }
  55. }
  56. type BrandList struct {
  57. ID int `json:"id"`
  58. BrandName string `json:"brand_name"`
  59. }
  60. brandList := make([]BrandList, 0)
  61. _, err := brand.GetList(where, []string{"id", "brand_name"}, app.Page{}, &brandList)
  62. if err != nil {
  63. app.Error(c, err.Error())
  64. }
  65. app.Success(c, brandList)
  66. }