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.

104 lines
2.3 KiB

8 months ago
package biz
8 months ago
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/transport/grpc"
"gorm.io/gorm"
"strconv"
"valuation/api/mapService"
)
type PriceRule struct {
gorm.Model
PriceRuleWork
}
type PriceRuleWork struct {
CityID uint `gorm:"" json:"city_id"`
StartFee int64 `gorm:"" json:"start_fee"`
DistanceFee int64 `gorm:"" json:"distance_fee"`
DurationFee int64 `gorm:"" json:"duration_fee"`
StartAt int `gorm:"type:int" json:"start_at"` // 0 [0
EndAt int `gorm:"type:int" json:"end_at"` // 7 0)
}
// 定义操作priceRule的接口
type PriceRuleInterface interface {
GetRule(cityid uint, curr int) (*PriceRule, error) // 获取规则
}
type ValuationBiz struct {
pri PriceRuleInterface
}
func NewValuationBiz(pri PriceRuleInterface) *ValuationBiz {
return &ValuationBiz{
pri: pri,
}
}
// 获取价格
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
}
fmt.Println("规则", rule)
// 二将距离和时长转换为int64
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
}
// 三基于rule计算
distancInt64 /= 1000
durationInt64 /= 60
var startDistance int64 = 5
total := rule.StartFee +
rule.DistanceFee*(distancInt64-startDistance) +
rule.DurationFee*durationInt64
return total, nil
}
// 获取距离和时长
func (*ValuationBiz) GetDrivingInfo(ctx context.Context, origin, destination string) (distance string, duration string, err error) {
// 一发出GRPC请求
// 使用服务发现
// 2.1,连接目标grpc服务器
conn, err := grpc.DialInsecure(
ctx,
grpc.WithEndpoint("localhost:9200"),
)
if err != nil {
return
}
//
defer func() {
_ = conn.Close()
}()
// 2.2,发送获取驾驶距离和时长请求RPC调用
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
}