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.

250 lines
6.1 KiB

package order
import (
"fmt"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/order"
"recook/internal/model/user"
"recook/internal/v2/lib/jcook"
jpush2 "recook/internal/v2/lib/jpush"
"recook/internal/v2/model/jpush"
"recook/internal/v2/model/jyy"
manage "recook/internal/v2/model/recook/order"
"recook/tools"
"git.oa00.com/go/mysql"
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
"github.com/shopspring/decimal"
gorm2 "gorm.io/gorm"
)
type confirmOrderReceiptParam struct {
UserID uint `json:"userId"`
OrderID uint `json:"orderId"`
}
/*
确认收货: 跟交易完成一个概念
*/
func ConfirmOrderReceipt(c *gin.Context) {
var p confirmOrderReceiptParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
var orderInfo order.Information
if err := dbc.DB.First(&orderInfo, p.OrderID).Error; err != nil {
back.Err(c, err.Error())
return
}
if orderInfo.Status == 4 {
back.Suc(c, "", nil)
return
}
if orderInfo.UserID != p.UserID {
back.Fail(c, "参数错误")
return
}
err := ConfirmOrderTradeSuc(&orderInfo, c.ClientIP())
if err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "", gin.H{
"upGrade": 0,
"userLevel": 0,
"roleLevel": 0,
})
}
/*
每2小时监控一次
标记订单交易成功
遍历订单中每件商品的提成
自己省的加入到自己 余额账户 个人未到账账户减去这笔提成 个人累积账户增加这笔提成
上级或者分享的 加到其余额账户 销售未到账账户减去这笔钱 销售累积账户增加这笔钱
团队表中增加商品的销售额
*/
// 重新构建 收益
func ConfirmOrderTradeSuc(orderInfo *order.Information, clientIP ...string) error {
if orderInfo.Status == 4 {
return nil
}
// jd 推送完成
if orderInfo.Kind == 1 || orderInfo.Kind == 2 {
if len(clientIP) == 0 {
clientIP = append(clientIP, "127.0.0.1")
}
client := jcook.GetClient()
req := jcook.OrderFinishReq{
OrderID: orderInfo.JCookOrderID,
ClientIp: clientIP[0],
ClientPort: "8888",
}
var resp jcook.OrderFinishResp
_ = client.Exec(req, &resp)
}
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 e := tx.First(&myWallet, "user_id = ?", orderInfo.UserID).Error; e != nil {
return e
}
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() {
// 修改订单下每个商品的状态
if e := tx.Model(&v).Updates(manage.RecookOrderGoodsDetailModel{
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)
}
}
}
// 标记订单交易成功
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 len(p) > 0 {
// 确认收益
var ju jpush.JPush
mysql.Db.First(&ju, "user_id = ?", userInfo.ID)
for _, v := range p {
// if v.Income.IsZero() {
// v.Status = 1
// tx.Save(&v)
// continue
// }
if v.Status != 0 {
continue
}
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{}{
"balance": gorm2.Expr("balance + ?", v.Income),
"version": gorm2.Expr("version + 1"),
}).RowsAffected; row == 0 {
e := fmt.Errorf("网络异常")
return e
}
v.Status = 2
if err := tx.Save(&v).Error; err != nil {
return err
}
// 添加收益记录
message := fmt.Sprintf("您的%s已到账", v.Type.GetStr())
if e := tx.Create(&jyy.AppUserMessage{
UserID: int(orderInfo.UserID),
Message: message,
Kind: jyy.Profit,
IsRead: false,
SubID: uint64(v.ID),
}).Error; e != nil {
return e
}
if ju.RegistrationId != "" {
client := jpush2.GetClient()
client.PushMsgWithRegister(ju.RegistrationId, "收益到账", message)
}
}
}
if userInfo.Level == 0 && orderInfo.SharerID != orderInfo.UserID {
var s1 user.Information
if e := tx.First(&s1, "id = ?", orderInfo.SharerID).Error; e != nil {
return e
}
level := 0
if userInfo.LoginApp {
level = 0
}
var parentID uint = 0
if userInfo.ParentID == 0 {
parentID = s1.ID
}
if s1.Level == 2 {
// upgrade 修改了升级逻辑
if e := tx.Model(&userInfo).Updates(user.Information{
Level: level,
ParentID: parentID,
UpgradeTime1: formatime.NewSecondNow(),
RootID: s1.RootID,
}).Error; e != nil {
return e
}
}
}
}
return nil
}); err != nil {
return err
}
return nil
}