You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.0 KiB
94 lines
2.0 KiB
package biz
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-kratos/kratos/contrib/registry/consul/v2"
|
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
|
"github.com/hashicorp/consul/api"
|
|
"gorm.io/gorm"
|
|
"strconv"
|
|
"valuation/api/mapService"
|
|
)
|
|
|
|
type PriceRuleInterface interface {
|
|
GetRule(cityid uint, curr int) (*PriceRule, error)
|
|
}
|
|
|
|
type PriceRule struct {
|
|
gorm.Model
|
|
PriceRuleWork
|
|
}
|
|
|
|
type PriceRuleWork struct {
|
|
CityID uint
|
|
StartFee int64
|
|
DistanceFee int64
|
|
DurationFee int64
|
|
StartAt int
|
|
EndAt int
|
|
}
|
|
|
|
type ValuationBiz struct {
|
|
pri PriceRuleInterface
|
|
}
|
|
|
|
func NewValuationBiz(pri PriceRuleInterface) *ValuationBiz {
|
|
return &ValuationBiz{
|
|
pri: pri,
|
|
}
|
|
}
|
|
|
|
func (*ValuationBiz) GetDrivingInfo(ctx context.Context, origin, destination string) (distance string, duration string, err error) {
|
|
consulConfig := api.DefaultConfig()
|
|
consulConfig.Address = "localhost:8500"
|
|
consulClient, err := api.NewClient(consulConfig)
|
|
if err != nil {
|
|
return
|
|
}
|
|
dis := consul.New(consulClient)
|
|
endpoint := "discovery:///Map"
|
|
conn, err := grpc.DialInsecure(
|
|
context.Background(),
|
|
grpc.WithEndpoint(endpoint),
|
|
grpc.WithDiscovery(dis),
|
|
)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer func() {
|
|
_ = conn.Close()
|
|
}()
|
|
client := mapService.NewMapServiceClient(conn)
|
|
reply, err := client.GetDrivingInfo(context.Background(), &mapService.GetDrivingInfoReq{
|
|
Origin: origin,
|
|
Destination: destination,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
distance, duration = reply.Distance, reply.Duration
|
|
return
|
|
}
|
|
|
|
func (vb *ValuationBiz) GetPrice(ctx context.Context, distance, duration string, cityId uint, curr int) (int64, error) {
|
|
rule, err := vb.pri.GetRule(cityId, curr)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
distancInt64, err := strconv.ParseInt(distance, 10, 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
durationInt64, err := strconv.ParseInt(duration, 10, 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
distancInt64 /= 1000
|
|
durationInt64 /= 60
|
|
var startDistance int64 = 5
|
|
total := rule.StartFee +
|
|
rule.DistanceFee*(distancInt64-startDistance) +
|
|
rule.DurationFee*durationInt64
|
|
return total, nil
|
|
}
|