+113
-5
@@ -1,19 +1,34 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cake_crm/internal/models/messenger"
|
||||
"cake_crm/internal/models/storage"
|
||||
proto "cake_crm/proto"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
proto.UnimplementedCRMServer
|
||||
storage storage.IStorage
|
||||
type orderItem struct {
|
||||
product *proto.Product
|
||||
count int64
|
||||
}
|
||||
|
||||
func NewServer(storage storage.IStorage) *Server {
|
||||
type Server struct {
|
||||
proto.UnsafeCRMServer
|
||||
storage storage.IStorage
|
||||
messenger messenger.IMessenger
|
||||
}
|
||||
|
||||
func NewServer(
|
||||
storage storage.IStorage,
|
||||
messenger messenger.IMessenger,
|
||||
) proto.CRMServer {
|
||||
return &Server{
|
||||
storage: storage,
|
||||
storage: storage,
|
||||
messenger: messenger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,3 +63,96 @@ func (s *Server) GetBreadcrumbs(ctx context.Context, req *proto.GetBreadcrumbsRe
|
||||
}
|
||||
return &proto.BreadcrumbsRsp{Categories: breadcrumbs}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Order(ctx context.Context, req *proto.OrderReq) (*proto.OrderRsp, error) {
|
||||
items := make([]*orderItem, 0, len(req.Order.Items))
|
||||
for _, item := range req.Order.Items {
|
||||
product, err := s.storage.GetProduct(ctx, item.ProductId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(
|
||||
items,
|
||||
&orderItem{
|
||||
product: product,
|
||||
count: item.Count,
|
||||
},
|
||||
)
|
||||
}
|
||||
text, err := createOrderText(req, items)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &proto.OrderRsp{}, s.messenger.SendMessage(text)
|
||||
}
|
||||
|
||||
func createOrderText(req *proto.OrderReq, items []*orderItem) (string, error) {
|
||||
buffer := bytes.Buffer{}
|
||||
orderAmount := 0.0
|
||||
buffer.WriteString(fmt.Sprintf("Заказ от:\n%s\n%s\n", req.Name, req.Phone))
|
||||
buffer.WriteString("\n")
|
||||
for _, item := range items {
|
||||
buffer.WriteString(item.product.Name)
|
||||
buffer.WriteString("\n")
|
||||
unit, err := unitToText(item.product.Unit)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
buffer.WriteString(fmt.Sprintf("Количество: %d%s\n", item.count, unit))
|
||||
amount, err := calcItemAmount(item)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
orderAmount += amount
|
||||
buffer.WriteString(fmt.Sprintf("Сумма: %.00fр\n", amount))
|
||||
buffer.WriteString("\n")
|
||||
}
|
||||
buffer.WriteString("\n")
|
||||
buffer.WriteString(fmt.Sprintf("ИТОГО: %.00fр\n", orderAmount))
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
func calcItemAmount(item *orderItem) (float64, error) {
|
||||
var variant *proto.Variant
|
||||
for _, v := range item.product.Variants {
|
||||
check := true
|
||||
for _, property := range v.Properties {
|
||||
if property.Name == "min" {
|
||||
minBorder, err := strconv.ParseInt(property.Value, 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if item.count < minBorder {
|
||||
check = false
|
||||
}
|
||||
}
|
||||
if property.Name == "max" {
|
||||
maxBorder, err := strconv.ParseInt(property.Value, 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if item.count > maxBorder {
|
||||
check = false
|
||||
}
|
||||
}
|
||||
if check {
|
||||
variant = v
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if variant == nil {
|
||||
return 0, errors.New("variant not found")
|
||||
}
|
||||
return float64(variant.Price) * float64(item.count) / 100, nil
|
||||
}
|
||||
|
||||
func unitToText(unit string) (string, error) {
|
||||
switch unit {
|
||||
case "kg":
|
||||
return "кг", nil
|
||||
case "piece":
|
||||
return "шт", nil
|
||||
}
|
||||
return "", errors.New("unit not found")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package messenger
|
||||
|
||||
type IMessenger interface {
|
||||
SendMessage(message string) error
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"cake_crm/internal/models/messenger"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type messengerTelegram struct {
|
||||
chatID int64
|
||||
bot *tgbotapi.BotAPI
|
||||
}
|
||||
|
||||
func NewMessenger(
|
||||
chatID int64,
|
||||
token string,
|
||||
) (messenger.IMessenger, error) {
|
||||
bot, err := tgbotapi.NewBotAPI(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &messengerTelegram{
|
||||
chatID: chatID,
|
||||
bot: bot,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *messengerTelegram) SendMessage(message string) error {
|
||||
_, err := m.bot.Send(tgbotapi.NewMessage(m.chatID, message))
|
||||
return err
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package server_web
|
||||
|
||||
import "context"
|
||||
|
||||
type IServer interface {
|
||||
Run(ctx context.Context) error
|
||||
Stop(ctx context.Context) error
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package server_web
|
||||
|
||||
import (
|
||||
"cake_crm/internal/models/storage"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-pkgz/routegroup"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
storage storage.IStorage
|
||||
port int
|
||||
}
|
||||
|
||||
func NewServer(
|
||||
storage storage.IStorage,
|
||||
port int,
|
||||
) IServer {
|
||||
return &server{
|
||||
storage: storage,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) Run(ctx context.Context) error {
|
||||
router := routegroup.New(http.NewServeMux())
|
||||
router.HandleFunc("GET /products", s.getAllProductsHandler)
|
||||
router.HandleFunc("GET /products/:id", s.getProductById)
|
||||
router.HandleFunc("GET /category/:id/breadcrumbs", s.getBreadcrumbsByCategoryId)
|
||||
return http.ListenAndServe(fmt.Sprintf(":%d", s.port), router)
|
||||
}
|
||||
|
||||
func (s *server) Stop(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (s *server) getAllProductsHandler(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
func (s *server) getProductById(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
func (s *server) getBreadcrumbsByCategoryId(w http.ResponseWriter, r *http.Request) {}
|
||||
Reference in New Issue
Block a user