|
|
@@ -0,0 +1,125 @@ |
|
|
|
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 ZhiosSettleTotal(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(20) |
|
|
|
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 = handleZhiosOrderSettleTotal(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 handleZhiosOrderSettleTotal(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 { |
|
|
|
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.UserStatistics |
|
|
|
sess.Where("uid=?", ordData.Uid).Get(&userStatistics) |
|
|
|
if userStatistics.Id == 0 { |
|
|
|
userStatistics = model.UserStatistics{ |
|
|
|
ImportFinTotal: "", |
|
|
|
FinTotal: "", |
|
|
|
Uid: ordData.Uid, |
|
|
|
} |
|
|
|
_, err := sess.Insert(&userStatistics) |
|
|
|
if err != nil { |
|
|
|
sess.Rollback() |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
userStatistics.FinTotal = utils.Float64ToStrByPrec(utils.StrToFloat64(userStatistics.FinTotal)+v.Amount+utils.StrToFloat64(v.AdditionalSubsidy), 4) |
|
|
|
_, err := sess.Where("id=?", userStatistics.Id).Cols("fin_total").Update(&userStatistics) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
sess.Commit() |
|
|
|
return nil |
|
|
|
} |