IotCard.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package Property
  2. import (
  3. "git.baozhida.cn/ERP_libs/lib"
  4. orm2 "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. "ERP_storage/logs"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. // 仓库
  12. type IotCard struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_iccid string `orm:"size(256);null"` // 编号
  15. T_type string `orm:"size(20);default(4G)"` // 类型
  16. T_sn string `orm:"size(256);null"` // 关联sn
  17. T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1-未使用 2-已使用 3-已作废
  18. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  19. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  20. }
  21. func (t *IotCard) TableName() string {
  22. return "iot_card" // 数据库名称 // ************** 替换 FormulaList **************
  23. }
  24. type IotCardDaoImpl struct {
  25. orm orm.Ormer
  26. }
  27. func NewIotCard(orm *orm.Ormer) *IotCardDaoImpl {
  28. return &IotCardDaoImpl{orm: *orm}
  29. }
  30. func init() {
  31. //注册模型
  32. orm.RegisterModel(new(IotCard))
  33. }
  34. type IotCard_R struct {
  35. Id int
  36. T_iccid string
  37. T_type string
  38. T_sn string
  39. T_State int
  40. }
  41. func IotCardToIotCard_R(t IotCard) (r IotCard_R) {
  42. r.Id = t.Id
  43. r.T_iccid = t.T_iccid
  44. r.T_type = t.T_type
  45. r.T_sn = t.T_sn
  46. r.T_State = t.T_State
  47. return r
  48. }
  49. // 添加
  50. func (dao *IotCardDaoImpl) Add_IotCard(r IotCard) (id int64, err error) {
  51. id, err = dao.orm.Insert(&r)
  52. if err != nil {
  53. logs.Error(lib.FuncName(), err)
  54. }
  55. return id, err
  56. }
  57. func (dao *IotCardDaoImpl) AddOrUpdate_IotCard(r IotCard) (id int64, err error) {
  58. qs := dao.orm.QueryTable(new(IotCard))
  59. var iotCard IotCard
  60. err = qs.Filter("T_iccid", r.T_iccid).Filter("T_State__gt", 0).One(&iotCard)
  61. if err != nil {
  62. if err.Error() == orm.ErrNoRows.Error() {
  63. id, err = dao.orm.Insert(&r)
  64. if err != nil {
  65. logs.Error(lib.FuncName(), err)
  66. }
  67. return
  68. } else {
  69. logs.Error(lib.FuncName(), err)
  70. return
  71. }
  72. }
  73. iotCard.T_State = 2
  74. iotCard.T_sn = r.T_sn
  75. _, err = dao.orm.Update(&iotCard, "T_State", "T_sn")
  76. if err != nil {
  77. logs.Error(lib.FuncName(), err)
  78. return
  79. }
  80. return
  81. }
  82. // 获取 ById
  83. func Read_IotCard_ByT_iccid(T_iccid string) (r IotCard, err error) {
  84. o := orm.NewOrm()
  85. qs := o.QueryTable(new(IotCard))
  86. err = qs.Filter("T_iccid", T_iccid).Filter("T_State__gt", 0).One(&r)
  87. if err != nil {
  88. logs.Error(lib.FuncName(), err)
  89. }
  90. return
  91. }
  92. func Read_IotCard_ById(Id int) (r IotCard, err error) {
  93. o := orm.NewOrm()
  94. qs := o.QueryTable(new(IotCard))
  95. err = qs.Filter("Id", Id).One(&r)
  96. if err != nil {
  97. logs.Error(lib.FuncName(), err)
  98. }
  99. return
  100. }
  101. // 修改
  102. func (dao *IotCardDaoImpl) Update_IotCard(m IotCard, cols ...string) error {
  103. _, err := dao.orm.Update(&m, cols...)
  104. if err != nil {
  105. logs.Error(lib.FuncName(), err)
  106. return err
  107. }
  108. return nil
  109. }
  110. // 删除
  111. func (dao *IotCardDaoImpl) Delete_IotCard(v IotCard) error {
  112. v.T_State = 0
  113. _, err := dao.orm.Update(&v, "T_State")
  114. if err != nil {
  115. logs.Error(lib.FuncName(), err)
  116. }
  117. return err
  118. }
  119. // 获取列表
  120. func Read_IotCard_List(T_name string, T_state, page, page_z int) (r_ []IotCard_R, cnt int64) {
  121. o := orm.NewOrm()
  122. // 也可以直接使用 Model 结构体作为表名
  123. qs := o.QueryTable(new(IotCard))
  124. var offset int64
  125. if page <= 1 {
  126. offset = 0
  127. } else {
  128. offset = int64((page - 1) * page_z)
  129. }
  130. // 过滤
  131. cond := orm.NewCondition()
  132. cond1 := cond.And("T_State__gt", 0)
  133. if T_state > 0 {
  134. cond1 = cond1.And("T_State", T_state)
  135. }
  136. if len(T_name) > 0 {
  137. cond1 = cond1.AndCond(cond.Or("T_iccid__icontains", T_name).Or("T_sn__icontains", T_name))
  138. }
  139. // 查询
  140. var r []IotCard
  141. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  142. if err != nil {
  143. logs.Error(lib.FuncName(), err)
  144. return
  145. }
  146. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  147. if err != nil {
  148. logs.Error(lib.FuncName(), err)
  149. return
  150. }
  151. for _, v := range r {
  152. r_ = append(r_, IotCardToIotCard_R(v))
  153. }
  154. return r_, cnt
  155. }