t_district.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package model
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. )
  6. type TDistrict struct {
  7. Id int64 `gorm:"column:id" json:"id"`
  8. Name string `gorm:"column:name" json:"name"`
  9. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  10. Initial string `gorm:"column:initial" json:"initial"`
  11. Initials string `gorm:"column:initials" json:"initials"`
  12. Pinyin string `gorm:"column:pinyin" json:"pinyin"`
  13. Extra string `gorm:"column:extra" json:"extra"`
  14. Suffix string `gorm:"column:suffix" json:"suffix"`
  15. Code string `gorm:"column:code" json:"code"`
  16. AreaCode string `gorm:"column:area_code" json:"area_code"`
  17. Order int64 `gorm:"column:order" json:"order"`
  18. }
  19. func (TDistrict) TableName() string {
  20. return "t_district"
  21. }
  22. type TDistrictItem struct {
  23. TDistrict
  24. }
  25. func (p *TDistrict) List(db *gorm.DB, parentId []int64) (list []TDistrictItem, err error) {
  26. sql := fmt.Sprintf("SELECT * FROM `t_district` where parent_id in(?) and name <> '海外'")
  27. err = db.Raw(sql, parentId).Scan(&list).Error
  28. return list, err
  29. }
  30. func (p *TDistrict) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  31. if len(where) > 0 || len(or) > 0{
  32. cond, val, err := whereBuildAndOr(where, or)
  33. if err != nil {
  34. return 0, err
  35. }
  36. ret := int64(0)
  37. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  38. return ret, err
  39. }
  40. ret := int64(0)
  41. err := db.Table(p.TableName()).Count(&ret).Error
  42. return ret, err
  43. }