package product

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"
)

// ModifyComment 修改评论
func CommentModify(ctx context.Context, in *pb.CommentItem) (reply *pb.IdentityStatusReply, err error) {
	// parse authorization meta.
	_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

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

	// 参数转换
	data, err := itemToCommentModel(in)
	if err != nil {
		return nil, errcode.ErrInternal
	}
	err = impl.DBService.Select("score", "reply", "product_identity", "store_identity", "spec_identity").Where("identity = ?", in.Identity).Updates(data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

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

}
