Depot.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package Basic
  2. import (
  3. "ERP_storage/logs"
  4. "git.baozhida.cn/ERP_libs/lib"
  5. _ "github.com/astaxie/beego/cache/redis"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. _ "github.com/go-sql-driver/mysql"
  9. "sync"
  10. "time"
  11. )
  12. // 仓库
  13. type Depot struct {
  14. Id int `orm:"column(ID);size(11);auto;pk"`
  15. T_name string `orm:"size(256);null"` // 名称
  16. T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1 正常
  17. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  18. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  19. }
  20. func (t *Depot) TableName() string {
  21. return "depot" // 数据库名称 // ************** 替换 FormulaList **************
  22. }
  23. var Depot_list *sync.Map
  24. func init() {
  25. //注册模型
  26. orm.RegisterModel(new(Depot))
  27. Depot_list = new(sync.Map)
  28. }
  29. type Depot_R struct {
  30. Id int
  31. T_name string // 名称
  32. T_State int // 0 删除(伪删除) 1 正常
  33. }
  34. func DepotToDepot_R(t Depot) (r Depot_R) {
  35. r.Id = t.Id
  36. r.T_name = t.T_name
  37. r.T_State = t.T_State
  38. return r
  39. }
  40. // 添加
  41. func Add_Depot(r Depot) (id int64, err error) {
  42. o := orm.NewOrm()
  43. id, err = o.Insert(&r)
  44. if err != nil {
  45. logs.Error(lib.FuncName(), err)
  46. }
  47. return id, err
  48. }
  49. // 获取 ById
  50. func Read_Depot_ById(Id int) (r Depot, err error) {
  51. o := orm.NewOrm()
  52. qs := o.QueryTable(new(Depot))
  53. err = qs.Filter("Id", Id).One(&r)
  54. if err != nil {
  55. logs.Error(lib.FuncName(), err)
  56. }
  57. return
  58. }
  59. // 修改
  60. func Update_Depot(m Depot, cols ...string) error {
  61. o := orm.NewOrm()
  62. _, err := o.Update(&m, cols...)
  63. if err != nil {
  64. logs.Error(lib.FuncName(), err)
  65. return err
  66. }
  67. return nil
  68. }
  69. // 删除
  70. func Delete_Depot(v Depot) error {
  71. o := orm.NewOrm()
  72. v.T_State = 0
  73. _, err := o.Update(&v, "T_State")
  74. if err != nil {
  75. logs.Error(lib.FuncName(), err)
  76. }
  77. return err
  78. }
  79. // 获取列表
  80. func Read_Depot_List(T_name string, page, page_z int) (r_ []Depot_R, cnt int64) {
  81. o := orm.NewOrm()
  82. // 也可以直接使用 Model 结构体作为表名
  83. qs := o.QueryTable(new(Depot))
  84. var offset int64
  85. if page <= 1 {
  86. offset = 0
  87. } else {
  88. offset = int64((page - 1) * page_z)
  89. }
  90. // 过滤
  91. cond := orm.NewCondition()
  92. cond = cond.And("T_State", 1)
  93. if len(T_name) > 0 {
  94. cond = cond.And("T_name__icontains", T_name)
  95. }
  96. // 查询
  97. var r []Depot
  98. var err error
  99. if page_z == 9999 {
  100. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  101. } else {
  102. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  103. }
  104. if err != nil {
  105. logs.Error(lib.FuncName(), err)
  106. return
  107. }
  108. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  109. if err != nil {
  110. logs.Error(lib.FuncName(), err)
  111. return
  112. }
  113. for _, v := range r {
  114. r_ = append(r_, DepotToDepot_R(v))
  115. }
  116. return r_, cnt
  117. }
  118. // 获取全部
  119. func Read_Depot_All_Map() {
  120. o := orm.NewOrm()
  121. var r []Depot
  122. qs := o.QueryTable(new(Depot))
  123. _, err := qs.All(&r)
  124. if err != nil {
  125. logs.Error(lib.FuncName(), err)
  126. }
  127. for _, v := range r {
  128. Depot_list.Store(v.Id, v.T_name)
  129. }
  130. }
  131. func Read_Depot_Get(Id int) string {
  132. // 有先加入 给全部人发消息
  133. v, ok := Depot_list.Load(Id) /*如果确定是真实的,则存在,否则不存在 */
  134. if ok {
  135. return v.(string)
  136. } else {
  137. return "未知仓库"
  138. }
  139. }