123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package Property
- import (
- "git.baozhida.cn/ERP_libs/lib"
- orm2 "github.com/beego/beego/v2/client/orm"
- "time"
- "ERP_storage/logs"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- )
- // 仓库
- type IotCard struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_iccid string `orm:"size(256);null"` // 编号
- T_type string `orm:"size(20);default(4G)"` // 类型
- T_sn string `orm:"size(256);null"` // 关联sn
- T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1-未使用 2-已使用 3-已作废
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- func (t *IotCard) TableName() string {
- return "iot_card" // 数据库名称 // ************** 替换 FormulaList **************
- }
- type IotCardDaoImpl struct {
- orm orm.Ormer
- }
- func NewIotCard(orm *orm.Ormer) *IotCardDaoImpl {
- return &IotCardDaoImpl{orm: *orm}
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(IotCard))
- }
- type IotCard_R struct {
- Id int
- T_iccid string
- T_type string
- T_sn string
- T_State int
- }
- func IotCardToIotCard_R(t IotCard) (r IotCard_R) {
- r.Id = t.Id
- r.T_iccid = t.T_iccid
- r.T_type = t.T_type
- r.T_sn = t.T_sn
- r.T_State = t.T_State
- return r
- }
- // 添加
- func (dao *IotCardDaoImpl) Add_IotCard(r IotCard) (id int64, err error) {
- id, err = dao.orm.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return id, err
- }
- func (dao *IotCardDaoImpl) AddOrUpdate_IotCard(r IotCard) (id int64, err error) {
- qs := dao.orm.QueryTable(new(IotCard))
- var iotCard IotCard
- err = qs.Filter("T_iccid", r.T_iccid).Filter("T_State__gt", 0).One(&iotCard)
- if err != nil {
- if err.Error() == orm.ErrNoRows.Error() {
- id, err = dao.orm.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- } else {
- logs.Error(lib.FuncName(), err)
- return
- }
- }
- iotCard.T_State = 2
- iotCard.T_sn = r.T_sn
- _, err = dao.orm.Update(&iotCard, "T_State", "T_sn")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return
- }
- // 获取 ById
- func Read_IotCard_ByT_iccid(T_iccid string) (r IotCard, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(IotCard))
- err = qs.Filter("T_iccid", T_iccid).Filter("T_State__gt", 0).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- func Read_IotCard_ById(Id int) (r IotCard, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(IotCard))
- err = qs.Filter("Id", Id).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // 修改
- func (dao *IotCardDaoImpl) Update_IotCard(m IotCard, cols ...string) error {
- _, err := dao.orm.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- return nil
- }
- // 删除
- func (dao *IotCardDaoImpl) Delete_IotCard(v IotCard) error {
- v.T_State = 0
- _, err := dao.orm.Update(&v, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return err
- }
- // 获取列表
- func Read_IotCard_List(T_name string, T_state, page, page_z int) (r_ []IotCard_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(IotCard))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- // 过滤
- cond := orm.NewCondition()
- cond1 := cond.And("T_State__gt", 0)
- if T_state > 0 {
- cond1 = cond1.And("T_State", T_state)
- }
- if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_iccid__icontains", T_name).Or("T_sn__icontains", T_name))
- }
- // 查询
- var r []IotCard
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- r_ = append(r_, IotCardToIotCard_R(v))
- }
- return r_, cnt
- }
|