CertificatePdf.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package Certificate
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "fmt"
  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. "time"
  10. )
  11. type CertificatePdf struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_Certificate_sn string `orm:"size(256);null"` // 证书编号
  14. T_release_time string `orm:"column(t_release_time);type(timestamp);null;"` // 发布时间
  15. T_failure_time string `orm:"column(t_failure_time);type(timestamp);null;"` // 失效时间
  16. T_pdf string `orm:"size(256);null"` // pdf链接
  17. T_State int `orm:"size(200);default(1)"` // 0删除 1 正常
  18. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  19. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  20. }
  21. type CertificatePdf_R struct {
  22. Id int
  23. T_Certificate_sn string // 证书编号
  24. T_release_time string // 发布时间
  25. T_failure_time string // 失效时间
  26. T_pdf string // pdf链接
  27. }
  28. func (t *CertificatePdf) TableName() string {
  29. return "certificate_pdf" // 数据库名称 // ************** 替换 FormulaList **************
  30. }
  31. func init() {
  32. //注册模型
  33. orm.RegisterModel(new(CertificatePdf))
  34. }
  35. // -------------------------------------------------------------
  36. func CertificatePdfToCertificatePdf_R(T CertificatePdf) (T_r CertificatePdf_R) {
  37. T_r.Id = T.Id
  38. T_r.T_Certificate_sn = T.T_Certificate_sn
  39. T_r.T_release_time = T.T_release_time
  40. T_r.T_failure_time = T.T_failure_time
  41. T_r.T_pdf = T.T_pdf
  42. return T_r
  43. }
  44. // ----------------
  45. // 获取 ById
  46. func Read_CertificatePdf_ById(id int) (r CertificatePdf, is bool) {
  47. o := orm.NewOrm()
  48. r = CertificatePdf{Id: id}
  49. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  50. if err != nil {
  51. logs.Error(lib.FuncName(), err)
  52. return r, false
  53. }
  54. return r, true
  55. }
  56. // 获取 By
  57. func Read_CertificatePdf(T_Certificate_sn, T_release_time, T_failure_time string) (r CertificatePdf, err error) {
  58. o := orm.NewOrm()
  59. qs := o.QueryTable(new(CertificatePdf))
  60. err = qs.Filter("T_Certificate_sn", T_Certificate_sn).Filter("T_release_time", T_release_time).Filter("T_failure_time", T_failure_time).Filter("T_State", 1).One(&r)
  61. if err != nil {
  62. logs.Println(lib.FuncName(), err)
  63. }
  64. return r, err
  65. }
  66. // 获取最新的校准证书
  67. func Read_CertificatePdf_Newest(T_Certificate_sn string) (r []CertificatePdf, err error) {
  68. o := orm.NewOrm()
  69. qs := o.QueryTable(new(CertificatePdf))
  70. _, err = qs.Limit(1, 0).Filter("T_Certificate_sn", T_Certificate_sn).Filter("T_State", 1).OrderBy("-T_failure_time").All(&r)
  71. if err != nil {
  72. logs.Println(lib.FuncName(), err)
  73. }
  74. return r, err
  75. }
  76. // 添加
  77. func Add_CertificatePdf(r CertificatePdf) (id int64, is bool) {
  78. o := orm.NewOrm()
  79. err := o.Read(&r, "T_Certificate_sn", "T_release_time", "T_failure_time", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  80. if err == nil {
  81. return 0, false
  82. }
  83. id, err = o.Insert(&r)
  84. if err != nil {
  85. logs.Println(lib.FuncName(), err)
  86. return 0, false
  87. }
  88. return id, true
  89. }
  90. // 删除
  91. func Delete_CertificatePdf(v CertificatePdf) bool {
  92. o := orm.NewOrm()
  93. if num, err := o.Delete(&v); err == nil {
  94. logs.Println("Number of records deleted in database:", num)
  95. } else {
  96. logs.Println(lib.FuncName(), err)
  97. return false
  98. }
  99. return true
  100. }
  101. // 删除
  102. func Delete_CertificatePdf_(v CertificatePdf) bool {
  103. o := orm.NewOrm()
  104. v.T_State = 0
  105. if num, err := o.Update(&v, "T_State"); err == nil {
  106. fmt.Println("Number of records updated in database:", num)
  107. } else {
  108. logs.Println(lib.FuncName(), err)
  109. return false
  110. }
  111. return true
  112. }
  113. // 修改
  114. func Update_CertificatePdf(m CertificatePdf, cols ...string) bool {
  115. o := orm.NewOrm()
  116. if num, err := o.Update(&m, cols...); err == nil {
  117. fmt.Println("Number of records updated in database:", num)
  118. return true
  119. } else {
  120. logs.Println(lib.FuncName(), err)
  121. }
  122. return false
  123. }
  124. // 获取列表
  125. func Read_CertificatePdf_List(T_Certificate_sn string, page int, page_z int) ([]CertificatePdf_R, int64) {
  126. o := orm.NewOrm()
  127. // 也可以直接使用 Model 结构体作为表名
  128. var r []CertificatePdf
  129. qs := o.QueryTable(new(CertificatePdf))
  130. var offset int64
  131. if page <= 1 {
  132. offset = 0
  133. } else {
  134. offset = int64((page - 1) * page_z)
  135. }
  136. cond := orm.NewCondition()
  137. cond1 := cond.And("T_Certificate_sn", T_Certificate_sn).And("T_State", 1)
  138. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  139. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  140. // 转换
  141. var CertificatePdf_r []CertificatePdf_R
  142. for _, v := range r {
  143. CertificatePdf_r = append(CertificatePdf_r, CertificatePdfToCertificatePdf_R(v))
  144. }
  145. return CertificatePdf_r, cnt
  146. }