package dao import ( "context" "database/sql" "fmt" "strconv" "time" "go-common/app/job/main/coin/model" "go-common/library/log" ) const ( // SHARDING table shard. SHARDING = 50 // coin_settle_x _getSettle = "SELECT id,mid,aid,type,coin_count,exp_sub,state FROM coin_settle_%d WHERE id> ? LIMIT 10000" _hitSettlePeriod = "SELECT id,from_year,from_month,from_day,to_year,to_month,to_day FROM coin_settle_period WHERE from_year*10000+from_month*100+from_day<=? AND to_year*10000+to_month*100+to_day>?" _getSettlePeriod = "SELECT id,from_year,from_month,from_day,to_year,to_month,to_day FROM coin_settle_period WHERE id=?" _clearCoinCount = "UPDATE coin_settle_%d SET coin_count=0, mtime=?" _updateCoinCount = "UPDATE coin_settle_%d SET coin_count=?, mtime=? WHERE aid=? AND type=?" _updateSettle = "UPDATE coin_settle_%d SET state=1, exp_total=?, mtime=? WHERE id=?" _upsertSettle = "INSERT INTO coin_settle_%d (mid,aid,type,coin_count,ctime,mtime) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE coin_count=coin_count+?,state=0, mtime=?" // coin_archive_x _getTotalCoins = "SELECT aid,type,SUM(multiply) FROM coin_archive_%d WHERE timestamp>=? AND timestamp maxid { maxid = settle.ID } settles = append(settles, settle) } return } // UpsertSettle update coin settle. func (dao *Dao) UpsertSettle(c context.Context, tableID, mid, aid, tp, coinCount int64, now time.Time) (err error) { sqlStr := fmt.Sprintf(_upsertSettle, tableID) if _, err = dao.coinDB.Exec(c, sqlStr, mid, aid, tp, coinCount, now, now, coinCount, now); err != nil { log.Error("dao.coinDB.Exec(%s,%d,%d,%d,%v,%v,%d,%v) error(%v)", sqlStr, mid, aid, coinCount, now, now, coinCount, now, err) } return } // ClearCoinCount clear settle. func (dao *Dao) ClearCoinCount(c context.Context, tableID int64, now time.Time) (err error) { sqlStr := fmt.Sprintf(_clearCoinCount, tableID) if _, err = dao.coinDB.Exec(c, sqlStr, now); err != nil { log.Error("dao.coinDB.Exec(%s, %v) error(%v)", sqlStr, now, err) } return } // TotalCoins get total coins. func (dao *Dao) TotalCoins(c context.Context, id int, start, end time.Time) (coins map[int64]int64, err error) { rows, err := dao.getTotalCoinsStmt[id].Query(c, start.Unix(), end.Unix()) if err != nil { log.Error("dao.getTotalCoinsStmt[%d].Query(%v, %v) error(%v)", id, start, end, err) return } coins = make(map[int64]int64) defer rows.Close() for rows.Next() { var aid, tp, count int64 if err = rows.Scan(&aid, &tp, &count); err != nil { log.Error("rows.Scan error(%v)", err) return } coins[hashField(aid, tp)] = count } return }