package post

import (
	"context"
	"time"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"git.apinb.com/bsm-sdk/core/types"
	"git.apinb.com/bsm-sdk/core/utils"
	"bsm/full/module/social/feed/internal/models"
	pb "bsm/full/module/social/feed/pb"
)

// 修改推文
func Change(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
	_, err = service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	if in.GetContent() == "" || in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}
	var postData = models.FeedPost{
		Std_IICUDS: types.Std_IICUDS{Identity: in.Identity},
		Content:    in.Content,
		IsOpen:     in.IsOpen,
	}

	if len(in.GetAttachs()) != 0 {
		for _, val := range in.Attachs {
			postData.Attachs = append(postData.Attachs, models.FeedRelateAttach{
				Identity:   utils.UUID(),
				AttachType: val.AttachType,
				Url:        val.Url,
			})
		}
	}
	err = models.ChargePost(&postData)
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.StatusReply{
		Data:    postData.Identity,
		Timeseq: time.Now().UnixNano(),
	}, nil

}
