package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"lapp_-wy/db"
|
|
"lapp_-wy/utils"
|
|
"xorm.io/core"
|
|
)
|
|
|
|
//车位表
|
|
type Carporttab struct {
|
|
Cid int `json:"cid" xorm:"not null pk INT(4)"`
|
|
Carportid string `json:"carportid" xorm:"not null pk VARCHAR(100)"`
|
|
Descr string `json:"descr" xorm:"VARCHAR(255)"`
|
|
Status int `json:"status" xorm:"INT(4)"`
|
|
Propertyid string `json:"propertyid" xorm:"VARCHAR(100)"`
|
|
Propertytypeid string `json:"propertytypeid" xorm:"VARCHAR(100)"`
|
|
Createtime string `json:"createtime" xorm:"VARCHAR(14)"`
|
|
Lastmodifytime string `json:"lastmodifytime" xorm:"VARCHAR(20)"`
|
|
Lastmodifyby string `json:"lastmodifyby" xorm:"VARCHAR(20)"`
|
|
Contractid string `json:"contractid" xorm:"VARCHAR(100)"`
|
|
Contact string `json:"contact" xorm:"VARCHAR(255)"`
|
|
Phone string `json:"phone" xorm:"VARCHAR(64)"`
|
|
LicensePlateNumber string `json:"licensePlateNumber" xorm:"VARCHAR(64) 'licensePlateNumber'"`
|
|
}
|
|
|
|
func (t *Carporttab) TableName() string {
|
|
return "carporttab"
|
|
}
|
|
|
|
// 清除string字段的右侧空格
|
|
func (t *Carporttab) Clipped() {
|
|
utils.TrimStruct(t, *t)
|
|
}
|
|
|
|
//增
|
|
func (t *Carporttab) Add() error {
|
|
e := db.MasterEngine()
|
|
countrole := new(Carporttab)
|
|
affw, err := e.Table("carporttab").ID(core.PK{t.Cid, t.Carportid}).Count(countrole)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affw > 0 {
|
|
return errors.New("数据已经存在!")
|
|
}
|
|
_, err = e.Table("carporttab").Insert(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//删
|
|
func (t *Carporttab) Del() bool {
|
|
e := db.MasterEngine()
|
|
_, err := e.ID(core.PK{t.Cid, t.Carportid}).Delete(&Carporttab{})
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
//改
|
|
func (t *Carporttab) Update() bool {
|
|
e := db.MasterEngine()
|
|
_, err := e.ID(core.PK{t.Cid, t.Carportid}).Update(t)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
//查
|
|
func (t *Carporttab) SelectOne() (Carporttab, error) {
|
|
e := db.MasterEngine()
|
|
var data Carporttab
|
|
exist, err := e.ID(core.PK{t.Cid, t.Carportid}).Get(&data)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
str, err := json.Marshal(data)
|
|
if err != nil {
|
|
fmt.Println("marshal error:", err)
|
|
}
|
|
fmt.Println("data:", string(str))
|
|
if !exist {
|
|
return data, errors.New("不存在该车位数据,请检查")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//分页
|
|
func (t *Carporttab) GetPage(pageSize int, pageIndex int) ([]Carporttab, int, error) {
|
|
data := make([]Carporttab, 0)
|
|
e := db.MasterEngine()
|
|
|
|
query := e.Table("carporttab").Where("cid = ? ", t.Cid)
|
|
table := e.Table("carporttab").Where("cid = ? ", t.Cid)
|
|
if !utils.ValueIsEmpty(t.Descr) {
|
|
descr := "%" + t.Descr + "%"
|
|
query = query.And("descr like ?", descr)
|
|
table = table.And("descr like ?", descr)
|
|
}
|
|
Offset := (pageIndex - 1) * pageSize
|
|
err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data)
|
|
|
|
pcount := new(Carporttab)
|
|
count, err := table.Count(pcount)
|
|
if err != nil {
|
|
return data, 0, err
|
|
}
|
|
return data, int(count), nil
|
|
}
|
|
|
|
type CarporttabContract struct {
|
|
Carporttab `xorm:"extends"`
|
|
Propertytab `xorm:"extends"`
|
|
Propertytypetab `xorm:"extends"`
|
|
Contracttab `xorm:"extends"`
|
|
}
|
|
type CarporttabInfo struct {
|
|
Propertytab `xorm:"extends"`
|
|
Propertytypetab `xorm:"extends"`
|
|
}
|
|
|
|
//车位费查询
|
|
func (t *Carporttab) Search() (data ContractInfo, err error) {
|
|
//联查
|
|
|
|
e := db.MasterEngine()
|
|
var carport Carporttab
|
|
exist, err := e.Table(t.TableName()).ID(core.PK{t.Cid, t.Carportid}).Get(&carport)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
if !exist {
|
|
return data, errors.New("车位数据不存在,请检查")
|
|
}
|
|
var propertytype Propertytypetab
|
|
exist, err = e.Table(propertytype.TableName()).ID(core.PK{t.Cid, carport.Propertytypeid}).Get(&propertytype)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
if !exist {
|
|
return data, errors.New("资产数据不存在,请检查")
|
|
}
|
|
var property Propertytab
|
|
if carport.Propertyid != "" {
|
|
exist, err = e.Table(property.TableName()).ID(core.PK{t.Cid, carport.Propertyid}).Get(&property)
|
|
if err == nil && exist {
|
|
data.Room = property.Room
|
|
data.Unit = property.Unit
|
|
data.Buildingid = property.Buildingid
|
|
}
|
|
}
|
|
var contract Contracttab
|
|
if carport.Contractid != "" {
|
|
exist, err = e.Table(contract.TableName()).ID(core.PK{t.Cid, carport.Contractid}).Get(&contract)
|
|
if err == nil && exist {
|
|
data.Contracttab = contract
|
|
}
|
|
}
|
|
data.Carportid = carport.Carportid
|
|
data.Propertyid = carport.Propertyid
|
|
data.Cid = carport.Cid
|
|
data.Contractid = carport.Contractid
|
|
data.Descr =carport.Descr
|
|
data.Contact = carport.Contact
|
|
data.Phone1 = carport.Phone
|
|
data.Unitprice = propertytype.Unitprice
|
|
data.Chargetype = 3
|
|
return data, nil
|
|
}
|