123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package Basic
- import (
- "ERP_storage/logs"
- "git.baozhida.cn/ERP_libs/lib"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "sync"
- "time"
- )
- // 仓库
- type Depot struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_name string `orm:"size(256);null"` // 名称
- T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1 正常
- 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 *Depot) TableName() string {
- return "depot" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var Depot_list *sync.Map
- func init() {
- //注册模型
- orm.RegisterModel(new(Depot))
- Depot_list = new(sync.Map)
- }
- type Depot_R struct {
- Id int
- T_name string // 名称
- T_State int // 0 删除(伪删除) 1 正常
- }
- func DepotToDepot_R(t Depot) (r Depot_R) {
- r.Id = t.Id
- r.T_name = t.T_name
- r.T_State = t.T_State
- return r
- }
- // 添加
- func Add_Depot(r Depot) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return id, err
- }
- // 获取 ById
- func Read_Depot_ById(Id int) (r Depot, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Depot))
- err = qs.Filter("Id", Id).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // 修改
- func Update_Depot(m Depot, cols ...string) error {
- o := orm.NewOrm()
- _, err := o.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- return nil
- }
- // 删除
- func Delete_Depot(v Depot) error {
- o := orm.NewOrm()
- v.T_State = 0
- _, err := o.Update(&v, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return err
- }
- // 获取列表
- func Read_Depot_List(T_name string, page, page_z int) (r_ []Depot_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Depot))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- // 过滤
- cond := orm.NewCondition()
- cond = cond.And("T_State", 1)
- if len(T_name) > 0 {
- cond = cond.And("T_name__icontains", T_name)
- }
- // 查询
- var r []Depot
- var err error
- if page_z == 9999 {
- _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
- } else {
- _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").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_, DepotToDepot_R(v))
- }
- return r_, cnt
- }
- // 获取全部
- func Read_Depot_All_Map() {
- o := orm.NewOrm()
- var r []Depot
- qs := o.QueryTable(new(Depot))
- _, err := qs.All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- for _, v := range r {
- Depot_list.Store(v.Id, v.T_name)
- }
- }
- func Read_Depot_Get(Id int) string {
- // 有先加入 给全部人发消息
- v, ok := Depot_list.Load(Id) /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- return v.(string)
- } else {
- return "未知仓库"
- }
- }
|