package category

import (
	"context"
	"time"

	"bsm/full/module/ec/mall/internal/impl"
	pb "bsm/full/module/ec/mall/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
)

// 修改分类
func Modify(ctx context.Context, in *pb.CategoryItem) (reply *pb.IdentityStatusReply, err error) {
	_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	if in.GetTitle() == "" || in.GetIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}

	data, err := ref(in)
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrInternal
	}
	err = impl.DBService.Select("title", "en_title", "parent_id", "paths", "intro", "icon", "sort", "status", "keys").Where("identity=?", in.GetIdentity()).Updates(&data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

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

}
