Ver código fonte

更新

three
huangjiajun 11 meses atrás
pai
commit
983cd6aeae
10 arquivos alterados com 417 adições e 3 exclusões
  1. +23
    -0
      app/db/db_guide_store_order.go
  2. +37
    -0
      app/db/db_newcomers_free_product.go
  3. +25
    -3
      app/db/db_user_virtual_coin_flow.go
  4. +26
    -0
      app/db/model/guide_store_order.go
  5. +36
    -0
      app/db/model/newcomers_free_product.go
  6. +1
    -0
      app/db/model/user_virtual_amount.go
  7. +2
    -0
      consume/init.go
  8. +10
    -0
      consume/md/consume_key.go
  9. +14
    -0
      consume/md/md_zhios_capital_pool_order_total.go
  10. +243
    -0
      consume/zhios_guide_store_order_success.go

+ 23
- 0
app/db/db_guide_store_order.go Ver arquivo

@@ -0,0 +1,23 @@
package db

import (
"applet/app/db/model"
"xorm.io/xorm"
)

func GuideStoreOrder(eg *xorm.Engine, oid int64) *model.GuideStoreOrder {
var data model.GuideStoreOrder
get, err := eg.Where("oid=?", oid).Get(&data)
if get == false || err != nil {
return nil
}
return &data
}
func GuideStoreOrderSess(sess *xorm.Session, oid int64) *model.GuideStoreOrder {
var data model.GuideStoreOrder
get, err := sess.Where("oid=?", oid).Get(&data)
if get == false || err != nil {
return nil
}
return &data
}

+ 37
- 0
app/db/db_newcomers_free_product.go Ver arquivo

@@ -0,0 +1,37 @@
package db

import (
"applet/app/db/model"
"applet/app/utils"
"errors"
"github.com/syyongx/php2go"
"strings"

"xorm.io/xorm"
)

// FreeProductByID is 获取新人免单对应的商品id
func FreeProductByID(Db *xorm.Engine, gid, provider string) (*model.NewcomersFreeProduct, error) {
m := new(model.NewcomersFreeProduct)
var has bool
var err error
if utils.InArr(provider, []string{"taobao", "tmall"}) && php2go.IsNumeric(gid) == false {
gidArr := strings.Split(gid, "-")
if len(gidArr) == 2 {
gid = gidArr[1]
}
has, err = Db.Where("good_id LIKE ?", "%-"+gid).Get(m)
} else {
m.GoodId = gid
has, err = Db.Get(m)
}

if err != nil {
return nil, err
}
if !has {
return nil, errors.New("Not Found")
}

return m, nil
}

+ 25
- 3
app/db/db_user_virtual_coin_flow.go Ver arquivo

@@ -44,9 +44,9 @@ func UserVirtualAmountFindByIdWithSession(session *xorm.Session, uid, coinId int
return &m, nil
}

