Commit 0ad91afc authored by hujiebin's avatar hujiebin

feat: 处理并发

parent 898a0b58
...@@ -22,6 +22,12 @@ func (ub *UserNoble) UpdateEndTime(db *gorm.DB, endTime time.Time) (int64, error ...@@ -22,6 +22,12 @@ func (ub *UserNoble) UpdateEndTime(db *gorm.DB, endTime time.Time) (int64, error
return r.RowsAffected, r.Error return r.RowsAffected, r.Error
} }
// 延长贵族
func (ub *UserNoble) ExtenedEndTime(db *gorm.DB, day int) (int64, error) {
r := db.Model(&UserNoble{}).Where(ub).Update("end_time", gorm.Expr("DATE_ADD(end_time,INTERVAL ? day)", day))
return r.RowsAffected, r.Error
}
// 查询用户未过期的贵族 // 查询用户未过期的贵族
func (ub *UserNoble) Find(db *gorm.DB) ([]UserNoble, error) { func (ub *UserNoble) Find(db *gorm.DB) ([]UserNoble, error) {
rows := make([]UserNoble, 0) rows := make([]UserNoble, 0)
......
...@@ -41,7 +41,7 @@ func ats32(a interface{}) string { ...@@ -41,7 +41,7 @@ func ats32(a interface{}) string {
} }
func main() { func main() {
url := "https://apiv2.faceline.live/v1/fruitMachine/rank/award?lang=zh-cn&pageIndex=1&pageSize=5000&beginDate=2023-08-30&endDate=2023-08-30&userCode=&timezone=1" url := "https://apiv2.faceline.live/v1/fruitMachine/rank/award?lang=zh-cn&pageIndex=1&pageSize=5000&beginDate=2023-08-31&endDate=2023-08-31&userCode=&timezone=1"
method := "GET" method := "GET"
client := &http.Client{} client := &http.Client{}
...@@ -95,7 +95,7 @@ func main() { ...@@ -95,7 +95,7 @@ func main() {
for _, v := range svips { for _, v := range svips {
svipMap[v.Code] = v.Level svipMap[v.Code] = v.Level
} }
excelFileName := fmt.Sprintf("./0830水果机中奖榜.xlsx") excelFileName := fmt.Sprintf("./0831水果机中奖榜.xlsx")
xlFile := xlsx.NewFile() xlFile := xlsx.NewFile()
sheet, _ := xlFile.AddSheet("charge") sheet, _ := xlFile.AddSheet("charge")
row := sheet.AddRow() row := sheet.AddRow()
......
...@@ -21,7 +21,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days ...@@ -21,7 +21,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days
return fmt.Errorf("level is 0") return fmt.Errorf("level is 0")
} }
cfg, err := noble_m.GetConfigByLevel(model.Db, level) cfg, err := noble_m.GetConfigByLevel(model.DB(), level)
if err != nil { if err != nil {
return err return err
} }
...@@ -29,7 +29,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days ...@@ -29,7 +29,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days
return fmt.Errorf("level is 0") return fmt.Errorf("level is 0")
} }
n := noble_m.UserNoble{UserId: receiverUserId, Level: level} n := noble_m.UserNoble{UserId: receiverUserId, Level: level}
records, err := n.FindAll(model.Db) records, err := n.FindAll(model.DB())
if err != nil { if err != nil {
return err return err
} }
...@@ -42,7 +42,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days ...@@ -42,7 +42,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days
// 新增贵族 // 新增贵族
endTime := time.Now().AddDate(0, 0, int(days)) endTime := time.Now().AddDate(0, 0, int(days))
n = noble_m.UserNoble{UserId: receiverUserId, Level: level, EndTime: endTime} n = noble_m.UserNoble{UserId: receiverUserId, Level: level, EndTime: endTime}
err = n.Create(model.Db) err = n.Create(model.DB())
if err != nil { if err != nil {
return err return err
} }
...@@ -55,7 +55,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days ...@@ -55,7 +55,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days
SrcType: noble_m.SRC_APP, SrcType: noble_m.SRC_APP,
NewEndTime: endTime, NewEndTime: endTime,
} }
err = nbl.Create(model.Db) err = nbl.Create(model.DB())
if err != nil { if err != nil {
return err return err
} }
...@@ -71,12 +71,16 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days ...@@ -71,12 +71,16 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days
Entity: mysql.Entity{ID: n.ID, UpdatedTime: n.UpdatedTime}, Entity: mysql.Entity{ID: n.ID, UpdatedTime: n.UpdatedTime},
} }
af, err := nn.UpdateEndTime(model.Db, endTime) af, err := nn.UpdateEndTime(model.DB(), endTime)
if err != nil { if err != nil {
return err return err
} }
if af <= 0 { if af <= 0 {
return fmt.Errorf("bizerr.TransactionFailed") if af2, err := nn.ExtenedEndTime(model.DB(), days); err != nil {
return err
} else if af2 <= 0 {
return fmt.Errorf("bizerr.TransactionFailed rows 0")
}
} }
nbl := noble_m.UserNobleLog{ nbl := noble_m.UserNobleLog{
...@@ -88,7 +92,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days ...@@ -88,7 +92,7 @@ func SendNoble(model *domain.Model, receiverUserId mysql.ID, level uint16, days
OldEndTime: n.EndTime, OldEndTime: n.EndTime,
NewEndTime: endTime, NewEndTime: endTime,
} }
err = nbl.Create(model.Db) err = nbl.Create(model.DB())
if err != nil { if err != nil {
return err return err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment