user_zone.go 6.8 KB

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