func UserVirtualAmountFindById(eg *xorm.Engine, uid, coinId int) (*model.UserVirtualAmount, error) {
func UserVirtualAmountFindById(sess *xorm.Session, uid, coinId int) (*model.UserVirtualAmount, error) {
var m model.UserVirtualAmount
has, err := eg.Where("uid = ? AND coin_id = ?", uid, coinId).Get(&m)
has, err := sess.Where("uid = ? AND coin_id = ?", uid, coinId).Get(&m)
if err != nil {
return nil, logx.Warn(err)
}
@@ -54,13 +54,35 @@ func UserVirtualAmountFindById(eg *xorm.Engine, uid, coinId int) (*model.UserVir
m.Amount = "0"
m.CoinId = coinId
m.Uid = uid
one, err := eg.InsertOne(&m)
one, err := sess.InsertOne(&m)
if err != nil || one == 0 {
return nil, logx.Warn(err)
}
}
return &m, nil
}
func UserVirtualAmountUpdate(sess *xorm.Session, uid, coinId interface{}, userVirtualAmount *model.UserVirtualAmount, forceCols ...string) (int64, error) {
var (
affected int64
err error
)
if forceCols != nil {
affected, err = sess.Where("uid = ? AND coin_id = ?", uid, coinId).Cols(forceCols...).Update(userVirtualAmount)
} else {
affected, err = sess.Where("uid = ? AND coin_id = ?", uid, coinId).Update(userVirtualAmount)
}
if err != nil {
return 0, logx.Warn(err)
}
return affected, nil
}
func UserVirtualCoinFlowInsertOne(sess *xorm.Session, m *model.UserVirtualCoinFlow) error {
_, err := sess.InsertOne(m)
if err != nil {
return err
}
return nil
}

// 在事务中更新用户信息
func UserVirtualAmountUpdateWithSession(session *xorm.Session, uid, coinId interface{}, userVirtualAmount *model.UserVirtualAmount, forceCols ...string) (int64, error) {


+ 26
- 0
app/db/model/guide_store_order.go Ver arquivo

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

import (
"time"
)

type GuideStoreOrder struct {
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
Uid int `json:"uid" xorm:"default 0 INT(11)"`
Time time.Time `json:"time" xorm:"DATETIME"`
StoreId int `json:"store_id" xorm:"default 0 INT(11)"`
Gid string `json:"gid" xorm:"VARCHAR(255)"`
State int `json:"state" xorm:"default 0 INT(11)"`
DeductAt time.Time `json:"deduct_at" xorm:"DATETIME"`
Title string `json:"title" xorm:"VARCHAR(255)"`
Pvd string `json:"pvd" xorm:"VARCHAR(255)"`
SubsidyPrice string `json:"subsidy_price" xorm:"DECIMAL(20,2)"`
Fee string `json:"fee" xorm:"DECIMAL(20,2)"`
DeductPrice string `json:"deduct_price" xorm:"DECIMAL(20,2)"`
Price string `json:"price" xorm:"DECIMAL(20,2)"`
Oid string `json:"oid" xorm:"VARCHAR(255)"`
DeductState int `json:"deduct_state" xorm:"default 0 INT(1)"`
SettleAt int `json:"settle_at" xorm:"default 0 INT(11)"`
PvdOid string `json:"pvd_oid" xorm:"unique VARCHAR(255)"`
OldDeductPrice string `json:"old_deduct_price" xorm:"DECIMAL(20,2)"`
}

+ 36
- 0
app/db/model/newcomers_free_product.go Ver arquivo

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

import (
"time"
)

type NewcomersFreeProduct struct {
Id int `json:"id" xorm:"not null pk autoincr INT(10)"`
GoodId string `json:"good_id" xorm:"not null default '' comment('平台商品ID') VARCHAR(255)"`
Source string `json:"source" xorm:"not null default 'taobao' comment('来源平台') VARCHAR(255)"`
SourceUrl string `json:"source_url" xorm:"not null default '' comment('用户输入地址') VARCHAR(255)"`
PriceType int `json:"price_type" xorm:"not null default 0 comment('所属价格类型') TINYINT(1)"`
OriginalPrice string `json:"original_price" xorm:"not null default 0.00 comment('原价') DECIMAL(10,2)"`
CouponPrice string `json:"coupon_price" xorm:"not null default 0.00 comment('券后价格') DECIMAL(10,2)"`
ReturnMoney string `json:"return_money" xorm:"not null default 0.00 comment('返还的钱') DECIMAL(10,2)"`
Bili string `json:"bili" xorm:"not null default 0.00 comment('返还的钱') DECIMAL(10,2)"`
Fee string `json:"fee" xorm:"not null default 0.00 comment('') DECIMAL(10,2)"`
Money string `json:"money" xorm:"not null default 0 comment('实付金额') DECIMAL(10)"`
Stock int `json:"stock" xorm:"not null default 0 comment('库存数量') INT(11)"`
Sale int `json:"sale" xorm:"not null default 0 comment('卖掉的数量') INT(11)"`
EndTime time.Time `json:"end_time" xorm:"not null comment('结束时间') DATETIME"`
IsShow int `json:"is_show" xorm:"not null default 1 comment('是否上架') TINYINT(1)"`
IsDel int `json:"is_del" xorm:"not null default 0 comment('是否删除') TINYINT(1)"`
CreatedAt int `json:"created_at" xorm:"not null default 0 INT(11)"`
UpdatedAt int `json:"updated_at" xorm:"not null default 0 INT(11)"`
Title string `json:"title" xorm:"not null default '' comment('标题') VARCHAR(255)"`
StartTime time.Time `json:"start_time" xorm:"not null comment('开始时间') DATETIME"`
Pictures string `json:"pictures" xorm:"not null default '' comment('图片地址') VARCHAR(255)"`
CouponUrl string `json:"coupon_url" xorm:"not null default '' comment('优惠券链接') VARCHAR(255)"`
ActivityId string `json:"activity_id" xorm:"not null default '' comment('优惠券链接') VARCHAR(255)"`
Amount int `json:"amount" xorm:"default 0 comment('总数') INT(11)"`
ReturnType int `json:"return_type" xorm:"default 0 comment('0平台补贴 1 淘礼金补贴') INT(1)"`
OwnbuyReturnType int `json:"ownbuy_return_type" xorm:"default 0 comment('自购补贴:1开启、0关闭') INT(1)"`
StoreId int `json:"store_id" xorm:"default 0 comment('') INT(11)"`
Sort int `json:"sort" xorm:"default 0 comment('') INT(11)"`
}

+ 1
- 0
app/db/model/user_virtual_amount.go Ver arquivo

@@ -7,4 +7,5 @@ type UserVirtualAmount struct {
Amount string `json:"amount" xorm:"DECIMAL(16,6)"`
FreezeAmount string `json:"freeze_amount" xorm:"DECIMAL(16,6)"`
WaitAmount string `json:"wait_amount" xorm:"DECIMAL(16,6)"`
UseAmount string `json:"use_amount" xorm:"DECIMAL(16,6)"`
}

+ 2
- 0
consume/init.go Ver arquivo

@@ -17,6 +17,8 @@ func Init() {

// 增加消费任务队列
func initConsumes() {
jobs[consumeMd.ZhiosGuideStoreOrderFunName] = ZhiosGuideStoreOrder

//jobs[consumeMd.ZhiosAppreciationDevFunName] = ZhiosAppreciation

jobs[consumeMd.ZhiosAppreciationFunName] = ZhiosAppreciation


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

@@ -254,6 +254,15 @@ var RabbitMqQueueKeyList = []*MqQueue{
BindKey: "",
ConsumeFunName: "ZhiosAppreciationDev",
},
{
ExchangeName: "zhios.guide_store.exchange",
Name: "zhios_guide_store_order",
Type: DirectQueueType,
IsPersistent: false,
RoutKey: "guide_store_order",
BindKey: "",
ConsumeFunName: "ZhiosGuideStoreOrder",
},
}

const (
@@ -283,5 +292,6 @@ const (
ZhiosValidUserFunName = "ZhiosValidUser"
ZhiosAppreciationFunName = "ZhiosAppreciation"
ZhiosAppreciationDevFunName = "ZhiosAppreciationDev"
ZhiosGuideStoreOrderFunName = "ZhiosGuideStoreOrder"
ZhiosAcquisitionConditionDevFunName = "ZhiosAcquisitionConditionDev"
)

+ 14
- 0
consume/md/md_zhios_capital_pool_order_total.go Ver arquivo

@@ -26,3 +26,17 @@ type ZhiosAppreciation struct {
Type string `json:"type"`
Ext string `json:"ext"`
}
type ZhiosGuideStoreOrder struct {
Uid string `json:"uid"`
Mid string `json:"mid"`
Oid string `json:"oid"`
StoreId string `json:"store_id"`
DeductPrice string `json:"deduct_price"`
ItemTitle string `json:"item_title"`
Pvd string `json:"pvd"`
SubsidyPrice string `json:"subsidy_price"`
Fee string `json:"fee"`
State string `json:"state"`
ItemId string `json:"item_id"`
Type string `json:"type"`
}

+ 243
- 0
consume/zhios_guide_store_order_success.go Ver arquivo

@@ -0,0 +1,243 @@
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"
"xorm.io/xorm"
)

func ZhiosGuideStoreOrder(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 = handleZhiosGuideStoreOrderSuccess(res.Body)
//_ = res.Reject(false)
if err == nil {
_ = res.Ack(true)
}
} else {
panic(errors.New("error getting message"))
}
}
fmt.Println("get msg done")
}

func handleZhiosGuideStoreOrderSuccess(msg []byte) error {
//1、解析canal采集至mq中queue的数据结构体
var canalMsg *md.ZhiosGuideStoreOrder
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
}
if canalMsg.Type == "success" {
err := success(eg, canalMsg)
if err != nil {
return err
}
}
if canalMsg.Type == "fail" {
err := fail(eg, canalMsg)
if err != nil {
return err
}
}
return nil
}
func success(eg *xorm.Engine, canalMsg *md.ZhiosGuideStoreOrder) error {
sess := eg.NewSession()
defer sess.Close()
sess.Begin()
var data model.GuideStoreOrder
get, _ := eg.Where("pvd_oid=?", canalMsg.Oid).Get(&data)
var newOrd model.OrdList
eg.Where("pvd_oid=?", canalMsg.Oid).Get(&newOrd)
if get == false {
deductPrice := canalMsg.DeductPrice
var storeOrder = model.GuideStoreOrder{
Uid: newOrd.Uid,
Time: time.Unix(int64(newOrd.CreateAt), 0),
StoreId: utils.StrToInt(canalMsg.StoreId),
Gid: newOrd.ItemId,
State: newOrd.State,
Title: canalMsg.ItemTitle,
Pvd: canalMsg.Pvd,
SubsidyPrice: canalMsg.SubsidyPrice,
Fee: canalMsg.Fee,
DeductPrice: deductPrice,
OldDeductPrice: deductPrice,
Price: utils.Float64ToStr(newOrd.PaidPrice),
Oid: utils.Int64ToStr(newOrd.OrdId),
PvdOid: newOrd.PvdOid,
DeductState: 1,
DeductAt: time.Now(),
}
if utils.StrToInt(canalMsg.State) == 4 {
storeOrder.DeductState = 0
}
_, err := sess.Insert(&storeOrder)
if err == nil && storeOrder.DeductState == 1 {
coinId := db.SysCfgGetWithDb(eg, canalMsg.Mid, "guide_store_coin_id")
err := UpdateUserFinValidAndInterFlowFreeze(sess,
utils.AnyToString(deductPrice), "商品下单扣除冻结积分", "0", 2, utils.StrToInt(canalMsg.StoreId), utils.StrToInt(coinId), 117, newOrd.OrdId)
if err != nil {
sess.Rollback()
return err
}
}
}
sess.Commit()
return nil
}

