|
|
- package models
-
- import (
- "errors"
- "lapp_-wy/db"
- "lapp_-wy/utils"
- "xorm.io/core"
- )
-
- //电梯卡表
- type Accesscardtab struct {
- Cid int `json:"cid" xorm:"not null pk INT(4)"`
- Accesscardid string `json:"accesscardid" 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)"`
- }
-
- func (t *Accesscardtab) TableName() string {
- return "accesscardtab"
- }
-
- // 清除string字段的右侧空格
- func (t *Accesscardtab) Clipped() {
- utils.TrimStruct(t, *t)
- }
-
- //增
- func (t *Accesscardtab) Add() error {
- e := db.MasterEngine()
- countrole := new(Accesscardtab)
- affw, err := e.Table("accesscardtab").ID(core.PK{t.Cid, t.Accesscardid}).Count(countrole)
- if err != nil {
- return err
- }
- if affw > 0 {
- return errors.New("数据已经存在!")
- }
- _, err = e.Table("accesscardtab").Insert(t)
- if err != nil {
- return err
- }
- return nil
- }
-
- //删
- func (t *Accesscardtab) Del() bool {
- e := db.MasterEngine()
- _, err := e.ID(core.PK{t.Cid, t.Accesscardid}).Delete(&Accesscardtab{})
- if err != nil {
- return false
- }
- return true
- }
-
- //改
- func (t *Accesscardtab) Update() bool {
- e := db.MasterEngine()
- _, err := e.ID(core.PK{t.Cid, t.Accesscardid}).Update(t)
- if err != nil {
- return false
- }
- return true
- }
-
- //查
- func (t *Accesscardtab) SelectOne() (Accesscardtab, error) {
- e := db.MasterEngine()
- var data Accesscardtab
- _, err := e.ID(core.PK{t.Cid, t.Accesscardid}).Get(&data)
- if err != nil {
- return data, err
- }
- return data, nil
- }
-
- //分页
- func (t *Accesscardtab) GetPage(pageSize int, pageIndex int) ([]Accesscardtab, int, error) {
- data := make([]Accesscardtab, 0)
- e := db.MasterEngine()
-
- query := e.Table("accesscardtab").Where("cid = ? ", t.Cid)
- table := e.Table("accesscardtab").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(Accesscardtab)
- count, err := table.Count(pcount)
- if err != nil {
- return data, 0, err
- }
- return data, int(count), nil
- }
-
- type AccesscardContract struct {
- Accesscardtab `xorm:"extends"`
- Propertytab `xorm:"extends"`
- Propertytypetab `xorm:"extends"`
- Contracttab `xorm:"extends"`
- }
-
- type ContractInfo struct {
- Cid int `json:"cid"`
- Carportid string `json:"carportid"`
- Propertyid string `json:"propertyid"`
- Descr string `json:"descr"`
- Buildingid string `json:"buildingid"`
- Chargetype int `json:"chargetype"`
- Begindate string `json:"begindate"`
- Enddate string `json:"enddate"`
- Unit string `json:"unit"`
- Chargeway string `json:"chargeway"`
- Room string `json:"room"`
- Constructionarea float64 `json:"constructionarea"`
- Usearea string `json:"usearea"`
- Contact string `json:"contact"`
- Phone1 string `json:"phone1"`
- Phone2 string `json:"phone2"`
- Contractid string `json:"contractid"`
- Accesscardid string `json:"accesscardid"`
- Unitprice float64 `json:"unitprice"`
- Chargetime string `json:"chargetime"`
- AccesscardidList []string `json:"accesscardlist"`
- Contracttab Contracttab `json:"contracttab"`
- }
-
- //电梯费查询
- func (t *Accesscardtab) Search(buildingid string, unit string, room string) (ContractInfo, error) {
- //联查
- data := ContractInfo{}
- accesscardidList := make([]string, 0)
- var info AccesscardContract
- e := db.MasterEngine()
- query := e.Table("accesscardtab").Join("RIGHT", "propertytab", "accesscardtab.propertyid=propertytab.propertyid and accesscardtab.cid=propertytab.cid").Join("LEFT", "propertytypetab", "accesscardtab.propertytypeid=propertytypetab.propertytypeid and accesscardtab.cid=propertytypetab.cid").Join("LEFT", "contracttab", "accesscardtab.contractid=contracttab.contractid and accesscardtab.cid=contracttab.cid").Where("propertytab.cid=? ", t.Cid)
- if !utils.ValueIsEmpty(buildingid) {
- query = query.And("propertytab.buildingid = ?", buildingid)
- }
- if !utils.ValueIsEmpty(unit) {
- query = query.And("propertytab.unit = ?", unit)
- }
- if !utils.ValueIsEmpty(room) {
- query = query.And("propertytab.room = ?", room)
- }
- if !utils.ValueIsEmpty(t.Accesscardid) {
- query = query.And("accesscardtab.accesscardid = ?", t.Accesscardid)
- }
- _, err := query.Get(&info)
- if err != nil {
- return data, err
- }
- err = e.Table(t.TableName()).Where("cid = ? and propertyid = ?", t.Cid, t.Propertyid).Cols("accesscardid").Find(&accesscardidList)
- if err != nil {
- return data, err
- }
- if utils.ValueIsEmpty(info.Propertytab.Propertyid) {
- data.Propertyid = buildingid + "-" + unit + "-" + room
- } else {
- data.Propertyid = info.Propertytab.Propertyid
- }
- if utils.ValueIsEmpty(info.Propertytab.Room) {
- data.Room = room
- } else {
- data.Room = info.Propertytab.Room
- }
- if utils.ValueIsEmpty(info.Propertytab.Unit) {
- data.Unit = unit
- } else {
- data.Unit = info.Propertytab.Unit
- }
- if utils.ValueIsEmpty(info.Propertytab.Buildingid) {
- data.Buildingid = buildingid
- } else {
- data.Buildingid = info.Propertytab.Buildingid
- }
- data.AccesscardidList = accesscardidList
- data.Accesscardid = t.Accesscardid
- data.Cid = t.Cid
- 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
- if utils.ValueIsEmpty(info.Propertytypetab.Unitprice) {
- //查询费率
- var property Propertytypetab
- _, err = e.Table("propertytypetab").Where("cid = ? and propertytypeid = ?", t.Cid, "电梯费").Get(&property)
- if err != nil {
- return data, err
- }
- data.Unitprice = property.Unitprice
- } else {
- data.Unitprice = info.Propertytypetab.Unitprice
- }
-
- data.Contracttab = info.Contracttab
- data.Chargetype = 2
- return data, err
- }
|