InfoTemplate.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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"` // 标题
  15. T_name string `orm:"size(256);null"` // 标题
  16. T_sort int `orm:"size(2);default(1)"` // 排
  17. T_scheme string `orm:"size(256);null"` // 方案模板
  18. T_reporting string `orm:"size(256);null"` // 报告模板
  19. T_inspect string `orm:"size(256);null"` // 自检
  20. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  21. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  22. }
  23. type InfoTemplate_R struct {
  24. T_InfoTemplate_id string `orm:"size(256);null"` // id
  25. T_name string `orm:"size(256);null"` // 标题
  26. T_sort int `orm:"size(200);null"` // 分类
  27. }
  28. func (t *InfoTemplate) TableName() string {
  29. return "info_template" // 数据库名称 // ************** 替换 FormulaList **************
  30. }
  31. func init() {
  32. //注册模型
  33. orm.RegisterModel(new(InfoTemplate))
  34. }
  35. // -------------------------------------------------------------
  36. func InfoTemplateToInfoTemplate_R(T InfoTemplate) (T_r InfoTemplate_R) {
  37. T_r.T_InfoTemplate_id = T.T_InfoTemplate_id
  38. T_r.T_name = T.T_name
  39. T_r.T_sort = T.T_sort
  40. return T_r
  41. }
  42. // ---------------- 特殊方法 -------------------
  43. // 获取 ById
  44. func Read_InfoTemplate_ById(id int) (r InfoTemplate, is bool) {
  45. o := orm.NewOrm()
  46. r = InfoTemplate{Id: id}
  47. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  48. if err != nil {
  49. logs.Error(lib.FuncName(), err)
  50. return r, false
  51. }
  52. return r, true
  53. }
  54. // 获取 By
  55. func Read_InfoTemplate(T_InfoTemplate_id string) (r InfoTemplate, is bool) {
  56. o := orm.NewOrm()
  57. qs := o.QueryTable(new(InfoTemplate))
  58. err := qs.Filter("T_InfoTemplate_id", T_InfoTemplate_id).One(&r)
  59. if err != nil {
  60. return r, false
  61. }
  62. return r, true
  63. }
  64. func Read_InfoTemplate_T_name(T_name string) (r InfoTemplate, is bool) {
  65. o := orm.NewOrm()
  66. qs := o.QueryTable(new(InfoTemplate))
  67. err := qs.Filter("T_name", T_name).One(&r)
  68. if err != nil {
  69. return r, false
  70. }
  71. return r, true
  72. }
  73. // 添加
  74. func Add_InfoTemplate(r InfoTemplate) (T_InfoTemplate_id string, is bool) {
  75. o := orm.NewOrm()
  76. // 生成编号
  77. rand_x := 0
  78. for true {
  79. r.T_InfoTemplate_id = lib.GetRandstring(4, "", int64(rand_x)) // 1,336,336
  80. err := o.Read(&r, "T_InfoTemplate_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  81. if err != nil {
  82. break
  83. }
  84. rand_x += 1
  85. }
  86. _, err := o.Insert(&r)
  87. if err != nil {
  88. logs.Error(lib.FuncName(), err)
  89. return "", false
  90. }
  91. return r.T_InfoTemplate_id, true
  92. }
  93. // 删除
  94. func Delete_InfoTemplate(v InfoTemplate) bool {
  95. o := orm.NewOrm()
  96. if num, err := o.Delete(&v); err == nil {
  97. logs.Println("Number of records deleted in database:", num)
  98. } else {
  99. return false
  100. }
  101. return true
  102. }
  103. // 修改
  104. func Update_InfoTemplate(m InfoTemplate, cols ...string) bool {
  105. o := orm.NewOrm()
  106. if num, err := o.Update(&m, cols...); err == nil {
  107. logs.Println("Number of records updated in database:", num)
  108. return true
  109. }
  110. return false
  111. }
  112. // 获取列表
  113. func Read_InfoTemplate_List(T_class int, T_name string, page int, page_z int) ([]InfoTemplate_R, int64) {
  114. o := orm.NewOrm()
  115. // 也可以直接使用 Model 结构体作为表名
  116. var r []InfoTemplate
  117. qs := o.QueryTable(new(InfoTemplate))
  118. var offset int64
  119. if page <= 1 {
  120. offset = 0
  121. } else {
  122. offset = int64((page - 1) * page_z)
  123. }
  124. cond := orm.NewCondition()
  125. cond1 := cond.And("T_class", T_class).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  126. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_name").All(&r)
  127. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  128. // 转换
  129. var InfoTemplate_r []InfoTemplate_R
  130. for _, v := range r {
  131. InfoTemplate_r = append(InfoTemplate_r, InfoTemplateToInfoTemplate_R(v))
  132. }
  133. return InfoTemplate_r, cnt
  134. }