From a61c19fb15be312e783c2afd028e992565f9158a Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Fri, 9 Dec 2022 11:20:30 +0800 Subject: [PATCH] test --- db/db.go | 9 ++++ db/db_day_luck_draw_order_jackpot.go | 58 ++++++++++++++++++++++++++ db/db_day_luck_draw_period.go | 30 +++++++++++++ db/db_day_luck_draw_period_reward.go | 24 +++++++++++ db/db_day_luck_draw_setting.go | 15 ++++++- db/model/day_luck_draw_setting.go | 1 + md/cfg_app.go | 16 +++++++ md/day_luck_draw_order_jackpot.go | 10 +++++ md/day_luck_draw_period.go | 6 +++ md/day_luck_draw_period_reward.go | 7 ++++ md/day_luck_draw_setting.go | 7 ++++ svc/svc_day_luck_draw_order_jackpot.go | 28 +++++++++++++ svc/svc_day_luck_draw_reward.go | 58 ++++++++++++++++++++++++++ 13 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 db/db.go create mode 100644 db/db_day_luck_draw_order_jackpot.go create mode 100644 db/db_day_luck_draw_period.go create mode 100644 md/cfg_app.go create mode 100644 md/day_luck_draw_order_jackpot.go create mode 100644 md/day_luck_draw_period.go create mode 100644 md/day_luck_draw_setting.go create mode 100644 svc/svc_day_luck_draw_order_jackpot.go create mode 100644 svc/svc_day_luck_draw_reward.go diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..431dbb6 --- /dev/null +++ b/db/db.go @@ -0,0 +1,9 @@ +package db + +import "xorm.io/xorm" + +// QueryNativeString 查询原生sql +func QueryNativeStringSess(sess *xorm.Session, sql string, args ...interface{}) ([]map[string]string, error) { + results, err := sess.SQL(sql, args...).QueryString() + return results, err +} diff --git a/db/db_day_luck_draw_order_jackpot.go b/db/db_day_luck_draw_order_jackpot.go new file mode 100644 index 0000000..9dbb115 --- /dev/null +++ b/db/db_day_luck_draw_order_jackpot.go @@ -0,0 +1,58 @@ +package db + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/db/model" + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/md" + zhios_day_luck_draw_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/utils" + "errors" + "time" + "xorm.io/xorm" +) + +func AddOrderJackpot(eg *xorm.Engine, req md.DayLuckDrawOrderJackpotReq) error { + count, _ := eg.Where("oid=? and uid=? and pvd=?", req.Oid, req.Uid, req.Pvd).Count(&model.DayLuckDrawOrderJackpot{}) + if count > 0 { + return nil + } + var data = &model.DayLuckDrawOrderJackpot{ + Oid: req.Oid, + Pvd: req.Pvd, + Amount: req.Amount, + Commission: req.Commission, + Reward: req.Reward, + Uid: zhios_day_luck_draw_utils.StrToInt(req.Uid), + CreateTime: time.Now(), + } + one, err := eg.InsertOne(data) + if one == 0 { + return errors.New("失败") + } + if err != nil { + return err + } + return nil +} + +/*** +1.创建期数 +2.获取还没分配的金额 就是 期数为0的 +3.把期数设置到订单 +4.查出所有订单号 +5.随机出订单号(每个名额随机一次 如果重复再次循环) 先按一等奖分 +*/ + +func GetAllOrderSum(sess *xorm.Session) float64 { + sum, _ := sess.Where("period=0").Sum(&model.DayLuckDrawOrderJackpot{}, "reward") + return sum +} +func GetAllOrderOid(sess *xorm.Session) []string { + sql := `SELECT oid,uid FROM day_luck_draw_order_jackpot where period=0;` + data, _ := QueryNativeStringSess(sess, sql) + var list = make([]string, 0) + if len(data) > 0 { + for _, v := range data { + list = append(list, v["uid"]) + } + } + return list +} diff --git a/db/db_day_luck_draw_period.go b/db/db_day_luck_draw_period.go new file mode 100644 index 0000000..87d5495 --- /dev/null +++ b/db/db_day_luck_draw_period.go @@ -0,0 +1,30 @@ +package db + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/db/model" + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/md" + zhios_day_luck_draw_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/utils" + "time" + "xorm.io/xorm" +) + +func AddPeriod(sess *xorm.Session, req md.DayLuckDrawPeriodReq) bool { + var data = &model.DayLuckDrawPeriod{ + Reward: req.Reward, + CreateTime: time.Now(), + Period: zhios_day_luck_draw_utils.StrToInt(req.Period), + } + insert, err := sess.Insert(data) + if insert == 0 || err != nil { + return false + } + return true +} +func GetPeriodMax(sess *xorm.Session) *model.DayLuckDrawPeriod { + var data model.DayLuckDrawPeriod + get, err := sess.Desc("period").Get(&data) + if get == false || err != nil { + return nil + } + return &data +} diff --git a/db/db_day_luck_draw_period_reward.go b/db/db_day_luck_draw_period_reward.go index b1a3c20..a02f599 100644 --- a/db/db_day_luck_draw_period_reward.go +++ b/db/db_day_luck_draw_period_reward.go @@ -4,9 +4,33 @@ import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/db/model" "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/md" zhios_day_luck_draw_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/utils" + "errors" + "time" "xorm.io/xorm" ) +func AddPeriodReward(eg *xorm.Engine, req md.DayLuckDrawRewardReq) error { + count, _ := eg.Where("uid=? and period=?", req.Uid, req.Period).Count(&model.DayLuckDrawPeriodReward{}) + if count > 0 { + return nil + } + var data = &model.DayLuckDrawPeriodReward{ + Reward: req.Reward, + Uid: zhios_day_luck_draw_utils.StrToInt(req.Uid), + CreateTime: time.Now(), + Period: zhios_day_luck_draw_utils.StrToInt(req.Period), + Lv: zhios_day_luck_draw_utils.StrToInt(req.Lv), + } + one, err := eg.InsertOne(data) + if one == 0 { + return errors.New("失败") + } + if err != nil { + return err + } + return nil +} + func GetDayLuckDrawPeriodRewardList(Db *xorm.Engine, parameter *md.DayLuckDrawPeriodRewardRequest) ([]*model.DayLuckDrawPeriodReward, int, error) { var list []*model.DayLuckDrawPeriodReward count := DayLuckDrawPeriodRewardGetCount(Db, parameter) diff --git a/db/db_day_luck_draw_setting.go b/db/db_day_luck_draw_setting.go index 18e4fe9..6ee2e09 100644 --- a/db/db_day_luck_draw_setting.go +++ b/db/db_day_luck_draw_setting.go @@ -19,7 +19,20 @@ func GetDayLuckDrawSetting(eg *xorm.Engine) *model.DayLuckDrawSetting { } return &data } - +func GetDayLuckDrawSettingSess(sess *xorm.Session) *model.DayLuckDrawSetting { + var data model.DayLuckDrawSetting + get, err := sess.Get(&data) + if err != nil { + return nil + } + if get == false { + one, err := sess.InsertOne(&data) + if one == 0 || err != nil { + return nil + } + } + return &data +} func InsertDayLuckDrawSetting(eg *xorm.Engine, data *model.DayLuckDrawSetting) bool { if data.Id == 0 { one, err := eg.InsertOne(data) diff --git a/db/model/day_luck_draw_setting.go b/db/model/day_luck_draw_setting.go index 9e6aac1..c2b83f3 100644 --- a/db/model/day_luck_draw_setting.go +++ b/db/model/day_luck_draw_setting.go @@ -16,5 +16,6 @@ type DayLuckDrawSetting struct { CycleDay int `json:"cycle_day" xorm:"default 0 comment('周期') INT(11)"` AdvUse int `json:"adv_use" xorm:"INT(11)"` AdvType string `json:"adv_type" xorm:"VARCHAR(255)"` + Amount string `json:"amount" xorm:"VARCHAR(255)"` RewardData string `json:"reward_data" xorm:"TEXT"` } diff --git a/md/cfg_app.go b/md/cfg_app.go new file mode 100644 index 0000000..c380303 --- /dev/null +++ b/md/cfg_app.go @@ -0,0 +1,16 @@ +package md + +import "time" + +//数据库配置结构体 +type DBCfg struct { + Host string `yaml:"host"` //ip及端口 + Name string `yaml:"name"` //库名 + User string `yaml:"user"` //用户 + Psw string `yaml:"psw"` //密码 + ShowLog bool `yaml:"show_log"` //是否显示SQL语句 + MaxLifetime time.Duration `yaml:"max_lifetime"` + MaxOpenConns int `yaml:"max_open_conns"` + MaxIdleConns int `yaml:"max_idle_conns"` + Path string `yaml:"path"` //日志文件存放路径 +} diff --git a/md/day_luck_draw_order_jackpot.go b/md/day_luck_draw_order_jackpot.go new file mode 100644 index 0000000..5394038 --- /dev/null +++ b/md/day_luck_draw_order_jackpot.go @@ -0,0 +1,10 @@ +package md + +type DayLuckDrawOrderJackpotReq struct { + Uid string `json:"uid"` + Oid string `json:"oid" ` + Pvd string `json:"pvd"` + Amount string `json:"amount"` + Commission string `json:"commission" ` + Reward string `json:"reward" ` +} diff --git a/md/day_luck_draw_period.go b/md/day_luck_draw_period.go new file mode 100644 index 0000000..69e33a3 --- /dev/null +++ b/md/day_luck_draw_period.go @@ -0,0 +1,6 @@ +package md + +type DayLuckDrawPeriodReq struct { + Reward string `json:"reward"` + Period string `json:"period" ` +} diff --git a/md/day_luck_draw_period_reward.go b/md/day_luck_draw_period_reward.go index 50492b8..27b54b5 100644 --- a/md/day_luck_draw_period_reward.go +++ b/md/day_luck_draw_period_reward.go @@ -17,3 +17,10 @@ type DayLuckDrawPeriodRewardOrdList struct { Period string `json:"period"` CreateTime string `json:"create_time"` } + +type DayLuckDrawRewardReq struct { + Uid string `json:"uid"` + Period string `json:"period"` + Reward string `json:"reward" ` + Lv string `json:"lv"` +} diff --git a/md/day_luck_draw_setting.go b/md/day_luck_draw_setting.go new file mode 100644 index 0000000..65ffecc --- /dev/null +++ b/md/day_luck_draw_setting.go @@ -0,0 +1,7 @@ +package md + +type RewardData struct { + Key string `json:"key"` + Bili string `json:"bili"` + Num string `json:"num"` +} diff --git a/svc/svc_day_luck_draw_order_jackpot.go b/svc/svc_day_luck_draw_order_jackpot.go new file mode 100644 index 0000000..6ea52fa --- /dev/null +++ b/svc/svc_day_luck_draw_order_jackpot.go @@ -0,0 +1,28 @@ +package svc + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/md" + zhios_day_luck_draw_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/utils" + "time" + "xorm.io/xorm" +) + +func AddOrderJackpot(eg *xorm.Engine, req md.DayLuckDrawOrderJackpotReq) error { + setting := db.GetDayLuckDrawSetting(eg) + if setting == nil { + return nil + } + now := time.Now().Unix() + if setting.IsUse == 0 || setting.StartTime.IsZero() || setting.EndTime.IsZero() || (now > setting.StartTime.Unix() && now < setting.EndTime.Unix()) { + return nil + } + if setting.OrderSoruce == "mall" && zhios_day_luck_draw_utils.InArr(req.Pvd, []string{"mall_goods", "mall_group_buy", "mall_goods_user_lv", "super_group_buy", "mall_supply", "mall_group_own_buy"}) == false { + return nil + } + err := db.AddOrderJackpot(eg, req) + if err != nil { + return err + } + return nil +} diff --git a/svc/svc_day_luck_draw_reward.go b/svc/svc_day_luck_draw_reward.go new file mode 100644 index 0000000..144a247 --- /dev/null +++ b/svc/svc_day_luck_draw_reward.go @@ -0,0 +1,58 @@ +package svc + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/md" + zhios_day_luck_draw_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_day_luck_draw.git/utils" + "encoding/json" + "math/rand" + "time" + "xorm.io/xorm" +) + +//随机几个用户 +func RandUser(sess *xorm.Session) []string { + var newOid = make([]string, 0) + + setting := db.GetDayLuckDrawSettingSess(sess) + if setting == nil { + return newOid + } + now := time.Now().Unix() + if setting.IsUse == 0 || setting.StartTime.IsZero() || setting.EndTime.IsZero() || (now > setting.StartTime.Unix() && now < setting.EndTime.Unix()) { + return newOid + } + count := 0 + var rewardData = make([]md.RewardData, 0) + err := json.Unmarshal([]byte(setting.RewardData), &rewardData) + if err == nil { + for _, v := range rewardData { + count += zhios_day_luck_draw_utils.StrToInt(v.Num) + } + } + if count > 0 { + oids := db.GetAllOrderOid(sess) + newOid = MicsSlice(oids, count) + return newOid + } + return newOid + +} +func MicsSlice(origin []string, count int) []string { + tmpOrigin := make([]string, len(origin)) + copy(tmpOrigin, origin) + //一定要seed + rand.Seed(time.Now().Unix()) + rand.Shuffle(len(tmpOrigin), func(i int, j int) { + tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i] + }) + + result := make([]string, 0, count) + for index, value := range tmpOrigin { + if index == count { + break + } + result = append(result, value) + } + return result +}