/** * @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" "strings" "time" ) var goodsCache *cache2go.CacheTable func init() { goodsCache = cache2go.Cache("goods") goodsCache.SetDataLoader(func(key interface{}, param ...interface{}) *cache2go.CacheItem { if v, ok := key.(string); ok { m := Goods{GOODSCODE: v} if d, err := m.GetData(); err == nil { if len(d) > 0 { val := d[0] item := cache2go.NewCacheItem(val.Key(), 0, val) return item } } } return nil }) // 延时5秒 加载所有商品 go func() { time.Sleep(time.Second * 5) loadData() }() } func loadData() { m := Goods{} if d, err := m.GetData(); err == nil { for _, v := range d { goodsCache.Add(v.Key(), 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) Key() string { return r.GOODSCODE } func (r *Goods) calc() { } func (r *Goods) buildSql() string { var sqlId utils.SQLVal = ` select t.goodsid, upper(t.goodscode) 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) if r.GOODSCODE != "" { // 不区分大小写加载商品 sqlId.JoinFormat(" and upper(t.goodscode) = '%v'", strings.ToUpper(r.GOODSCODE)) } return sqlId.String() } // GetDataEx 获取商品信息 func (r *Goods) GetData() ([]Goods, error) { sData := make([]Goods, 0) e := db.GetEngine() if e == nil { return sData, nil } err := e.SQL(r.buildSql()).Find(&sData) for i := range sData { sData[i].calc() } return sData, err } // GetGoods 获取商品信息 func GetGoods(goodsCode string) (Goods, bool) { // 缓存是用大写的goodscode, 查找也转成大写的来查找 goodsCode2 := strings.ToUpper(goodsCode) if item, err := goodsCache.Value(goodsCode2); err == nil { d := item.Data() if v, ok := d.(Goods); ok { return v, true } else if v2, ok2 := d.(*Goods); ok2 { return *v2, true } } return Goods{}, false } // GetGoodsDecimalplace 获取商品小数位 func GetGoodsDecimalplace(goodsCode string) int { if d, ok := GetGoods(strings.ToUpper(goodsCode)); ok { return int(d.DECIMALPLACE) } if d, ok := GetGoods(goodsCode); ok { return int(d.DECIMALPLACE) } return 0 }