huangjiajun hace 11 meses
padre
commit
83ef85c5ef
Se han modificado 5 ficheros con 145 adiciones y 1 borrados
  1. +8
    -0
      app/db/model/user_statistics.go
  2. +1
    -0
      consume/init.go
  3. +10
    -0
      consume/md/consume_key.go
  4. +1
    -1
      consume/zhios_order_hjy.go
  5. +125
    -0
      consume/zhios_order_settle_total.go

+ 8
- 0
app/db/model/user_statistics.go Ver fichero

@@ -0,0 +1,8 @@
package model

type UserStatistics struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
ImportFinTotal string `json:"import_fin_total" xorm:"comment('导入的累计收益') DECIMAL(30,4)"`
FinTotal string `json:"fin_total" xorm:"comment('系统统计的累计收益') DECIMAL(30,4)"`
Uid int `json:"uid" xorm:"default 0 INT(11)"`
}

+ 1
- 0
consume/init.go Ver fichero

@@ -18,6 +18,7 @@ func Init() {
// 增加消费任务队列
func initConsumes() {

jobs[consumeMd.ZhiosOrderSettleTotalFunName] = ZhiosSettleTotal
jobs[consumeMd.ZhiosOrderHjyFunName] = ZhiosOrderHjy
jobs[consumeMd.ZhiosOrderBuckleFunName] = ZhiosOrderBuckle
//


+ 10
- 0
consume/md/consume_key.go Ver fichero

@@ -290,6 +290,15 @@ var RabbitMqQueueKeyList = []*MqQueue{
BindKey: "",
ConsumeFunName: "ZhiosOrderHjy",
},
{
ExchangeName: "zhios.order_settle_total.exchange",
Name: "zhios_order_settle_total",
Type: DirectQueueType,
IsPersistent: false,
RoutKey: "order_settle_total",
BindKey: "",
ConsumeFunName: "ZhiosOrderSettleTotal",
},
//{
// ExchangeName: "zhios.order_buckle.exchange",
// Name: "zhios_order_buckle_dev",
@@ -302,6 +311,7 @@ var RabbitMqQueueKeyList = []*MqQueue{
}

const (
ZhiosOrderSettleTotalFunName = "ZhiosOrderSettleTotal"
ZhiosOrderHjyFunName = "ZhiosOrderHjy"
ZhiosOrderBuckleFunName = "ZhiosOrderBuckle"
ZhiosSupplierAfterOrderFunName = "ZhiosSupplierAfterOrder"


+ 1
- 1
consume/zhios_order_hjy.go Ver fichero

@@ -159,7 +159,7 @@ func handleZhiosOrderHjy(msg []byte) error {
return nil
}

if ordData.State == 5 {
if ordData.State != 1 && ordData.State != 3 {
sess.Commit()
return nil
}


+ 125
- 0
consume/zhios_order_settle_total.go Ver fichero

@@ -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
}

Cargando…
Cancelar
Guardar