package summary

import (
	"context"
	"errors"
	"time"

	"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 SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	_, err = service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	if in.GetMemberIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}

	err = models.DBService.Where("identity = ?", in.Identity).Updates(&models.OrderSummary{Status: 3, DeliveryIdentity: in.MemberIdentity}).Error
	if err != nil {
		printer.Error(err.Error())
		if errors.Is(err, models.ErrNotFound) {
			return nil, errcode.ErrRecordNotFound
		}
		return nil, errcode.ErrDB
	}
	return &pb.StatusReply{
		Code:    0,
		Message: "OK",
		Timeseq: time.Now().UnixNano(),
	}, nil

}
