| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /**
- * @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,
- 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)
- 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) {
- if item, err := goodsCache.Value(goodsCode); 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(goodsCode); ok {
- return int(d.DECIMALPLACE)
- }
- if d, ok := GetGoods(strings.ToUpper(goodsCode)); ok {
- return int(d.DECIMALPLACE)
- }
- return 0
- }
|