CertificatePdf.go 5.7 KB

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