zero prices methods
This commit is contained in:
parent
14bb2783c3
commit
e5b1b407de
2 changed files with 96 additions and 0 deletions
|
|
@ -42,6 +42,10 @@ type Prices interface {
|
|||
insertPrices(ctx context.Context, prices []Price) error
|
||||
getPricesWithDays(ctx context.Context, userId int64, days time.Time) ([]Price, error)
|
||||
getDistinctPrices(ctx context.Context, userId int64, merchUuid string, days time.Time) ([]Price, error)
|
||||
|
||||
getZeroPrices(ctx context.Context, userId int64) ([]ZeroPrice, error)
|
||||
deleteZeroPricesPeriod(ctx context.Context, userId int64, start, end time.Time, now sql.NullTime) error
|
||||
deleteZeroPrices(ctx context.Context, now sql.NullTime, list []int64) error
|
||||
}
|
||||
|
||||
type Tasks interface {
|
||||
|
|
@ -469,3 +473,82 @@ func (r *repo) getDistinctPrices(ctx context.Context, userId int64, merchUuid st
|
|||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *repo) getZeroPrices(ctx context.Context, userId int64) ([]ZeroPrice, error) {
|
||||
q := `
|
||||
WITH price_with_neighbors AS (
|
||||
SELECT
|
||||
p.id, p.created_at, p.merch_id, p.price, p.origin_id, m.name,
|
||||
LAG(price) OVER (PARTITION BY p.merch_id, p.origin_id ORDER BY p.created_at, p.id) AS prev_price,
|
||||
LEAD(price) OVER (PARTITION BY p.merch_id, p.origin_id ORDER BY p.created_at, p.id) AS next_price
|
||||
FROM merch_prices AS p
|
||||
JOIN merch as m ON m.id = p.merch_id
|
||||
WHERE p.deleted_at IS NULL
|
||||
AND m.deleted_at IS NULL
|
||||
AND m.user_id = 3)
|
||||
|
||||
SELECT
|
||||
pwn.id, pwn.created_at, pwn.merch_uuid, pwn.name, mo.name
|
||||
FROM price_with_neighbors AS pwn
|
||||
JOIN merch_origins AS mo ON mo.id = pwn.origin_id
|
||||
WHERE
|
||||
pwn.price = 0
|
||||
AND pwn.prev_price IS NOT NULL
|
||||
AND pwn.prev_price > 0
|
||||
AND pwn.next_price IS NOT NULL
|
||||
AND pwn.next_price > 0
|
||||
ORDER BY pwn.created_at DESC
|
||||
`
|
||||
|
||||
rows, err := r.db.Query(ctx, q, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var zeroPrices []ZeroPrice
|
||||
for rows.Next() {
|
||||
var p ZeroPrice
|
||||
if err = rows.Scan(&p.Id, &p.CreatedAt, &p.MerchUuid, &p.Name, &p.Origin); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
zeroPrices = append(zeroPrices, p)
|
||||
}
|
||||
|
||||
rows.Close()
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return zeroPrices, nil
|
||||
}
|
||||
|
||||
func (r *repo) deleteZeroPricesPeriod(ctx context.Context, userId int64, start, end time.Time, now sql.NullTime) error {
|
||||
q := `
|
||||
UPDATE merch_prices
|
||||
SET deleted_at = $1
|
||||
FROM merch
|
||||
WHERE merch_prices.merch_id = merch.id
|
||||
AND merch.user_id = $2
|
||||
AND merch_prices.price = 0
|
||||
AND merch_prices.deleted_at IS NULL
|
||||
AND merch_prices.created_at BETWEEN $3 AND $4;
|
||||
`
|
||||
|
||||
_, err := r.db.Exec(ctx, q, now, userId, start, end)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) deleteZeroPrices(ctx context.Context, now sql.NullTime, list []int64) error {
|
||||
q := `UPDATE merch_prices SET deleted_at = $1 WHERE id IN $2`
|
||||
|
||||
_, err := r.db.Exec(ctx, q, now, list)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue