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.

248 lines
6.0 KiB

package order
import (
"fmt"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/order"
"recook/internal/model/user"
"recook/internal/v2/lib/jcook"
"recook/internal/v2/logic/jPushLogic"
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, "参数错误")
return
}
if orderInfo.Status == 4 {
c.Set("status", "SuccessOrder")
c.Set("id", orderInfo.ID)
}
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 {
// 确认收益
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("网络异常!\n")
return e
}
v.Status = 2
if err := tx.Save(&v).Error; err != nil {
return err
}
}
}
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
}
if s1.Level == 2 {
// upgrade
if e := tx.Model(&userInfo).Updates(user.Information{
Level: 1,
ParentID: s1.ID,
UpgradeTime1: formatime.NewSecondNow(),
AgentID: s1.AgentID,
}).Error; e != nil {
return e
}
}
}
}
return nil
}); err != nil {
return err
}
return nil
}
type jPush struct {
UserId uint `json:"user_id"`
Profit decimal.Decimal `json:"profit"`
Type int `json:"type"`
}
func jPushMessage(p jPush) {
fmt.Println("自购导购收益推送开始")
fmt.Println(p)
jp := jPushLogic.JLogic
if p.Type == 1 {
jp.PushByUserIdToNotice(p.UserId, "恭喜您获得一笔自购收益:"+fmt.Sprintf("%v", p.Profit.Truncate(2))+"元")
}
if p.Type == 2 {
jp.PushByUserIdToNotice(p.UserId, "恭喜您获得一笔导购收益:"+fmt.Sprintf("%v", p.Profit.Truncate(2))+"元")
}
fmt.Println("自购导购收益推送开始结束")
}