package product

import (
	"context"
	"time"

	"bsm/full/module/ec/mall/internal/impl"
	"bsm/full/module/ec/mall/internal/models"
	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"
)

// Modify 发布/取消发布 产品
func ItemModify(ctx context.Context, in *pb.ModifyRequest) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	// 检测参数
	if in.GetIdentity() == "" || in.GetStatus() == 0 {
		return nil, errcode.ErrInvalidArgument
	}

	// TODO: add your logic code & delete this line.
	if err := impl.DBService.Model(&models.MallProduct{}).Where("identity = ?", in.Identity).Update("status", in.Status).Error; err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.StatusReply{
		Code:    0,
		Message: "OK",
		Timeseq: time.Now().UnixMilli(),
	}, nil

}
