package material import ( "github.com/gin-gonic/gin" "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/services/material/brand" "zhiyuan/services/material/item" ) func ItemList(c *gin.Context) { page := app.HandlePageNum(c) where := map[string]interface{}{"_orderby": "id desc", "deleted_at": 0} if name := c.Query("name"); name != "" { where["item_name like"] = "%" + name + "%" } if brand_id := utils.StrTo(c.Query("brand_id")).MustInt(); brand_id > 0 { where["brand_id"] = brand_id } if type_id := utils.StrTo(c.Query("type_id")).MustInt(); type_id > 0 { where["type_id"] = type_id } type Item struct { ID int `json:"id"` ItemName string `json:"item_name"` Level int `json:"level"` Unit int `json:"unit"` UnitName string `json:"unit_name"` BrandID int `json:"brand_id"` BrandName string `json:"brand_name"` TypeID int `json:"type_id"` TypeName string `json:"type_name"` Colors []string `json:"colors"` ColorText string `json:"color_text"` } itemList := make([]Item, 0) if _, err := item.GetList(where, nil, page, &itemList); err != nil { app.Error(c, err.Error()) return } itemIds := make([]int, 0) brandIds := make([]int, 0) for _, v := range itemList { brandIds = append(brandIds, v.BrandID) itemIds = append(itemIds, v.ID) } brandListMap := make(map[int]string, 0) if len(brandIds) > 0 { if brandList, err := brand.GetList(map[string]interface{}{"id in": brandIds}, nil, app.Page{}, nil); err != nil { app.Error(c, err.Error()) return } else { for _, v := range brandList { brandListMap[v.ID] = v.BrandName } } } colorListMap := make(map[int][]string, 0) if len(itemIds) > 0 { if colorList, err := item.GetOptionList(map[string]interface{}{"item_id in": itemIds}, nil, app.Page{}, nil); err == nil { for _, v := range colorList { colorListMap[v.ItemID] = append(colorListMap[v.ItemID], v.OptionName) } } } typeListMap := item.GetTypeMapByCache() unitTypes := utils.ParseSliceMap(item.Params.Unit, "id") for k, v := range itemList { v.UnitName = utils.ToStr(unitTypes[utils.ToStr(v.Unit)]["name"]) v.BrandName = brandListMap[v.BrandID] v.TypeName = typeListMap[v.TypeID] v.ColorText = "" if colorListMap[v.ID] == nil { v.Colors = make([]string, 0) } else { v.Colors = colorListMap[v.ID] } itemList[k] = v } app.Success(c, itemList) } func TypeList(c *gin.Context) { where := make(map[string]interface{}) types, err := item.GetTypeList(where, []string{"id", "type_name"}, app.Page{}, nil) if err != nil { app.Error(c, err.Error()) return } app.Success(c, types) }