沈阳玫苑物业管理后端
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.

209 lines
6.4 KiB

  1. package models
  2. import (
  3. "errors"
  4. "lapp_-wy/db"
  5. "lapp_-wy/utils"
  6. "xorm.io/core"
  7. )
  8. //电梯卡表
  9. type Accesscardtab struct {
  10. Cid int `json:"cid" xorm:"not null pk INT(4)"`
  11. Accesscardid string `json:"accesscardid" xorm:"not null pk VARCHAR(100)"`
  12. Descr string `json:"descr" xorm:"VARCHAR(255)"`
  13. Status int `json:"status" xorm:"INT(4)"`
  14. Propertyid string `json:"propertyid" xorm:"VARCHAR(100)"`
  15. Propertytypeid string `json:"propertytypeid" xorm:"VARCHAR(100)"`
  16. Createtime string `json:"createtime" xorm:"VARCHAR(14)"`
  17. Lastmodifytime string `json:"lastmodifytime" xorm:"VARCHAR(20)"`
  18. Lastmodifyby string `json:"lastmodifyby" xorm:"VARCHAR(20)"`
  19. Contractid string `json:"contractid" xorm:"VARCHAR(100)"`
  20. }
  21. func (t *Accesscardtab) TableName() string {
  22. return "accesscardtab"
  23. }
  24. // 清除string字段的右侧空格
  25. func (t *Accesscardtab) Clipped() {
  26. utils.TrimStruct(t, *t)
  27. }
  28. //增
  29. func (t *Accesscardtab) Add() error {
  30. e := db.MasterEngine()
  31. countrole := new(Accesscardtab)
  32. affw, err := e.Table("accesscardtab").ID(core.PK{t.Cid, t.Accesscardid}).Count(countrole)
  33. if err != nil {
  34. return err
  35. }
  36. if affw > 0 {
  37. return errors.New("数据已经存在!")
  38. }
  39. _, err = e.Table("accesscardtab").Insert(t)
  40. if err != nil {
  41. return err
  42. }
  43. return nil
  44. }
  45. //删
  46. func (t *Accesscardtab) Del() bool {
  47. e := db.MasterEngine()
  48. _, err := e.ID(core.PK{t.Cid, t.Accesscardid}).Delete(&Accesscardtab{})
  49. if err != nil {
  50. return false
  51. }
  52. return true
  53. }
  54. //改
  55. func (t *Accesscardtab) Update() bool {
  56. e := db.MasterEngine()
  57. _, err := e.ID(core.PK{t.Cid, t.Accesscardid}).Update(t)
  58. if err != nil {
  59. return false
  60. }
  61. return true
  62. }
  63. //查
  64. func (t *Accesscardtab) SelectOne() (Accesscardtab, error) {
  65. e := db.MasterEngine()
  66. var data Accesscardtab
  67. _, err := e.ID(core.PK{t.Cid, t.Accesscardid}).Get(&data)
  68. if err != nil {
  69. return data, err
  70. }
  71. return data, nil
  72. }
  73. //分页
  74. func (t *Accesscardtab) GetPage(pageSize int, pageIndex int) ([]Accesscardtab, int, error) {
  75. data := make([]Accesscardtab, 0)
  76. e := db.MasterEngine()
  77. query := e.Table("accesscardtab").Where("cid = ? ", t.Cid)
  78. table := e.Table("accesscardtab").Where("cid = ? ", t.Cid)
  79. if !utils.ValueIsEmpty(t.Descr) {
  80. descr := "%" + t.Descr + "%"
  81. query = query.And("descr like ?", descr)
  82. table = table.And("descr like ?", descr)
  83. }
  84. Offset := (pageIndex - 1) * pageSize
  85. err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data)
  86. pcount := new(Accesscardtab)
  87. count, err := table.Count(pcount)
  88. if err != nil {
  89. return data, 0, err
  90. }
  91. return data, int(count), nil
  92. }
  93. type AccesscardContract struct {
  94. Accesscardtab `xorm:"extends"`
  95. Propertytab `xorm:"extends"`
  96. Propertytypetab `xorm:"extends"`
  97. Contracttab `xorm:"extends"`
  98. }
  99. type ContractInfo struct {
  100. Cid int `json:"cid"`
  101. Carportid string `json:"carportid"`
  102. Propertyid string `json:"propertyid"`
  103. Descr string `json:"descr"`
  104. Buildingid string `json:"buildingid"`
  105. Chargetype int `json:"chargetype"`
  106. Begindate string `json:"begindate"`
  107. Enddate string `json:"enddate"`
  108. Unit string `json:"unit"`
  109. Chargeway string `json:"chargeway"`
  110. Room string `json:"room"`
  111. Constructionarea float64 `json:"constructionarea"`
  112. Usearea string `json:"usearea"`
  113. Contact string `json:"contact"`
  114. Phone1 string `json:"phone1"`
  115. Phone2 string `json:"phone2"`
  116. Contractid string `json:"contractid"`
  117. Accesscardid string `json:"accesscardid"`
  118. Unitprice float64 `json:"unitprice"`
  119. Chargetime string `json:"chargetime"`
  120. AccesscardidList []string `json:"accesscardlist"`
  121. Contracttab Contracttab `json:"contracttab"`
  122. }
  123. //电梯费查询
  124. func (t *Accesscardtab) Search(buildingid string, unit string, room string) (ContractInfo, error) {
  125. //联查
  126. data := ContractInfo{}
  127. accesscardidList := make([]string, 0)
  128. var info AccesscardContract
  129. e := db.MasterEngine()
  130. 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)
  131. if !utils.ValueIsEmpty(buildingid) {
  132. query = query.And("propertytab.buildingid = ?", buildingid)
  133. }
  134. if !utils.ValueIsEmpty(unit) {
  135. query = query.And("propertytab.unit = ?", unit)
  136. }
  137. if !utils.ValueIsEmpty(room) {
  138. query = query.And("propertytab.room = ?", room)
  139. }
  140. if !utils.ValueIsEmpty(t.Accesscardid) {
  141. query = query.And("accesscardtab.accesscardid = ?", t.Accesscardid)
  142. }
  143. _, err := query.Get(&info)
  144. if err != nil {
  145. return data, err
  146. }
  147. err = e.Table(t.TableName()).Where("cid = ? and propertyid = ?", t.Cid, t.Propertyid).Cols("accesscardid").Find(&accesscardidList)
  148. if err != nil {
  149. return data, err
  150. }
  151. if utils.ValueIsEmpty(info.Propertytab.Propertyid) {
  152. data.Propertyid = buildingid + "-" + unit + "-" + room
  153. } else {
  154. data.Propertyid = info.Propertytab.Propertyid
  155. }
  156. if utils.ValueIsEmpty(info.Propertytab.Room) {
  157. data.Room = room
  158. } else {
  159. data.Room = info.Propertytab.Room
  160. }
  161. if utils.ValueIsEmpty(info.Propertytab.Unit) {
  162. data.Unit = unit
  163. } else {
  164. data.Unit = info.Propertytab.Unit
  165. }
  166. if utils.ValueIsEmpty(info.Propertytab.Buildingid) {
  167. data.Buildingid = buildingid
  168. } else {
  169. data.Buildingid = info.Propertytab.Buildingid
  170. }
  171. data.AccesscardidList = accesscardidList
  172. data.Accesscardid = t.Accesscardid
  173. data.Cid = t.Cid
  174. data.Contractid = info.Propertytab.Contractid
  175. data.Descr = info.Propertytab.Descr
  176. data.Contact = info.Propertytab.Contact
  177. data.Phone1 = info.Propertytab.Phone1
  178. data.Phone2 = info.Propertytab.Phone2
  179. data.Constructionarea = info.Propertytab.Constructionarea
  180. if utils.ValueIsEmpty(info.Propertytypetab.Unitprice) {
  181. //查询费率
  182. var property Propertytypetab
  183. _, err = e.Table("propertytypetab").Where("cid = ? and propertytypeid = ?", t.Cid, "电梯费").Get(&property)
  184. if err != nil {
  185. return data, err
  186. }
  187. data.Unitprice = property.Unitprice
  188. } else {
  189. data.Unitprice = info.Propertytypetab.Unitprice
  190. }
  191. data.Contracttab = info.Contracttab
  192. data.Chargetype = 2
  193. return data, err
  194. }