package Stock import ( "ERP_storage/models/Basic" "errors" "fmt" "git.baozhida.cn/ERP_libs/lib" orm2 "github.com/beego/beego/v2/client/orm" "strconv" "sync" "time" "ERP_storage/logs" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" _ "github.com/go-sql-driver/mysql" ) // 库存 type Stock struct { Id int `orm:"column(ID);size(11);auto;pk"` T_depot_id int `orm:"size(20);null"` // 仓库id T_product_id int `orm:"size(20);null"` // 产品id T_product_class int `orm:"size(20);null"` // 产品分类 T_product_name string `orm:"size(256);null"` // 产品名称 T_product_model string `orm:"size(256);null"` // 产品型号 T_total int `orm:"size(20);null"` // 库存数量 T_reality int `orm:"size(20);null"` // 盘点数量 T_sort int `orm:"size(20);null"` // 排序 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } func (t *Stock) TableName() string { return "stock" // 数据库名称 // ************** 替换 FormulaList ************** } type StockDaoImpl struct { orm orm.Ormer } func NewStock(orm orm.Ormer) *StockDaoImpl { return &StockDaoImpl{orm: orm} } var Stock_list *sync.Map func init() { //注册模型 orm.RegisterModel(new(Stock)) Stock_list = new(sync.Map) } type Stock_R struct { Id int T_depot_id int // 仓库id T_total int // 库存数量 T_reality int // 实际库存数量 T_sort int // 排序 // ---------产品信息----------- T_product_id int T_product_name string T_product_class int T_product_class_name string T_product_model string T_product_spec string T_product_relation_sn int T_product_img string } func StockToStock_R(t Stock) (r Stock_R) { r.Id = t.Id r.T_depot_id = t.T_depot_id r.T_total = t.T_total r.T_reality = t.T_reality r.T_sort = t.T_sort r.T_product_id = t.T_product_id r.T_product_class = t.T_product_class product, _ := Basic.Read_Product_ById(t.T_product_id) r.T_product_name = product.T_name r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class) r.T_product_model = product.T_model r.T_product_spec = product.T_spec r.T_product_relation_sn = product.T_relation_sn r.T_product_img = product.T_img return r } // 添加 func (dao *StockDaoImpl) Add_Stock(r Stock) (id int64, err error) { id, err = dao.orm.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } // T_type 1-出库 2-入库 func (dao *StockDaoImpl) AddOrUpdate_Stock(T_depot_id, T_product_id, T_product_class int, T_product_name, T_product_model string, T_num, T_type int) (id int64, err error) { qs := dao.orm.QueryTable(new(Stock)) var stock Stock err = qs.Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).One(&stock) if err != nil { if err.Error() == orm.ErrNoRows.Error() { r := Stock{ T_depot_id: T_depot_id, T_product_id: T_product_id, T_product_name: T_product_name, T_product_class: T_product_class, T_product_model: T_product_model, T_total: T_num, T_reality: T_num, } id, err = dao.orm.Insert(&r) if err != nil { err = errors.New("添加库存失败") logs.Error(lib.FuncName(), err) } return } else { err = errors.New("查询库存失败") logs.Error(lib.FuncName(), err) return } } // T_type 1-出库 2-入库 if T_type == 1 { stock.T_total -= T_num stock.T_reality -= T_num } if stock.T_total < 0 { err = errors.New(fmt.Sprintf("%s%s库存不足", T_product_name, T_product_model)) logs.Error(lib.FuncName(), err.Error()) return } if T_type == 2 { stock.T_total += T_num stock.T_reality += T_num } _, err = dao.orm.Update(&stock, "T_total", "T_reality") if err != nil { err = errors.New(fmt.Sprintf("修改库存失败", T_product_name, T_product_model)) logs.Error(lib.FuncName(), err) return } return } // 获取 ById func (dao *StockDaoImpl) Read_Stock_ByT_depot_id_T_product_id(T_depot_id, T_product_id int) (r Stock, err error) { qs := dao.orm.QueryTable(new(Stock)) err = qs.Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 同步库存产品信息 func (dao *StockDaoImpl) Read_Stock_List_ByT_product_id(T_product_id int) (r []Stock, err error) { qs := dao.orm.QueryTable(new(Stock)) _, err = qs.Filter("T_product_id", T_product_id).All(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 获取 ById func (dao *StockDaoImpl) Read_Stock_ById(Id int) (r Stock, err error) { o := orm.NewOrm() qs := o.QueryTable(new(Stock)) err = qs.Filter("Id", Id).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 修改 func (dao *StockDaoImpl) Update_Stock(m Stock, cols ...string) error { _, err := dao.orm.Update(&m, cols...) if err != nil { logs.Error(lib.FuncName(), err) return err } return nil } // 获取列表 func (dao *StockDaoImpl) Read_Stock_List(T_depot_id, T_product_class int, T_product_name, T_product_model string, page, page_z int) (r_ []Stock_R, cnt int64) { qs := dao.orm.QueryTable(new(Stock)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } // 过滤 cond := orm.NewCondition() if T_depot_id > 0 { cond = cond.And("T_depot_id", T_depot_id) } if T_product_class > 0 { cond = cond.And("T_product_class", T_product_class) } if len(T_product_name) > 0 { cond = cond.And("T_product_name__icontains", T_product_name) } if len(T_product_model) > 0 { cond = cond.And("T_product_model__icontains", T_product_model) } // 查询 var r []Stock var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort").All(&r) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("T_sort").All(&r) } if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { r_ = append(r_, StockToStock_R(v)) } return r_, cnt } // 获取仓库的货物总数量 func (dao *StockDaoImpl) Read_Stock_Count_ByT_depot_id(T_depot_id int) (cnt int64) { sql := "SELECT SUM(t_total) FROM stock WHERE t_depot_id = " + strconv.Itoa(T_depot_id) var pl_lists orm2.ParamsList _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists) if err != nil { logs.Error(lib.FuncName(), err) return } if pl_lists[0] == nil { return 0 } key, _ := strconv.ParseInt(pl_lists[0].(string), 10, 64) return key } // 获取全部 func Read_Stock_All_Map(T_depot_id int) { o := orm.NewOrm() var r []Stock qs := o.QueryTable(new(Stock)) _, err := qs.Filter("T_depot_id", T_depot_id).All(&r) if err != nil { logs.Error(lib.FuncName(), err) } for _, v := range r { key := fmt.Sprintf("%d-%d", v.T_depot_id, v.T_product_id) Stock_list.Store(key, v.T_reality) } } func Read_Stock_Get(T_depot_id, T_product_id int) int { // 有先加入 给全部人发消息 key := fmt.Sprintf("%d-%d", T_depot_id, T_product_id) v, ok := Stock_list.Load(key) /*如果确定是真实的,则存在,否则不存在 */ if ok { return v.(int) } else { return 0 } }