Browse Source

更新

three
huangjiajun 1 year ago
parent
commit
60f6bf7c38
6 changed files with 212 additions and 2 deletions
  1. +1
    -1
      app/db/dbs_map.go
  2. +1
    -0
      app/db/model/ord_list.go
  3. +2
    -1
      consume/init.go
  4. +10
    -0
      consume/md/consume_key.go
  5. +2
    -0
      consume/zhios_order_buckle.go
  6. +196
    -0
      consume/zhios_order_total_second.go

+ 1
- 1
app/db/dbs_map.go View File

@@ -92,7 +92,7 @@ func GetAllDatabaseDev() *[]model.DbMapping {
fmt.Println("cfg.Local is: ", cfg.Local)
if cfg.Local { // 本地调试 加快速度
fmt.Println("notice:LOCAL TEST, only masterId:** 123456 ** available!")
err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id=?", 1, 123456).Find(&m)
err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id=?", 1, 123999).Find(&m)
} else {
err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m)
}


+ 1
- 0
app/db/model/ord_list.go View File

@@ -42,6 +42,7 @@ type OrdList struct {
UpdateFrom int `xorm:"not null default 0 TINYINT(1)" json:"update_from"`
CreateFrom int `xorm:"not null default 0 TINYINT(1)" json:"create_from"`
IsRefund int `xorm:"not null default 0 INT(1)" json:"is_refund"`
IsTotal int `xorm:"not null default 0 INT(1)" json:"is_total"`
IsSendMsg int `xorm:"not null default 0 INT(1)" json:"is_send_msg"`
PvdPid string `xorm:"not null default '' VARCHAR(100)" json:"pvd_pid"`
CostPrice float64 `xorm:"not null default 0.00 FLOAT(10,2)" json:"cost_price"`


+ 2
- 1
consume/init.go View File

@@ -18,7 +18,8 @@ func Init() {
// 增加消费任务队列
func initConsumes() {
jobs[consumeMd.ZhiosOrderTotalFunName] = ZhiosOrderTotal

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


+ 10
- 0
consume/md/consume_key.go View File

@@ -308,6 +308,15 @@ var RabbitMqQueueKeyList = []*MqQueue{
BindKey: "",
ConsumeFunName: "ZhiosOrderTotal",
},
{
ExchangeName: "zhios.order_total.exchange",
Name: "zhios_order_total_second",
Type: DirectQueueType,
IsPersistent: false,
RoutKey: "order_total_second",
BindKey: "",
ConsumeFunName: "ZhiosOrderTotalSecond",
},
//{
// ExchangeName: "zhios.order_buckle.exchange",
// Name: "zhios_order_buckle_dev",
@@ -322,6 +331,7 @@ var RabbitMqQueueKeyList = []*MqQueue{
const (
ZhiosOrderSettleTotalFunName = "ZhiosOrderSettleTotal"
ZhiosOrderTotalFunName = "ZhiosOrderTotal"
ZhiosOrderTotalSecondFunName = "ZhiosOrderTotalSecond"
ZhiosOrderHjyFunName = "ZhiosOrderHjy"
ZhiosOrderBuckleFunName = "ZhiosOrderBuckle"
ZhiosSupplierAfterOrderFunName = "ZhiosSupplierAfterOrder"


+ 2
- 0
consume/zhios_order_buckle.go View File

@@ -211,6 +211,7 @@ func handleZhiosOrderBuckle(msg []byte) error {
ordData.PlanCommissionId = opt.PlanCommissionId
ordData.BenefitList = SerializeLvUser(NewCalcLvUserFee(lvUser))
}

has, _ := db.OrderListByUpdateOrd(sess, ordData)
if has == false {
sess.Rollback()
@@ -331,6 +332,7 @@ func OrderRelateInsert(eg *xorm.Engine, sess *xorm.Session, oid int64, pvd strin
if err != nil {
return err
}

}
}



+ 196
- 0
consume/zhios_order_total_second.go View File

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

Loading…
Cancel
Save