2024-03-28 18:20:57 +03:00
package storage
import (
2024-05-03 11:57:53 +03:00
"bytes"
2024-03-28 18:20:57 +03:00
"context"
2024-05-03 11:57:53 +03:00
"encoding/json"
"github.com/uptrace/bun"
"os/exec"
"relynolli-server/external/bitrix"
2024-03-28 18:20:57 +03:00
"relynolli-server/internal"
2024-05-03 11:57:53 +03:00
"relynolli-server/models/cart"
"relynolli-server/models/discount"
"relynolli-server/models/order"
2024-03-28 18:20:57 +03:00
)
type StorageOrder interface {
2024-05-03 11:57:53 +03:00
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 )
2024-03-28 18:20:57 +03:00
}
func NewStorageOrder ( ) StorageOrder {
if instance == nil {
instance = & storage {
db : internal . InitDatabase ( ) . GetInstance ( ) ,
rdb : internal . InitRedis ( ) ,
}
}
return instance
}
2024-05-03 11:57:53 +03:00
type DiscountQuery struct {
Name string
Value int64
}
type QueryItem struct {
Cart * cart . DBCart ` json:"cart" `
Discount * DiscountQuery ` json:"discount" `
}
2024-03-28 18:20:57 +03:00
type TotalQuery struct {
2024-05-03 11:57:53 +03:00
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
2024-03-28 18:20:57 +03:00
}
2024-05-03 11:57:53 +03:00
func ( s * storage ) GetTotal ( ctx context . Context , fuserId int64 , coupon * string ) ( * TotalQuery , error ) {
2024-03-28 18:20:57 +03:00
model := new ( TotalQuery )
2024-05-03 11:57:53 +03:00
api := bitrix . Initialize ( )
data , err := api . GetTotalForProduct ( int ( fuserId ) , coupon )
2024-03-28 18:20:57 +03:00
if err != nil {
return nil , err
}
2024-05-03 11:57:53 +03:00
model . Total = data . Price
model . BasePrice = data . BasePrice
2024-03-28 18:20:57 +03:00
return model , nil
}
2024-05-03 11:57:53 +03:00
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
}