relynolli-server/storage/order.go

137 lines
3.4 KiB
Go

package storage
import (
"bytes"
"context"
"encoding/json"
"github.com/uptrace/bun"
"os/exec"
"relynolli-server/external/bitrix"
"relynolli-server/internal"
"relynolli-server/models/cart"
"relynolli-server/models/discount"
"relynolli-server/models/order"
)
type StorageOrder interface {
GetTotal(ctx context.Context, fuserId int64, coupon *string) (*TotalQuery, error)
UpdatePayment(ctx context.Context, payment *order.DBPayment) error
GetPayments(ctx context.Context) (*[]order.DBPayment, error)
}
func NewStorageOrder() StorageOrder {
if instance == nil {
instance = &storage{
db: internal.InitDatabase().GetInstance(),
rdb: internal.InitRedis(),
}
}
return instance
}
type DiscountQuery struct {
Name string
Value int64
}
type QueryItem struct {
Cart *cart.DBCart `json:"cart"`
Discount *DiscountQuery `json:"discount"`
}
type TotalQuery struct {
Total float64 `bun:"total" json:"total"`
BasePrice float64 `bun:"-" json:"basePrice"`
Items *[]QueryItem `bun:"-" json:"items"`
}
func (s *storage) fetchDiscounts(ctx context.Context) (*[]discount.DBDiscount, error) {
model := new([]discount.DBDiscount)
err := s.db.NewSelect().Model(model).Scan(ctx)
if err != nil {
return nil, err
}
return model, nil
}
type DiscountType struct {
CLASSID string `json:"CLASS_ID"`
DATA struct {
All string `json:"All"`
} `json:"DATA"`
CHILDREN []struct {
CLASSID string `json:"CLASS_ID"`
DATA struct {
Type string `json:"Type"`
Value int `json:"Value"`
Unit string `json:"Unit"`
Max int `json:"Max"`
All string `json:"All"`
True string `json:"True"`
} `json:"DATA"`
CHILDREN []interface{} `json:"CHILDREN"`
} `json:"CHILDREN"`
}
func (s *storage) getDiscountForCoupon(ctx context.Context, coupon string) (*DiscountType, error) {
var couponDiscountPhpQuery string
stmt := "select disc.ACTIONS from b_sale_discount_coupon coupon join b_sale_discount disc on coupon.DISCOUNT_ID = disc.ID where coupon.COUPON = ? and coupon.ACTIVE = 'Y'"
err := s.db.NewRaw(stmt, coupon).Scan(ctx, &couponDiscountPhpQuery)
if err != nil {
return nil, err
}
var out bytes.Buffer
var result DiscountType
cmd := exec.Command("php", "test.php", couponDiscountPhpQuery)
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return nil, err
}
err = json.Unmarshal(out.Bytes(), &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *storage) GetTotal(ctx context.Context, fuserId int64, coupon *string) (*TotalQuery, error) {
model := new(TotalQuery)
api := bitrix.Initialize()
data, err := api.GetTotalForProduct(int(fuserId), coupon)
if err != nil {
return nil, err
}
model.Total = data.Price
model.BasePrice = data.BasePrice
return model, nil
}
func (s *storage) GetPayments(ctx context.Context) (*[]order.DBPayment, error) {
var model []order.DBPayment
err := s.db.NewSelect().Model(&model).Where("status not in (?)", bun.In([]string{"succeeded", "canceled"})).Order("order_id DESC").Scan(ctx)
if err != nil {
return nil, err
}
for idx, item := range model {
bitrixPayment := new(order.DBOrderPayment)
s.db.NewSelect().Model(bitrixPayment).Where("ORDER_ID = ?", item.OrderId).Scan(ctx)
model[idx].BitrixPayment = bitrixPayment
}
return &model, nil
}
func (s *storage) UpdatePayment(ctx context.Context, payment *order.DBPayment) error {
_, err := s.db.NewUpdate().Model(payment).WherePK().Exec(ctx)
return err
}