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.

147 lines
4.5 KiB

8 months ago
package car
import (
"base/app/logic/broker/user/wallet"
"base/app/logic/car"
"base/app/model"
"encoding/json"
"errors"
"git.oa00.com/go/mysql"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"time"
)
var PriceLogic = &priceLogic{}
type priceLogic struct {
}
type FastEstimateData struct {
ModelId uint `binding:"required" label:"车型"`
CityId uint `json:"cityId"`
LicensePlate string `label:"车牌"`
Color string
//`binding:"required" label:"车身颜色"`
LicensingDate string `binding:"required" label:"上牌日期"`
Mileage decimal.Decimal `binding:"required" label:"里程"`
}
type fastEstimateItem struct {
Price decimal.Decimal `json:"price"`
}
// FastEstimate @Title 快速评估价格
func (p *priceLogic) FastEstimate(brokerId uint, data FastEstimateData) (result fastEstimateItem, err error) {
licensingDate, err := time.ParseInLocation("2006-01-02", data.LicensingDate, time.Local)
if err != nil {
return result, errors.New("上牌日期错误")
}
fastPrice := model.BrokerCarFastPrice{
BrokerId: brokerId,
ModelId: data.ModelId,
Color: data.Color,
LicensingDate: licensingDate,
Mileage: data.Mileage,
CityId: data.CityId,
LicensePlate: data.LicensePlate,
}
if err = mysql.Db.Transaction(func(tx *gorm.DB) error {
if tx.Create(&fastPrice).Error != nil {
return errors.New("估价失败")
}
if err := wallet.AssessLogic.Use(tx, model.WalletAssessKindEstimate, model.WalletAssessHistoryKindEstimate, brokerId, fastPrice.Id, 1); err != nil {
return err
}
priceModel, err := car.PriceLogic.Price(model.CarPricePriceTypeBroker, brokerId, data.ModelId, data.CityId, data.LicensePlate, data.LicensingDate, data.Mileage)
if err != nil {
return err
}
fastPrice.PriceId = priceModel.Id
fastPrice.Price = priceModel.GoodNormalPrice
result.Price = priceModel.GoodNormalPrice
if tx.Updates(&fastPrice).Error != nil {
return errors.New("估价失败")
}
return nil
}); err != nil {
return
}
return
}
type PriceEstimateData struct {
car.EstimateData
Vin string `binding:"required" label:"车架号"`
EngineNo string `binding:"required" label:"发动机号"`
Source uint `binding:"required" label:"车辆来源"`
ShamMileage uint `label:"虚假里程"`
}
type resPriceEstimate struct {
Id uint `json:"id"`
Price decimal.Decimal `json:"price"`
FinalPrice decimal.Decimal `json:"finalPrice"`
}
// Estimate @Title 估算价格
func (p *priceLogic) Estimate(brokerId uint, data PriceEstimateData) (result resPriceEstimate, err error) {
licensingDate, err := time.ParseInLocation("2006-01-02", data.LicensingDate, time.Local)
if err != nil {
return result, errors.New("上牌日期错误")
}
carPrice := model.BrokerCarPrice{
BrokerId: brokerId,
ModelId: data.ModelId,
Color: data.Color,
LicensingDate: licensingDate,
Mileage: data.Mileage,
Transfer: data.Transfer,
Paint: data.Paint,
Plate: data.Plate,
EngineStatus: data.Engine,
Maintain: data.Maintain,
Vin: data.Vin,
LicensePlate: data.LicensePlate,
CityId: data.CityId,
Engine: data.EngineNo,
Source: data.Source,
//ShamMileage: data.ShamMileage,
}
parts, _ := json.Marshal(data.Parts)
carPrice.Parts = string(parts)
accidents, _ := json.Marshal(data.Accidents)
carPrice.Accidents = string(accidents)
if err = mysql.Db.Transaction(func(tx *gorm.DB) error {
if tx.Create(&carPrice).Error != nil {
return errors.New("估价失败")
}
if err = wallet.AssessLogic.Use(tx, model.WalletAssessKindEstimate, model.WalletAssessHistoryKindEstimate, brokerId, carPrice.Id, 1); err != nil {
return err
}
priceModel, err := car.PriceLogic.Price(model.CarPricePriceTypeBroker, brokerId, data.ModelId, data.CityId, data.LicensePlate, data.LicensingDate, data.Mileage)
if err != nil {
return err
}
carPrice.PriceId = priceModel.Id
score, price, discount, _ := car.PriceLogic.Estimate(priceModel, data.EstimateData)
// 云云问车自定义价格方案
strategy := car.PriceLogic.PriceStrategy(price)
price = strategy.Price
carPrice.FinalPrice = strategy.FinalPrice
result.FinalPrice = strategy.FinalPrice
carPrice.Score = decimal.NewFromFloat(score)
carPrice.Discount = discount
carPrice.Price = price
result.Id = carPrice.Id
result.Price = price
if tx.Updates(&carPrice).Error != nil {
return errors.New("估价失败")
}
return nil
}); err != nil {
return result, err
}
return
}