get tasks + insert result
This commit is contained in:
parent
47700dfd14
commit
718101ee67
6 changed files with 268 additions and 8 deletions
|
|
@ -18,6 +18,7 @@ type Repository interface {
|
|||
getMany(ctx context.Context, userId int64) ([]merchDTO, error)
|
||||
|
||||
getMerchIdByUuid(ctx context.Context, userId int64, uuid string) (int64, error)
|
||||
getMerchUuidMap(ctx context.Context, merchUuids []string) (map[string]int64, error)
|
||||
|
||||
updateMerch(ctx context.Context, userId int64, merch *updateMerchDTO) (*merchDTO, error)
|
||||
updateExtraData(ctx context.Context, merchId int64, insertData []ExtraData) ([]ExtraData, error)
|
||||
|
|
@ -26,6 +27,8 @@ type Repository interface {
|
|||
deleteOneMerchRecord(ctx context.Context, userId int64, merchUuid string, delTime time.Time) error
|
||||
|
||||
Origins
|
||||
Prices
|
||||
Tasks
|
||||
}
|
||||
|
||||
type Origins interface {
|
||||
|
|
@ -34,6 +37,14 @@ type Origins interface {
|
|||
deleteOriginByName(ctx context.Context, name string, deletedAt sql.NullTime) error
|
||||
}
|
||||
|
||||
type Prices interface {
|
||||
insertPrices(ctx context.Context, prices []Price) error
|
||||
}
|
||||
|
||||
type Tasks interface {
|
||||
getTaskData(ctx context.Context) ([]taskData, error)
|
||||
}
|
||||
|
||||
type repo struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
|
@ -179,6 +190,35 @@ func (r *repo) getMerchIdByUuid(ctx context.Context, userId int64, uuid string)
|
|||
return merchId, nil
|
||||
}
|
||||
|
||||
func (r *repo) getMerchUuidMap(ctx context.Context, merchUuids []string) (map[string]int64, error) {
|
||||
q := `SELECT merch_uuid, id FROM merch WHERE deleted_at IS NULL AND merch_uuid = ANY($1)`
|
||||
|
||||
rows, err := r.db.Query(ctx, q, merchUuids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var merchUuidMap map[string]int64
|
||||
for rows.Next() {
|
||||
var (
|
||||
uuid string
|
||||
id int64
|
||||
)
|
||||
if err = rows.Scan(&uuid, &id); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
merchUuidMap[uuid] = id
|
||||
}
|
||||
|
||||
rows.Close()
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return merchUuidMap, 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 {
|
||||
|
|
@ -263,3 +303,65 @@ func (r *repo) updateExtraData(ctx context.Context, merchId int64, insertData []
|
|||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *repo) insertPrices(ctx context.Context, prices []Price) error {
|
||||
q := `
|
||||
INSERT INTO merch_prices (created_at, updated_at, merch_id, origin_id, price)
|
||||
SELECT $1, $2, src.merch_id, src.origin_id, src.price
|
||||
FROM UNNEST(
|
||||
$3::bigint[],
|
||||
$4::bigint[],
|
||||
$5::int[]
|
||||
) AS src (merch_id, origin_id, price)
|
||||
`
|
||||
|
||||
var (
|
||||
merchIds []int64
|
||||
originIds []int64
|
||||
priceValues []int
|
||||
)
|
||||
|
||||
for _, price := range prices {
|
||||
merchIds = append(merchIds, price.MerchId)
|
||||
originIds = append(originIds, price.OriginId)
|
||||
priceValues = append(priceValues, price.Price)
|
||||
}
|
||||
|
||||
_, err := r.db.Exec(ctx, q, merchIds, originIds, priceValues)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) getTaskData(ctx context.Context) ([]taskData, error) {
|
||||
q := `SELECT m.merch_uuid, med.url, mo.name
|
||||
FROM merch AS m
|
||||
JOIN merch_extra_data AS med ON m.id = med.merch_id
|
||||
JOIN merch_origins AS mo ON mo.id = med.origin_id
|
||||
WHERE m.deleted_at IS NULL
|
||||
AND med.deleted_at IS NULL
|
||||
AND mo.deleted_at IS NULL
|
||||
`
|
||||
rows, err := r.db.Query(ctx, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []taskData
|
||||
for rows.Next() {
|
||||
var t taskData
|
||||
if err = rows.Scan(&t.MerchUuid, &t.Url, &t.OriginName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, t)
|
||||
}
|
||||
|
||||
rows.Close()
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue