沈阳玫苑物业管理后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.6 KiB

  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "lapp_-wy/db"
  7. "lapp_-wy/utils"
  8. "xorm.io/core"
  9. )
  10. //车位表
  11. type Carporttab struct {
  12. Cid int `json:"cid" xorm:"not null pk INT(4)"`
  13. Carportid string `json:"carportid" xorm:"not null pk VARCHAR(100)"`
  14. Descr string `json:"descr" xorm:"VARCHAR(255)"`
  15. Status int `json:"status" xorm:"INT(4)"`
  16. Propertyid string `json:"propertyid" xorm:"VARCHAR(100)"`
  17. Propertytypeid string `json:"propertytypeid" xorm:"VARCHAR(100)"`
  18. Createtime string `json:"createtime" xorm:"VARCHAR(14)"`
  19. Lastmodifytime string `json:"lastmodifytime" xorm:"VARCHAR(20)"`
  20. Lastmodifyby string `json:"lastmodifyby" xorm:"VARCHAR(20)"`
  21. Contractid string `json:"contractid" xorm:"VARCHAR(100)"`
  22. Contact string `json:"contact" xorm:"VARCHAR(255)"`
  23. Phone string `json:"phone" xorm:"VARCHAR(64)"`
  24. LicensePlateNumber string `json:"licensePlateNumber" xorm:"VARCHAR(64) 'licensePlateNumber'"`
  25. }
  26. func (t *Carporttab) TableName() string {
  27. return "carporttab"
  28. }
  29. // 清除string字段的右侧空格
  30. func (t *Carporttab) Clipped() {
  31. utils.TrimStruct(t, *t)
  32. }
  33. //增
  34. func (t *Carporttab) Add() error {
  35. e := db.MasterEngine()
  36. countrole := new(Carporttab)
  37. affw, err := e.Table("carporttab").ID(core.PK{t.Cid, t.Carportid}).Count(countrole)
  38. if err != nil {
  39. return err
  40. }
  41. if affw > 0 {
  42. return errors.New("数据已经存在!")
  43. }
  44. _, err = e.Table("carporttab").Insert(t)
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. //删
  51. func (t *Carporttab) Del() bool {
  52. e := db.MasterEngine()
  53. _, err := e.ID(core.PK{t.Cid, t.Carportid}).Delete(&Carporttab{})
  54. if err != nil {
  55. return false
  56. }
  57. return true
  58. }
  59. //改
  60. func (t *Carporttab) Update() bool {
  61. e := db.MasterEngine()
  62. _, err := e.ID(core.PK{t.Cid, t.Carportid}).Update(t)
  63. if err != nil {
  64. return false
  65. }
  66. return true
  67. }
  68. //查
  69. func (t *Carporttab) SelectOne() (Carporttab, error) {
  70. e := db.MasterEngine()
  71. var data Carporttab
  72. exist, err := e.ID(core.PK{t.Cid, t.Carportid}).Get(&data)
  73. if err != nil {
  74. return data, err
  75. }
  76. str, err := json.Marshal(data)
  77. if err != nil {
  78. fmt.Println("marshal error:", err)
  79. }
  80. fmt.Println("data:", string(str))
  81. if !exist {
  82. return data, errors.New("不存在该车位数据,请检查")
  83. }
  84. return data, nil
  85. }
  86. //分页
  87. func (t *Carporttab) GetPage(pageSize int, pageIndex int) ([]Carporttab, int, error) {
  88. data := make([]Carporttab, 0)
  89. e := db.MasterEngine()
  90. query := e.Table("carporttab").Where("cid = ? ", t.Cid)
  91. table := e.Table("carporttab").Where("cid = ? ", t.Cid)
  92. if !utils.ValueIsEmpty(t.Descr) {
  93. descr := "%" + t.Descr + "%"
  94. query = query.And("descr like ?", descr)
  95. table = table.And("descr like ?", descr)
  96. }
  97. Offset := (pageIndex - 1) * pageSize
  98. err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data)
  99. pcount := new(Carporttab)
  100. count, err := table.Count(pcount)
  101. if err != nil {
  102. return data, 0, err
  103. }
  104. return data, int(count), nil
  105. }
  106. type CarporttabContract struct {
  107. Carporttab `xorm:"extends"`
  108. Propertytab `xorm:"extends"`
  109. Propertytypetab `xorm:"extends"`
  110. Contracttab `xorm:"extends"`
  111. }
  112. type CarporttabInfo struct {
  113. Propertytab `xorm:"extends"`
  114. Propertytypetab `xorm:"extends"`
  115. }
  116. //车位费查询
  117. func (t *Carporttab) Search() (data ContractInfo, err error) {
  118. //联查
  119. e := db.MasterEngine()
  120. var carport Carporttab
  121. exist, err := e.Table(t.TableName()).ID(core.PK{t.Cid, t.Carportid}).Get(&carport)
  122. if err != nil {
  123. return data, err
  124. }
  125. if !exist {
  126. return data, errors.New("车位数据不存在,请检查")
  127. }
  128. var propertytype Propertytypetab
  129. exist, err = e.Table(propertytype.TableName()).ID(core.PK{t.Cid, carport.Propertytypeid}).Get(&propertytype)
  130. if err != nil {
  131. return data, err
  132. }
  133. if !exist {
  134. return data, errors.New("资产数据不存在,请检查")
  135. }
  136. var property Propertytab
  137. if carport.Propertyid != "" {
  138. exist, err = e.Table(property.TableName()).ID(core.PK{t.Cid, carport.Propertyid}).Get(&property)
  139. if err == nil && exist {
  140. data.Room = property.Room
  141. data.Unit = property.Unit
  142. data.Buildingid = property.Buildingid
  143. }
  144. }
  145. var contract Contracttab
  146. if carport.Contractid != "" {
  147. exist, err = e.Table(contract.TableName()).ID(core.PK{t.Cid, carport.Contractid}).Get(&contract)
  148. if err == nil && exist {
  149. data.Contracttab = contract
  150. }
  151. }
  152. data.Carportid = carport.Carportid
  153. data.Propertyid = carport.Propertyid
  154. data.Cid = carport.Cid
  155. data.Contractid = carport.Contractid
  156. data.Descr =carport.Descr
  157. data.Contact = carport.Contact
  158. data.Phone1 = carport.Phone
  159. data.Unitprice = propertytype.Unitprice
  160. data.Chargetype = 3
  161. return data, nil
  162. }