// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "github.com/jinzhu/gorm" "go.uber.org/zap" "smart-site-management/errors" "time" "github.com/jaryhe/gopkgs/logger" "encoding/json" ) type TCompany struct { Id int64 `gorm:"column:id" json:"id" form:"id"` Name string `gorm:"column:name" json:"name" form:"name"` SocialCode string `gorm:"column:social_code" json:"social_code" form:"social_code"` LegalPerson string `gorm:"column:legal_person" json:"legal_person" form:"legal_person"` IdCert string `gorm:"column:id_cert" json:"id_cert" form:"id_cert"` BusinessLicense string `gorm:"column:business_license" json:"business_license" form:"business_license"` UserName string `gorm:"column:user_name" json:"user_name" form:"user_name"` Passwd string `gorm:"column:passwd" json:"passwd" form:"passwd"` BusinessContact string `gorm:"column:business_contact" json:"business_contact" form:"business_contact"` BusinessContactPhone string `gorm:"column:business_contact_phone" json:"business_contact_phone" form:"business_contact_phone"` BusinessResponsible string `gorm:"column:business_responsible" json:"business_responsible" form:"business_responsible"` BusinessResponsiblePhone string `gorm:"column:business_responsible_phone" json:"business_responsible_phone" form:"business_responsible_phone"` EmergencyContact string `gorm:"column:emergency_contact" json:"emergency_contact" form:"emergency_contact"` EmergencyContactPhone string `gorm:"column:emergency_contact_phone" json:"emergency_contact_phone" form:"emergency_contact_phone"` Province string `gorm:"column:province" json:"province" form:"province"` City string `gorm:"column:city" json:"city" form:"city"` Zone string `gorm:"column:zone" json:"zone" form:"zone"` Status int64 `gorm:"column:status" json:"status" form:"status"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"` } var PageSize = 10 func (TCompany) TableName() string { return "t_company" } func (p *TCompany) Insert(db *gorm.DB) error { return db.Create(p).Error } func (p *TCompany) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error { cond, val, err := whereBuildAndOr(where, or) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).First(p).Error } func (p *TCompany) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } err = db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error if err != nil { fields, _ := json.Marshal(where) updateValues, _ := json.Marshal(values) logger.Error("mysql", zap.String("sql", "update t_company"), zap.String("fields", string(fields)), zap.String("values", string(updateValues)), zap.String("error", err.Error())) return errors.DataBaseError } return err } func (p *TCompany) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error { cond, val, err := whereBuild(where) if err != nil { return err } ps := []TCompany{} err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error if err != nil { fields, _ := json.Marshal(where) logger.Error("mysql", zap.String("sql", "select from t_company order by " + sort), zap.String("fields", string(fields)), zap.String("error", err.Error())) return errors.DataBaseError } if len(ps) > 0 { *p = ps[0] } return nil } func (p *TCompany) Save(db *gorm.DB) error { err := db.Save(p).Error if err != nil { fields, _ := json.Marshal(p) logger.Error("mysql", zap.String("sql", "save t_company"), zap.String("fields", string(fields)), zap.String("error", err.Error())) return errors.DataBaseError } return err } func (p *TCompany) List(db *gorm.DB, where map[string]interface{}, page int) (list []TCompany, err error) { if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return list, err } err = db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list).Error if err != nil { fields, _ := json.Marshal(where) logger.Error("mysql", zap.String("sql", "select * from t_company "), zap.String("fields", string(fields)), zap.String("error", err.Error())) return list, errors.DataBaseError } return list, err } err = db.Table(p.TableName()).Limit(10).Offset(page).Find(&list).Error if err != nil { fields, _ := json.Marshal(where) logger.Error("mysql", zap.String("sql", "select * from t_company "), zap.String("fields", string(fields)), zap.String("error", err.Error())) return list, errors.DataBaseError } return list, err } func (p *TCompany) All(db *gorm.DB) (list []TCompany, err error) { err = db.Table(p.TableName()).Find(&list).Error if err != nil { logger.Error("mysql", zap.String("sql", "select * from t_company "), zap.String("error", err.Error())) return list, errors.DataBaseError } return list, err } func (p *TCompany) Count(db *gorm.DB, where map[string]interface{}) (int64, error) { if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return 0, err } ret := int64(0) err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error if err != nil { fields, _ := json.Marshal(where) logger.Error("mysql", zap.String("sql", "select count(1) from t_company "), zap.String("fields", string(fields)), zap.String("error", err.Error())) return ret, errors.DataBaseError } return ret, err } ret := int64(0) err := db.Table(p.TableName()).Count(&ret).Error if err != nil { fields, _ := json.Marshal(where) logger.Error("mysql", zap.String("sql", "select count(1) from t_company "), zap.String("fields", string(fields)), zap.String("error", err.Error())) return ret, errors.DataBaseError } return ret, err } func (p *TCompany) Del(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...).Delete(p).Error if err != nil { fields, _ := json.Marshal(where) logger.Error("mysql", zap.String("sql", "delete from t_company "), zap.String("fields", string(fields)), zap.String("error", err.Error())) return errors.DataBaseError } return err }