Certificate.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type Certificate struct {
  14. Id int `orm:"column(ID);size(11);auto;pk"`
  15. T_sn string `orm:"size(256);null"` // 证书编号
  16. T_layout_no string `orm:"size(256);null"` // 布局编号
  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 Certificate_R struct {
  22. T_sn string `orm:"size(256);null"` // 编号
  23. }
  24. func (t *Certificate) TableName() string {
  25. return "certificate" // 数据库名称 // ************** 替换 FormulaList **************
  26. }
  27. func init() {
  28. //注册模型
  29. orm.RegisterModel(new(Certificate))
  30. }
  31. // ----------------
  32. // 获取 ById
  33. func Read_Certificate_ById(id int) (r Certificate, is bool) {
  34. o := orm.NewOrm()
  35. r = Certificate{Id: id}
  36. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  37. if err != nil {
  38. logs.Error(lib.FuncName(), err)
  39. return r, false
  40. }
  41. return r, true
  42. }
  43. // 获取 By
  44. func Read_Certificate(T_sn string) (r Certificate, is bool) {
  45. o := orm.NewOrm()
  46. qs := o.QueryTable(new(Certificate))
  47. err := qs.Filter("T_sn", T_sn).Filter("T_State", 1).One(&r)
  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_Certificate_ByT_layout_no(T_layout_no string) (r Certificate, is bool) {
  56. o := orm.NewOrm()
  57. qs := o.QueryTable(new(Certificate))
  58. err := qs.Filter("T_layout_no", T_layout_no).Filter("T_State", 1).One(&r)
  59. if err != nil {
  60. logs.Error(lib.FuncName(), err)
  61. return r, false
  62. }
  63. return r, true
  64. }
  65. // 添加
  66. func Add_Certificate(r Certificate) (id int64, is bool) {
  67. o := orm.NewOrm()
  68. id, err := o.Insert(&r)
  69. if err != nil {
  70. logs.Error(lib.FuncName(), err)
  71. return 0, false
  72. }
  73. return id, true
  74. }
  75. // 删除
  76. func Delete_Certificate(v Certificate) bool {
  77. o := orm.NewOrm()
  78. if num, err := o.Delete(&v); err == nil {
  79. logs.Println("Number of records deleted in database:", num)
  80. } else {
  81. logs.Error(lib.FuncName(), err)
  82. return false
  83. }
  84. return true
  85. }
  86. // 删除
  87. func Delete_Certificate_(v Certificate) bool {
  88. o := orm.NewOrm()
  89. v.T_State = 0
  90. if num, err := o.Update(&v, "T_State"); err == nil {
  91. fmt.Println("Number of records updated in database:", num)
  92. } else {
  93. logs.Error(lib.FuncName(), err)
  94. return false
  95. }
  96. return true
  97. }
  98. // 修改
  99. func Update_Certificate(m Certificate, cols ...string) bool {
  100. o := orm.NewOrm()
  101. if num, err := o.Update(&m, cols...); err == nil {
  102. fmt.Println("Number of records updated in database:", num)
  103. return true
  104. } else {
  105. logs.Error(lib.FuncName(), err)
  106. }
  107. return false
  108. }
  109. type Certificate_ struct {
  110. Id int `orm:"column(ID)"`
  111. T_sn string // 编号
  112. T_layout_no string // 布局编号
  113. T_release_time string `orm:"column(t_release_time)"` // 发布时间
  114. T_failure_time string `orm:"column(t_failure_time)"` // 失效时间
  115. T_pdf string // pdf链接
  116. }
  117. // 获取列表
  118. func Read_Certificate_List(T_sn, T_layout_no, Time_start, Time_end string, T_layout_no_sort, T_release_time_sort, T_failure_time_sort, page int, page_z int) ([]Certificate_, int) {
  119. o := orm.NewOrm()
  120. var offset int
  121. if page <= 1 {
  122. offset = 0
  123. } else {
  124. offset = (page - 1) * page_z
  125. }
  126. var maps_z []orm2.ParamsList
  127. var maps []Certificate_
  128. sqlWhere := ""
  129. if len(T_sn) > 0 {
  130. sqlWhere += " AND t_sn like \"%" + T_sn + "%\""
  131. }
  132. if len(T_layout_no) > 0 {
  133. sqlWhere += " AND t_layout_no like \"%" + T_layout_no + "%\""
  134. }
  135. if len(Time_start) > 0 {
  136. sqlWhere += " AND t_failure_time >= \"" + Time_start + "\""
  137. }
  138. if len(Time_end) > 0 {
  139. sqlWhere += " AND t_failure_time <= \"" + Time_end + "\""
  140. }
  141. // 获取总条数
  142. sql := "SELECT COUNT(ID) FROM certificate WHERE t__state=1"
  143. sql = sql + sqlWhere
  144. fmt.Println(sql)
  145. _, err := o.Raw(sql).ValuesList(&maps_z)
  146. if err != nil {
  147. return maps, 0
  148. }
  149. if len(maps_z) == 0 {
  150. return maps, 0
  151. }
  152. //sql = "SELECT * FROM Certificate AS c LEFT JOIN (SELECT any_value(t_release_time) t_release_time,any_value(t_failure_time) t_failure_time,any_value(t_pdf) t_pdf,t__certificate_sn FROM (SELECT DISTINCT * FROM `CertificatePdf` WHERE t__state=1 ORDER BY id DESC) f GROUP BY t__certificate_sn) AS pdf ON pdf.t__certificate_sn=c.t_sn WHERE t__state=1"
  153. sql = "SELECT * FROM certificate AS c LEFT JOIN (SELECT t_release_time,t_failure_time,t_pdf,t__certificate_sn FROM (SELECT DISTINCT * FROM `certificate_pdf` WHERE t__state=1 ORDER BY id DESC) f GROUP BY t__certificate_sn) AS pdf ON pdf.t__certificate_sn=c.t_sn WHERE t__state=1"
  154. sql = sql + sqlWhere
  155. sqlOrder := make([]string, 0)
  156. if T_layout_no_sort == 1 {
  157. sqlOrder = append(sqlOrder, "c.t_layout_no ASC")
  158. } else if T_layout_no_sort == 2 {
  159. sqlOrder = append(sqlOrder, "c.t_layout_no DESC")
  160. }
  161. if T_release_time_sort == 1 {
  162. sqlOrder = append(sqlOrder, "pdf.t_release_time ASC")
  163. } else if T_release_time_sort == 2 {
  164. sqlOrder = append(sqlOrder, "pdf.t_release_time DESC")
  165. }
  166. if T_failure_time_sort == 1 {
  167. sqlOrder = append(sqlOrder, "pdf.t_failure_time ASC")
  168. } else if T_failure_time_sort == 2 {
  169. sqlOrder = append(sqlOrder, "pdf.t_failure_time DESC")
  170. }
  171. if len(sqlOrder) > 0 {
  172. sql += " ORDER BY " + strings.Join(sqlOrder, ", ")
  173. }
  174. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  175. fmt.Println(sql)
  176. _, err = o.Raw(sql).QueryRows(&maps)
  177. cnt, _ := strconv.Atoi(maps_z[0][0].(string))
  178. return maps, cnt
  179. }