package model import ( "fmt" "github.com/jinzhu/gorm" ) type TDistrict struct { Id int64 `gorm:"column:id" json:"id"` Name string `gorm:"column:name" json:"name"` ParentId int64 `gorm:"column:parent_id" json:"parent_id"` Initial string `gorm:"column:initial" json:"initial"` Initials string `gorm:"column:initials" json:"initials"` Pinyin string `gorm:"column:pinyin" json:"pinyin"` Extra string `gorm:"column:extra" json:"extra"` Suffix string `gorm:"column:suffix" json:"suffix"` Code string `gorm:"column:code" json:"code"` AreaCode string `gorm:"column:area_code" json:"area_code"` Order int64 `gorm:"column:order" json:"order"` } func (TDistrict) TableName() string { return "t_district" } type TDistrictItem struct { TDistrict } func (p *TDistrict) List(db *gorm.DB, parentId []int64) (list []TDistrictItem, err error) { sql := fmt.Sprintf("SELECT * FROM `t_district` where parent_id in(?) and name <> '海外'") err = db.Raw(sql, parentId).Scan(&list).Error return list, err } func (p *TDistrict) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) { if len(where) > 0 || len(or) > 0{ 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 return ret, err } ret := int64(0) err := db.Table(p.TableName()).Count(&ret).Error return ret, err }