area.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. )
  10. type TArea struct {
  11. ID int64 `gorm:"column:id" json:"id"`
  12. Name string `gorm:"column:name" json:"name"`
  13. Pid int64 `gorm:"column:pid" json:"pid"`
  14. ProvinceCode int64 `gorm:"column:province_code" json:"province_code"`
  15. CityCode int64 `gorm:"column:city_code" json:"city_code"`
  16. AreaCode int64 `gorm:"column:area_code" json:"area_code"`
  17. StreetCode int64 `gorm:"column:street_code" json:"street_code"`
  18. CommitteeCode int64 `gorm:"column:committee_code" json:"committee_code"`
  19. CommitteeType int64 `gorm:"column:committee_type" json:"committee_type"`
  20. Sort int64 `gorm:"column:sort" json:"sort"`
  21. Level int64 `gorm:"column:level" json:"level"`
  22. }
  23. func (p *TArea) TableName() string {
  24. return "t_area"
  25. }
  26. func (p *TArea) Find(db *gorm.DB, where map[string]interface{}) error {
  27. cond, val, err := whereBuild(where)
  28. if err != nil {
  29. if err != nil {
  30. logger.Error("mysql",
  31. zap.String("sql", "select "+p.TableName()),
  32. zap.String("error", err.Error()))
  33. }
  34. return err
  35. }
  36. err = db.Table(p.TableName()).Where(cond, val...).Find(p).Error
  37. if err != nil {
  38. fields, _ := util.MarshalToString(where)
  39. logger.Error("mysql",
  40. zap.String("sql", "select from "+p.TableName()),
  41. zap.String("fields", fields),
  42. zap.String("error", err.Error()))
  43. }
  44. return err
  45. }
  46. func (p *TArea) Last(db *gorm.DB) error {
  47. err := db.Table(p.TableName()).Last(p).Error
  48. if err != nil {
  49. logger.Error("mysql",
  50. zap.String("sql", "select last from "+p.TableName()),
  51. zap.String("fields", ""),
  52. zap.String("error", err.Error()))
  53. }
  54. return err
  55. }
  56. // Insert 插入一条记录
  57. func (p *TArea) Insert(db *gorm.DB) error {
  58. err := db.Create(p).Error
  59. if err != nil {
  60. fields, _ := util.MarshalToString(*p)
  61. logger.Error("mysql",
  62. zap.String("sql", "insert into "+p.TableName()),
  63. zap.String("fields", fields),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. func (p *TArea) Delete(db *gorm.DB, filter map[string]interface{}) error {
  69. err := db.Where(filter).Delete(p).Error
  70. if err != nil {
  71. fields, _ := util.MarshalToString(filter)
  72. logger.Error("mysql",
  73. zap.String("sql", "delete from "+p.TableName()),
  74. zap.String("where", fields),
  75. zap.String("error", err.Error()))
  76. }
  77. return err
  78. }
  79. func (p *TArea) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  80. cond, val, err := whereBuild(where)
  81. if err != nil {
  82. if err != nil {
  83. fields, _ := util.MarshalToString(values)
  84. logger.Error("mysql",
  85. zap.String("sql", "update "+p.TableName()),
  86. zap.String("fields", fields),
  87. zap.String("error", err.Error()))
  88. }
  89. return err
  90. }
  91. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  92. }
  93. func (p *TArea) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  94. cond, val, err := whereBuildAndOr(where, or)
  95. if err != nil {
  96. return 0, err
  97. }
  98. ret := int64(0)
  99. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  100. if err != nil {
  101. fields, _ := util.MarshalToString(where)
  102. logger.Error("mysql",
  103. zap.String("sql", "select count "+p.TableName()),
  104. zap.String("fields", fields),
  105. zap.String("error", err.Error()))
  106. }
  107. return ret, err
  108. }
  109. func (p *TArea) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TArea, err error) {
  110. cond, val, err := whereBuildAndOr(where, or)
  111. if err != nil {
  112. return list, err
  113. }
  114. if pageSize < 0 {
  115. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  116. if result.Error != nil {
  117. wherefields, _ := util.MarshalToString(where)
  118. logger.Error("mysql",
  119. zap.String("sql", "select * from "+p.TableName()),
  120. zap.String("where", wherefields),
  121. zap.String("error", result.Error.Error()))
  122. }
  123. return list, result.Error
  124. }
  125. offset := (page - 1) * pageSize
  126. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  127. if result.Error != nil {
  128. wherefields, _ := util.MarshalToString(where)
  129. logger.Error("mysql",
  130. zap.String("sql", "select * from "+p.TableName()),
  131. zap.String("where", wherefields),
  132. zap.String("error", result.Error.Error()))
  133. }
  134. return list, result.Error
  135. }