123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "cp-organization-management/errors"
- "fmt"
- "time"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- )
- type Zone struct {
- Id int64 `json:"id" gorm:"primary_key"`
- ZoneCode string `json:"zone_code" description:"区域编号" gorm:"zone_code;commnet:'区域编号';not null;default:''"`
- ZoneName string `json:"zone_name" gorm:"zone_name;commnet:'区域名';not null;default:''"`
- ParentZoneCode string `json:"parent_zone_code" gorm:"parent_zone_code;commnet:'父区域';not null;default:''"`
- CreatedAt time.Time `json:"created_at" gorm:"created_at"`
- UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
- Level int `json:"level" gorm:"level"`
- table string `json:"table"`
- }
- func (p *Zone) TableName() string {
- return p.table
- }
- func NewZone(database string) *Zone {
- return &Zone{table: fmt.Sprintf("%s.%s", database, "t_zone")}
- }
- func (p *Zone) CreateTable(db *gorm.DB) error {
- err := db.CreateTable(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return nil
- }
- func (p *Zone) Find(db *gorm.DB, where map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) FindLast(db *gorm.DB, where map[string]interface{}) error {
- err := db.Table(p.TableName()).Where(where).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) Last(db *gorm.DB) error {
- err := db.Table(p.TableName()).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入一条记录
- func (p *Zone) Insert(db *gorm.DB) error {
- timeNow := time.Now()
- p.CreatedAt = timeNow
- p.UpdatedAt = timeNow
- err := db.Create(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) Delete(db *gorm.DB, filter map[string]interface{}) error {
- cond, val, _ := whereBuild(filter)
- err := db.Where(cond, val...).Delete(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) Save(db *gorm.DB) error {
- p.UpdatedAt = time.Now()
- err := db.Save(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) Update(db *gorm.DB) error {
- p.UpdatedAt = time.Now()
- err := db.Model(p).Updates(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
- func (p *Zone) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
- if filed == nil {
- return errors.ParamsError
- }
- filed["updated_at"] = time.Now()
- err := db.Model(p).Updates(filed).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
- err := db.Table(p.TableName()).Where(filter).Find(out).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) QueryAllNew(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
- cond, val, _ := whereBuild(filter)
- err := db.Table(p.TableName()).Where(cond, val...).Find(out).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) List(db *gorm.DB, limit, offset int, out interface{}) error {
- err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) Count(db *gorm.DB) (int64, error) {
- var count int64
- err := db.Table(p.TableName()).Count(&count).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return count, err
- }
- func (p *Zone) LevelUpdate(db *gorm.DB, srcCode, dstCode string, levelDiff int) (error) {
- 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)
- sql = sql + fmt.Sprintf(" where zone_code like '%s%%'", srcCode)
- err := db.Exec(sql).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", sql)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Zone) GetZonesByCodes(db *gorm.DB, codes []string) ([]Zone, error) {
- list := []Zone{}
- if len(codes) == 0 {
- return list, nil
- }
- sql := fmt.Sprintf("select * from %s", p.TableName())
- where := ""
- for _, v := range codes {
- if where == "" {
- where = fmt.Sprintf(" where zone_code like '%s%%'", v)
- continue
- }
- where = fmt.Sprintf("%s or zone_code like '%s%%'", where, v)
- }
- sql = sql + where
- err := db.Raw(sql).Scan(&list).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", sql)),
- zap.String("error", err.Error()))
- }
- return list, err
- }
|