feat: confirm order

master
howell 4 years ago
parent 62e60d3197
commit 93faac26cd

@ -5,16 +5,15 @@ import (
"git.oa00.com/go/mysql"
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
"github.com/jinzhu/gorm"
"github.com/shopspring/decimal"
gorm2 "gorm.io/gorm"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/order"
"recook/internal/model/user"
"recook/internal/service/app/user_role"
"recook/internal/v2/hook"
"recook/internal/v2/lib/jcook"
"recook/internal/v2/logic/jPushLogic"
user2 "recook/internal/v2/logic/manage/user"
manage "recook/internal/v2/model/recook/order"
"recook/tools"
)
@ -50,16 +49,19 @@ func ConfirmOrderReceipt(c *gin.Context) {
return
}
isUp, UserLevel, RoleLevel := ConfirmOrderTradeSuc(&orderInfo, c.ClientIP())
err := ConfirmOrderTradeSuc(&orderInfo, c.ClientIP())
if err != nil {
back.Fail(c, "参数错误")
return
}
if orderInfo.Status == 4 {
c.Set("status", "SuccessOrder")
c.Set("id", orderInfo.ID)
}
back.Suc(c, "", gin.H{
"upGrade": isUp,
"userLevel": UserLevel,
"roleLevel": RoleLevel,
"upGrade": 0,
"userLevel": 0,
"roleLevel": 0,
})
}
@ -78,10 +80,11 @@ func ConfirmOrderReceipt(c *gin.Context) {
// 重新构建 收益
func ConfirmOrderTradeSuc(orderInfo *order.Information, clientIP ...string) (isUp int, UserLevel int, RoleLevel int) {
func ConfirmOrderTradeSuc(orderInfo *order.Information, clientIP ...string) error {
if orderInfo.Status == 4 {
return
return nil
}
// jd 推送完成
if orderInfo.Kind == 1 || orderInfo.Kind == 2 {
if len(clientIP) == 0 {
clientIP = append(clientIP, "127.0.0.1")
@ -95,118 +98,120 @@ func ConfirmOrderTradeSuc(orderInfo *order.Information, clientIP ...string) (isU
var resp jcook.OrderFinishResp
_ = client.Exec(req, &resp)
}
isUp = 0
tx := dbc.DB.Begin()
{
var userInfo user.Information
tx.Find(&userInfo, orderInfo.UserID)
}
var jp jPush //新增收益推送
var sharerWallet user.Wallet
if orderInfo.SharerID > 0 {
if err := tx.First(&sharerWallet, "user_id = ?", orderInfo.SharerID).Error; err != nil {
tx.Rollback()
return
if err := mysql.Db.Transaction(func(tx *gorm2.DB) error {
var userInfo user.Information
if e := tx.First(&userInfo, orderInfo.UserID).Error; e != nil {
return e
}
}
var myWallet user.Wallet
if err := tx.First(&myWallet, "user_id = ?", orderInfo.UserID).Error; err != nil {
tx.Rollback()
return
}
var myWallet user.Wallet
if e := tx.First(&myWallet, "user_id = ?", orderInfo.UserID).Error; e != nil {
return e
}
var goodsList []manage.RecookOrderGoodsDetailModel
if err := tx.Find(&goodsList, "order_id = ?", orderInfo.ID).Error; err != nil {
tx.Rollback()
return
}
var goodsList []manage.RecookOrderGoodsDetailModel
if e := tx.Find(&goodsList, "order_id = ?", orderInfo.ID).Error; e != nil {
return e
}
// 计算该笔订单实际成交商品的提成总和
totalCommission := decimal.NewFromFloat(0.0)
actTotalAmount := orderInfo.ActualTotalAmount
coinTotalAmount := orderInfo.CoinTotalAmount
totalAmount := decimal.Zero
for _, v := range goodsList {
if v.IsFinished() {
// 修改订单下每个商品的状态
err := tx.Model(&v).Updates(order.GoodsDetail{
Status: 1,
ExpressStatus: 2,
}).Error
if err != nil {
tx.Rollback()
return
}
if v.AssType == 0 || (v.AssType == 1 && v.ExpressStatus == 1) {
totalCommission = totalCommission.Add(v.TotalCommission)
totalAmount = totalAmount.Add(v.GoodsAmount)
totalCommission := decimal.NewFromFloat(0.0)
actTotalAmount := orderInfo.ActualTotalAmount
coinTotalAmount := orderInfo.CoinTotalAmount
totalAmount := decimal.Zero
for _, v := range goodsList {
if v.IsFinished() {
// 修改订单下每个商品的状态
if e := tx.Model(&v).Updates(order.GoodsDetail{
Status: 1,
ExpressStatus: 2,
}).Error; e != nil {
return e
}
if v.AssType == 0 || (v.AssType == 1 && v.ExpressStatus == 1) {
totalCommission = totalCommission.Add(v.TotalCommission)
totalAmount = totalAmount.Add(v.GoodsAmount)
}
}
}
}
var rUserCoinHistory user.RecookUserCoinHistory
dbc.DB.Where("order_id = ?", orderInfo.ID).Find(&rUserCoinHistory)
if rUserCoinHistory.ID > 0 {
if rUserCoinHistory.UserId == orderInfo.UserID {
// 自购
if err := user2.TeamLogic.PurchaseIncome(orderInfo.UserID, totalAmount, totalCommission, orderInfo.PayTime, tx); err != nil {
tx.Rollback()
return
}
//新增推送的部门
jp.Type = 1
jp.UserId = orderInfo.UserID
jp.Profit = totalCommission
// 标记订单交易成功
if e := tx.Model(orderInfo).Updates(order.Information{
Status: 4,
ExpressStatus: 2,
CompletedAt: formatime.NewSecondNow(),
GoodsTotalCommission: totalCommission,
ActualTotalAmount: actTotalAmount,
CoinTotalAmount: coinTotalAmount,
}).Error; e != nil {
return e
}
// 更新钱包逻辑
var p []order.Profit
if e := tx.Set("gorm:query_option", "FOR UPDATE").Find(&p, "order_id = ?", orderInfo.ID).Error; e != nil {
return e
}
if len(p) == 0 {
return nil
} else {
// 导购
if err := user2.TeamLogic.GuideIncome(rUserCoinHistory.UserId, totalAmount, totalCommission, orderInfo.PayTime, tx); err != nil {
tx.Rollback()
return
if len(p) > 0 {
// 确认收益
for _, v := range p {
var w1 user.Wallet
if e := tx.First(&w1, "user_id = ?", v.UserID).Error; e != nil {
return e
}
r1 := user.WalletBalanceList{
UserID: v.UserID,
IncomeType: uint(10 + v.Type),
Amount: v.Income,
Title: orderInfo.Title,
Comment: v.Type.GetStr(),
OrderID: v.OrderID,
OrderTime: orderInfo.CreatedAt,
CreatedAt: formatime.NewSecondNow(),
ProfitID: uint(v.ID),
}
if e := tx.Create(r1).Error; e != nil {
return e
}
if row := tx.Model(&w1).Where("balance = ? AND version = ?", w1.Balance, w1.Version).Updates(map[string]interface{}{
"amount": gorm.Expr("balance + ?", v.Income),
"version": gorm.Expr("version + 1"),
}).RowsAffected; row == 0 {
e := fmt.Errorf("网络异常!\n")
return e
}
v.Status = 2
tx.Save(v)
}
}
if userInfo.Level == 0 && orderInfo.SharerID != 0 {
var s1 user.Information
if e := tx.First(&s1, "user_id = ?", orderInfo.SharerID).Error; e != nil {
return e
}
if s1.Level == 2 {
// upgrade
if e := tx.Model(&userInfo).Updates(user.Information{
Level: 1,
ParentID: s1.ID,
UpgradeTime1: formatime.NewSecondNow(),
}).Error; e != nil {
return e
}
}
}
//新增推送的部门
jp.Type = 2
jp.UserId = orderInfo.UserID
jp.Profit = totalCommission
}
}
// 标记订单交易成功
if err := tx.Model(orderInfo).Updates(order.Information{
Status: 4,
ExpressStatus: 2,
CompletedAt: formatime.NewSecondNow(),
GoodsTotalCommission: totalCommission,
ActualTotalAmount: actTotalAmount,
CoinTotalAmount: coinTotalAmount,
//IsFirst: isFirst,
}).Error; err != nil {
tx.Rollback()
return
}
orderInfoModel := manage.RecookOrderInfoModel{Id: orderInfo.ID}
mysql.Db.First(&orderInfoModel)
// 订单完成钩子
if err := hook.OrderHook.Finish(&orderInfoModel); err != nil {
tx.Rollback()
return
return nil
}); err != nil {
return err
}
//新增收益推送部门
go jPushMessage(jp)
tx.Commit()
// 完成订单状态后 反佣金
user_role.OrderProfitWhenCompleted(orderInfo.ID)
//如果升级,就返回有效信息
return isUp, UserLevel, RoleLevel
return nil
}
type jPush struct {

@ -5,20 +5,6 @@ import (
"github.com/shopspring/decimal"
)
type BalanceRecord struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Amount decimal.Decimal `json:"amount"`
Type int `json:"type"`
Balance decimal.Decimal `json:"balance"`
ProfitID int `json:"profit_id"`
CreatedAt formatime.Second `json:"created_at"`
}
func (o BalanceRecord) TableName() string {
return "jyy_balance_record"
}
type Profit struct {
ID int `json:"id"`
OrderID uint `json:"order_id"`
@ -43,6 +29,17 @@ const (
Shop
)
var profitStr = map[IncomeType]string{
Self: "自购收益",
Guide: "导购收益",
Brand: "品牌补贴",
Shop: "开店补贴",
}
func (o IncomeType) GetStr() string {
return profitStr[o]
}
var profitMap = map[IncomeType]decimal.Decimal{
Self: decimal.NewFromFloat(0.4),
Guide: decimal.NewFromFloat(0.4),

@ -43,6 +43,8 @@ type Information struct {
City string `json:"city"`
Level int `json:"level"`
ParentID uint `json:"parent_id"`
UpgradeTime1 formatime.Second `json:"upgrade_time1"`
UpgradeTime2 formatime.Second `json:"upgrade_time2"`
}
const (

@ -19,6 +19,7 @@ type Wallet struct {
SumSalesIncome decimal.Decimal `gorm:"column:sum_sales_income" json:"sumSalesIncome"`
SalesUnaccountedIncome decimal.Decimal `gorm:"column:sales_unaccounted_income" json:"salesUnaccountedIncome"`
SumTeamIncome decimal.Decimal `gorm:"column:sum_team_income" json:"sumTeamIncome"`
Version int `json:"version"`
}
/*

@ -16,6 +16,7 @@ type WalletBalanceList struct {
OrderGoodsId uint `gorm:"column:order_goods_id" json:"-"`
OrderTime formatime.Second `gorm:"column:order_time" json:"orderTime,omitempty"`
CreatedAt formatime.Second `gorm:"column:created_at" json:"createdAt,omitempty"`
ProfitID uint `json:"profit_id"`
}
func (r *WalletBalanceList) TableName() string {

@ -28,9 +28,11 @@ type RecookUserInfoModel struct {
IDCard string
Province string
City string
Wallet user.Wallet `gorm:"foreignKey:UserID"`
SuperiorUserId uint `gorm:"column:superior_user_id" json:"superior_user_id"`
LoginApp uint `gorm:"column:login_app" json:"login_app"`
Wallet user.Wallet `gorm:"foreignKey:UserID"`
Level int `json:"level"`
ParentID uint `json:"parent_id"`
UpgradeTime1 formatime.Second `json:"upgrade_time1"`
UpgradeTime2 formatime.Second `json:"upgrade_time2"`
}
// TableName sets the insert table name for this struct type

@ -26,6 +26,7 @@ type RecookUserWalletBalanceListModel struct {
OrderGoodsId uint `gorm:"column:order_goods_id" json:"-"`
OrderTime formatime.Second `gorm:"column:order_time" json:"orderTime,omitempty"`
CreatedAt formatime.Second `gorm:"column:created_at" json:"createdAt,omitempty"`
ProfitID uint `json:"profit_id"`
}
// TableName sets the insert table name for this struct type

Loading…
Cancel
Save