package mgt

import (
	"context"
	"time"

	"bsm/full/module/ec/order/internal/impl"
	"bsm/full/module/ec/order/internal/models"
	pb "bsm/full/module/ec/order/pb"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
)

// 取消订单
func OrderCancel(ctx context.Context, in *pb.OrderIdentRequest) (reply *pb.OrderStatusReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	if auth.Owner == nil {
		return nil, errcode.ErrPermissionDenied
	}

	// valildate request id,identity.
	if in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	// 验证输入参数是否有效。
	if in.GetIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	err = impl.DBService.Model(&models.OrderSummary{}).Where("identity=?", in.Identity).Update("status", -1).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.OrderStatusReply{
		Code:     0,
		Message:  "OK",
		Identity: in.GetIdentity(),
		Timeseq:  time.Now().UnixNano(),
	}, nil

}
