package coupon

import (
	"context"

	"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 ByStatus(ctx context.Context, in *pb.Status) (reply *pb.CouponListReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	var (
		coupon = make([]*models.OrderCoupon, 0)
		result = make([]*pb.CouponItem, 0)
	)

	err = impl.DBService.Where("passport_id=?", auth.ID).Find(&coupon).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	for _, item := range coupon {
		result = append(result, ReflectProtoOrderCoupon(item))
	}

	return &pb.CouponListReply{
		Data: result,
	}, nil

}
func ReflectProtoOrderCoupon(v *models.OrderCoupon) *pb.CouponItem {
	if nil == v {
		return nil
	}
	result := &pb.CouponItem{
		Id:       int64(v.ID),
		Identity: v.Identity,
		Title:    v.Title,
		Intro:    v.Intro,
		Amount:   v.Intro,
	}
	return result
}
