package post

import (
	"context"
	"time"

	"bsm/full/module/base/cms/internal/models"
	"bsm/full/module/base/cms/internal/utils"
	pb "bsm/full/module/base/cms/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/service"
)

// 修改文章
func Modify(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
	_, err = service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	if in.GetSiteIdentity() == "" || in.GetIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	var data = &models.CmsPost{
		SiteIdentity: in.SiteIdentity,
		Title:        in.Title,
		Description:  in.Description,
		CoverPath:    in.CoverPath,
		Author:       in.Author,
		Content:      in.Content,
		TargetUrl:    in.TargetUrl,
		SourceUrl:    in.SourceUrl,
		HasAccessory: in.HasAccessory,
		Types:        in.PostType,
		Hash:         utils.FormatKey(in.Key),
		Lang:         in.Lang,
		SourceOrigin: in.SourceOrigin,
		Rights:       in.Rights,
		ExtendUrl:    in.ExtendUrl,
		ExtendData:   in.ExtendData,
		ExtendImg:    in.ExtendImg,
		ExtendDesc:   in.ExtendDesc,
	}
	err = models.ModifyPost(in.Identity, data, in.AccessoryIdentityArray, in.CategoryIdentityArray, in.TagsIdentityArray)
	if err != nil {
		return nil, errcode.ErrDB
	}

	return &pb.StatusReply{
		Code:    0,
		Message: "OK",
		Timeseq: time.Now().Unix(),
	}, nil

}
