CertificatePdf.go 5.7 KB

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