diff --git a/dal/types/user_meta.go b/dal/types/user_meta.go new file mode 100644 index 0000000..3f2916e --- /dev/null +++ b/dal/types/user_meta.go @@ -0,0 +1,17 @@ +package types + +import "git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/types" + +// AddressMeta 地址数据,保存地区编号 +type AddressMeta struct { + Country string `json:"country"` // 国家(目前固定为CN) + Province string `json:"province"` // 省级 + City string `json:"city"` // 地级 + Area string `json:"area"` // 县级 + Street string `json:"street"` // 乡级 +} + +type CommonUserMeta struct { + *AddressMeta + Gender types.Gender `json:"gender"` // 性别 +} diff --git a/go.mod b/go.mod index 8791fc3..7cb79c6 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,9 @@ require ( github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgraph-io/ristretto v0.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dustin/go-humanize v1.0.0 // indirect github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/fatih/color v1.15.0 // indirect github.com/go-logr/logr v1.2.4 // indirect @@ -37,6 +39,7 @@ require ( github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/gofrs/uuid/v5 v5.0.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/glog v1.1.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/mock v1.6.0 // indirect github.com/golang/protobuf v1.5.3 // indirect diff --git a/go.sum b/go.sum index 2c24372..35a0ad6 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,7 @@ github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= github.com/alicebob/miniredis/v2 v2.30.4 h1:8S4/o1/KoUArAGbGwPxcwf0krlzceva2XVOSchFS7Eo= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= @@ -84,9 +85,14 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= +github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -99,10 +105,12 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= @@ -123,6 +131,7 @@ github.com/go-redis/redismock/v8 v8.0.6 h1:rtuijPgGynsRB2Y7KDACm09WvjHWS4RaG44Nm github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M= github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= @@ -131,6 +140,7 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= +github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -214,10 +224,12 @@ github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -229,6 +241,7 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4= +github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= @@ -254,9 +267,11 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nicksnyder/go-i18n/v2 v2.2.1 h1:aOzRCdwsJuoExfZhoiXHy4bjruwCMdt5otbYojM/PaA= github.com/nicksnyder/go-i18n/v2 v2.2.1/go.mod h1:fF2++lPHlo+/kPaj3nB0uxtPwzlPm+BlgwGX7MkeGj0= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= @@ -288,6 +303,7 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -320,6 +336,7 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE= github.com/zclconf/go-cty v1.13.2 h1:4GvrUxe/QUDYuJKAav4EYqdM47/kZa672LwmXFmEKT0= github.com/zclconf/go-cty v1.13.2/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= +github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8= github.com/zeromicro/go-zero v1.5.4 h1:kRvcYuxcHOkUZvg7887KQl77Qv4klGL7MqGkTBgkpS8= github.com/zeromicro/go-zero v1.5.4/go.mod h1:x/aUyLmSwRECvOyjOf+lhwThBOilJIY+s3slmPAeboA= go.etcd.io/etcd/api/v3 v3.5.9 h1:4wSsluwyTbGGmyjJktOf3wFQoTBIURXHnq9n/G/JQHs= @@ -492,6 +509,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -665,6 +683,7 @@ gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/rpc/core/internal/logic/district/cache.go b/rpc/core/internal/logic/district/cache.go new file mode 100644 index 0000000..9e242da --- /dev/null +++ b/rpc/core/internal/logic/district/cache.go @@ -0,0 +1,33 @@ +package district + +import ( + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" + "github.com/dgraph-io/ristretto" +) + +var nameCache *ristretto.Cache + +func init() { + nameCache, _ = ristretto.NewCache(&ristretto.Config{ + NumCounters: 1024, + MaxCost: 10 * 1024 * 1024, + BufferItems: 64, + IgnoreInternalCost: true, + }) +} + +func getCachedDistrictItem(code string) (*svc.DistrictCacheItem, bool) { + v, ok := nameCache.Get(code) + if ok { + return v.(*svc.DistrictCacheItem), true + } + return nil, false +} + +func cacheDistrictItem(code string, nameList []string) { + nameCache.Set(code, &svc.DistrictCacheItem{ + Code: code, + NameList: nameList, + }, 1) + nameCache.Wait() +} diff --git a/rpc/core/internal/logic/district/get_district_list_logic.go b/rpc/core/internal/logic/district/get_district_list_logic.go index d9b3430..8406e35 100644 --- a/rpc/core/internal/logic/district/get_district_list_logic.go +++ b/rpc/core/internal/logic/district/get_district_list_logic.go @@ -2,6 +2,12 @@ package district import ( "context" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/district" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander" + "strings" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" @@ -20,7 +26,98 @@ func NewGetDistrictListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G } func (l *GetDistrictListLogic) GetDistrictList(in *core.DistrictReq) (*core.DistrictListResp, error) { - // todo: add your logic here and delete this line + var predicates []predicate.District + switch in.QueryType { + case core.DistrictQueryType_Children: + if in.ParentCode == "" { + // 参数为空时,查询第一级 + predicates = relationPredicates("", "", "", "", 1, core.Operator_EQ) + } else { + first, err := l.svcCtx.DB.District.Query().Where(district.CodeEQ(in.ParentCode)).First(l.ctx) + if err != nil { + return nil, hander.HandleEntErr(err, in) + } + lv := first.Level + 1 + if first.Level == 1 && strings.Contains(first.Name, "市") { + // 直辖市 Directly + lv = lv + 1 + } + predicates = relationPredicates(first.Province, first.City, first.Area, first.Street, lv, core.Operator_EQ) + } + case core.DistrictQueryType_ChildrenByLevel: + if in.ParentCode != "" { + // 参数为空时,查询指定级别 + predicates = relationPredicates("", "", "", "", in.Level, core.Operator_EQ) + } else { + first, err := l.svcCtx.DB.District.Query().Where(district.CodeEQ(in.ParentCode)).First(l.ctx) + if err != nil { + return nil, hander.HandleEntErr(err, in) + } + lv := first.Level + in.Level + if first.Level == 1 && strings.Contains(first.Name, "市") { + // 直辖市 Directly + lv = lv + 1 + } + predicates = relationPredicates(first.Province, first.City, first.Area, first.Street, lv, core.Operator_EQ) + } + case core.DistrictQueryType_Parents: + first, err := l.svcCtx.DB.District.Query().Where(district.CodeEQ(in.Code)).First(l.ctx) + if err != nil { + return nil, hander.HandleEntErr(err, in) + } + predicates = relationPredicates(first.Province, first.City, first.Area, first.Street, first.Level, core.Operator_LTE) + case core.DistrictQueryType_Normal: + fallthrough + default: + if in.Code != "" { + predicates = append(predicates, district.CodeEQ(in.Code)) + } + if in.Province != "" { + predicates = append(predicates, district.ProvinceEQ(in.Province)) + } + if in.City != "" { + predicates = append(predicates, district.CityEQ(in.City)) + } + if in.Area != "" { + predicates = append(predicates, district.AreaEQ(in.Area)) + } + if in.Street != "" { + predicates = append(predicates, district.StreetEQ(in.Street)) + } + if in.Level != 0 { + predicates = append(predicates, district.LevelEQ(in.Level)) + } + } + + var ( + pagination *core.Pagination + list []*ent.District + err error + ) + + query := l.svcCtx.DB.District.Query().Where(predicates...) + if in.Page != nil { + result, pageErr := query.Page(l.ctx, in.Page.Current, in.Page.Size) + if pageErr == nil { + pagination = utils.ConvertPaginationDb(result.PageDetails) + list = result.List + } else { + err = pageErr + } + } else { + list, err = query.All(l.ctx) + } + if err != nil { + return nil, hander.HandleEntErr(err, in) + } + + resp := &core.DistrictListResp{ + Page: pagination, + Data: make([]*core.DistrictInfo, len(list)), + } + for i, v := range list { + resp.Data[i] = l.svcCtx.Convert.ConvertDistrictRpc(l.ctx, v) + } - return &core.DistrictListResp{}, nil + return resp, nil } diff --git a/rpc/core/internal/logic/district/get_district_logic.go b/rpc/core/internal/logic/district/get_district_logic.go index 57aebdd..7b66b5a 100644 --- a/rpc/core/internal/logic/district/get_district_logic.go +++ b/rpc/core/internal/logic/district/get_district_logic.go @@ -2,6 +2,8 @@ package district import ( "context" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/district" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" @@ -21,7 +23,12 @@ func NewGetDistrictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDi // District management func (l *GetDistrictLogic) GetDistrict(in *core.DistrictCodeReq) (*core.DistrictInfo, error) { - // todo: add your logic here and delete this line + dbData, err := l.svcCtx.DB.District.Query(). + Where(district.CodeEQ(in.Code)). + First(l.ctx) + if err != nil { + return nil, hander.HandleEntErr(err, in) + } - return &core.DistrictInfo{}, nil + return l.svcCtx.Convert.ConvertDistrictRpc(l.ctx, dbData), nil } diff --git a/rpc/core/internal/logic/district/get_district_name_logic.go b/rpc/core/internal/logic/district/get_district_name_logic.go index 0191986..3251fc2 100644 --- a/rpc/core/internal/logic/district/get_district_name_logic.go +++ b/rpc/core/internal/logic/district/get_district_name_logic.go @@ -2,9 +2,9 @@ package district import ( "context" - "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" + "git.noahlan.cn/noahlan/ntool/nstr" ) type GetDistrictNameLogic struct { @@ -20,7 +20,36 @@ func NewGetDistrictNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G } func (l *GetDistrictNameLogic) GetDistrictName(in *core.GetDistrictNameReq) (*core.GetDistrictNameResp, error) { - // todo: add your logic here and delete this line + // cached data + if cached, ok := getCachedDistrictItem(in.Code); ok { + return &core.GetDistrictNameResp{ + Code: cached.Code, + Name: nstr.JoinAny(in.Separator, cached.NameList), + NameList: cached.NameList, + }, nil + } + + // get data + // 获取当前code的所有父级列表 + listLogic := NewGetDistrictListLogic(l.ctx, l.svcCtx) + dbData, err := listLogic.GetDistrictList(&core.DistrictReq{ + Code: in.Code, + QueryType: core.DistrictQueryType_Parents, + }) + if err != nil { + return nil, err + } + nameList := make([]string, len(dbData.Data)) + for i, item := range dbData.Data { + nameList[i] = item.Name + } + + // caching + cacheDistrictItem(in.Code, nameList) - return &core.GetDistrictNameResp{}, nil + return &core.GetDistrictNameResp{ + Code: in.Code, + Name: nstr.JoinAny(in.Separator, nameList), + NameList: nameList, + }, nil } diff --git a/rpc/core/internal/logic/district/get_district_tree_logic.go b/rpc/core/internal/logic/district/get_district_tree_logic.go index 96b1b8e..efbfa13 100644 --- a/rpc/core/internal/logic/district/get_district_tree_logic.go +++ b/rpc/core/internal/logic/district/get_district_tree_logic.go @@ -2,6 +2,9 @@ package district import ( "context" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/district" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" @@ -20,7 +23,71 @@ func NewGetDistrictTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *G } func (l *GetDistrictTreeLogic) GetDistrictTree(in *core.DistrictCodeReq) (*core.DistrictTree, error) { - // todo: add your logic here and delete this line + var ( + err error + all []*ent.District + ) + if in.Code == "" { + all, err = l.svcCtx.DB.District.Query(). + All(l.ctx) + } else { + dbData, err := l.svcCtx.DB.District.Query(). + Where(district.CodeEQ(in.Code)). + First(l.ctx) + if err != nil { + return nil, hander.HandleEntErr(err, in) + } + all, err = l.svcCtx.DB.District.Query(). + Where(relationPredicates(dbData.Province, dbData.City, dbData.Area, dbData.Street, 0, core.Operator_EQ)...). + All(l.ctx) + } + if err != nil { + return nil, hander.HandleEntErr(err, in) + } + resp := &core.DistrictTree{ + Tree: make([]*core.DistrictInfo, 0), + } + + // 树根 + for _, v := range all { + if v.Level == 1 { + resp.Tree = append(resp.Tree, l.svcCtx.Convert.ConvertDistrictRpc(l.ctx, v)) + } + } + l.makeTree(all, resp.Tree) + + return resp, nil +} + +func (l *GetDistrictTreeLogic) makeTree(dataList []*ent.District, nodes []*core.DistrictInfo) { + for _, node := range nodes { + node.Children = l.findChildren(dataList, node) + } +} + +func (l *GetDistrictTreeLogic) findChildren(dataList []*ent.District, node *core.DistrictInfo) []*core.DistrictInfo { + resp := make([]*core.DistrictInfo, 0) + for _, v := range dataList { + // 跳一个层级是由于有直辖市的存在 + if v.Level != node.Level+1 && v.Level != node.Level+2 { + continue + } + if v.Province != node.Province { + continue + } + if node.City != "" && node.City != v.City { + continue + } + if node.Area != "" && node.Area != v.Area { + continue + } + child := l.svcCtx.Convert.ConvertDistrictRpc(l.ctx, v) + if child.Level < 3 || child.Street == "" { + child.Children = l.findChildren(dataList, child) + } + + resp = append(resp, child) + } - return &core.DistrictTree{}, nil + return resp } diff --git a/rpc/core/internal/logic/district/helper_injector.go b/rpc/core/internal/logic/district/helper_injector.go new file mode 100644 index 0000000..2530867 --- /dev/null +++ b/rpc/core/internal/logic/district/helper_injector.go @@ -0,0 +1,21 @@ +package district + +import ( + "context" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" +) + +func init() { + svc.DistrictHelperInst.SetGetDistrictNameFn(func(ctx context.Context, svcCtx *svc.ServiceContext, code, separator string) (*core.GetDistrictNameResp, error) { + l := NewGetDistrictNameLogic(ctx, svcCtx) + resp, err := l.GetDistrictName(&core.GetDistrictNameReq{ + Code: code, + Separator: separator, + }) + if err != nil { + return nil, err + } + return resp, nil + }) +} diff --git a/rpc/core/internal/logic/district/relation_query.go b/rpc/core/internal/logic/district/relation_query.go new file mode 100644 index 0000000..9a208d8 --- /dev/null +++ b/rpc/core/internal/logic/district/relation_query.go @@ -0,0 +1,38 @@ +package district + +import ( + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/district" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" +) + +func relationPredicates(province, city, area, street string, level uint32, lvOperator core.Operator) []predicate.District { + var predicates []predicate.District + if province != "" { + predicates = append(predicates, district.ProvinceEQ(province)) + } + if city != "" { + predicates = append(predicates, district.Or(district.CityEQ(city), district.CityEQ(""))) + } + if area != "" { + predicates = append(predicates, district.Or(district.AreaEQ(area), district.AreaEQ(""))) + } + if street != "" { + predicates = append(predicates, district.Or(district.StreetEQ(street), district.StreetEQ(""))) + } + if level != 0 { + switch lvOperator { + case core.Operator_EQ: + predicates = append(predicates, district.LevelEQ(level)) + case core.Operator_GT: + predicates = append(predicates, district.LevelGT(level)) + case core.Operator_LT: + predicates = append(predicates, district.LevelLT(level)) + case core.Operator_GTE: + predicates = append(predicates, district.LevelGTE(level)) + case core.Operator_LTE: + predicates = append(predicates, district.LevelLTE(level)) + } + } + return predicates +} diff --git a/rpc/core/internal/svc/convert.go b/rpc/core/internal/svc/convert.go index 4bce151..345debc 100644 --- a/rpc/core/internal/svc/convert.go +++ b/rpc/core/internal/svc/convert.go @@ -183,3 +183,13 @@ func (c *Convert) ConvertOAuthProviderRpc(_ context.Context, v *ent.OauthProvide return &tmp } + +func (c *Convert) ConvertDistrictRpc(_ context.Context, v *ent.District) *core.DistrictInfo { + if v == nil { + return nil + } + var tmp core.DistrictInfo + _ = copier.Copy(&tmp, v) + + return &tmp +} diff --git a/rpc/core/internal/svc/district_helper.go b/rpc/core/internal/svc/district_helper.go new file mode 100644 index 0000000..bb0de0f --- /dev/null +++ b/rpc/core/internal/svc/district_helper.go @@ -0,0 +1,34 @@ +package svc + +import ( + "context" + "git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core" +) + +var DistrictHelperInst = NewDistrictHelper() + +type ( + DistrictHelper struct { + getNameFn GetDistrictNameFunc + svcCtx *ServiceContext + } + + DistrictCacheItem struct { + Code string // 区域代码 + NameList []string // 区域名称列表(从根节点开始) + } + + GetDistrictNameFunc func(ctx context.Context, svcCtx *ServiceContext, code, separator string) (*core.GetDistrictNameResp, error) +) + +func NewDistrictHelper() *DistrictHelper { + return &DistrictHelper{} +} + +func (h *DistrictHelper) SetGetDistrictNameFn(fn GetDistrictNameFunc) { + h.getNameFn = fn +} + +func (h *DistrictHelper) GetDistrictName(ctx context.Context, code, separator string) (*core.GetDistrictNameResp, error) { + return h.getNameFn(ctx, h.svcCtx, code, separator) +} diff --git a/rpc/core/internal/svc/service_context.go b/rpc/core/internal/svc/service_context.go index 8a38331..88aa4d9 100644 --- a/rpc/core/internal/svc/service_context.go +++ b/rpc/core/internal/svc/service_context.go @@ -24,8 +24,9 @@ type ServiceContext struct { DB *ent.Client Redis *redis.Redis - Mapper *nmapper.MapperObject - Convert *Convert + Mapper *nmapper.MapperObject + Convert *Convert + DistrictHelper *DistrictHelper } func NewServiceContext(c config.Config) *ServiceContext { @@ -67,5 +68,10 @@ func NewServiceContext(c config.Config) *ServiceContext { // convert tmp.Convert = NewConvert(tmp) + + // helper + DistrictHelperInst.svcCtx = tmp + tmp.DistrictHelper = DistrictHelperInst + return tmp }