zone.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. "cp-organization-management/errors"
  6. "fmt"
  7. "time"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "github.com/jinzhu/gorm"
  10. "go.uber.org/zap"
  11. )
  12. type Zone struct {
  13. Id int64 `json:"id" gorm:"primary_key"`
  14. ZoneCode string `json:"zone_code" description:"区域编号" gorm:"zone_code;commnet:'区域编号';not null;default:''"`
  15. ZoneName string `json:"zone_name" gorm:"zone_name;commnet:'区域名';not null;default:''"`
  16. ParentZoneCode string `json:"parent_zone_code" gorm:"parent_zone_code;commnet:'父区域';not null;default:''"`
  17. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  18. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  19. Level int `json:"level" gorm:"level"`
  20. table string `json:"table"`
  21. }
  22. func (p *Zone) TableName() string {
  23. return p.table
  24. }
  25. func NewZone(database string) *Zone {
  26. return &Zone{table: fmt.Sprintf("%s.%s", database, "t_zone")}
  27. }
  28. func (p *Zone) CreateTable(db *gorm.DB) error {
  29. err := db.CreateTable(p).Error
  30. if err != nil {
  31. logger.Error("mysql",
  32. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  33. zap.String("error", err.Error()))
  34. }
  35. return nil
  36. }
  37. func (p *Zone) Find(db *gorm.DB, where map[string]interface{}) error {
  38. cond, val, err := whereBuild(where)
  39. if err != nil {
  40. return err
  41. }
  42. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  43. if err != nil {
  44. logger.Error("mysql",
  45. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  46. zap.String("error", err.Error()))
  47. }
  48. return err
  49. }
  50. func (p *Zone) FindLast(db *gorm.DB, where map[string]interface{}) error {
  51. err := db.Table(p.TableName()).Where(where).Last(p).Error
  52. if err != nil {
  53. logger.Error("mysql",
  54. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *Zone) Last(db *gorm.DB) error {
  60. err := db.Table(p.TableName()).Last(p).Error
  61. if err != nil {
  62. logger.Error("mysql",
  63. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. // Insert 插入一条记录
  69. func (p *Zone) Insert(db *gorm.DB) error {
  70. timeNow := time.Now()
  71. p.CreatedAt = timeNow
  72. p.UpdatedAt = timeNow
  73. err := db.Create(p).Error
  74. if err != nil {
  75. logger.Error("mysql",
  76. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *Zone) Delete(db *gorm.DB, filter map[string]interface{}) error {
  82. cond, val, _ := whereBuild(filter)
  83. err := db.Where(cond, val...).Delete(p).Error
  84. if err != nil {
  85. logger.Error("mysql",
  86. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  87. zap.String("error", err.Error()))
  88. }
  89. return err
  90. }
  91. func (p *Zone) Save(db *gorm.DB) error {
  92. p.UpdatedAt = time.Now()
  93. err := db.Save(p).Error
  94. if err != nil {
  95. logger.Error("mysql",
  96. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  97. zap.String("error", err.Error()))
  98. }
  99. return err
  100. }
  101. func (p *Zone) Update(db *gorm.DB) error {
  102. p.UpdatedAt = time.Now()
  103. err := db.Model(p).Updates(p).Error
  104. if err != nil {
  105. logger.Error("mysql",
  106. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  107. zap.String("error", err.Error()))
  108. }
  109. return err
  110. }
  111. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  112. func (p *Zone) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  113. if filed == nil {
  114. return errors.ParamsError
  115. }
  116. filed["updated_at"] = time.Now()
  117. err := db.Model(p).Updates(filed).Error
  118. if err != nil {
  119. logger.Error("mysql",
  120. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  121. zap.String("error", err.Error()))
  122. }
  123. return err
  124. }
  125. func (p *Zone) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  126. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  127. if err != nil {
  128. logger.Error("mysql",
  129. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  130. zap.String("error", err.Error()))
  131. }
  132. return err
  133. }
  134. func (p *Zone) QueryAllNew(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  135. cond, val, _ := whereBuild(filter)
  136. err := db.Table(p.TableName()).Where(cond, val...).Find(out).Error
  137. if err != nil {
  138. logger.Error("mysql",
  139. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  140. zap.String("error", err.Error()))
  141. }
  142. return err
  143. }
  144. func (p *Zone) List(db *gorm.DB, limit, offset int, out interface{}) error {
  145. err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error
  146. if err != nil {
  147. logger.Error("mysql",
  148. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  149. zap.String("error", err.Error()))
  150. }
  151. return err
  152. }
  153. func (p *Zone) Count(db *gorm.DB) (int64, error) {
  154. var count int64
  155. err := db.Table(p.TableName()).Count(&count).Error
  156. if err != nil {
  157. logger.Error("mysql",
  158. zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
  159. zap.String("error", err.Error()))
  160. }
  161. return count, err
  162. }
  163. func (p *Zone) LevelUpdate(db *gorm.DB, srcCode, dstCode string, levelDiff int) (error) {
  164. sql := fmt.Sprintf("update %s set zone_code=concat('%s', right(zone_code, length(zone_code)-%d)), parent_zone_code=concat('%s', right(parent_zone_code, length(parent_zone_code)-%d)), level=level+%d", p.TableName(), dstCode, len(srcCode), dstCode, len(srcCode), levelDiff)
  165. sql = sql + fmt.Sprintf(" where zone_code like '%s%%'", srcCode)
  166. err := db.Exec(sql).Error
  167. if err != nil {
  168. logger.Error("mysql",
  169. zap.String("sql", fmt.Sprintf("%+v", sql)),
  170. zap.String("error", err.Error()))
  171. }
  172. return err
  173. }
  174. func (p *Zone) GetZonesByCodes(db *gorm.DB, codes []string) ([]Zone, error) {
  175. list := []Zone{}
  176. if len(codes) == 0 {
  177. return list, nil
  178. }
  179. sql := fmt.Sprintf("select * from %s", p.TableName())
  180. where := ""
  181. for _, v := range codes {
  182. if where == "" {
  183. where = fmt.Sprintf(" where zone_code like '%s%%'", v)
  184. continue
  185. }
  186. where = fmt.Sprintf("%s or zone_code like '%s%%'", where, v)
  187. }
  188. sql = sql + where
  189. err := db.Raw(sql).Scan(&list).Error
  190. if err != nil {
  191. logger.Error("mysql",
  192. zap.String("sql", fmt.Sprintf("%+v", sql)),
  193. zap.String("error", err.Error()))
  194. }
  195. return list, err
  196. }