附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_virtaul_coin_cfg.go 2.9 KiB

1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. "applet/app/utils/cache"
  7. "applet/app/utils/logx"
  8. "fmt"
  9. "reflect"
  10. "xorm.io/xorm"
  11. )
  12. func GetVirtualCoinList(eg *xorm.Engine) ([]*model.VirtualCoin, error) {
  13. var m []*model.VirtualCoin
  14. err := eg.Find(&m)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return m, nil
  19. }
  20. func GetVirtualCoinIsUseList(eg *xorm.Engine) ([]*model.VirtualCoin, error) {
  21. var m []*model.VirtualCoin
  22. err := eg.Where("is_use = ?", 1).Find(&m)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return m, nil
  27. }
  28. // VirtualCoinListInUse 查询正在使用中的虚拟币
  29. func VirtualCoinListInUse(Db *xorm.Engine, masterId string) ([]*model.VirtualCoin, error) {
  30. var m []*model.VirtualCoin
  31. cacheKey := fmt.Sprintf(md.VirtualCoinCfgCacheKey, masterId)
  32. err := cache.GetJson(cacheKey, &m)
  33. if err != nil || len(m) == 0 {
  34. err := Db.Where("is_use=1").Find(&m)
  35. if err != nil {
  36. return nil, err
  37. }
  38. cache.SetJson(cacheKey, m, md.CfgCacheTime)
  39. }
  40. return m, nil
  41. }
  42. // VirtualCoinGetOneByParams 通过参数查询数据(单条)
  43. func VirtualCoinGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.VirtualCoin, error) {
  44. var m model.VirtualCoin
  45. var query = fmt.Sprintf("%s =?", params["key"])
  46. if has, err := Db.Where(query, params["value"]).Get(&m); err != nil || has == false {
  47. return nil, logx.Error(err)
  48. }
  49. return &m, nil
  50. }
  51. // VirtualCoinFindByParams 通过传入的参数查询数据(多条)
  52. func VirtualCoinFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.VirtualCoin, error) {
  53. var m []model.VirtualCoin
  54. if params["key"] == nil {
  55. //查询全部数据
  56. err := Db.Find(&m)
  57. if err != nil {
  58. return nil, logx.Error(err)
  59. }
  60. return &m, nil
  61. } else {
  62. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  63. //指定In查询
  64. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
  65. return nil, logx.Warn(err)
  66. }
  67. return &m, nil
  68. } else {
  69. var query = fmt.Sprintf("%s =?", params["key"])
  70. err := Db.Where(query, params["value"]).Find(&m)
  71. if err != nil {
  72. return nil, logx.Error(err)
  73. }
  74. return &m, nil
  75. }
  76. }
  77. }
  78. func VirtualCoinGetOneById(Db *xorm.Engine, coinId string) (*model.VirtualCoin, error) {
  79. var m model.VirtualCoin
  80. if has, err := Db.ID(coinId).Get(&m); err != nil || has == false {
  81. return nil, logx.Error(err)
  82. }
  83. return &m, nil
  84. }
  85. func VirtualCoinOrderRelateDeleteByOid(Db *xorm.Engine, oid int64, pvd string) (int64, error) {
  86. _, err := Db.Where("oid=? and pvd=?", oid, pvd).Delete(model.VirtualCoinRelate{})
  87. if err != nil {
  88. return 0, err
  89. }
  90. return 1, nil
  91. }
  92. func VirtualCoinMapInUse(Db *xorm.Engine, masterId string) (map[string]model.VirtualCoin, error) {
  93. virtualCoinMap := make(map[string]model.VirtualCoin)
  94. listInUse, err := VirtualCoinListInUse(Db, masterId)
  95. if err != nil {
  96. return nil, err
  97. }
  98. for _, coin := range listInUse {
  99. virtualCoinMap[utils.AnyToString(coin.Id)] = *coin
  100. }
  101. return virtualCoinMap, nil
  102. }