123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package Certificate
- import (
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- type CertificatePdf struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_layout_no string `orm:"size(256);null"` // 布局编号
- T_Certificate_sn string `orm:"size(256);null"` // 证书编号
- T_release_time string `orm:"column(t_release_time);type(timestamp);null;"` // 发布时间
- T_failure_time string `orm:"column(t_failure_time);type(timestamp);null;"` // 失效时间
- T_pdf string `orm:"size(256);null"` // pdf链接
- T_State int `orm:"size(200);default(1)"` // 0删除 1 正常
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
- }
- type CertificatePdf_R struct {
- Id int
- T_layout_no string // 布局编号
- T_Certificate_sn string // 证书编号 TODO后期删除
- T_release_time string // 发布时间
- T_failure_time string // 失效时间
- T_pdf string // pdf链接
- }
- func (t *CertificatePdf) TableName() string {
- return "certificate_pdf" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(CertificatePdf))
- }
- // -------------------------------------------------------------
- func CertificatePdfToCertificatePdf_R(T CertificatePdf) (T_r CertificatePdf_R) {
- T_r.Id = T.Id
- T_r.T_layout_no = T.T_layout_no
- T_r.T_Certificate_sn = T.T_Certificate_sn
- T_r.T_release_time = T.T_release_time
- T_r.T_failure_time = T.T_failure_time
- T_r.T_pdf = T.T_pdf
- return T_r
- }
- // ----------------
- // 获取 ById
- func Read_CertificatePdf_ById(id int) (r CertificatePdf, is bool) {
- o := orm.NewOrm()
- r = CertificatePdf{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 获取 By
- func Read_CertificatePdf(T_Certificate_sn, T_release_time, T_failure_time string) (r CertificatePdf, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(CertificatePdf))
- 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)
- if err != nil {
- logs.Println(lib.FuncName(), err)
- }
- return r, err
- }
- // 获取最新的校准证书
- func Read_CertificatePdf_Newest(T_layout_no string) (r []CertificatePdf, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(CertificatePdf))
- _, err = qs.Limit(1, 0).Filter("T_layout_no", T_layout_no).Filter("T_State", 1).OrderBy("-T_failure_time").All(&r)
- if err != nil {
- logs.Println(lib.FuncName(), err)
- }
- return r, err
- }
- // 通过T_layout_no获取对应时间的证书
- func Read_CertificatePdf_T_layout_no(T_layout_no, operation_time string) (r []CertificatePdf, err error) {
- if len(operation_time) == 0 {
- operation_time = time.Now().Format("2006-01-02")
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(CertificatePdf))
- _, err = qs.Limit(1, 0).Filter("T_layout_no", T_layout_no).
- Filter("T_release_time__lte", operation_time).
- Filter("T_failure_time__gte", operation_time).
- Filter("T_State", 1).OrderBy("-T_failure_time").All(&r)
- if err != nil {
- logs.Println(lib.FuncName(), err)
- }
- return r, err
- }
- // 添加
- func Add_CertificatePdf(r CertificatePdf) (id int64, is bool) {
- o := orm.NewOrm()
- err := o.Read(&r, "T_Certificate_sn", "T_release_time", "T_failure_time", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err == nil {
- return 0, false
- }
- id, err = o.Insert(&r)
- if err != nil {
- logs.Println(lib.FuncName(), err)
- return 0, false
- }
- return id, true
- }
- // 删除
- func Delete_CertificatePdf(v CertificatePdf) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&v); err == nil {
- logs.Println("Number of records deleted in database:", num)
- } else {
- logs.Println(lib.FuncName(), err)
- return false
- }
- return true
- }
- // 删除
- func Delete_CertificatePdf_(v CertificatePdf) bool {
- o := orm.NewOrm()
- v.T_State = 0
- if num, err := o.Update(&v, "T_State"); err == nil {
- logs.Println("Number of records updated in database:", num)
- } else {
- logs.Println(lib.FuncName(), err)
- return false
- }
- return true
- }
- // 修改
- func Update_CertificatePdf(m CertificatePdf, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Println("Number of records updated in database:", num)
- return true
- } else {
- logs.Println(lib.FuncName(), err)
- }
- return false
- }
- // 获取列表
- func Read_CertificatePdf_List(T_layout_no string, page int, page_z int) ([]CertificatePdf_R, int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var r []CertificatePdf
- qs := o.QueryTable(new(CertificatePdf))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_layout_no", T_layout_no).And("T_State", 1)
- if page_z == 9999 {
- qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- } else {
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- }
- cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
- // 转换
- var CertificatePdf_r []CertificatePdf_R
- for _, v := range r {
- CertificatePdf_r = append(CertificatePdf_r, CertificatePdfToCertificatePdf_R(v))
- }
- return CertificatePdf_r, cnt
- }
|