123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package material
- import (
- "github.com/gin-gonic/gin"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/material/brand"
- "zhiyuan/services/material/item"
- )
- func BrandList(c *gin.Context) {
- isSelect := utils.ToInt(c.Query("is_select"))
- if isSelect == 1 {
- BrandListSelect(c)
- return
- }
- page := app.HandlePageNum(c)
- where := make(map[string]interface{})
- total, err := brand.Count(where)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- type BrandList struct {
- ID int `json:"id"`
- BrandName string `json:"brand_name"`
- CreatedAt string `json:"created_at"`
- }
- brandList := make([]BrandList, 0)
- if _, err := brand.GetList(where, nil, page, &brandList); err != nil {
- app.Error(c, err.Error())
- return
- }
- for k, v := range brandList {
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
- brandList[k] = v
- }
- data := gin.H{
- "list": brandList,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func BrandListSelect(c *gin.Context) {
- where := map[string]interface{}{}
- if typeID := utils.ToInt(c.Query("type_id")); typeID > 0 {
- brandIds := make([]int, 0)
- if brandList, err := item.GetList(map[string]interface{}{"type_id": typeID, "_groupby": "brand_id"}, []string{"brand_id"}, app.Page{}, nil); err == nil {
- for _, v := range brandList {
- brandIds = append(brandIds, v.BrandID)
- }
- }
- if len(brandIds) > 0 {
- where["id in"] = brandIds
- }
- }
- type BrandList struct {
- ID int `json:"id"`
- BrandName string `json:"brand_name"`
- }
- brandList := make([]BrandList, 0)
- _, err := brand.GetList(where, []string{"id", "brand_name"}, app.Page{}, &brandList)
- if err != nil {
- app.Error(c, err.Error())
- }
- app.Success(c, brandList)
- }
|