// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. 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 TCompany struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` Applicant string `gorm:"column:applicant" json:"applicant"` ApplicantPhone string `gorm:"column:applicant_phone" json:"applicant_phone"` CompanyName string `gorm:"column:company_name" json:"company_name"` CompanyPhone string `gorm:"column:company_phone" json:"company_phone"` ApplicantDuty string `gorm:"column:applicant_duty" json:"applicant_duty"` LegalPerson string `gorm:"column:legal_person" json:"legal_person"` CompanyAddr string `gorm:"column:company_addr" json:"company_addr"` SocialCode string `gorm:"column:social_code" json:"social_code"` BusinessLicense string `gorm:"column:business_license" json:"business_license"` ApplicantIdentification string `gorm:"column:applicant_identification" json:"applicant_identification"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"` ApproveStatus int64 `gorm:"column:approve_status" json:"approve_status"` LicenseType int64 `gorm:"column:license_type" json:"license_type"` FreeGardenCount int64 `gorm:"column:free_garden_count" json:"free_garden_count"` GardenCount int64 `gorm:"column:garden_count" json:"garden_count"` User string `gorm:"column:user" json:"user"` Password string `gorm:"column:password" json:"password"` Expire int64 `gorm:"column:expire" json:"expire"` UniqFlag int `gorm:"column:uniq_flag" json:"uniq_flag"` Feedback string `gorm:"column:feedback" json:"feedback"` Logo string `gorm:"column:logo" json:"logo"` Site string `gorm:"column:site" json:"site"` Desc string `gorm:"column:desc" json:"desc"` //PayMode int32 `gorm:"column:pay_mode" json:"pay_mode"` } func (p *TCompany) TableName() string { return "t_company" } func (p *TCompany) 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 *TCompany) 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 *TCompany) 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 *TCompany) Delete(db *gorm.DB, filter map[string]interface{}) error { err := db.Where(filter).Delete(p).Error if err != nil { fields, _ := util.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "delete from "+p.TableName()), zap.String("where", fields), zap.String("error", err.Error())) } return err } func (p *TCompany) 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 *TCompany) 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 *TCompany) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TCompany, err error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } if pageSize < 0 { result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").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 } offset := (page - 1) * pageSize result := db.Table(p.TableName()).Where(cond, val...).Order("id desc").Limit(pageSize).Offset(offset).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 *TCompany) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TCompany, 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 limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), pageSize, offset) if cond != "" { sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, pageSize, offset) } 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 }