package logic

import (
	"context"
	"time"

	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTopicListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetTopicListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTopicListLogic {
	return &GetTopicListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 话题列表
func (l *GetTopicListLogic) GetTopicList(in *plant.IdReq) (*plant.TopicListResp, error) {
	var list []plantModel.Topic
	if err := l.svcCtx.DB.Order("post_count desc").Find(&list).Error; err != nil {
		return nil, err
	}
	var result []*plant.TopicInfo
	for _, item := range list {
		start, end := "", ""
		if item.StartTime != nil {
			start = item.StartTime.Format(time.DateTime)
		}
		if item.EndTime != nil {
			end = item.EndTime.Format(time.DateTime)
		}
		result = append(result, &plant.TopicInfo{
			Id:        item.ID,
			Name:      item.Name,
			PostCount: int32(item.PostCount),
			Icon:      item.Icon,
			Desc:      item.Desc,
			Title:     item.Title,
			Remark:    item.Remark,
			StartTime: start,
			EndTime:   end,
		})
	}
	return &plant.TopicListResp{List: result}, nil
}
