InfoTemplate.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package InfoCollection
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. _ "github.com/go-sql-driver/mysql"
  8. "time"
  9. )
  10. // 模板
  11. type InfoTemplate struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_class int `orm:"size(8);default(0)"` // 分类
  14. T_InfoTemplate_id string `orm:"size(256);null"` // id
  15. T_name string `orm:"size(256);null"` // 标题
  16. T_sort int `orm:"size(2);default(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. type InfoTemplate_R struct {
  21. T_InfoTemplate_id string `orm:"size(256);null"` // id
  22. T_name string `orm:"size(256);null"` // 标题
  23. T_sort int `orm:"size(200);null"` // 分类
  24. }
  25. func (t *InfoTemplate) TableName() string {
  26. return "info_template" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. func init() {
  29. //注册模型
  30. orm.RegisterModel(new(InfoTemplate))
  31. }
  32. // -------------------------------------------------------------
  33. func InfoTemplateToInfoTemplate_R(T InfoTemplate) (T_r InfoTemplate_R) {
  34. T_r.T_InfoTemplate_id = T.T_InfoTemplate_id
  35. T_r.T_name = T.T_name
  36. T_r.T_sort = T.T_sort
  37. return T_r
  38. }
  39. func Read_InfoTemplate_List_ALL() (maps []InfoTemplate) {
  40. o := orm.NewOrm()
  41. qs := o.QueryTable(new(InfoTemplate))
  42. qs.OrderBy("Id").All(&maps)
  43. return maps
  44. }
  45. func InfoTemplateListToMap(A []InfoTemplate) map[string]string {
  46. maps := make(map[string]string, len(A))
  47. for _, v := range A {
  48. maps[v.T_InfoTemplate_id] = v.T_name
  49. }
  50. return maps
  51. }
  52. // ---------------- 特殊方法 -------------------
  53. // 获取 ById
  54. func Read_InfoTemplate_ById(id int) (r InfoTemplate, is bool) {
  55. o := orm.NewOrm()
  56. r = InfoTemplate{Id: id}
  57. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  58. if err != nil {
  59. logs.Error(lib.FuncName(), err)
  60. return r, false
  61. }
  62. return r, true
  63. }
  64. // 获取 By
  65. func Read_InfoTemplate(T_InfoTemplate_id string) (r InfoTemplate, is bool) {
  66. o := orm.NewOrm()
  67. qs := o.QueryTable(new(InfoTemplate))
  68. err := qs.Filter("T_InfoTemplate_id", T_InfoTemplate_id).One(&r)
  69. if err != nil {
  70. return r, false
  71. }
  72. return r, true
  73. }
  74. func Read_InfoTemplate_T_name(T_name string) (r InfoTemplate, is bool) {
  75. o := orm.NewOrm()
  76. qs := o.QueryTable(new(InfoTemplate))
  77. err := qs.Filter("T_name", T_name).One(&r)
  78. if err != nil {
  79. return r, false
  80. }
  81. return r, true
  82. }
  83. // 添加
  84. func Add_InfoTemplate(r InfoTemplate) (T_InfoTemplate_id string, is bool) {
  85. o := orm.NewOrm()
  86. // 生成编号
  87. rand_x := 0
  88. for true {
  89. r.T_InfoTemplate_id = lib.GetRandstring(4, "", int64(rand_x)) // 1,336,336
  90. err := o.Read(&r, "T_InfoTemplate_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  91. if err != nil {
  92. break
  93. }
  94. rand_x += 1
  95. }
  96. _, err := o.Insert(&r)
  97. if err != nil {
  98. logs.Error(lib.FuncName(), err)
  99. return "", false
  100. }
  101. return r.T_InfoTemplate_id, true
  102. }
  103. // 删除
  104. func Delete_InfoTemplate(v InfoTemplate) bool {
  105. o := orm.NewOrm()
  106. if num, err := o.Delete(&v); err == nil {
  107. logs.Println("Number of records deleted in database:", num)
  108. } else {
  109. return false
  110. }
  111. return true
  112. }
  113. // 修改
  114. func Update_InfoTemplate(m InfoTemplate, cols ...string) bool {
  115. o := orm.NewOrm()
  116. if num, err := o.Update(&m, cols...); err == nil {
  117. logs.Println("Number of records updated in database:", num)
  118. return true
  119. }
  120. return false
  121. }
  122. // 获取列表
  123. func Read_InfoTemplate_List(T_class int, T_name string, page int, page_z int) ([]InfoTemplate_R, int64) {
  124. o := orm.NewOrm()
  125. // 也可以直接使用 Model 结构体作为表名
  126. var r []InfoTemplate
  127. qs := o.QueryTable(new(InfoTemplate))
  128. var offset int64
  129. if page <= 1 {
  130. offset = 0
  131. } else {
  132. offset = int64((page - 1) * page_z)
  133. }
  134. cond := orm.NewCondition()
  135. cond1 := cond.And("T_class", T_class).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  136. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_name").All(&r)
  137. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  138. // 转换
  139. var InfoTemplate_r []InfoTemplate_R
  140. for _, v := range r {
  141. InfoTemplate_r = append(InfoTemplate_r, InfoTemplateToInfoTemplate_R(v))
  142. }
  143. return InfoTemplate_r, cnt
  144. }