goods.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/10/27 14:20
  4. * @Modify : 2021/10/27 14:20
  5. * @note : 商品信息缓存
  6. */
  7. package mtpcache
  8. import (
  9. "github.com/muesli/cache2go"
  10. "mtp2_if/db"
  11. "mtp2_if/utils"
  12. "time"
  13. )
  14. var goodsCache *cache2go.CacheTable
  15. func init() {
  16. goodsCache = cache2go.Cache("goods")
  17. goodsCache.SetDataLoader(func(key interface{}, param ...interface{}) *cache2go.CacheItem {
  18. if v, ok := key.(string); ok {
  19. m := Goods{GOODSCODE: v}
  20. if d, err := m.GetData(); err == nil {
  21. if len(d) > 0 {
  22. val := d[0]
  23. item := cache2go.NewCacheItem(val.Key(), 0, val)
  24. return item
  25. }
  26. }
  27. }
  28. return nil
  29. })
  30. // 延时5秒 加载所有商品
  31. go func() {
  32. time.Sleep(time.Second * 5)
  33. loadData()
  34. }()
  35. }
  36. func loadData() {
  37. m := Goods{}
  38. if d, err := m.GetData(); err == nil {
  39. for _, v := range d {
  40. goodsCache.Add(v.Key(), 0, &v)
  41. }
  42. }
  43. }
  44. // Goods 商品信息
  45. type Goods struct {
  46. GOODSID int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID(自增ID SEQ_GOODS)
  47. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码(内部)
  48. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  49. MARKETID string `json:"marketid" xorm:"'MARKETID'"` // 所属市场ID
  50. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 所属商品组ID
  51. GOODSSTATUS int32 `json:"goodsstatus" xorm:"'GOODSSTATUS'"` // 商品状态- 1:待审核 2:未上市 3:上市 4:已注销 5:审核拒绝 6:退市 7:待退市
  52. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  53. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  54. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  55. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  56. QTYDECIMALPLACE int32 `json:"qtydecimalplace" xorm:"'QTYDECIMALPLACE'"` // 成交量小数位
  57. GOODSCURRENCYID int32 `json:"goodscurrencyid" xorm:"'GOODSCURRENCYID'"` // 合约货币ID
  58. REFGOODSID int64 `json:"refgoodsid" xorm:"'REFGOODSID'"` // 参考商品ID
  59. REFGOODSCODE string `json:"refgoodscode" xorm:"'REFGOODSCODE'"` // 参考商品代码
  60. }
  61. func (r *Goods) Key() string {
  62. return r.GOODSCODE
  63. }
  64. func (r *Goods) calc() {
  65. }
  66. func (r *Goods) buildSql() string {
  67. var sqlId utils.SQLVal = `
  68. select t.goodsid,
  69. t.goodscode,
  70. t.goodsname,
  71. t.marketid,
  72. t.goodsgroupid,
  73. t.goodsstatus,
  74. t.currencyid,
  75. t.goodunitid,
  76. t.agreeunit,
  77. t.decimalplace,
  78. t.qtydecimalplace,
  79. t.goodscurrencyid,
  80. t.goodsquotetype,
  81. t.refgoodsid,
  82. t.refgoodscode
  83. from goods t
  84. where 1=1
  85. `
  86. sqlId.AndEx("t.goodsid", r.GOODSID, r.GOODSID > 0)
  87. sqlId.AndEx("t.goodscode", r.GOODSCODE, r.GOODSCODE != "")
  88. return sqlId.String()
  89. }
  90. // GetDataEx 获取商品信息
  91. func (r *Goods) GetData() ([]Goods, error) {
  92. sData := make([]Goods, 0)
  93. e := db.GetEngine()
  94. if e == nil {
  95. return sData, nil
  96. }
  97. err := e.SQL(r.buildSql()).Find(&sData)
  98. for i := range sData {
  99. sData[i].calc()
  100. }
  101. return sData, err
  102. }
  103. // GetGoods 获取商品信息
  104. func GetGoods(goodsCode string) (Goods, bool) {
  105. if item, err := goodsCache.Value(goodsCode); err == nil {
  106. d := item.Data()
  107. if v, ok := d.(Goods); ok {
  108. return v, true
  109. }
  110. }
  111. return Goods{}, false
  112. }
  113. // GetGoodsDecimalplace 获取商品小数位
  114. func GetGoodsDecimalplace(goodsCode string) int {
  115. if d, ok := GetGoods(goodsCode); ok {
  116. return int(d.DECIMALPLACE)
  117. }
  118. return 0
  119. }