2024-05-26 20:21:47 +00:00
|
|
|
|
package order
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"cake_crm/proto"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Service struct{}
|
|
|
|
|
|
|
|
|
|
func NewService() *Service {
|
|
|
|
|
return &Service{}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 20:54:12 +00:00
|
|
|
|
func (s *Service) CreateOrderText(req *proto.OrderReq, cart *proto.CartRsp) (string, error) {
|
2024-05-26 20:21:47 +00:00
|
|
|
|
buffer := bytes.Buffer{}
|
|
|
|
|
buffer.WriteString(fmt.Sprintf("Заказ от:\n%s\n%s\n", req.Order.Name, req.Order.Phone))
|
|
|
|
|
buffer.WriteString("\n")
|
2024-05-29 20:54:12 +00:00
|
|
|
|
for _, item := range cart.Items {
|
2024-05-26 20:21:47 +00:00
|
|
|
|
buffer.WriteString(item.Name)
|
|
|
|
|
buffer.WriteString("\n")
|
|
|
|
|
unit, err := unitToText(item.Unit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
buffer.WriteString(fmt.Sprintf("Количество: %d%s\n", item.Count, unit))
|
|
|
|
|
buffer.WriteString(fmt.Sprintf("Сумма: %.00fр\n", float64(item.Amount)/100))
|
|
|
|
|
buffer.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
buffer.WriteString("\n")
|
2024-05-29 20:54:12 +00:00
|
|
|
|
buffer.WriteString(fmt.Sprintf("ИТОГО: %.00fр\n", float64(cart.Amount)/100))
|
2024-05-26 20:21:47 +00:00
|
|
|
|
return buffer.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unitToText(unit string) (string, error) {
|
|
|
|
|
switch unit {
|
|
|
|
|
case "kg":
|
|
|
|
|
return "кг", nil
|
|
|
|
|
case "piece":
|
|
|
|
|
return "шт", nil
|
|
|
|
|
}
|
|
|
|
|
return "", errors.New("unit not found")
|
|
|
|
|
}
|