/** * @Author: zou.yingbin * @Create : 2021/10/27 14:20 * @Modify : 2021/10/27 14:20 * @note : 商品信息缓存 */ package mtpcache import ( "github.com/muesli/cache2go" "mtp2_if/db" "mtp2_if/utils" "time" ) var goodsCache *cache2go.CacheTable func init() { goodsCache = cache2go.Cache("goods") goodsCache.SetDataLoader(func(key interface{}, param ...interface{}) *cache2go.CacheItem { if v, ok := key.(int64); ok { m := Goods{GOODSID: v} if d, err := m.GetData(); err == nil { if len(d) > 0 { val := d[0] item := cache2go.NewCacheItem(val.GOODSID, 0, val) return item } } } return nil }) // go loadData() } func loadData() { time.Sleep(time.Second * 5) m := Goods{} if d, err := m.GetData(); err == nil { for _, v := range d { goodsCache.Add(v.GOODSID, 0, &v) } } } // Goods 商品信息 type Goods struct { GOODSID int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID(自增ID SEQ_GOODS) GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码(内部) GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称 MARKETID string `json:"marketid" xorm:"'MARKETID'"` // 所属市场ID GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 所属商品组ID GOODSSTATUS int32 `json:"goodsstatus" xorm:"'GOODSSTATUS'"` // 商品状态- 1:待审核 2:未上市 3:上市 4:已注销 5:审核拒绝 6:退市 7:待退市 CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数 DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位 QTYDECIMALPLACE int32 `json:"qtydecimalplace" xorm:"'QTYDECIMALPLACE'"` // 成交量小数位 GOODSCURRENCYID int32 `json:"goodscurrencyid" xorm:"'GOODSCURRENCYID'"` // 合约货币ID REFGOODSID int64 `json:"refgoodsid" xorm:"'REFGOODSID'"` // 参考商品ID REFGOODSCODE string `json:"refgoodscode" xorm:"'REFGOODSCODE'"` // 参考商品代码 } func (r *Goods) calc() { } func (r *Goods) buildSql() string { var sqlId utils.SQLVal = ` select t.goodsid, t.goodscode, t.goodsname, t.marketid, t.goodsgroupid, t.goodsstatus, t.currencyid, t.goodunitid, t.agreeunit, t.decimalplace, t.qtydecimalplace, t.goodscurrencyid, t.goodsquotetype, t.refgoodsid, t.refgoodscode from goods t where 1=1 ` sqlId.AndEx("t.goodsid", r.GOODSID, r.GOODSID > 0) sqlId.AndEx("t.goodscode", r.GOODSCODE, r.GOODSCODE != "") return sqlId.String() } // GetDataEx 获取商品信息 func (r *Goods) GetData() ([]Goods, error) { sData := make([]Goods, 0) err := db.GetEngine().SQL(r.buildSql()).Find(&sData) for i := range sData { sData[i].calc() } return sData, err }