126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
|
|
package merch
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (s *service) getPrices(ctx context.Context, userId int64, days int) ([]PricesResponse, error) {
|
||
|
|
fmt.Println("Enter service")
|
||
|
|
merchList, err := s.repo.getAllUserMerch(ctx, userId)
|
||
|
|
if err != nil {
|
||
|
|
logErr(serviceLogHeader, err)
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(merchList) == 0 {
|
||
|
|
errMsg := errors.New("no merch found")
|
||
|
|
logErr(serviceLogHeader, errMsg)
|
||
|
|
return nil, errMsg
|
||
|
|
}
|
||
|
|
|
||
|
|
merchMap := make(map[int64]Merch, len(merchList))
|
||
|
|
for _, merch := range merchList {
|
||
|
|
merchMap[merch.Id] = merch
|
||
|
|
}
|
||
|
|
|
||
|
|
var response []PricesResponse
|
||
|
|
for _, item := range merchList {
|
||
|
|
response = append(response, PricesResponse{
|
||
|
|
MerchUuid: item.MerchUuid,
|
||
|
|
Name: item.Name,
|
||
|
|
Origins: []OriginWithPrices{},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
pricesList, err := s.repo.getPricesWithDays(ctx, userId, getPeriod(days))
|
||
|
|
if err != nil {
|
||
|
|
logErr(serviceLogHeader, err)
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
_, originNamesMap, err := s.getOriginsMaps(ctx)
|
||
|
|
if err != nil {
|
||
|
|
logErr(serviceLogHeader, err)
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
pricesMap := make(map[string]map[string][]PriceEntry)
|
||
|
|
for _, item := range pricesList {
|
||
|
|
merchUuid := merchMap[item.MerchId].MerchUuid
|
||
|
|
|
||
|
|
if _, ok := pricesMap[merchUuid]; !ok {
|
||
|
|
pricesMap[merchUuid] = make(map[string][]PriceEntry)
|
||
|
|
}
|
||
|
|
|
||
|
|
originName := originNamesMap[item.OriginId]
|
||
|
|
pricesMap[merchUuid][originName] = append(pricesMap[merchUuid][originName], PriceEntry{
|
||
|
|
CreatedAt: item.CreatedAt.Unix(),
|
||
|
|
Value: item.Price,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
for i := range response {
|
||
|
|
for _, name := range originNamesMap {
|
||
|
|
prices := pricesMap[response[i].MerchUuid][name]
|
||
|
|
if prices == nil {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
response[i].Origins = append(response[i].Origins, OriginWithPrices{
|
||
|
|
Origin: name,
|
||
|
|
Prices: prices,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return response, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *service) getDistinctPrices(ctx context.Context, userId int64, merchUuid string, days int) (*PricesResponse, error) {
|
||
|
|
result, err := s.repo.getDistinctPrices(ctx, userId, merchUuid, getPeriod(days))
|
||
|
|
if err != nil {
|
||
|
|
logErr(serviceLogHeader, err)
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if result == nil {
|
||
|
|
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
_, originNamesMap, err := s.getOriginsMaps(ctx)
|
||
|
|
if err != nil {
|
||
|
|
logErr(serviceLogHeader, err)
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
response := PricesResponse{
|
||
|
|
MerchUuid: merchUuid,
|
||
|
|
Origins: []OriginWithPrices{},
|
||
|
|
}
|
||
|
|
|
||
|
|
pricesMap := make(map[string][]PriceEntry)
|
||
|
|
for _, item := range result {
|
||
|
|
originName := originNamesMap[item.OriginId]
|
||
|
|
if _, ok := pricesMap[originName]; !ok {
|
||
|
|
pricesMap[originName] = make([]PriceEntry, 0)
|
||
|
|
}
|
||
|
|
|
||
|
|
pricesMap[originName] = append(pricesMap[originName], PriceEntry{
|
||
|
|
CreatedAt: item.CreatedAt.Unix(),
|
||
|
|
Value: item.Price,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
for key, item := range pricesMap {
|
||
|
|
response.Origins = append(response.Origins, OriginWithPrices{
|
||
|
|
Origin: key,
|
||
|
|
Prices: item,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
return &response, nil
|
||
|
|
}
|