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.

256 lines
8.6 KiB

package order
import (
"base/app/common"
"base/app/constant"
"base/app/lib/bean"
"base/app/logic/broker/message"
"base/app/model"
"errors"
"git.oa00.com/go/mysql"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"time"
)
var CallCarLogic = &callCarLogic{}
type callCarLogic struct {
}
type callCarItem struct {
Id uint `json:"id"`
OrderSn string `json:"orderSn"`
MainPhoto string `json:"mainPhoto"`
ModelName string `json:"modelName"`
LicensingDate int64 `json:"licensingDate"`
Mileage decimal.Decimal `json:"mileage"`
Transfer uint `json:"transfer"`
Amount decimal.Decimal `json:"amount"`
Status uint `json:"status"`
}
type CallCarSearch struct {
Status uint
}
// Lists @Title 叫车订单列表
func (c *callCarLogic) Lists(brokerId uint, search CallCarSearch, page bean.Page) (lists []callCarItem, total int64) {
lists = []callCarItem{}
var callCars []model.OrderCallCar
//where := mysql.Db.Where("broker_id = ? or car_broker_id = ?", brokerId, brokerId)
where := mysql.Db.Where("broker_id = ?", brokerId)
if search.Status > 0 {
switch search.Status {
case 1: // 待支付
where = where.Where("status = ?", model.OrderCallCarStatusUnPay)
case 2: // 待交车
where = where.Where("status = ?", model.OrderCallCarStatusPay)
case 3: // 已交车
where = where.Where("status = ?", model.OrderCallCarStatusFinal)
case 4: // 申请退款
where = where.Where("status = ?", model.OrderCallCarStatusRefundAudit)
case 5: // 已退款
where = where.Where("status = ?", model.OrderCallCarStatusRefund)
case 6: // 已驳回
where = where.Where("status = ?", model.OrderCallCarStatusRefundReject)
}
}
mysql.Db.Model(&callCars).Where(where).Count(&total)
if page.HasPage(total) {
mysql.Db.Preload("Car.Model").
Where(where).Offset(page.GetStart()).Limit(page.GetLimit()).Order("id desc").Find(&callCars)
for _, callCar := range callCars {
lists = append(lists, callCarItem{
Id: callCar.Id,
OrderSn: callCar.OrderSn,
MainPhoto: callCar.Car.MainPhoto,
ModelName: callCar.Car.Model.Name,
LicensingDate: callCar.Car.LicensingDate.Time.Unix(),
Mileage: callCar.Car.Mileage,
Transfer: callCar.Car.Transfer,
Amount: callCar.Amount,
Status: callCar.Status,
})
}
}
return
}
// Count @Title 车商寄卖统计
func (c *callCarLogic) Count(brokerIds []uint) (total int64) {
mysql.Db.Model(&model.OrderCallCar{}).Where("broker_id in ? or car_broker_id = ?", brokerIds, brokerIds).Count(&total)
return
}
type CallCarAdd struct {
CarId uint `binding:"required" label:"车辆"` // 车辆id
CustomerId uint `binding:"required" label:"客户"` // 客户id
Phone string `binding:"required" label:"手机号"` // 手机号
ReserveTime string `binding:"required" label:"预定时间"` // 预定时间
Address string `binding:"required" label:"预定地址"` // 预定地址
Remark string // 备注
}
// Add @Title 添加叫车订单
func (c *callCarLogic) Add(brokerId uint, data CallCarAdd) error {
reserveAt, err := time.ParseInLocation("2006-01-02 15:04:05", data.ReserveTime, time.Local)
if err != nil {
return errors.New("预定时间错误")
}
car := model.Car{Id: data.CarId}
if mysql.Db.First(&car).Error != nil {
return errors.New("车辆错误")
}
orderSn, err := common.GenNo(constant.NoPrefixOrderCallCar)
if err != nil {
return errors.New("提交失败")
}
callCar := model.OrderCallCar{
OrderSn: orderSn,
CarId: data.CarId,
CustomerId: data.CustomerId,
BrokerId: brokerId,
CarBrokerId: car.BrokerId,
Phone: data.Phone,
ReserveAt: reserveAt,
Address: data.Address,
Remark: data.Remark,
Amount: decimal.NewFromInt(100),
Status: model.OrderCallCarStatusUnPay,
}
return mysql.Db.Transaction(func(tx *gorm.DB) error {
if tx.Create(&callCar).Error != nil {
return errors.New("提交失败")
}
message.MessageLogic.MessageToCustomer(tx, []uint{data.CustomerId}, brokerId, model.CustomerMessageTypeContract, message.MessageItem{
Title: "订单提示",
Content: "您有一份叫车订单等待支付"})
return nil
})
}
type callCarInfo struct {
Id uint `json:"id"`
ModelName string `json:"modelName"`
MainPhoto string `json:"mainPhoto"`
LicensingDate int64 `json:"licensingDate"`
Mileage decimal.Decimal `json:"mileage"`
Transfer uint `json:"transfer"`
Price decimal.Decimal `json:"carPrice"`
CreatedAt int64 `json:"createdAt"`
Status uint `json:"status"`
ReserveAt int64 `json:"reserveAt"` // 预定时间
Address string `json:"address"` // 预定地址
Remark string `json:"remark"` // 备注
Amount decimal.Decimal `json:"amount"` // 叫车费用
CustomerInfo CustomerInfo `json:"customerInfo"`
BrokerInfo BrokerInfo `json:"brokerInfo"`
PayInfo CallCarPay `json:"payInfo"`
RefundInfo CallCarRefund `json:"refundInfo"`
}
type CustomerInfo struct {
Name string `json:"name"`
Phone string `json:"phone"` // 手机号
}
type BrokerInfo struct {
Name string `json:"name"`
Phone string `json:"phone"` // 手机号
}
type CallCarPay struct {
Id uint `json:"id"`
PayType uint `json:"payType"`
Amount decimal.Decimal `json:"amount"`
PayTime int64 `json:"payTime"`
}
type CallCarRefund struct {
Id uint `json:"id"`
CreatedAt int64 `json:"createdAt"`
Reason string `json:"reason"` // 退款原因
Proof string `json:"proof"` // 退款凭证
Status uint `json:"status"` // 1=未审核 2=审核通过 3=审核驳回
AuditAt int64 `json:"auditAt"` // 审核时间
RejectReason string `json:"rejectReason"` // 驳回原因
}
// Info @Title 详情
func (c *callCarLogic) Info(brokerId, orderId uint) (result callCarInfo, err error) {
callCar := model.OrderCallCar{Id: orderId}
//if mysql.Db.Where("broker_id = ? or car_broker_id = ?", brokerId, brokerId).First(&callCar).Error != nil {
// return result, errors.New("订单错误")
//}
if mysql.Db.Preload("Car.Model").Where("broker_id = ?", brokerId).First(&callCar).Error != nil {
return result, errors.New("订单错误")
}
result = callCarInfo{
Id: callCar.Id,
ModelName: callCar.Car.Model.Name,
MainPhoto: callCar.Car.MainPhoto,
LicensingDate: callCar.Car.LicensingDate.Time.Unix(),
Mileage: callCar.Car.Mileage,
Transfer: callCar.Car.Transfer,
Price: callCar.Car.Price,
CreatedAt: callCar.Car.CreatedAt.Unix(),
Status: callCar.Status,
Amount: callCar.Amount,
ReserveAt: callCar.ReserveAt.Unix(),
Address: callCar.Address,
Remark: callCar.Remark,
CustomerInfo: CustomerInfo{
Name: callCar.Customer.Nickname,
Phone: callCar.Phone,
},
BrokerInfo: BrokerInfo{
Name: callCar.Broker.Nickname,
Phone: callCar.Broker.Phone,
},
}
if callCar.PayId > 0 {
payModel := model.Pay{Id: callCar.PayId}
mysql.Db.Where(&payModel).First(&payModel)
result.PayInfo = CallCarPay{
Id: callCar.PayId,
Amount: payModel.Amount,
PayType: payModel.PayType,
PayTime: payModel.PayTime.Time.Unix(),
}
}
if callCar.RefundId > 0 {
callCarRefund := model.OrderCallCarRefund{Id: callCar.RefundId}
mysql.Db.Where(&callCarRefund).First(&callCarRefund)
result.RefundInfo = CallCarRefund{
Id: callCarRefund.Id,
CreatedAt: callCarRefund.CreatedAt.Unix(),
Reason: callCarRefund.Reason,
Proof: callCarRefund.Proof,
Status: callCarRefund.Status,
AuditAt: callCarRefund.AuditAt.Time.Unix(),
RejectReason: callCarRefund.RejectReason,
}
}
return
}
// Final @Title 交车完成
func (c *callCarLogic) Final(brokerId, orderId uint) error {
//callCar := model.OrderCallCar{Id: orderId, CarBrokerId: brokerId}
callCar := model.OrderCallCar{Id: orderId, BrokerId: brokerId}
if mysql.Db.Where(&callCar).First(&callCar).Error != nil {
return errors.New("订单错误")
}
if callCar.Status == model.OrderCallCarStatusUnPay {
return errors.New("订单尚未支付")
}
if callCar.Status >= model.OrderCallCarStatusFinal {
return errors.New("已交车,请勿重复操作")
}
if mysql.Db.Model(&model.OrderCallCar{}).Where(&callCar).Updates(map[string]interface{}{
"status": model.OrderCallCarStatusFinal,
"final_at": time.Now(),
}).RowsAffected != 1 {
return errors.New("交车失败")
}
return nil
}