diff --git a/dsBigData/Business/School/SchoolController/SchoolController.go b/dsBigData/Business/School/SchoolController/SchoolController.go index 4a1c3a47..896d3c5f 100644 --- a/dsBigData/Business/School/SchoolController/SchoolController.go +++ b/dsBigData/Business/School/SchoolController/SchoolController.go @@ -3,10 +3,10 @@ package SchoolController import ( "dsBigData/Business/Area/AreaService" "dsBigData/Business/Dict/DictService" + "dsBigData/Business/School/SchoolDao" "dsBigData/Business/School/SchoolModel" "dsBigData/Business/School/SchoolService" "dsBigData/Business/Student/StudentService" - "dsBigData/Business/Teacher/TeacherDao" "dsBigData/Business/Teacher/TeacherService" "dsBigData/Model" "dsBigData/Utils/CommonUtil" @@ -27,11 +27,13 @@ func Routers(r *gin.RouterGroup) { rr.GET("/GetDistrictBaseInfo", GetDistrictBaseInfo) rr.GET("/GetSchoolBaseInfo", GetSchoolBaseInfo) + rr.GET("/GetBaseAccessTopByAreaCode", GetBaseAccessTopByAreaCode) + return } func test(c *gin.Context) { - count, _ := TeacherDao.GetTeacherCurrentTermAddCount("150400") + count, _ := SchoolDao.GetOrgInfoByOrgIds([]string{"e2d0131a-b906-11ea-8315-f48e38f73cf7", "e2d01323-b906-11ea-8315-f48e38f73cf7", "e2d013b9-b906-11ea-8315-f48e38f73cf7", "e2d0147c-b906-11ea-8315-f48e38f73cf7", "e2d01485-b906-11ea-8315-f48e38f73cf7", "e2d01509-b906-11ea-8315-f48e38f73cf7", "e2d0151b-b906-11ea-8315-f48e38f73cf7", "e2d01524-b906-11ea-8315-f48e38f73cf7", "e2d01536-b906-11ea-8315-f48e38f73cf7", "e2d01549-b906-11ea-8315-f48e38f73cf7"}) c.JSON(http.StatusOK, Model.Res{ Success: true, @@ -39,6 +41,60 @@ func test(c *gin.Context) { }) } +func GetBaseAccessTopByAreaCode(c *gin.Context) { + areaCode := c.Query("areaCode") + topNum := c.Query("topNum") + + resArr, err := SchoolService.GetBaseAccessTop(areaCode, topNum) + if err != nil { + c.JSON(http.StatusOK, Model.Res{ + Success: false, + Message: err.Error(), + }) + return + } + resArrByte, _ := json.Marshal(resArr) + resList := CommonUtil.ConvertJsonStringToMapArray(string(resArrByte)) + resMap := make(map[string]int) + orgArr := make([]string, 0) + for i := 0; i < len(resList); i++ { + orgId := resList[i]["org_id"].(string) + count := int(resList[i]["count"].(float64)) + resMap[orgId] = count + orgArr = append(orgArr, orgId) + } + + orgInfo, err := SchoolService.GetOrgInfoByOrgIds(orgArr) + if err != nil { + c.JSON(http.StatusOK, Model.Res{ + Success: false, + Message: err.Error(), + }) + return + } + + baseAccessTopArr := make([]SchoolModel.BaseAccessTop, 0) + for i := range orgInfo { + var baseAccessTop SchoolModel.BaseAccessTop + orgId := orgInfo[i].OrgId + orgName := orgInfo[i].OrgName + baseAccessTop.OrgId = orgId + baseAccessTop.OrgName = orgName + if _, ok := resMap[orgId]; ok { + baseAccessTop.Count = resMap[orgId] + } + baseAccessTopArr = append(baseAccessTopArr, baseAccessTop) + } + + CommonUtil.SortItems(baseAccessTopArr, "Count", "desc") + + c.JSON(http.StatusOK, Model.Res{ + Success: true, + List: baseAccessTopArr, + }) + +} + /** 功能:获取市级的基本信息统计 */ @@ -276,7 +332,7 @@ func GetSchoolBaseInfo(c *gin.Context) { TotalCountAndCurrentTermCountArr = append(TotalCountAndCurrentTermCountArr, classTotalCountAndCurrentTermCount) var orgTotalCountAndCurrentTermCount SchoolModel.TotalCountAndCurrentTermCount - orgTotalCount, err := SchoolService.GetOrgTotalCount(schoolId,"3") + orgTotalCount, err := SchoolService.GetOrgTotalCount(schoolId, "3") if err != nil { c.JSON(http.StatusOK, Model.Res{ Success: false, @@ -284,7 +340,7 @@ func GetSchoolBaseInfo(c *gin.Context) { }) return } - orgCurrentTermCount, err := SchoolService.GetOrgCurrentTermAddCount(schoolId,"3") + orgCurrentTermCount, err := SchoolService.GetOrgCurrentTermAddCount(schoolId, "3") if err != nil { c.JSON(http.StatusOK, Model.Res{ Success: false, diff --git a/dsBigData/Business/School/SchoolDao/SchoolDao.go b/dsBigData/Business/School/SchoolDao/SchoolDao.go index 6be9b13e..6439a3bb 100644 --- a/dsBigData/Business/School/SchoolDao/SchoolDao.go +++ b/dsBigData/Business/School/SchoolDao/SchoolDao.go @@ -466,3 +466,77 @@ func GetClassCurrentTermAddCount(schoolId string) (int, error) { return int(result.TotalHits()), nil } + +func GetBaseAccessTop(areaCode string, topNum string) ([]SchoolModel.BaseAccessTopAggsOrgId, error) { + var areaCodeTerm *elastic.TermQuery + if strings.HasSuffix(areaCode, "00") { //市 + areaCodeTerm = elastic.NewTermQuery("city_code", areaCode) + } else { //区 + areaCodeTerm = elastic.NewTermQuery("district_code", areaCode) + } + enableFlagTerm := elastic.NewTermQuery("enable_flag", 1) + delFlagTerm := elastic.NewTermQuery("del_flag", 0) + + boolQuery := elastic.NewBoolQuery().Must(areaCodeTerm, enableFlagTerm, delFlagTerm) + + orgIdAggs := elastic. + NewTermsAggregation(). + Field("org_id"). + Size(CommonUtil.ConvertStringToInt(topNum)) + + result, err := esClient.Search(). + Index("log_login"). + Query(boolQuery). + Size(0). + Aggregation("orgId", orgIdAggs). + Do(CTX) + + if err != nil { + return nil, err + } + + resByte, err := json.Marshal(result.Aggregations) + resStr := string(resByte) + + var baseAccessTopAggsOrgIdArr []SchoolModel.BaseAccessTopAggsOrgId + resCount := gjson.Get(resStr, "orgId.buckets.#") + for i := 0; i < int(resCount.Num); i++ { + var baseAccessTopAggsOrgId SchoolModel.BaseAccessTopAggsOrgId + baseAccessTopAggsOrgId.OrgId = gjson.Get(resStr, "orgId.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str + baseAccessTopAggsOrgId.Count = int(gjson.Get(resStr, "orgId.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num) + + baseAccessTopAggsOrgIdArr = append(baseAccessTopAggsOrgIdArr, baseAccessTopAggsOrgId) + } + return baseAccessTopAggsOrgIdArr, nil +} + +func GetOrgInfoByOrgIds(orgIds []string) ([]SchoolModel.Org, error) { + interfaceArr := CommonUtil.ConvertStringArrToInterfaceArr(orgIds) + orgIdTerms := elastic.NewTermsQuery("org_id", interfaceArr...) + + boolQuery := elastic.NewBoolQuery().Must(orgIdTerms) + + result, err := esClient.Search(). + Index("org_school"). + Query(boolQuery). + Size(100). + Do(CTX) + + if err != nil { + return nil, err + } + + resByte, err := json.Marshal(result.Hits) + resStr := string(resByte) + + var orgArr []SchoolModel.Org + resCount := gjson.Get(resStr, "total.value") + for i := 0; i < int(resCount.Num); i++ { + var org SchoolModel.Org + org.OrgId = gjson.Get(resStr, "hits."+CommonUtil.ConvertIntToString(i)+"._source.data_content.org_id").Str + org.OrgName = gjson.Get(resStr, "hits."+CommonUtil.ConvertIntToString(i)+"._source.data_content.org_name").Str + + orgArr = append(orgArr, org) + } + return orgArr, nil +} diff --git a/dsBigData/Business/School/SchoolModel/SchoolModel.go b/dsBigData/Business/School/SchoolModel/SchoolModel.go index 2ff74096..4a6963eb 100644 --- a/dsBigData/Business/School/SchoolModel/SchoolModel.go +++ b/dsBigData/Business/School/SchoolModel/SchoolModel.go @@ -5,6 +5,11 @@ type School struct { SchoolName string `json:"school_name"` } +type Org struct { + OrgId string `json:"org_id"` + OrgName string `json:"org_name"` +} + type EduAssist struct { EduAssistId string `json:"eduassist_id"` EduAssistName string `json:"eduassist_name"` @@ -50,7 +55,18 @@ type Stage struct { } type TotalCountAndCurrentTermCount struct { - Title string `json:"title"` - TotalCount int `json:"total_count"` + Title string `json:"title"` + TotalCount int `json:"total_count"` CurrentTermAddCount int `json:"current_term_add_count"` } + +type BaseAccessTopAggsOrgId struct { + OrgId string `json:"org_id"` + Count int `json:"count"` +} + +type BaseAccessTop struct { + OrgId string `json:"org_id"` + OrgName string `json:"org_name"` + Count int `json:"count"` +} diff --git a/dsBigData/Business/School/SchoolService/SchoolService.go b/dsBigData/Business/School/SchoolService/SchoolService.go index 10c1bce7..3792fa2c 100644 --- a/dsBigData/Business/School/SchoolService/SchoolService.go +++ b/dsBigData/Business/School/SchoolService/SchoolService.go @@ -72,3 +72,13 @@ func GetClassCurrentTermAddCount(schoolId string) (int, error) { count, err := SchoolDao.GetClassCurrentTermAddCount(schoolId) return count, err } + +func GetBaseAccessTop(areaCode string, topNum string) ([]SchoolModel.BaseAccessTopAggsOrgId, error) { + arr, err := SchoolDao.GetBaseAccessTop(areaCode, topNum) + return arr, err +} + +func GetOrgInfoByOrgIds(orgIds []string) ([]SchoolModel.Org, error) { + arr, err := SchoolDao.GetOrgInfoByOrgIds(orgIds) + return arr, err +} diff --git a/dsBigData/Utils/CommonUtil/SortStruct.go b/dsBigData/Utils/CommonUtil/SortStruct.go new file mode 100644 index 00000000..3722d7a4 --- /dev/null +++ b/dsBigData/Utils/CommonUtil/SortStruct.go @@ -0,0 +1,52 @@ +package CommonUtil + +import ( + "reflect" + "sort" +) + +type Items struct { + data interface{} + field string +} + +func (items *Items) Len() int { + if reflect.ValueOf(items.data).Kind() != reflect.Slice { + return -1 + } + return reflect.ValueOf(items.data).Len() +} + +func (items *Items) Less(i, j int) bool { + a := reflect.ValueOf(items.data).Index(i) + b := reflect.ValueOf(items.data).Index(j) + if a.Kind() == reflect.Ptr { + a = a.Elem() + } + if b.Kind() == reflect.Ptr { + b = b.Elem() + } + va := a.FieldByName(items.field).Int() + vb := b.FieldByName(items.field).Int() + return va < vb +} + +func (items *Items) Swap(i, j int) { + reflect.Swapper(items.data)(i, j) +} + +func SortItems(i interface{}, str string, sortType string) { + if reflect.ValueOf(i).Kind() != reflect.Slice { + return + } + a := &Items{ + data: i, + field: str, + } + if sortType == "desc" { + sort.Sort(sort.Reverse(a)) + } else { + sort.Sort(a) + } + +}