package common

import (
	"bsm/full/module/ec/order/internal/models"
	order "bsm/full/module/ec/order/pb"
	pb "bsm/full/module/ec/order/pb"
	"git.apinb.com/bsm-sdk/core/vars"
)

// ReflectProtoOrderDetails 订单详情
func ReflectProtoOrderDetails(v []models.OrderDetails) []*pb.OrderDetails {
	if nil == v {
		return nil
	}
	result := make([]*pb.OrderDetails, 0)
	for _, val := range v {
		result = append(result, &pb.OrderDetails{
			Id:              int64(val.ID),
			ProductId:       val.ProductID,
			SpecNo:          val.SpecNo,
			SpecId:          val.SpecID,
			Type:            int64(val.Type),
			Title:           val.Title,
			CoverImage:      val.CoverImage,
			SalesPrice:      val.SalesPrice,
			ProductArgs:     val.ProductArgs,
			Number:          int64(val.Number),
			UnitPrice:       val.UnitPrice,
			ProductIdentity: val.ProductIdentity,
			GasTypes:        int64(val.GasType),
			Spec:            val.SpecTitle,
			TotalPrice:      int64(val.Number) * val.UnitPrice,
		})
	}

	return result
}

func ReflectProtoOrderSummary(summary *models.OrderSummary) *pb.OrderSummaryItem {
	if summary == nil {
		return nil
	}
	item := pb.OrderSummaryItem{
		Id:               int64(summary.ID),
		Identity:         summary.Identity,
		OrderNo:          summary.OrderNo,
		PartnerId:        int64(summary.PartnerID),
		TotalPrice:       summary.TotalPrice,
		TransPrice:       summary.TransPrice,
		RefundPrice:      summary.RefundPrice,
		LogisticsFee:     summary.LogisticsFee,
		CouponAmount:     summary.CouponAmount,
		Remark:           summary.Remark,
		Status:           summary.Status,
		LogisticsNumber:  summary.LogisticsNumber,
		AddressIdentity:  summary.AddressIdentity,
		County:           summary.County,
		Province:         summary.Province,
		City:             summary.City,
		Area:             summary.Area,
		Address:          summary.Province + summary.City + summary.Area + summary.Address,
		Contact:          summary.Contact,
		Phone:            summary.Phone,
		DeliveryTime:     summary.DeliveryTime,
		DeliveryIdentity: summary.DeliveryIdentity,
		PayType:          int32(summary.PayType),
		PayAmount:        summary.PayAmount,
		PayTradeNo:       summary.PayTradeNo,
		PayTime:          summary.PayTime.Format(vars.YYYY_MM_DD_HH_MM_SS),
		PayRemark:        summary.PayRemark,
		Created:          summary.CreatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
		Updated:          summary.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
		Addr:             summary.Address,
		CarIdentity:      summary.CarIdentity,
		DeliveryAddress:  summary.DeliveryAddress,
		Brand:            summary.CarBrand,
		Version:          summary.CarVersion,
		LicenseNumber:    summary.LicenseNumber,
		MemberName:       summary.MemberName,
		MemberPhone:      summary.MemberPhone,
		Details:          make([]*order.OrderDetails, 0),
		Approve:          int32(summary.Approve),
	}
	if summary.OrderDetails != nil {
		item.Details = ReflectProtoOrderDetails(summary.OrderDetails)
	}
	if summary.PayTime.IsZero() {
		item.PayTime = ""
	}

	return &item
}

func ListModelToReply(list []*models.OrderSummary) []*pb.OrderSummaryItem {
	var data = make([]*order.OrderSummaryItem, 0)
	for _, summary := range list {
		data = append(data, ReflectProtoOrderSummary(summary))
	}
	return data
}
