package category

import (
	"context"
	"time"

	"bsm/full/module/base/cms/internal/impl"
	"bsm/full/module/base/cms/internal/models"
	pb "bsm/full/module/base/cms/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/types"
)

// 修改分类
func Modify(ctx context.Context, in *pb.ModifyCategoryRequest) (reply *pb.StatusReply, err error) {
	if in.GetSiteIdentity() == "" || in.GetIdentity() == "" || len(in.GetIdentity()) > 255 {
		return nil, errcode.ErrInvalidArgument
	}
	if in.GetTitle() == "" || len(in.GetTitle()) > 255 {
		return nil, errcode.ErrInvalidArgument
	}

	data := &models.CmsCategory{
		SiteIdentity: in.SiteIdentity,
		ParentId:     uint(in.GetParentId()),
		CategoryKey:  in.GetCategoryKey(),
		Title:        in.GetTitle(),
		CoverPath:    in.GetCoverPath(),
		Intro:        in.GetIntro(),
		Std_Identity: types.Std_Identity{Identity: in.Identity},
	}
	if err := impl.DBService.Where("identity = ?", data.Identity).Updates(data).Error; err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

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

}
