|
@@ -0,0 +1,196 @@ |
|
|
|
|
|
package consume |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"applet/app/db" |
|
|
|
|
|
"applet/app/db/model" |
|
|
|
|
|
"applet/app/utils" |
|
|
|
|
|
"applet/app/utils/logx" |
|
|
|
|
|
"applet/consume/md" |
|
|
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" |
|
|
|
|
|
"encoding/json" |
|
|
|
|
|
"errors" |
|
|
|
|
|
"fmt" |
|
|
|
|
|
"github.com/streadway/amqp" |
|
|
|
|
|
"time" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func ZhiosOrderTotalSecond(queue md.MqQueue) { |
|
|
|
|
|
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>") |
|
|
|
|
|
ch, err := rabbit.Cfg.Pool.GetChannel() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logx.Error(err) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
defer ch.Release() |
|
|
|
|
|
//1、将自己绑定到交换机上 |
|
|
|
|
|
ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) |
|
|
|
|
|
//2、取出数据进行消费 |
|
|
|
|
|
ch.Qos(1) |
|
|
|
|
|
delivery := ch.Consume(queue.Name, false) |
|
|
|
|
|
|
|
|
|
|
|
var res amqp.Delivery |
|
|
|
|
|
var ok bool |
|
|
|
|
|
for { |
|
|
|
|
|
res, ok = <-delivery |
|
|
|
|
|
if ok == true { |
|
|
|
|
|
//fmt.Println(string(res.Body)) |
|
|
|
|
|
fmt.Println(">>>>>>>>>>>>>>>>CanalOrderConsume<<<<<<<<<<<<<<<<<<<<<<<<<") |
|
|
|
|
|
err = handleZhiosOrderTotalSecond(res.Body) |
|
|
|
|
|
//_ = res.Reject(false) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
_ = res.Reject(false) |
|
|
|
|
|
//TODO::重新推回队列末尾,避免造成队列堵塞 |
|
|
|
|
|
var msg *md.ZhiosOrderBuckle |
|
|
|
|
|
var tmpString string |
|
|
|
|
|
err := json.Unmarshal(res.Body, &tmpString) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
fmt.Println(tmpString) |
|
|
|
|
|
err = json.Unmarshal([]byte(tmpString), &msg) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
ch.Publish(queue.ExchangeName, utils.SerializeStr(msg), queue.RoutKey) |
|
|
|
|
|
} else { |
|
|
|
|
|
_ = res.Ack(true) |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
panic(errors.New("error getting message")) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
fmt.Println("get msg done") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func handleZhiosOrderTotalSecond(msg []byte) error { |
|
|
|
|
|
time.Sleep(time.Microsecond * 20) // 等待500毫秒 |
|
|
|
|
|
//1、解析canal采集至mq中queue的数据结构体 |
|
|
|
|
|
var canalMsg *md.ZhiosOrderBuckle |
|
|
|
|
|
fmt.Println(string(msg)) |
|
|
|
|
|
var tmpString string |
|
|
|
|
|
err := json.Unmarshal(msg, &tmpString) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println("===with", err.Error()) |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
fmt.Println(tmpString) |
|
|
|
|
|
err = json.Unmarshal([]byte(tmpString), &canalMsg) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println("===with", err.Error()) |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
mid := canalMsg.Mid |
|
|
|
|
|
eg := db.DBs[mid] |
|
|
|
|
|
if eg == nil { |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
sess := eg.NewSession() |
|
|
|
|
|
defer sess.Close() |
|
|
|
|
|
sess.Begin() |
|
|
|
|
|
|
|
|
|
|
|
ordData, err := db.OrderListByPvdOid(sess, canalMsg.Oid) |
|
|
|
|
|
if err != nil || ordData == nil { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
now := time.Unix(int64(ordData.CreateAt), 0).Format("200601") |
|
|
|
|
|
isTotal := ordData.IsTotal |
|
|
|
|
|
if ordData.OrderFormType >= 0 { |
|
|
|
|
|
if ordData.IsTotal == 1 && ordData.State != 4 { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.IsTotal == 2 && ordData.State == 4 { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
var ordRelate = make([]model.OrdListRelate, 0) |
|
|
|
|
|
sess.Where("oid=?", canalMsg.Oid).Find(&ordRelate) |
|
|
|
|
|
if len(ordRelate) > 0 { |
|
|
|
|
|
for _, v := range ordRelate { |
|
|
|
|
|
if v.Amount == 0 && utils.StrToFloat64(v.AdditionalSubsidy) == 0 { |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
var userStatistics model.UserMonthAmount |
|
|
|
|
|
sess.Where("uid=? and date=?", v.Uid, now).Get(&userStatistics) |
|
|
|
|
|
if userStatistics.Id == 0 { |
|
|
|
|
|
userStatistics = model.UserMonthAmount{ |
|
|
|
|
|
Date: utils.StrToInt(now), |
|
|
|
|
|
Uid: v.Uid, |
|
|
|
|
|
} |
|
|
|
|
|
_, err := sess.Insert(&userStatistics) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.State == 4 && ordData.IsTotal == 1 { |
|
|
|
|
|
isTotal = 2 |
|
|
|
|
|
userStatistics.Amount = utils.Float64ToStrByPrec(utils.StrToFloat64(userStatistics.Amount)-v.Amount-utils.StrToFloat64(v.AdditionalSubsidy), 4) |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.State != 4 && ordData.IsTotal == 0 { |
|
|
|
|
|
isTotal = 1 |
|
|
|
|
|
userStatistics.Amount = utils.Float64ToStrByPrec(utils.StrToFloat64(userStatistics.Amount)+v.Amount+utils.StrToFloat64(v.AdditionalSubsidy), 4) |
|
|
|
|
|
} |
|
|
|
|
|
_, err := sess.Where("id=?", userStatistics.Id).Cols("amount").Update(&userStatistics) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
if ordData.IsTotal == 3 && ordData.State != 4 { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.IsTotal == 2 && ordData.State == 4 { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
var ordRelate = make([]model.BuckleRecord, 0) |
|
|
|
|
|
sess.Where("ord_id=?", canalMsg.Oid).Find(&ordRelate) |
|
|
|
|
|
if len(ordRelate) > 0 { |
|
|
|
|
|
for _, v := range ordRelate { |
|
|
|
|
|
if utils.StrToFloat64(v.Amount) == 0 { |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
var userStatistics model.UserMonthAmount |
|
|
|
|
|
sess.Where("uid=? and date=?", v.Uid, now).Get(&userStatistics) |
|
|
|
|
|
if userStatistics.Id == 0 { |
|
|
|
|
|
userStatistics = model.UserMonthAmount{ |
|
|
|
|
|
Date: utils.StrToInt(now), |
|
|
|
|
|
Uid: v.Uid, |
|
|
|
|
|
} |
|
|
|
|
|
_, err := sess.Insert(&userStatistics) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.State == 4 && (ordData.IsTotal == 1 || ordData.IsTotal == 3) { |
|
|
|
|
|
isTotal = 2 |
|
|
|
|
|
userStatistics.Amount = utils.Float64ToStrByPrec(utils.StrToFloat64(userStatistics.Amount)-utils.StrToFloat64(v.AfterAmount), 4) |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.State != 4 && (ordData.IsTotal == 0) { |
|
|
|
|
|
isTotal = 3 |
|
|
|
|
|
userStatistics.Amount = utils.Float64ToStrByPrec(utils.StrToFloat64(userStatistics.Amount)+utils.StrToFloat64(v.AfterAmount), 4) |
|
|
|
|
|
} |
|
|
|
|
|
if ordData.State != 4 && (ordData.IsTotal == 1) { |
|
|
|
|
|
isTotal = 3 |
|
|
|
|
|
userStatistics.Amount = utils.Float64ToStrByPrec(utils.StrToFloat64(userStatistics.Amount)-utils.StrToFloat64(v.Amount), 4) |
|
|
|
|
|
} |
|
|
|
|
|
_, err := sess.Where("id=?", userStatistics.Id).Cols("amount").Update(&userStatistics) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
sess.Rollback() |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
ordData.IsTotal = isTotal |
|
|
|
|
|
sess.Where("ord_id=?", ordData.OrdId).Update(ordData) |
|
|
|
|
|
sess.Commit() |
|
|
|
|
|
return nil |
|
|
|
|
|
} |