package models import ( "lapp_-wy/db" "lapp_-wy/utils" "errors" "xorm.io/core" ) type Company struct { Cnr int `json:"cnr" xorm:"not null pk comment('小区ID') INT(11)"` Cid int `json:"cid" xorm:"not null pk INT(11)"` Descr string `json:"descr" xorm:"default 'NULL' comment('描述') VARCHAR(100)"` Active int `json:"active" xorm:"default NULL comment('是否激活') INT(11)"` Address string `json:"address" xorm:"default 'NULL' comment('地址') VARCHAR(255)"` Constructionarea string `json:"constructionarea" xorm:"default NULL comment('建筑面积') DECIMAL(10,2)"` Totalhouseholds int `json:"totalhouseholds" xorm:"default NULL comment('住户数') INT(11)"` Totalbuildings int `json:"totalbuildings" xorm:"default NULL comment('建筑数') INT(11)"` Createtime string `json:"createtime" xorm:"not null comment('创建时间') VARCHAR(40)"` Lastmodifytime string `json:"lastmodifytime" xorm:"not null comment('上次更新时间') VARCHAR(40)"` Lastmodifyby string `json:"lastmodifyby" xorm:"default 'NULL' comment('更新人员') VARCHAR(40)"` } func (t *Company) TableName() string { return "company" } // 清除string字段的右侧空格 func (t *Company) Clipped() { utils.TrimStruct(t, *t) } //增 func (t *Company) Add() error { e := db.MasterEngine() countrole := new(Company) affw, err := e.Table("company").ID(core.PK{t.Cnr, t.Cid}).Count(countrole) if err != nil { return err } if affw > 0 { return errors.New("数据已经存在!") } _, err = e.Table("company").Insert(t) if err != nil { return err } return nil } //删 func (t *Company) Del() bool { e := db.MasterEngine() _, err := e.ID(core.PK{t.Cnr, t.Cid}).Delete(&Company{}) if err != nil { return false } return true } //改 func (t *Company) Update() bool { e := db.MasterEngine() _, err := e.ID(core.PK{t.Cnr, t.Cid}).Update(t) if err != nil { return false } return true } //查 func (t *Company) SelectOne() (Company, error) { e := db.MasterEngine() var data Company _, err := e.ID(core.PK{t.Cnr, t.Cid}).Get(&data) if err != nil { return data, err } return data, nil } //分页 func (t *Company) GetPage(pageSize int, pageIndex int) ([]Company, int, error) { data := make([]Company, 0) e := db.MasterEngine() query := e.Table("company").Where("cid = ? ", t.Cid) table := e.Table("company").Where("cid = ? ", t.Cid) if t.Cnr > 0 { query = query.And("cnr = ?", t.Cnr) table = table.And("cnr = ?", t.Cnr) } Offset := (pageIndex - 1) * pageSize err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data) pcount := new(Company) count, err := table.Count(pcount) if err != nil { return data, 0, err } return data, int(count), nil }