func fail(eg *xorm.Engine, canalMsg *md.ZhiosGuideStoreOrder) error {
sess := eg.NewSession()
defer sess.Close()
sess.Begin()
m, _ := db.FreeProductByID(eg, canalMsg.ItemId, canalMsg.Pvd)
//直接退回账号了 定制
storeOrder := db.GuideStoreOrderSess(sess, utils.StrToInt64(canalMsg.Oid))
if m != nil {
if storeOrder != nil && utils.StrToFloat64(storeOrder.DeductPrice) > 0 {
coinId := db.SysCfgGetWithDb(eg, canalMsg.Mid, "guide_store_coin_id")
err := UpdateUserVirtualCoinFinValidAndInterFlow(sess,
utils.AnyToString(storeOrder.DeductPrice), "商品退款退回", "0", 1, storeOrder.StoreId, utils.StrToInt(coinId), 114, utils.StrToInt64(canalMsg.Oid))
if err != nil {
sess.Rollback()
return err
}
storeOrder.DeductPrice = "0"
storeOrder.DeductState = 2
storeOrder.State = 4
_, err = sess.Where("id=?", storeOrder.Id).Update(storeOrder)
if err != nil {
sess.Rollback()
return err
}
}
}
sess.Commit()
return nil
}
func UpdateUserVirtualCoinFinValidAndInterFlow(sess *xorm.Session, money, Title, fee string, types, uid, coinId, transferType int, ordId int64) error {
UserVirtualAmount, err := db.UserVirtualAmountFindById(sess, uid, coinId)
if err != nil || UserVirtualAmount == nil {
if err == nil {
err = errors.New("获取用户余额信息失败")
}
return err
}
beforeAmount := UserVirtualAmount.Amount
if types == 2 {
UserVirtualAmount.Amount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.Amount) - utils.StrToFloat64(money))
UserVirtualAmount.UseAmount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.UseAmount) + utils.StrToFloat64(money))
} else if types == 1 {
UserVirtualAmount.Amount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.Amount) + utils.StrToFloat64(money))
UserVirtualAmount.UseAmount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.UseAmount) - utils.StrToFloat64(money))
}
afterAmount := UserVirtualAmount.Amount
affected, err := db.UserVirtualAmountUpdate(sess, uid, coinId, UserVirtualAmount, "amount")
if err != nil || affected == 0 {
if err == nil {
err = errors.New("更新用户余额信息失败")
}
return err
}
err = virtualCoinFlowInsert(sess, uid, coinId, money, fee, ordId, Title, types, transferType, beforeAmount, afterAmount)
if err != nil {
return err
}
return nil
}

