enumdicitem.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/1/13 9:15
  4. * @Modify : 2021/1/13 9:15
  5. */
  6. package mtpcache
  7. import (
  8. "mtp2_if/db"
  9. "mtp2_if/logger"
  10. "mtp2_if/utils"
  11. "sync"
  12. "time"
  13. )
  14. var (
  15. vDicCurrency = enumdicitem{data: map[int32]string{}, nType: 2}
  16. vDic = enumdicitem{data: map[int32]string{}, nType: 1}
  17. vDicCardName = enumdicitem{data: map[int32]string{}, nType: 3}
  18. )
  19. // IGetEnumdicName 获取字典名称接口
  20. type IGetEnumdicName interface {
  21. get(int32) string
  22. load()
  23. }
  24. // 商品单位名称缓存
  25. type enumdicitem struct {
  26. mtx sync.RWMutex //锁
  27. data map[int32]string //数据[enumitemname]enumdicname
  28. last time.Time
  29. nType int32 // 1-单位名称 2-币种名称
  30. }
  31. type enumData struct {
  32. ENUMITEMNAME int32 `json:"enumitemname" xorm:"'enumitemname'"` //ID
  33. ENUMDICNAME string `json:"enumdicname" xorm:"'enumdicname'"` //名称
  34. }
  35. func (r *enumdicitem) buildSql() string {
  36. var sqlId utils.SQLVal = "select t.enumitemname, t.enumdicname" +
  37. " from enumdicitem t" +
  38. " where t.enumitemstatus = 1"
  39. sqlId.AndEx("t.enumdiccode", "goodsunit", r.nType == 1)
  40. sqlId.AndEx("t.enumdiccode", "currency", r.nType == 2)
  41. sqlId.AndEx("t.enumdiccode", "certificatetype", r.nType == 3)
  42. return sqlId.String()
  43. }
  44. func init() {
  45. go func() {
  46. time.Sleep(time.Second * 6)
  47. vDicCurrency.load()
  48. vDic.load()
  49. vDicCardName.load()
  50. }()
  51. }
  52. // 加载数据
  53. func (r *enumdicitem) load() {
  54. now := time.Now()
  55. // 间隔3秒以上才重新加载
  56. if d := now.Sub(r.last); d.Seconds() < 3 {
  57. return
  58. }
  59. r.mtx.Lock()
  60. defer r.mtx.Unlock()
  61. e := db.GetEngine()
  62. if e == nil {
  63. return
  64. }
  65. sData := make([]enumData, 0)
  66. s := e.SQL(r.buildSql())
  67. if err := s.Find(&sData); err != nil {
  68. logger.GetLogger().Errorf("cache load enumdicitem, %v", err)
  69. } else {
  70. r.data = make(map[int32]string)
  71. for i := range sData {
  72. r.data[sData[i].ENUMITEMNAME] = sData[i].ENUMDICNAME
  73. }
  74. r.last = time.Now()
  75. }
  76. }
  77. func (r *enumdicitem) get(id int32) string {
  78. r.mtx.RLock()
  79. defer r.mtx.RUnlock()
  80. if r.data == nil {
  81. return ""
  82. }
  83. if v, ok := r.data[id]; ok {
  84. return v
  85. }
  86. return ""
  87. }
  88. // getDicEnumName 获取字典名称
  89. func getDicEnumName(obj IGetEnumdicName, id int32) string {
  90. if id <= 0 {
  91. return ""
  92. }
  93. if v := obj.get(id); v != "" {
  94. return v
  95. } else {
  96. obj.load()
  97. }
  98. return obj.get(id)
  99. }
  100. // GetEnumDicitemName 获取商品单位枚举名称
  101. func GetEnumDicitemName(id int32) string {
  102. return getDicEnumName(&vDic, id)
  103. }
  104. // GetCurrencyName 获取币种名称
  105. func GetCurrencyName(id int32) string {
  106. return getDicEnumName(&vDicCurrency, id)
  107. }
  108. // GetCardName 获取卡类型名称
  109. func GetCardName(cardId int32) string {
  110. return getDicEnumName(&vDicCardName, cardId)
  111. }