82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package ec
|
|
|
|
import (
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ecCategoryView struct {
|
|
Identity string `json:"identity"`
|
|
ParentIdentity string `json:"parent_identity,omitempty"`
|
|
Name string `json:"name"`
|
|
SortNo int `json:"sort_no"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func ecCategoryViews(list []models.EcCategory) ([]ecCategoryView, error) {
|
|
parents := make(map[uint64]string)
|
|
for _, item := range list {
|
|
if item.ParentID != 0 {
|
|
parents[item.ParentID] = ""
|
|
}
|
|
}
|
|
if len(parents) > 0 {
|
|
var rows []struct {
|
|
ID uint64
|
|
Identity string
|
|
}
|
|
ids := make([]uint64, 0, len(parents))
|
|
for id := range parents {
|
|
ids = append(ids, id)
|
|
}
|
|
if err := impl.DBService.Model(&models.EcCategory{}).Select("id", "identity").Where("id IN ?", ids).Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range rows {
|
|
parents[row.ID] = row.Identity
|
|
}
|
|
}
|
|
views := make([]ecCategoryView, 0, len(list))
|
|
for _, item := range list {
|
|
views = append(views, ecCategoryView{Identity: item.Identity, ParentIdentity: parents[item.ParentID], Name: item.Name, SortNo: item.SortNo, Status: item.Status})
|
|
}
|
|
return views, nil
|
|
}
|
|
|
|
func ListEcCategory(ctx *gin.Context) {
|
|
page, size := common.PageSize(ctx)
|
|
var list []models.EcCategory
|
|
var total int64
|
|
if err := impl.DBService.Model(&models.EcCategory{}).Count(&total).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
if err := impl.DBService.Order("sort_no asc, id asc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
views, err := ecCategoryViews(list)
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, gin.H{"total": total, "list": views})
|
|
}
|
|
|
|
func GetEcCategory(ctx *gin.Context) {
|
|
var category models.EcCategory
|
|
if err := impl.DBService.Where("identity = ?", ctx.Param("identity")).First(&category).Error; err != nil {
|
|
common.RespondRecordError(ctx, err)
|
|
return
|
|
}
|
|
views, err := ecCategoryViews([]models.EcCategory{category})
|
|
if err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
infra.Response.Success(ctx, views[0])
|
|
}
|