item.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 ItemList(c *gin.Context) {
  10. page := app.HandlePageNum(c)
  11. where := map[string]interface{}{"_orderby": "id desc", "deleted_at": 0}
  12. if name := c.Query("name"); name != "" {
  13. where["item_name like"] = "%" + name + "%"
  14. }
  15. if brand_id := utils.StrTo(c.Query("brand_id")).MustInt(); brand_id > 0 {
  16. where["brand_id"] = brand_id
  17. }
  18. if type_id := utils.StrTo(c.Query("type_id")).MustInt(); type_id > 0 {
  19. where["type_id"] = type_id
  20. }
  21. type Item struct {
  22. ID int `json:"id"`
  23. ItemName string `json:"item_name"`
  24. Level int `json:"level"`
  25. Unit int `json:"unit"`
  26. UnitName string `json:"unit_name"`
  27. BrandID int `json:"brand_id"`
  28. BrandName string `json:"brand_name"`
  29. TypeID int `json:"type_id"`
  30. TypeName string `json:"type_name"`
  31. Colors []string `json:"colors"`
  32. ColorText string `json:"color_text"`
  33. }
  34. itemList := make([]Item, 0)
  35. if _, err := item.GetList(where, nil, page, &itemList); err != nil {
  36. app.Error(c, err.Error())
  37. return
  38. }
  39. itemIds := make([]int, 0)
  40. brandIds := make([]int, 0)
  41. for _, v := range itemList {
  42. brandIds = append(brandIds, v.BrandID)
  43. itemIds = append(itemIds, v.ID)
  44. }
  45. brandListMap := make(map[int]string, 0)
  46. if len(brandIds) > 0 {
  47. if brandList, err := brand.GetList(map[string]interface{}{"id in": brandIds}, nil, app.Page{}, nil); err != nil {
  48. app.Error(c, err.Error())
  49. return
  50. } else {
  51. for _, v := range brandList {
  52. brandListMap[v.ID] = v.BrandName
  53. }
  54. }
  55. }
  56. colorListMap := make(map[int][]string, 0)
  57. if len(itemIds) > 0 {
  58. if colorList, err := item.GetOptionList(map[string]interface{}{"item_id in": itemIds}, nil, app.Page{}, nil); err == nil {
  59. for _, v := range colorList {
  60. colorListMap[v.ItemID] = append(colorListMap[v.ItemID], v.OptionName)
  61. }
  62. }
  63. }
  64. typeListMap := item.GetTypeMapByCache()
  65. unitTypes := utils.ParseSliceMap(item.Params.Unit, "id")
  66. for k, v := range itemList {
  67. v.UnitName = utils.ToStr(unitTypes[utils.ToStr(v.Unit)]["name"])
  68. v.BrandName = brandListMap[v.BrandID]
  69. v.TypeName = typeListMap[v.TypeID]
  70. v.ColorText = ""
  71. if colorListMap[v.ID] == nil {
  72. v.Colors = make([]string, 0)
  73. } else {
  74. v.Colors = colorListMap[v.ID]
  75. }
  76. itemList[k] = v
  77. }
  78. app.Success(c, itemList)
  79. }
  80. func TypeList(c *gin.Context) {
  81. where := make(map[string]interface{})
  82. types, err := item.GetTypeList(where, []string{"id", "type_name"}, app.Page{}, nil)
  83. if err != nil {
  84. app.Error(c, err.Error())
  85. return
  86. }
  87. app.Success(c, types)
  88. }