diff --git a/app/db/db_order.go b/app/db/db_order.go index f81c648..6b67352 100644 --- a/app/db/db_order.go +++ b/app/db/db_order.go @@ -455,8 +455,9 @@ func OrderListCountByPriceType(sess *xorm.Session, uid string, priceType, day in } // OrderListCountByItemId is 计算免单单个商品购买的次数 -func OrderListCountByItemId(sess *xorm.Session, uid string, itemId string, day int) (int64, error) { - sess = sess.Where(" uid = ? AND state != 4 AND item_id = ? and price_type>0", uid, itemId) +func OrderListCountByItemId(sess *xorm.Session, uid string, itemId, skuId string, day int) (int64, error) { + + sess = sess.Where(" uid = ? AND state != 4 AND (item_id = ? or (num_item_id=? and num_item_id!='')) and price_type>0", uid, itemId, skuId) if day > 0 { today := utils.GetTimeRange("today") sess = sess.And("create_at>=?", today["end"]-int64(day*86400)) diff --git a/app/db/dbs_map.go b/app/db/dbs_map.go index ceb1e16..bc2472d 100644 --- a/app/db/dbs_map.go +++ b/app/db/dbs_map.go @@ -110,8 +110,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). - Or("db_master_id = ?", 32053480).Find(&m) + err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id=?", 1, 123456).Find(&m) } else { err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m) } diff --git a/consume/init.go b/consume/init.go index 8eaa96f..a6c4300 100644 --- a/consume/init.go +++ b/consume/init.go @@ -60,6 +60,7 @@ func initConsumes() { jobs[consumeMd.ZhiosExpressOrderFail] = ZhiosExpressOrderFail jobs[consumeMd.ZhiosWithdrawReward] = ZhiosWithdrawReward jobs[consumeMd.ZhiosTaskTotal] = ZhiosTaskTotal + //jobs[consumeMd.ZhiosUserProfileInviteCode] = ZhiosUserProfileInviteCode // diff --git a/consume/md/consume_key.go b/consume/md/consume_key.go index a5473a5..84cafb4 100644 --- a/consume/md/consume_key.go +++ b/consume/md/consume_key.go @@ -290,6 +290,15 @@ var RabbitMqQueueKeyList = []*MqQueue{ BindKey: "", ConsumeFunName: "zhiosTaskTotal", }, + { + ExchangeName: "zhios.user_profile.exchange", + Name: "zhios_user_profile_invite_code", + Type: DirectQueueType, + IsPersistent: false, + RoutKey: "user_profile_invite_code", + BindKey: "", + ConsumeFunName: "ZhiosUserProfileInviteCode", + }, { ExchangeName: "zhios.user_valid.exchange", Name: "zhios_user_valid", @@ -584,5 +593,6 @@ const ( FlexibleEmploymentWithdrawForGongMaoConsumeFunName = "FlexibleEmploymentWithdrawForGongMaoConsume" FlexibleEmploymentWithdrawForPupiaoConsumeFunName = "FlexibleEmploymentWithdrawForPupiaoConsume" ZhiosTaskTotal = "zhiosTaskTotal" + ZhiosUserProfileInviteCode = "ZhiosUserProfileInviteCode" InstallmentPaymentAutoRepaidConsumeFunName = "InstallmentPaymentAutoRepaidConsume" ) diff --git a/consume/md/md.go b/consume/md/md.go index 61a9acd..6af4330 100644 --- a/consume/md/md.go +++ b/consume/md/md.go @@ -7,7 +7,10 @@ type ZhiosAcquisition struct { Mid string `json:"mid"` Id string `json:"id"` } - +type ZhiosUserProfile struct { + Uid string `json:"uid"` + Mid string `json:"mid"` +} type AcquisitionCfg struct { Id string `json:"id"` Status string `json:"status"` diff --git a/consume/zhios_order_free.go b/consume/zhios_order_free.go index 15c21a6..2e9c236 100644 --- a/consume/zhios_order_free.go +++ b/consume/zhios_order_free.go @@ -376,7 +376,7 @@ func orderFreeCheck(eg *xorm.Engine, sess *xorm.Session, canalMsg *md.ZhiosOrder } //判断当前栏目购买商品数量 并且开启了 if limitBuyConditions.OpenOnegoodsBuy == "1" && utils.StrToInt64(limitBuyConditions.OnegoodsBuyLimit) > 0 { - moreGoodsCount, _ := db.OrderListCountByItemId(sess, canalMsg.Uid, canalMsg.ItemId, utils.StrToInt(limitBuyConditions.OnegoodsBuyLimitDay)) + moreGoodsCount, _ := db.OrderListCountByItemId(sess, canalMsg.Uid, canalMsg.ItemId, canalMsg.SkuId, utils.StrToInt(limitBuyConditions.OnegoodsBuyLimitDay)) if moreGoodsCount+1 > utils.StrToInt64(limitBuyConditions.OnegoodsBuyLimit) { sess.Rollback() return nil diff --git a/consume/zhios_user_profile_invite_code.go b/consume/zhios_user_profile_invite_code.go new file mode 100644 index 0000000..593c291 --- /dev/null +++ b/consume/zhios_user_profile_invite_code.go @@ -0,0 +1,173 @@ +package consume + +import ( + "applet/app/db" + "applet/app/utils/logx" + "applet/consume/md" + "applet/mall/utils" + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" + "encoding/json" + "errors" + "fmt" + "github.com/streadway/amqp" + "math/rand" + "unicode" + "xorm.io/xorm" +) + +func ZhiosUserProfileInviteCode(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 = UserProfileInviteCode(res.Body) + //_ = res.Reject(false) + if err == nil { + _ = res.Ack(true) + //TODO::重新推回队列末尾,避免造成队列堵塞 + var msg *md.ZhiosUserProfile + 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 { + panic(errors.New("error getting message")) + } + } + fmt.Println("get msg done") +} + +func UserProfileInviteCode(msg []byte) error { + //1、解析canal采集至mq中queue的数据结构体 + var canalMsg *md.ZhiosUserProfile + fmt.Println(string(msg)) + var tmpString string + err := json.Unmarshal(msg, &tmpString) + if err != nil { + fmt.Println(err.Error()) + return err + } + fmt.Println(tmpString) + err = json.Unmarshal([]byte(tmpString), &canalMsg) + if err != nil { + return err + } + mid := canalMsg.Mid + eg := db.DBs[mid] + if eg == nil { + return nil + } + userProfile, _ := db.UserProfileFindByID(eg, canalMsg.Uid) + if userProfile == nil { + return nil + } + if userProfile.InviteCode != "" { + return nil + } + inviteData := db.SysCfgFindWithDb(eg, mid, "app_invite_length", "app_invite_type") + l := utils.StrToInt(inviteData["app_invite_length"]) + types := utils.StrToInt(inviteData["app_invite_type"]) + code := returnCode(eg, l, types, 0) + if code == "" { + return errors.New("继续") + } + userProfile.InviteCode = code + fmt.Println(userProfile) + _, err = db.UserProfileUpdate(eg, userProfile.Uid, userProfile, "invite_code") + if err != nil { + return nil + } + return nil +} +func IsLetter(s string) bool { + for _, r := range s { + if !unicode.IsLetter(r) { + return false + } + } + return true +} + +func GetRandomString(l, isLetter int) string { + str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + if isLetter != 1 { + str = "0123456789" + } + strs := []rune(str) + result := make([]rune, l) + for i := range result { + result[i] = strs[rand.Intn(len(strs))] + } + if IsLetter(string(result)) && isLetter == 1 { + return GetRandomString(l, isLetter) + } + return string(result) +} + +func returnCode(eg *xorm.Engine, l, types, num int) string { + if num > 5 { + return "" + } + //循环3次判断是否存在该邀请码 + var code string + var ( + codes []string + ) + for i := 0; i < 3; i++ { + oneCode := GetRandomString(l, types) + codes = append(codes, oneCode) + } + + //判断是不是存在邀请码了 + tmp, _ := db.UserProfileFindByInviteCodes(eg, codes...) + //判断自定义是不是存在邀请码了 + customTmp, _ := db.UserProfileFindByCustomInviteCodes(eg, codes...) + //循环生成的邀请码 判断tmp里有没有这个邀请码 如果邀请码没有就赋值 再判断是否存在 存在就清空 + for _, v := range codes { + if code != "" { //如果存在并且数据库没有就跳过 + continue + } + code = v + for _, v1 := range *tmp { + //如果存在就清空 + if v1.InviteCode == v { + code = "" + } + } + for _, v1 := range *customTmp { + //如果存在就清空 + if v1.CustomInviteCode == v { + code = "" + } + } + } + //如果都没有就继续加一位继续查 + if code == "" { + return returnCode(eg, l+1, types, num+1) + } + return code +} diff --git a/go.mod b/go.mod index b7ff074..2d2d74b 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/syyongx/php2go v0.9.8 github.com/tidwall/gjson v1.14.1 go.uber.org/zap v1.16.0 - google.golang.org/grpc v1.32.0 + google.golang.org/grpc v1.33.2 google.golang.org/protobuf v1.33.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v2 v2.4.0 @@ -98,7 +98,7 @@ require ( golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.6.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect - google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect + google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect honnef.co/go/tools v0.0.1-2020.1.4 // indirect xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect