get prices methods

This commit is contained in:
nquidox 2026-03-10 23:44:24 +03:00
parent c772f0a4d2
commit 9e97f082fa
4 changed files with 324 additions and 14 deletions

View file

@ -19,6 +19,7 @@ type Repository interface {
getMerchIdByUuid(ctx context.Context, userId int64, uuid string) (int64, error)
getMerchUuidMap(ctx context.Context, merchUuids []string) (map[string]int64, error)
getAllUserMerch(ctx context.Context, userId int64) ([]Merch, error)
updateMerch(ctx context.Context, userId int64, merch *updateMerchDTO) (*merchDTO, error)
updateExtraData(ctx context.Context, merchId int64, insertData []ExtraData) ([]ExtraData, error)
@ -39,6 +40,8 @@ type Origins interface {
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)
}
type Tasks interface {
@ -219,6 +222,32 @@ func (r *repo) getMerchUuidMap(ctx context.Context, merchUuids []string) (map[st
return merchUuidMap, nil
}
func (r *repo) getAllUserMerch(ctx context.Context, userId int64) ([]Merch, error) {
var userMerch []Merch
q := `SELECT id, merch_uuid, name FROM merch WHERE user_id = $1 AND deleted_at IS NULL`
rows, err := r.db.Query(ctx, q, userId)
if err != nil {
return nil, err
}
for rows.Next() {
var m Merch
if err = rows.Scan(&m.Id, &m.MerchUuid, &m.Name); err != nil {
rows.Close()
return nil, err
}
userMerch = append(userMerch, m)
}
rows.Close()
if err = rows.Err(); err != nil {
return nil, err
}
return userMerch, nil
}
func (r *repo) deleteOneMerchRecord(ctx context.Context, userId int64, merchUuid string, delTime time.Time) error {
tx, err := r.db.Begin(ctx)
if err != nil {
@ -365,3 +394,78 @@ func (r *repo) getTaskData(ctx context.Context) ([]taskData, error) {
return result, nil
}
func (r *repo) getPricesWithDays(ctx context.Context, userId int64, days time.Time) ([]Price, error) {
q := `
SELECT mp.created_at, mp.merch_id, mp.price, mp.origin_id
FROM merch_prices AS mp
JOIN merch AS m ON m.id = mp.merch_id
WHERE m.user_id = $1
AND mp.created_at > $2
AND mp.deleted_at IS NULL
AND m.deleted_at IS NULL
`
rows, err := r.db.Query(ctx, q, userId, days)
if err != nil {
return nil, err
}
var result []Price
for rows.Next() {
var p Price
if err = rows.Scan(&p.CreatedAt, &p.MerchId, &p.Price, &p.OriginId); err != nil {
rows.Close()
return nil, err
}
result = append(result, p)
}
rows.Close()
if err = rows.Err(); err != nil {
return nil, err
}
return result, nil
}
func (r *repo) getDistinctPrices(ctx context.Context, userId int64, merchUuid string, days time.Time) ([]Price, error) {
q := `
SELECT price, created_at, origin_id
FROM (
SELECT DISTINCT ON (price) price, created_at, origin_id
FROM merch_prices
WHERE merch_id = (
SELECT id
FROM merch
WHERE merch_uuid = $1
AND user_id = $2
AND deleted_at IS NULL
)
AND deleted_at IS NULL
AND created_at > $3
)
ORDER BY created_at;
`
fmt.Println(merchUuid, userId, days)
rows, err := r.db.Query(ctx, q, merchUuid, userId, days)
if err != nil {
return nil, err
}
var result []Price
for rows.Next() {
var p Price
if err = rows.Scan(&p.Price, &p.CreatedAt, &p.OriginId); err != nil {
rows.Close()
return nil, err
}
result = append(result, p)
}
rows.Close()
if err = rows.Err(); err != nil {
return nil, err
}
return result, nil
}