Certificate.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_layout_no string) (r Certificate, is bool) {
  45. o := orm.NewOrm()
  46. qs := o.QueryTable(new(Certificate))
  47. err := qs.Filter("T_layout_no", T_layout_no).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_Certificate_sn string // 编号
  113. T_layout_no string // 布局编号
  114. T_release_time string `orm:"column(t_release_time)"` // 发布时间
  115. T_failure_time string `orm:"column(t_failure_time)"` // 失效时间
  116. T_pdf string // pdf链接
  117. }
  118. // 获取列表
  119. 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) {
  120. o := orm.NewOrm()
  121. var offset int
  122. if page <= 1 {
  123. offset = 0
  124. } else {
  125. offset = (page - 1) * page_z
  126. }
  127. var maps_z []orm2.ParamsList
  128. var maps []Certificate_
  129. sqlWhere := ""
  130. if len(T_Certificate_sn) > 0 {
  131. sqlWhere += " AND pdf.t__certificate_sn like \"%" + T_Certificate_sn + "%\""
  132. }
  133. if len(T_layout_no) > 0 {
  134. sqlWhere += " AND c.t_layout_no like \"%" + T_layout_no + "%\""
  135. }
  136. if len(Time_start) > 0 {
  137. sqlWhere += " AND t_failure_time >= \"" + Time_start + "\""
  138. }
  139. if len(Time_end) > 0 {
  140. sqlWhere += " AND t_failure_time <= \"" + Time_end + "\""
  141. }
  142. // 获取总条数
  143. 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"
  144. sql = sql + sqlWhere
  145. fmt.Println(sql)
  146. _, err := o.Raw(sql).ValuesList(&maps_z)
  147. if err != nil {
  148. return maps, 0
  149. }
  150. if len(maps_z) == 0 {
  151. return maps, 0
  152. }
  153. //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"
  154. 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"
  155. sql = sql + sqlWhere
  156. sqlOrder := make([]string, 0)
  157. if T_release_time_sort == 1 {
  158. sqlOrder = append(sqlOrder, "pdf.t_release_time ASC")
  159. } else if T_release_time_sort == 2 {
  160. sqlOrder = append(sqlOrder, "pdf.t_release_time DESC")
  161. }
  162. if T_failure_time_sort == 1 {
  163. sqlOrder = append(sqlOrder, "pdf.t_failure_time ASC")
  164. } else if T_failure_time_sort == 2 {
  165. sqlOrder = append(sqlOrder, "pdf.t_failure_time DESC")
  166. }
  167. if T_layout_no_sort == 1 {
  168. sqlOrder = append(sqlOrder, "c.t_layout_no ASC")
  169. } else if T_layout_no_sort == 2 {
  170. sqlOrder = append(sqlOrder, "c.t_layout_no DESC")
  171. }
  172. if len(sqlOrder) > 0 {
  173. sql += " ORDER BY " + strings.Join(sqlOrder, ", ")
  174. }
  175. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  176. fmt.Println(sql)
  177. _, err = o.Raw(sql).QueryRows(&maps)
  178. cnt, _ := strconv.Atoi(maps_z[0][0].(string))
  179. return maps, cnt
  180. }