123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package model
- import (
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "time"
- )
- type THouse struct {
- ID int64 `gorm:"column:id" json:"id"`
- GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
- Uid int64 `gorm:"column:uid" json:"uid"`
- ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
- ApproveStatus int64 `gorm:"column:approve_status" json:"approve_status"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- UserType int32 `gorm:"column:user_type" json:"user_type"`
- HouseId int64 `gorm:"column:house_id" json:"house_id"`
- BuildingNumber string `gorm:"column:building_number" json:"building_number"`
- UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
- HouseNumber string `gorm:"column:house_number" json:"house_number"`
- UniqFlag int64 `gorm:"column:uniq_flag" json:"uniq_flag"`
- Appendix string `gorm:"column:appendix" json:"appendix"`
- Feedback string `gorm:"column:feedback" json:"feedback"`
- BuildingId int64 `gorm:"column:building_id" json:"building_id"`
- UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
- Phone string `gorm:"column:phone" json:"phone"`
- Name string `gorm:"column:name" json:"name"`
- IdNumber string `gorm:"column:id_number" json:"id_number"`
- IdType int32 `gorm:"column:id_type" json:"id_type"`
- OpenId string `gorm:"column:open_id" json:"open_id"`
- }
- func (p *THouse) TableName() string {
- return "t_house"
- }
- func (p *THouse) Find(db *gorm.DB, where map[string]interface{}) error {
- err := db.Table(p.TableName()).Where(where).Find(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouse) Last(db *gorm.DB) error {
- err := db.Table(p.TableName()).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "select last from "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入一条记录
- func (p *THouse) Insert(db *gorm.DB) error {
- err := db.Create(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouse) Delete(db *gorm.DB, filter map[string]interface{}) error {
- cond, val, err := whereBuild(filter)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
- }
- func (p *THouse) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *THouse) UpdateAffected(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) (int64, error) {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return 0, err
- }
- r := db.Table(p.TableName()).Where(cond, val...).Updates(values)
- return r.RowsAffected, r.Error
- }
- func (p *THouse) UpdateByModel(db *gorm.DB) error {
- err := db.Model(p).Updates(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouse) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return ret, err
- }
- func (p *THouse) CountDistinct(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, distinct string) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Distinct(distinct).Count(&ret).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return ret, err
- }
- func (p *THouse) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouse, err error) {
- if page >= 0 {
- return p.ListByJoin(db, where, or, page, pageSize)
- }
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- func (p *THouse) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouse, err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- offset := (page - 1) * pageSize
- sql := fmt.Sprintf("select * from %s t1 join (select id from %s order by created_at desc limit %d offset %d) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), pageSize, offset)
- if cond != "" {
- sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s order by created_at desc limit %d offset %d) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), cond, pageSize, offset)
- }
- if page == -1 || pageSize == -1 {
- sql = fmt.Sprintf("select * from %s t1 join (select id from %s ) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName())
- if cond != "" {
- sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s ) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), cond)
- }
- }
- result := db.Raw(sql, val...).Scan(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
|