赛思维服务调研
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.

100 lines
2.7 KiB

  1. package models
  2. import (
  3. "SSW_WebPlatform/db"
  4. "SSW_WebPlatform/utils"
  5. "errors"
  6. "xorm.io/core"
  7. )
  8. type Company struct {
  9. Cnr int `json:"cnr" xorm:"not null pk comment('小区ID') INT(11)"`
  10. Cid int `json:"cid" xorm:"not null pk INT(11)"`
  11. Descr string `json:"descr" xorm:"default 'NULL' comment('描述') VARCHAR(100)"`
  12. Active int `json:"active" xorm:"default NULL comment('是否激活') INT(11)"`
  13. Address string `json:"address" xorm:"default 'NULL' comment('地址') VARCHAR(255)"`
  14. Constructionarea string `json:"constructionarea" xorm:"default NULL comment('建筑面积') DECIMAL(10,2)"`
  15. Totalhouseholds int `json:"totalhouseholds" xorm:"default NULL comment('住户数') INT(11)"`
  16. Totalbuildings int `json:"totalbuildings" xorm:"default NULL comment('建筑数') INT(11)"`
  17. Createtime string `json:"createtime" xorm:"not null comment('创建时间') VARCHAR(40)"`
  18. Lastmodifytime string `json:"lastmodifytime" xorm:"not null comment('上次更新时间') VARCHAR(40)"`
  19. Lastmodifyby string `json:"lastmodifyby" xorm:"default 'NULL' comment('更新人员') VARCHAR(40)"`
  20. }
  21. func (t *Company) TableName() string {
  22. return "company"
  23. }
  24. // 清除string字段的右侧空格
  25. func (t *Company) Clipped() {
  26. utils.TrimStruct(t, *t)
  27. }
  28. //增
  29. func (t *Company) Add() error {
  30. e := db.MasterEngine()
  31. countrole := new(Company)
  32. affw, err := e.Table("company").ID(core.PK{t.Cnr, t.Cid}).Count(countrole)
  33. if err != nil {
  34. return err
  35. }
  36. if affw > 0 {
  37. return errors.New("数据已经存在!")
  38. }
  39. _, err = e.Table("company").Insert(t)
  40. if err != nil {
  41. return err
  42. }
  43. return nil
  44. }
  45. //删
  46. func (t *Company) Del() bool {
  47. e := db.MasterEngine()
  48. _, err := e.ID(core.PK{t.Cnr, t.Cid}).Delete(&Company{})
  49. if err != nil {
  50. return false
  51. }
  52. return true
  53. }
  54. //改
  55. func (t *Company) Update() bool {
  56. e := db.MasterEngine()
  57. _, err := e.ID(core.PK{t.Cnr, t.Cid}).Update(t)
  58. if err != nil {
  59. return false
  60. }
  61. return true
  62. }
  63. //查
  64. func (t *Company) SelectOne() (Company, error) {
  65. e := db.MasterEngine()
  66. var data Company
  67. _, err := e.ID(core.PK{t.Cnr, t.Cid}).Get(&data)
  68. if err != nil {
  69. return data, err
  70. }
  71. return data, nil
  72. }
  73. //分页
  74. func (t *Company) GetPage(pageSize int, pageIndex int) ([]Company, int, error) {
  75. data := make([]Company, 0)
  76. e := db.MasterEngine()
  77. query := e.Table("company").Where("cid = ? ", t.Cid)
  78. table := e.Table("company").Where("cid = ? ", t.Cid)
  79. if t.Cnr > 0 {
  80. query = query.And("cnr = ?", t.Cnr)
  81. table = table.And("cnr = ?", t.Cnr)
  82. }
  83. Offset := (pageIndex - 1) * pageSize
  84. err := query.Limit(pageSize, Offset).Desc("createtime").Find(&data)
  85. pcount := new(Company)
  86. count, err := table.Count(pcount)
  87. if err != nil {
  88. return data, 0, err
  89. }
  90. return data, int(count), nil
  91. }