package Certificate

import (
	"ColdVerify_server/lib"
	"ColdVerify_server/logs"
	"fmt"
	"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_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_Certificate_sn string // 证书编号
	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_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_Certificate_sn string) (r []CertificatePdf, err error) {

	o := orm.NewOrm()
	qs := o.QueryTable(new(CertificatePdf))
	_, err = qs.Limit(1, 0).Filter("T_Certificate_sn", T_Certificate_sn).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 {
		fmt.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 {
		fmt.Println("Number of records updated in database:", num)
		return true
	} else {
		logs.Println(lib.FuncName(), err)
	}
	return false
}

// 获取列表
func Read_CertificatePdf_List(T_Certificate_sn 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_Certificate_sn", T_Certificate_sn).And("T_State", 1)
	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
}
