假设我有
reply1
、reply2
、reply3
三个表,根据不同的user_id,比如5940f98fb8e88447b81fd7
,如何通过取模操作向特定表插入数据?同一个用户的数据只会插入一张表,这样三张表都会有一个用户的数据
func (d *db) QueryTable(ReplyNo string) string {
tableNo := crc32.ChecksumIEEE([]byte(ReplyNo)) % 3
if tableNo == 0 {
return "reply1"
} else if tableID == 1 {
return "reply2"
} else {
return "reply3"
}
}
func (d *db) InsertRecord(m *model, tx *dbr.Tx) error {
_, err := tx.InsertInto(d.QueryTable(m.ReplyNo)).Columns(util.AttrToUnderscore(m)...).Record(m).Exec()
return err
}
func (m *Reply) CreateReply(c *Context) {
UserID := c.Get("uid")
ReplayNo := pkg.GenerUUID()
err = m.db.InsertRecord(&model{
ReplayNo: ReplayNo,
Replyer: UserID,
}, tx)
if err != nil {
tx.Rollback()
c.ResponseError(errors.New("Send reply failed"))
return
}
err = tx.Commit()
if err != nil {
tx.Rollback()
c.ResponseError(errors.New("Commit failed"))
return
}
c.JSON(http.StatusOK)
}