package category

import (
	"encoding/json"

	"bsm/full/module/ec/mall/internal/models"
	pb "bsm/full/module/ec/mall/pb"
	"git.apinb.com/bsm-sdk/core/vars"
)

// ref 获取分类详情
func ref(in *pb.CategoryItem) (*models.MallCategory, error) {
	var category = models.MallCategory{}
	data, err := json.Marshal(in)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal(data, &category)
	if err != nil {
		return nil, err
	}
	return &category, err
}

func refList(data []*models.MallCategory, types string) (list []*pb.CategoryItem) {
	if len(data) == 0 {
		return
	}

	for _, v := range data {
		low := pb.CategoryItem{
			Id:        int64(v.ID),
			Identity:  v.Identity,
			Title:     v.Title,
			EnTitle:   v.EnTitle,
			Keys:      v.Keys,
			ParentId:  v.ParentID,
			Paths:     v.Paths,
			Intro:     v.Intro,
			Icon:      v.Icon,
			Sort:      v.Sort,
			CreatedAt: v.CreatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
			Status:    int32(v.Status),
		}
		if types == "all" {
			low.Categories = refList(v.Categories, types)
		}
		list = append(list, &low)
	}
	return
}
