Certificate.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type Certificate struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_sn string `orm:"size(256);null"` // 证书编号
  15. T_layout_no string `orm:"size(256);null"` // 布局编号
  16. T_State int `orm:"size(200);default(1)"` // 0删除 1 正常
  17. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  18. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  19. }
  20. type Certificate_R struct {
  21. T_sn string `orm:"size(256);null"` // 编号
  22. }
  23. func (t *Certificate) TableName() string {
  24. return "certificate" // 数据库名称 // ************** 替换 FormulaList **************
  25. }
  26. func init() {
  27. //注册模型
  28. orm.RegisterModel(new(Certificate))
  29. }
  30. // ----------------
  31. // 获取 ById
  32. func Read_Certificate_ById(id int) (r Certificate, is bool) {
  33. o := orm.NewOrm()
  34. r = Certificate{Id: id}
  35. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  36. if err != nil {
  37. logs.Error(lib.FuncName(), err)
  38. return r, false
  39. }
  40. return r, true
  41. }
  42. // 获取 By
  43. func Read_Certificate(T_layout_no string) (r Certificate, is bool) {
  44. o := orm.NewOrm()
  45. qs := o.QueryTable(new(Certificate))
  46. err := qs.Filter("T_layout_no", T_layout_no).Filter("T_State", 1).One(&r)
  47. if err != nil {
  48. logs.Error(lib.FuncName(), err)
  49. return r, false
  50. }
  51. return r, true
  52. }
  53. // 获取 By
  54. func Read_Certificate_ByT_layout_no(T_layout_no string) (r Certificate, is bool) {
  55. o := orm.NewOrm()
  56. qs := o.QueryTable(new(Certificate))
  57. err := qs.Filter("T_layout_no", T_layout_no).Filter("T_State", 1).One(&r)
  58. if err != nil {
  59. logs.Error(lib.FuncName(), err)
  60. return r, false
  61. }
  62. return r, true
  63. }
  64. // 添加
  65. func Add_Certificate(r Certificate) (id int64, is bool) {
  66. o := orm.NewOrm()
  67. id, err := o.Insert(&r)
  68. if err != nil {
  69. logs.Error(lib.FuncName(), err)
  70. return 0, false
  71. }
  72. return id, true
  73. }
  74. // 删除
  75. func Delete_Certificate(v Certificate) bool {
  76. o := orm.NewOrm()
  77. if num, err := o.Delete(&v); err == nil {
  78. logs.Println("Number of records deleted in database:", num)
  79. } else {
  80. logs.Error(lib.FuncName(), err)
  81. return false
  82. }
  83. return true
  84. }
  85. // 删除
  86. func Delete_Certificate_(v Certificate) bool {
  87. o := orm.NewOrm()
  88. v.T_State = 0
  89. if num, err := o.Update(&v, "T_State"); err == nil {
  90. logs.Println("Number of records updated in database:", num)
  91. } else {
  92. logs.Error(lib.FuncName(), err)
  93. return false
  94. }
  95. return true
  96. }
  97. // 修改
  98. func Update_Certificate(m Certificate, cols ...string) bool {
  99. o := orm.NewOrm()
  100. if num, err := o.Update(&m, cols...); err == nil {
  101. logs.Println("Number of records updated in database:", num)
  102. return true
  103. } else {
  104. logs.Error(lib.FuncName(), err)
  105. }
  106. return false
  107. }
  108. type Certificate_ struct {
  109. Id int `orm:"column(ID)"`
  110. //T_sn string // 编号
  111. T_Certificate_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_Certificate_sn, T_layout_no, Time_start, Time_end string, T_release_time_sort, T_failure_time_sort, T_layout_no_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_Certificate_sn) > 0 {
  130. sqlWhere += " AND pdf.t__certificate_sn like \"%" + T_Certificate_sn + "%\""
  131. }
  132. if len(T_layout_no) > 0 {
  133. sqlWhere += " AND c.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 AS c LEFT JOIN (SELECT t_release_time,t_failure_time,t_pdf,t__certificate_sn,t_layout_no FROM (SELECT DISTINCT * FROM `certificate_pdf` WHERE t__state=1 ORDER BY id DESC) f GROUP BY t_layout_no) AS pdf ON pdf.t_layout_no=c.t_layout_no WHERE t__state=1"
  143. sql = sql + sqlWhere
  144. logs.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 *,c.t_layout_no as t_layout_no FROM certificate AS c LEFT JOIN (SELECT t_release_time,t_failure_time,t_pdf,t__certificate_sn,t_layout_no FROM (SELECT DISTINCT * FROM `certificate_pdf` WHERE t__state=1 ORDER BY id DESC) f GROUP BY t_layout_no) AS pdf ON pdf.t_layout_no=c.t_layout_no WHERE t__state=1"
  154. sql = sql + sqlWhere
  155. sqlOrder := make([]string, 0)
  156. if T_release_time_sort == 1 {
  157. sqlOrder = append(sqlOrder, "pdf.t_release_time ASC")
  158. } else if T_release_time_sort == 2 {
  159. sqlOrder = append(sqlOrder, "pdf.t_release_time DESC")
  160. }
  161. if T_failure_time_sort == 1 {
  162. sqlOrder = append(sqlOrder, "pdf.t_failure_time ASC")
  163. } else if T_failure_time_sort == 2 {
  164. sqlOrder = append(sqlOrder, "pdf.t_failure_time DESC")
  165. }
  166. if T_layout_no_sort == 1 {
  167. sqlOrder = append(sqlOrder, "c.t_layout_no ASC")
  168. } else if T_layout_no_sort == 2 {
  169. sqlOrder = append(sqlOrder, "c.t_layout_no DESC")
  170. }
  171. if len(sqlOrder) > 0 {
  172. sql += " ORDER BY " + strings.Join(sqlOrder, ", ")
  173. }
  174. if page_z != 9999 {
  175. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  176. }
  177. logs.Println(sql)
  178. _, err = o.Raw(sql).QueryRows(&maps)
  179. cnt, _ := strconv.Atoi(maps_z[0][0].(string))
  180. return maps, cnt
  181. }