package models import ( "lapp_-wy/db" "lapp_-wy/utils" "xorm.io/core" "errors" ) type Propertytab struct { Cid int `json:"cid" xorm:"not null pk INT(4)"` Propertyid string `json:"propertyid" xorm:"not null pk VARCHAR(100)"` Descr string `json:"descr" xorm:"VARCHAR(255)"` Buildingid string `json:"buildingid" xorm:"VARCHAR(100)"` Unit string `json:"unit" xorm:"VARCHAR(40)"` Isfree string `json:"isfree" xorm:"VARCHAR(10)"`//1.普通业主,2.业主委员会3.空置房 Room string `json:"room" xorm:"VARCHAR(40)"` Status string `json:"status" xorm:"VARCHAR(40)"` Ownertype string `json:"ownertype" xorm:"VARCHAR(40)"` Layout string `json:"layout" xorm:"VARCHAR(255)"` Orientation string `json:"orientation" xorm:"VARCHAR(255)"` Constructionarea float64 `json:"constructionarea" xorm:"DECIMAL(10,2)"` Usearea float64 `json:"usearea" xorm:"DECIMAL(10,2)"` Contact string `json:"contact" xorm:"VARCHAR(255)"` Phone1 string `json:"phone1" xorm:"VARCHAR(15)"` Phone2 string `json:"phone2" xorm:"VARCHAR(15)"` Createtime string `json:"createtime" xorm:"VARCHAR(14)"` Lastmodifytime string `json:"lastmodifytime" xorm:"VARCHAR(14)"` Lastmodifyby string `json:"lastmodifyby" xorm:"VARCHAR(40)"` Contractid string `json:"contractid" xorm:"VARCHAR(100)"` } func (t *Propertytab) TableName() string { return "propertytab" } // 清除string字段的右侧空格 func (t *Propertytab) Clipped() { utils.TrimStruct(t, *t) } //增 func (t *Propertytab) Add() error { e := db.MasterEngine() countrole := new(Propertytab) affw, err := e.Table("propertytab").ID(core.PK{t.Cid, t.Propertyid}).Count(countrole) if err != nil { return err } if affw > 0 { return errors.New("数据已经存在!") } _, err = e.Table("propertytab").Insert(t) if err != nil { return err } return nil } //删 func (t *Propertytab) Del() bool { e := db.MasterEngine() _, err := e.ID(core.PK{t.Cid, t.Propertyid}).Delete(&Propertytab{}) if err != nil { return false } return true } //改 func (t *Propertytab) Update() bool { e := db.MasterEngine() _, err := e.ID(core.PK{t.Cid, t.Propertyid}).Update(t) if err != nil { return false } return true } //查 func (t *Propertytab) SelectOne() (Propertytab, error) { e := db.MasterEngine() var data Propertytab _, err := e.ID(core.PK{t.Cid, t.Propertyid}).Get(&data) if err != nil { return data, err } return data, nil } //分页 func (t *Propertytab) GetPage(pageSize int, pageIndex int) ([]Propertytab, int, error) { data := make([]Propertytab, 0) e := db.MasterEngine() query := e.Table("propertytab").Where("cid = ? ", t.Cid) table := e.Table("propertytab").Where("cid = ? ", t.Cid) if !utils.ValueIsEmpty(t.Propertyid) { query = query.And("propertyid = ?", t.Propertyid) table = table.And("propertyid = ?", t.Propertyid) } Offset := (pageIndex - 1) * pageSize err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data) pcount := new(Propertytab) count, err := table.Count(pcount) if err != nil { return data, 0, err } return data, int(count), nil } type PropertytabContract struct { Propertytab `xorm:"extends"` Buildingtab `xorm:"extends"` Propertytypetab `xorm:"extends"` Contracttab `xorm:"extends"` } //物业费查询 func (t *Propertytab) Search() (ContractInfo, error) { data := ContractInfo{} //联查 var info PropertytabContract e := db.MasterEngine() _, err := e.Table("propertytab").Join("LEFT", "buildingtab", "buildingtab.buildingid = propertytab.buildingid and buildingtab.cid = propertytab.cid").Join("LEFT", "propertytypetab", "propertytypetab.propertytypeid=buildingtab.propertytypeid and propertytypetab.cid=buildingtab.cid").Join("LEFT", "contracttab", "propertytab.contractid=contracttab.contractid and propertytab.cid=contracttab.cid").Where("propertytab.cid = ? and propertytab.buildingid = ? and propertytab.unit = ? and propertytab.room =?", t.Cid, t.Buildingid, t.Unit, t.Room).Get(&info) if err != nil { return data, err } data.Propertyid = info.Propertytab.Propertyid data.Cid = info.Propertytab.Cid data.Room = info.Propertytab.Room data.Unit = info.Unit data.Buildingid = info.Propertytab.Buildingid data.Contractid = info.Propertytab.Contractid data.Descr = info.Propertytab.Descr data.Contact = info.Propertytab.Contact data.Phone1 = info.Propertytab.Phone1 data.Phone2 = info.Propertytab.Phone2 data.Constructionarea = info.Propertytab.Constructionarea data.Unitprice = info.Propertytypetab.Unitprice data.Contracttab = info.Contracttab data.Chargetype = 1 return data, err }