func UpdateUserFinValidAndInterFlowFreeze(sess *xorm.Session, money, Title, fee string, types, uid, coinId, transferType int, ordId int64) error {
UserVirtualAmount, err := db.UserVirtualAmountFindById(sess, uid, coinId)
if err != nil || UserVirtualAmount == nil {
if err == nil {
err = errors.New("获取用户余额信息失败")
}
return err
}
beforeAmount := UserVirtualAmount.Amount
if types == 2 {
UserVirtualAmount.FreezeAmount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.FreezeAmount) - utils.StrToFloat64(money))
UserVirtualAmount.UseAmount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.UseAmount) + utils.StrToFloat64(money))
} else if types == 1 {
UserVirtualAmount.FreezeAmount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.FreezeAmount) + utils.StrToFloat64(money))
UserVirtualAmount.UseAmount = utils.AnyToString(utils.AnyToFloat64(UserVirtualAmount.UseAmount) - utils.StrToFloat64(money))
}
afterAmount := UserVirtualAmount.Amount
affected, err := db.UserVirtualAmountUpdate(sess, uid, coinId, UserVirtualAmount, "freeze_amount,use_amount")
if err != nil || affected == 0 {
if err == nil {
err = errors.New("更新用户余额信息失败")
}
return err
}
err = virtualCoinFlowInsert(sess, uid, coinId, money, fee, ordId, Title, types, transferType, beforeAmount, afterAmount)
if err != nil {
return err
}
return nil
}
func virtualCoinFlowInsert(sess *xorm.Session, uid, coinId int, money, SysFee string, ordId int64, ItemTitle string, types, transferType int, beforeAmount string, afterAmount string) error {
now := time.Now()
if err := db.UserVirtualCoinFlowInsertOne(
sess,
&model.UserVirtualCoinFlow{
Uid: uid,
OrdId: utils.Int64ToStr(ordId),
CoinId: coinId,
Direction: types,
Title: ItemTitle,
Amout: money,
BeforeAmout: beforeAmount,
AfterAmout: afterAmount,
SysFee: SysFee,
CreateTime: now,
TransferType: transferType,
}); err != nil {
_ = logx.Warn(err)
return err
}
return nil
}

Carregando…
Cancelar
Salvar