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.
138 lines
4.0 KiB
138 lines
4.0 KiB
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
"recook/internal/dbc"
|
|
"recook/internal/v2/model/flashsale"
|
|
"recook/internal/v2/model/operationstaties"
|
|
|
|
message2 "recook/internal/v2/logic/manage/message"
|
|
manage "recook/internal/v2/model/recook/order"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
SyncExpress = "SyncExpress"
|
|
SyncGoods = "SyncGoods"
|
|
SyncOrder = "SyncOrder"
|
|
SuccessOrder = "SuccessOrder"
|
|
AfterApply = "AfterApply"
|
|
)
|
|
|
|
//func ThirdManager() gin.HandlerFunc {
|
|
// return func(c *gin.Context) {
|
|
// if value, ok := c.Get("status"); ok {
|
|
// if id, ok := c.Get("id"); ok {
|
|
// fmt.Println(id)
|
|
// value = value.(string)
|
|
// switch value {
|
|
// case SyncOrder:
|
|
// //go orderDeliver(id)
|
|
// //go addOrderNum(id)
|
|
// //go syncSecKillStock(id)
|
|
// case AfterApply:
|
|
// //go afterMessage(id)
|
|
// }
|
|
// }
|
|
// }
|
|
// return
|
|
// }
|
|
//}
|
|
|
|
//传入售后表id数组
|
|
func afterMessage(id interface{}) {
|
|
err := message2.MessageLogic.AddAfter(id.([]uint))
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
}
|
|
|
|
//下单后同步订单id到发货物流消息关联表
|
|
func orderDeliver(id interface{}) {
|
|
m := message2.MessageLogic
|
|
switch id.(type) {
|
|
case uint, int:
|
|
err := m.AddOrderId(id.(uint))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func addOrderNum(oid interface{}) {
|
|
var op operationstaties.OperationStatisticsModel
|
|
t1 := time.Now().Format("2006-01-02") //获取当日时间
|
|
t2, _ := time.ParseInLocation("2006-01-02", t1, time.Local)
|
|
fmt.Println(t1)
|
|
//查询当日时间是否有数据
|
|
dbc.DB.Table(op.TableName()).Where("day_time=?", t2).First(&op)
|
|
var info manage.RecookOrderInfoModel
|
|
dbc.DB.Table(info.TableName()).Where("id=?", oid.(uint)).First(&info)
|
|
if op.Id > 0 { //有数据则更新
|
|
//根据oid查询订单的金额
|
|
|
|
op1 := operationstaties.OperationStatisticsModel{
|
|
SalesVolume: op.SalesVolume.Add(info.GoodsTotalAmount),
|
|
OrderNewAdd: op.OrderNewAdd + 1,
|
|
}
|
|
dbc.DB.Table(op.TableName()).Where("day_time=?", t2).Updates(&op1)
|
|
} else { //没有则新建数据
|
|
op2 := operationstaties.OperationStatisticsModel{
|
|
OrderNewAdd: 1,
|
|
SalesVolume: info.GoodsTotalAmount,
|
|
DayTime: t2,
|
|
}
|
|
dbc.DB.Table(op.TableName()).Create(&op2)
|
|
}
|
|
}
|
|
|
|
//同步秒杀商品的库存
|
|
func syncSecKillStock(id interface{}) {
|
|
orderId := id.(uint)
|
|
//获取本次订单的商品list
|
|
var order manage.RecookOrderInfoModel
|
|
mysql2.Db.Table(order.TableName()).Where("id=?", orderId).First(&order)
|
|
var orderList []manage.RecookOrderGoodsDetailModel
|
|
mysql2.Db.Table((&manage.RecookOrderGoodsDetailModel{}).TableName()).Where("order_id=?", orderId).Find(&orderList)
|
|
//获取本次活动该时间段的商品map
|
|
now := order.CreatedAt.Time
|
|
var seckill flashsale.RecookSecKillModel
|
|
mysql2.Db.Table(seckill.TableName()).Where("activity_start_time<?", now).Where("activity_end_time>?", now).First(&seckill)
|
|
//获取下单时间段的商品
|
|
now2 := time.Date(0, 1, 1, now.Hour(), now.Minute(), now.Second(), 0, time.Local)
|
|
var secgoods []flashsale.RecookSecKillSortModel
|
|
mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("show_time_start<?", now2).Where("show_time_end>?", now2).Where("sec_kill_activity_id=?", seckill.Id).Find(&secgoods)
|
|
mp := make(map[uint]flashsale.RecookSecKillSortModel)
|
|
for _, model := range secgoods {
|
|
mp[model.SkuId] = model
|
|
}
|
|
|
|
err := mysql2.Db.Transaction(func(tx *gorm.DB) error {
|
|
for _, v := range orderList {
|
|
if _, ok := mp[v.SkuId]; ok {
|
|
//该商品存在
|
|
var srck flashsale.RecookSecKillGoodsModel
|
|
err := tx.Table(srck.TableName()).Where("sec_kill_activity_id=?", seckill.Id).Where("goods_sku_id=?", v.SkuId).First(&srck).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newstoc := srck.SaleNum.Add(decimal.NewFromInt(int64(v.Quantity)))
|
|
err = tx.Table(srck.TableName()).Where("id=?", srck.Id).Update("sale_num", newstoc).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
}
|