coinstream.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "fmt"
  6. "go-common/app/service/live/wallet/model"
  7. "go-common/library/log"
  8. "time"
  9. )
  10. const (
  11. _newCoinStremaRecord = "INSERT INTO %s (uid, transaction_id, extend_tid, coin_type, delta_coin_num, org_coin_num, op_result, op_reason, op_type, op_time, reserved2, area, source, reserved3, reserved4,platform,reserved1, reserved5) values(?, ?, ?,?, ?, ?,?, ?, ?,?, ?, ?,?, ?, ?, ?, ?, ?)"
  12. _getCoinStream = "SELECT uid,transaction_id,extend_tid, coin_type,delta_coin_num,org_coin_num,op_result,op_reason,op_type, reserved3,area,reserved2,source,reserved4,platform,reserved1,reserved5 FROM %s WHERE transaction_id = ? order by id desc limit %d,1"
  13. _getCoinStreamByUid = "SELECT uid,transaction_id,extend_tid, coin_type,delta_coin_num,org_coin_num,op_result,op_reason,op_type, reserved3,area,reserved2,source,reserved4, platform , reserved1,reserved5 FROM %s WHERE transaction_id = ? and uid = ? order by id desc limit %d,1"
  14. )
  15. func hexdec(s string) uint64 {
  16. d := uint64(0)
  17. for i := 0; i < len(s); i++ {
  18. x := uint64(s[i])
  19. if x >= 'a' {
  20. x -= 'a' - 'A'
  21. }
  22. d1 := x - '0'
  23. if d1 > 9 {
  24. d1 = 10 + d1 - ('A' - '0')
  25. }
  26. if 0 > d1 || d1 > 15 {
  27. return 0
  28. }
  29. d = (16 * d) + d1
  30. }
  31. return d
  32. }
  33. func getCoinStreamTable(transactionId string) string {
  34. tlen := len(transactionId)
  35. year := hexdec(transactionId[tlen-3 : tlen-1])
  36. if year == 0 {
  37. log.Error("illegal tid : %s", transactionId)
  38. }
  39. year = year + 2000
  40. t := fmt.Sprintf("t_coin_stream_%d%02d", year, hexdec(transactionId[tlen-1:tlen]))
  41. return t
  42. }
  43. func GetTid(serviceType model.ServiceType, v interface{}) string {
  44. s := fmt.Sprintf("%v%s", v, randomString(5))
  45. bizTid := fmt.Sprintf("%x", md5.Sum([]byte(s)))
  46. now := time.Now()
  47. year := now.Year() - 2000
  48. month := int(now.Month())
  49. return fmt.Sprintf("%s%x%02x%x", bizTid, serviceType, year, month)
  50. }
  51. func (d *Dao) NewCoinStreamRecord(c context.Context, record *model.CoinStreamRecord) (int64, error) {
  52. s := fmt.Sprintf(_newCoinStremaRecord, getCoinStreamTable(record.TransactionId))
  53. date := model.GetWalletFormatTime(record.OpTime)
  54. return execSqlWithBindParams(d, c, &s, record.Uid, record.TransactionId, record.ExtendTid, record.CoinType,
  55. record.DeltaCoinNum, record.OrgCoinNum, record.OpResult, record.OpReason, record.OpType, date,
  56. record.BizCode, record.Area, record.Source, record.MetaData, record.BizSource,
  57. record.Platform, record.Reserved1, record.Version,
  58. )
  59. }
  60. func (d *Dao) GetCoinStreamByTid(c context.Context, tid string) (record *model.CoinStreamRecord, err error) {
  61. return d.GetCoinStreamByTidAndOffset(c, tid, 0)
  62. }
  63. func (d *Dao) GetCoinStreamByTidAndOffset(c context.Context, tid string, offset int) (record *model.CoinStreamRecord, err error) {
  64. s := fmt.Sprintf(_getCoinStream, getCoinStreamTable(tid), offset)
  65. row := d.db.QueryRow(c, s, tid)
  66. record = &model.CoinStreamRecord{}
  67. err = row.Scan(&record.Uid, &record.TransactionId, &record.ExtendTid, &record.CoinType, &record.DeltaCoinNum,
  68. &record.OrgCoinNum, &record.OpResult, &record.OpReason, &record.OpType, &record.MetaData, &record.Area,
  69. &record.BizCode, &record.Source, &record.BizSource, &record.Platform, &record.Reserved1, &record.Version)
  70. return
  71. }
  72. func (d *Dao) GetCoinStreamByUidTid(c context.Context, uid int64, tid string) (record *model.CoinStreamRecord, err error) {
  73. s := fmt.Sprintf(_getCoinStreamByUid, getCoinStreamTable(tid), 0)
  74. row := d.db.QueryRow(c, s, tid, uid)
  75. record = &model.CoinStreamRecord{}
  76. err = row.Scan(&record.Uid, &record.TransactionId, &record.ExtendTid, &record.CoinType, &record.DeltaCoinNum,
  77. &record.OrgCoinNum, &record.OpResult, &record.OpReason, &record.OpType, &record.MetaData, &record.Area,
  78. &record.BizCode, &record.Source, &record.BizSource, &record.Platform, &record.Reserved1, &record.Version)
  79. return
  80. }