新增金额为0时退还优惠卷
This commit is contained in:
parent
96308ff608
commit
ef1222b803
@ -11,8 +11,8 @@ type OrderInquiryDao struct {
|
||||
}
|
||||
|
||||
// GetOrderInquiryById 获取问诊订单数据-问诊订单id
|
||||
func (r *OrderInquiryDao) GetOrderInquiryById(doctorId int64) (m *model.OrderInquiry, err error) {
|
||||
err = global.Db.First(&m, doctorId).Error
|
||||
func (r *OrderInquiryDao) GetOrderInquiryById(orderInquiryId int64) (m *model.OrderInquiry, err error) {
|
||||
err = global.Db.First(&m, orderInquiryId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
81
api/dao/orderInquiryCoupon.go
Normal file
81
api/dao/orderInquiryCoupon.go
Normal file
@ -0,0 +1,81 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/global"
|
||||
)
|
||||
|
||||
type OrderInquiryCouponDao struct {
|
||||
}
|
||||
|
||||
// GetOrderInquiryCouponById 获取问诊订单优惠卷数据-问诊订单优惠卷id
|
||||
func (r *OrderInquiryCouponDao) GetOrderInquiryCouponById(orderCouponId int64) (m *model.OrderInquiryCoupon, err error) {
|
||||
err = global.Db.First(&m, orderCouponId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (r *OrderInquiryCouponDao) GetOrderInquiryCouponByOrderInquiryId(orderInquiryId int64) (m *model.OrderInquiryCoupon, err error) {
|
||||
err = global.Db.Where("order_inquiry_id = ?", orderInquiryId).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetOrderInquiryCouponPreloadById 获取问诊订单优惠卷数据-加载全部关联-问诊订单优惠卷id
|
||||
func (r *OrderInquiryCouponDao) GetOrderInquiryCouponPreloadById(orderCouponId int64) (m *model.OrderInquiryCoupon, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, orderCouponId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteOrderInquiryCoupon 删除问诊订单优惠卷
|
||||
func (r *OrderInquiryCouponDao) DeleteOrderInquiryCoupon(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.OrderInquiryCoupon{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditOrderInquiryCoupon 修改问诊订单优惠卷
|
||||
func (r *OrderInquiryCouponDao) EditOrderInquiryCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.OrderInquiryCoupon{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditOrderInquiryCouponById 修改问诊订单优惠卷-问诊订单优惠卷id
|
||||
func (r *OrderInquiryCouponDao) EditOrderInquiryCouponById(tx *gorm.DB, orderCouponId int64, data interface{}) error {
|
||||
err := tx.Model(&model.OrderInquiryCoupon{}).Where("order_coupon_id = ?", orderCouponId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOrderInquiryCouponList 获取问诊订单优惠卷列表
|
||||
func (r *OrderInquiryCouponDao) GetOrderInquiryCouponList(maps interface{}) (m []*model.OrderInquiryCoupon, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddOrderInquiryCoupon 新增问诊订单优惠卷
|
||||
func (r *OrderInquiryCouponDao) AddOrderInquiryCoupon(tx *gorm.DB, model *model.OrderInquiryCoupon) (*model.OrderInquiryCoupon, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
81
api/dao/userCoupon.go
Normal file
81
api/dao/userCoupon.go
Normal file
@ -0,0 +1,81 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/global"
|
||||
)
|
||||
|
||||
type UserCouponDao struct {
|
||||
}
|
||||
|
||||
// GetUserCouponById 获取用户优惠卷数据-用户优惠卷id
|
||||
func (r *UserCouponDao) GetUserCouponById(userCouponId int64) (m *model.UserCoupon, err error) {
|
||||
err = global.Db.First(&m, userCouponId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (r *UserCouponDao) GetUserCouponByUserId(userId int64) (m *model.UserCoupon, err error) {
|
||||
err = global.Db.Where("user_id = ?", userId).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetUserCouponPreloadById 获取用户优惠卷数据-加载全部关联-用户优惠卷id
|
||||
func (r *UserCouponDao) GetUserCouponPreloadById(userCouponId int64) (m *model.UserCoupon, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, userCouponId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteUserCoupon 删除用户优惠卷
|
||||
func (r *UserCouponDao) DeleteUserCoupon(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.UserCoupon{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditUserCoupon 修改用户优惠卷
|
||||
func (r *UserCouponDao) EditUserCoupon(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.UserCoupon{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditUserCouponById 修改用户优惠卷-用户优惠卷id
|
||||
func (r *UserCouponDao) EditUserCouponById(tx *gorm.DB, userCouponId int64, data interface{}) error {
|
||||
err := tx.Model(&model.UserCoupon{}).Where("user_coupon_id = ?", userCouponId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserCouponList 获取用户优惠卷列表
|
||||
func (r *UserCouponDao) GetUserCouponList(maps interface{}) (m []*model.UserCoupon, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddUserCoupon 新增用户优惠卷
|
||||
func (r *UserCouponDao) AddUserCoupon(tx *gorm.DB, model *model.UserCoupon) (*model.UserCoupon, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
35
api/model/orderInquiryCoupon.go
Normal file
35
api/model/orderInquiryCoupon.go
Normal file
@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"hospital-admin-api/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// OrderInquiryCoupon 订单-问诊-优惠卷表
|
||||
type OrderInquiryCoupon struct {
|
||||
OrderCouponId int64 `gorm:"column:order_coupon_id;type:bigint(19);primary_key;comment:主键id" json:"order_coupon_id"`
|
||||
OrderInquiryId int64 `gorm:"column:order_inquiry_id;type:bigint(19);comment:订单-问诊id;NOT NULL" json:"order_inquiry_id"`
|
||||
UserCouponId int64 `gorm:"column:user_coupon_id;type:bigint(19);comment:用户优惠卷表;NOT NULL" json:"user_coupon_id"`
|
||||
CouponName string `gorm:"column:coupon_name;type:varchar(255);comment:优惠卷名称" json:"coupon_name"`
|
||||
CouponUsePrice float64 `gorm:"column:coupon_use_price;type:decimal(10,2);default:0.00;comment:优惠卷使用金额" json:"coupon_use_price"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *OrderInquiryCoupon) TableName() string {
|
||||
return "gdxz_order_inquiry_coupon"
|
||||
}
|
||||
|
||||
func (m *OrderInquiryCoupon) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.OrderCouponId == 0 {
|
||||
m.OrderCouponId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
38
api/model/userCoupon.go
Normal file
38
api/model/userCoupon.go
Normal file
@ -0,0 +1,38 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"hospital-admin-api/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserCoupon 用户优惠卷表
|
||||
type UserCoupon struct {
|
||||
UserCouponId int64 `gorm:"column:user_coupon_id;type:bigint(19);primary_key;comment:主键id" json:"user_coupon_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
PatientId int64 `gorm:"column:patient_id;type:bigint(19);comment:患者id" json:"patient_id"`
|
||||
CouponId int64 `gorm:"column:coupon_id;type:bigint(19);comment:优惠卷id;NOT NULL" json:"coupon_id"`
|
||||
UserCouponStatus int `gorm:"column:user_coupon_status;type:tinyint(1);default:0;comment:状态(0:未使用 1:已使用 3:已过期)" json:"user_coupon_status"`
|
||||
CouponUseDate time.Time `gorm:"column:coupon_use_date;type:datetime;comment:使用时间" json:"coupon_use_date"`
|
||||
ValidStartTime time.Time `gorm:"column:valid_start_time;type:datetime;comment:有效使用时间" json:"valid_start_time"`
|
||||
ValidEndTime time.Time `gorm:"column:valid_end_time;type:datetime;comment:过期使用时间" json:"valid_end_time"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *UserCoupon) TableName() string {
|
||||
return "gdxz_user_coupon"
|
||||
}
|
||||
|
||||
func (m *UserCoupon) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.UserCouponId == 0 {
|
||||
m.UserCouponId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/config"
|
||||
@ -86,11 +87,16 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
|
||||
// 问诊订单修改数据
|
||||
orderInquiryData := make(map[string]interface{})
|
||||
|
||||
// 退款编号
|
||||
inquiryRefundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||||
|
||||
// 退款状态转换
|
||||
var inquiryRefundStatus int
|
||||
var successTime time.Time
|
||||
var refundId string
|
||||
|
||||
// 发起退款
|
||||
if orderInquiry.PaymentAmountTotal > 0 {
|
||||
// 退款编号
|
||||
inquiryRefundNo := strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||||
|
||||
refundRequest := weChat.RefundRequest{
|
||||
TransactionId: orderInquiry.EscrowTradeNo,
|
||||
OutTradeNo: orderInquiry.InquiryNo,
|
||||
@ -111,10 +117,6 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
|
||||
return false, errors.New("退款状态错误")
|
||||
}
|
||||
|
||||
// 退款状态转换
|
||||
var inquiryRefundStatus int
|
||||
var successTime time.Time
|
||||
|
||||
if *refund.Status == "SUCCESS" {
|
||||
// 退款成功
|
||||
inquiryRefundStatus = 3
|
||||
@ -126,9 +128,11 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
|
||||
} else if *refund.Status == "CLOSED" {
|
||||
// 退款关闭
|
||||
inquiryRefundStatus = 5
|
||||
|
||||
} else if *refund.Status == "PROCESSING" {
|
||||
// 退款处理中
|
||||
inquiryRefundStatus = 2
|
||||
|
||||
} else if *refund.Status == "ABNORMAL" {
|
||||
// 退款异常
|
||||
tx.Rollback()
|
||||
@ -143,27 +147,25 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
|
||||
return false, errors.New("缺少退款订单编号")
|
||||
}
|
||||
|
||||
// 新增退款表
|
||||
orderInquiryRefundDao := dao.OrderInquiryRefundDao{}
|
||||
// 退款编号
|
||||
refundId = *refund.RefundId
|
||||
} else {
|
||||
// 支付金额为0,模拟退款
|
||||
refundId = "模拟退款:" + strconv.FormatInt(global.Snowflake.Generate().Int64(), 10)
|
||||
inquiryRefundStatus = 3
|
||||
successTime = time.Now()
|
||||
|
||||
orderInquiryRefund := &model.OrderInquiryRefund{
|
||||
PatientId: orderInquiry.PatientId,
|
||||
OrderInquiryId: orderInquiryId,
|
||||
InquiryNo: orderInquiry.InquiryNo,
|
||||
InquiryRefundNo: inquiryRefundNo,
|
||||
RefundId: *refund.RefundId,
|
||||
InquiryRefundStatus: inquiryRefundStatus,
|
||||
RefundTotal: orderInquiry.PaymentAmountTotal,
|
||||
RefundReason: "客服取消",
|
||||
SuccessTime: successTime,
|
||||
}
|
||||
orderInquiryRefund, err = orderInquiryRefundDao.AddOrderInquiryRefund(tx, orderInquiryRefund)
|
||||
if err != nil || orderInquiryRefund == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
// 模拟退款时手动退还优惠卷
|
||||
if orderInquiry.CouponAmountTotal > 0 {
|
||||
res := ReturnInquiryCoupon(tx, orderInquiry.OrderInquiryId)
|
||||
if !res {
|
||||
tx.Rollback()
|
||||
return false, errors.New("退还优惠卷失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
orderInquiryData["inquiry_refund_status"] = inquiryRefundStatus
|
||||
orderInquiryData["inquiry_status"] = 7 // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||||
orderInquiryData["cancel_time"] = time.Now().Format("2006-01-02 15:04:05") // 订单取消时间
|
||||
orderInquiryData["cancel_reason"] = 4 // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
|
||||
@ -176,7 +178,65 @@ func (r *OrderInquiryService) CancelOrderInquiry(orderInquiryId int64) (bool, er
|
||||
return false, errors.New("取消订单失败")
|
||||
}
|
||||
|
||||
// 新增退款表
|
||||
orderInquiryRefund := &model.OrderInquiryRefund{
|
||||
PatientId: orderInquiry.PatientId,
|
||||
OrderInquiryId: orderInquiryId,
|
||||
InquiryNo: orderInquiry.InquiryNo,
|
||||
InquiryRefundNo: inquiryRefundNo,
|
||||
RefundId: refundId,
|
||||
InquiryRefundStatus: inquiryRefundStatus,
|
||||
RefundTotal: orderInquiry.PaymentAmountTotal,
|
||||
RefundReason: "客服取消",
|
||||
SuccessTime: successTime,
|
||||
}
|
||||
|
||||
orderInquiryRefundDao := dao.OrderInquiryRefundDao{}
|
||||
orderInquiryRefund, err = orderInquiryRefundDao.AddOrderInquiryRefund(tx, orderInquiryRefund)
|
||||
if err != nil || orderInquiryRefund == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ReturnInquiryCoupon 退还问诊订单优惠卷
|
||||
func ReturnInquiryCoupon(tx *gorm.DB, orderInquiryId int64) bool {
|
||||
orderInquiryCouponDao := dao.OrderInquiryCouponDao{}
|
||||
userCouponDao := dao.UserCouponDao{}
|
||||
|
||||
// 获取用户优惠卷信息
|
||||
orderInquiry, err := orderInquiryCouponDao.GetOrderInquiryCouponByOrderInquiryId(orderInquiryId)
|
||||
if err != nil || orderInquiry == nil {
|
||||
// 订单未使用优惠卷,无需退还
|
||||
return true
|
||||
}
|
||||
|
||||
// 获取用户优惠卷数据
|
||||
userCoupon, err := userCouponDao.GetUserCouponById(orderInquiry.UserCouponId)
|
||||
if err != nil || userCoupon == nil {
|
||||
// 无该优惠卷数据,无需处理
|
||||
return true
|
||||
}
|
||||
|
||||
// 恢复优惠卷
|
||||
userCouponData := make(map[string]interface{})
|
||||
|
||||
// 检测优惠卷过期时间。判断是否需要退还
|
||||
if userCoupon.ValidEndTime.Before(time.Now()) {
|
||||
userCouponData["user_coupon_status"] = 3
|
||||
} else {
|
||||
userCouponData["user_coupon_status"] = 0
|
||||
userCouponData["coupon_use_date"] = nil
|
||||
}
|
||||
|
||||
err = userCouponDao.EditUserCouponById(tx, userCoupon.UserCouponId, userCouponData)
|
||||
if err != nil {
|
||||
// 恢复优惠卷失败
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user