base_district.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package base
  2. import (
  3. "context"
  4. "github.com/gogf/gf/errors/gerror"
  5. "github.com/gogf/gf/frame/g"
  6. "dashoo.cn/micro/app/dao/base"
  7. model "dashoo.cn/micro/app/model/base"
  8. "dashoo.cn/micro/app/service"
  9. )
  10. type districtService struct {
  11. *service.ContextService
  12. Dao *base.BaseDistrictDao
  13. }
  14. func NewDistrictService(ctx context.Context) (svc *districtService, err error) {
  15. svc = new(districtService)
  16. if svc.ContextService, err = svc.Init(ctx); err != nil {
  17. return nil, err
  18. }
  19. svc.Dao = base.NewBaseDistrictDao(svc.Tenant)
  20. return svc, nil
  21. }
  22. //区域省市区 树形结构
  23. func (d *districtService) ListToTree(pid int) []*model.T {
  24. var distributorList []model.BaseDistrict
  25. g.Log().Info("ID", pid)
  26. d.Dao.M.Where(base.BaseDistrict.Columns.ParentId, pid).Scan(&distributorList)
  27. treeList := []*model.T{}
  28. for _, v := range distributorList {
  29. if v.ParentId == pid {
  30. child := d.ListToTree(v.Id)
  31. node := &model.T{
  32. Id: v.Id,
  33. ParentId: v.ParentId,
  34. DistName: v.DistName,
  35. }
  36. node.Children = child
  37. treeList = append(treeList, node)
  38. }
  39. }
  40. g.Log().Info("xc11", treeList)
  41. return treeList
  42. }
  43. func (d *districtService) GetProvinceInfo() (list []*model.Province) {
  44. Model := d.Dao.M
  45. err := Model.Where(base.BaseDistrict.Columns.ParentId, 0).Scan(&list)
  46. if err != nil {
  47. g.Log().Error(err)
  48. err = gerror.New("获取数据失败")
  49. return
  50. }
  51. return
  52. }