You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
290 lines
9.9 KiB
290 lines
9.9 KiB
package StudentDao
|
|
|
|
import (
|
|
"dsBigData/Business/Student/StudentModel"
|
|
"dsBigData/Utils/CommonUtil"
|
|
"dsBigData/Utils/EsUtil"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/olivere/elastic/v7"
|
|
"github.com/tidwall/gjson"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var esClient = EsUtil.EsClient
|
|
var CTX = EsUtil.CTX
|
|
|
|
func GetStudentCountAggsXb(schoolId string) ([]StudentModel.StudentCountXb, error) {
|
|
orgIdTerm := elastic.NewTermQuery("org_id", schoolId)
|
|
bUseTerm := elastic.NewTermQuery("data_content.b_use", 1)
|
|
enableFlagTerm := elastic.NewTermQuery("enable_flag", 1)
|
|
delFlagTerm := elastic.NewTermQuery("del_flag", 0)
|
|
interfaceArr := CommonUtil.ConvertStringArrToInterfaceArr([]string{"男", "女"})
|
|
xbTerms := elastic.NewTermsQuery("data_content.xbm", interfaceArr...)
|
|
|
|
boolQuery := elastic.NewBoolQuery().Must(orgIdTerm, bUseTerm, enableFlagTerm, delFlagTerm, xbTerms)
|
|
|
|
xbAggs := elastic.
|
|
NewTermsAggregation().
|
|
Field("data_content.xbm").
|
|
Size(10)
|
|
|
|
result, err := esClient.Search().
|
|
Index("user_student").
|
|
Query(boolQuery).
|
|
Size(0).
|
|
Aggregation("xb", xbAggs).
|
|
Do(CTX)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resByte, err := json.Marshal(result.Aggregations)
|
|
resStr := string(resByte)
|
|
|
|
var studentCountXbArr []StudentModel.StudentCountXb
|
|
resCount := gjson.Get(resStr, "xb.buckets.#")
|
|
totalCount := CommonUtil.ConvertInt64ToString(result.TotalHits())
|
|
totalCountFloat, _ := strconv.ParseFloat(totalCount, 64)
|
|
for i := 0; i < int(resCount.Num); i++ {
|
|
var studentCountXb StudentModel.StudentCountXb
|
|
studentCountXb.XbName = gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str
|
|
studentCountXb.Count = int(gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num)
|
|
percent := gjson.Get(resStr, "xb.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num / totalCountFloat * 100
|
|
percentFloat, _ := strconv.ParseFloat(fmt.Sprintf("%.0f", percent), 64)
|
|
percentString := CommonUtil.ConverFloat64ToString(percentFloat)
|
|
studentCountXb.Percent = percentString + "%"
|
|
studentCountXbArr = append(studentCountXbArr, studentCountXb)
|
|
|
|
}
|
|
return studentCountXbArr, nil
|
|
|
|
}
|
|
|
|
func GetStudentCountAggsXxbxlx(districtCode []string, schoolIds []string, xxbxlxs []string) ([]StudentModel.StudentCountAggsXxbxlx, error) {
|
|
enableFlagTerm := elastic.NewTermQuery("enable_flag", 1)
|
|
delFlagTerm := elastic.NewTermQuery("del_flag", 0)
|
|
bUseTerm := elastic.NewTermQuery("data_content.b_use", 1)
|
|
interfaceArr := CommonUtil.ConvertStringArrToInterfaceArr(xxbxlxs)
|
|
xxbxlxTerms := elastic.NewTermsQuery("school_type", interfaceArr...)
|
|
|
|
var boolQuery *elastic.BoolQuery
|
|
|
|
if len(districtCode) > 0 {
|
|
interfaceArr := CommonUtil.ConvertStringArrToInterfaceArr(districtCode)
|
|
districtCodeTerms := elastic.NewTermsQuery("district_code", interfaceArr...)
|
|
boolQuery = elastic.NewBoolQuery().Must(districtCodeTerms, enableFlagTerm, delFlagTerm, bUseTerm, xxbxlxTerms)
|
|
} else if len(schoolIds) > 0 {
|
|
interfaceArr := CommonUtil.ConvertStringArrToInterfaceArr(schoolIds)
|
|
orgIdTerms := elastic.NewTermsQuery("org_id", interfaceArr...)
|
|
boolQuery = elastic.NewBoolQuery().Must(orgIdTerms, enableFlagTerm, delFlagTerm, bUseTerm, xxbxlxTerms)
|
|
}
|
|
|
|
xxbxlxAggs := elastic.
|
|
NewTermsAggregation().
|
|
Field("school_type").
|
|
Size(50)
|
|
|
|
result, err := esClient.Search().
|
|
Index("user_student").
|
|
Query(boolQuery).
|
|
Size(0).
|
|
Aggregation("xxbxlx", xxbxlxAggs).
|
|
TrackTotalHits(true).
|
|
Do(CTX)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resByte, err := json.Marshal(result.Aggregations)
|
|
resStr := string(resByte)
|
|
totalCount := CommonUtil.ConvertInt64ToString(result.TotalHits())
|
|
totalCountFloat, _ := strconv.ParseFloat(totalCount, 64)
|
|
var studentCountAggsXxbxlxArr []StudentModel.StudentCountAggsXxbxlx
|
|
resCount := gjson.Get(resStr, "xxbxlx.buckets.#")
|
|
for i := 0; i < int(resCount.Num); i++ {
|
|
var studentCountAggsXxbxlx StudentModel.StudentCountAggsXxbxlx
|
|
studentCountAggsXxbxlx.XxbxlxCode = gjson.Get(resStr, "xxbxlx.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str
|
|
studentCountAggsXxbxlx.Count = int(gjson.Get(resStr, "xxbxlx.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num)
|
|
percent := gjson.Get(resStr, "xxbxlx.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num / totalCountFloat * 100
|
|
percentFloat, _ := strconv.ParseFloat(fmt.Sprintf("%.0f", percent), 64)
|
|
percentString := CommonUtil.ConverFloat64ToString(percentFloat)
|
|
studentCountAggsXxbxlx.Percent = percentString + "%"
|
|
|
|
studentCountAggsXxbxlxArr = append(studentCountAggsXxbxlxArr, studentCountAggsXxbxlx)
|
|
}
|
|
return studentCountAggsXxbxlxArr, nil
|
|
|
|
}
|
|
|
|
func GetStudentCountAggsOrgId(schoolIds []string) ([]StudentModel.StudentCountAggsOrg, error) {
|
|
enableFlagTerm := elastic.NewTermQuery("enable_flag", 1)
|
|
delFlagTerm := elastic.NewTermQuery("del_flag", 0)
|
|
bUseTerm := elastic.NewTermQuery("data_content.b_use", 1)
|
|
interfaceArr := CommonUtil.ConvertStringArrToInterfaceArr(schoolIds)
|
|
orgIdTerms := elastic.NewTermsQuery("org_id", interfaceArr...)
|
|
|
|
boolQuery := elastic.NewBoolQuery().Must(orgIdTerms, enableFlagTerm, delFlagTerm, bUseTerm, orgIdTerms)
|
|
|
|
orgIdAggs := elastic.
|
|
NewTermsAggregation().
|
|
Field("org_id").
|
|
Size(1000)
|
|
|
|
result, err := esClient.Search().
|
|
Index("user_student").
|
|
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 StudentCountAggsOrgArr []StudentModel.StudentCountAggsOrg
|
|
resCount := gjson.Get(resStr, "orgId.buckets.#")
|
|
for i := 0; i < int(resCount.Num); i++ {
|
|
var studentCountAggsOrg StudentModel.StudentCountAggsOrg
|
|
studentCountAggsOrg.OrgId = gjson.Get(resStr, "orgId.buckets."+CommonUtil.ConvertIntToString(i)+".key").Str
|
|
studentCountAggsOrg.Count = int(gjson.Get(resStr, "orgId.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num)
|
|
|
|
StudentCountAggsOrgArr = append(StudentCountAggsOrgArr, studentCountAggsOrg)
|
|
}
|
|
return StudentCountAggsOrgArr, nil
|
|
}
|
|
|
|
func GetStudentCountAggsRxnf(schoolId string, stageId string) ([]StudentModel.StudentCountRxnf, error) {
|
|
|
|
endYear := time.Now().Year()
|
|
startYear := 0
|
|
month := CommonUtil.ConvertStringToInt32(time.Now().Format("1"))
|
|
if month < 8 {
|
|
endYear = endYear - 1
|
|
}
|
|
|
|
if stageId == "2" {
|
|
startYear = endYear - 5
|
|
} else {
|
|
startYear = endYear - 2
|
|
}
|
|
|
|
orgIdTerm := elastic.NewTermQuery("org_id", schoolId)
|
|
stageIdTerm := elastic.NewTermQuery("data_content.stage_id", stageId)
|
|
rxnfRang := elastic.NewRangeQuery("data_content.rxnf").Gte(startYear).Lte(endYear)
|
|
bUseTerm := elastic.NewTermQuery("data_content.b_use", 1)
|
|
enableFlagTerm := elastic.NewTermQuery("enable_flag", 1)
|
|
delFlagTerm := elastic.NewTermQuery("del_flag", 0)
|
|
|
|
boolQuery := elastic.NewBoolQuery().Must(orgIdTerm, stageIdTerm, bUseTerm, rxnfRang, enableFlagTerm, delFlagTerm)
|
|
|
|
rxnfHistogramAggregation := elastic.NewHistogramAggregation().Field("data_content.rxnf").MinDocCount(0).ExtendedBounds(float64(startYear), float64(endYear)).Interval(1)
|
|
|
|
result, err := esClient.Search().
|
|
Index("user_student").
|
|
Query(boolQuery).
|
|
Aggregation("rxnf", rxnfHistogramAggregation).
|
|
Do(CTX)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resByte, err := json.Marshal(result.Aggregations)
|
|
resStr := string(resByte)
|
|
|
|
var studentCountRxnfArr []StudentModel.StudentCountRxnf
|
|
resCount := gjson.Get(resStr, "rxnf.buckets.#")
|
|
for i := 0; i < int(resCount.Num); i++ {
|
|
var studentCountRxnf StudentModel.StudentCountRxnf
|
|
studentCountRxnf.Rxnf = CommonUtil.ConverFloat64ToString(gjson.Get(resStr, "rxnf.buckets."+CommonUtil.ConvertIntToString(i)+".key").Num)
|
|
studentCountRxnf.Count = int(gjson.Get(resStr, "rxnf.buckets."+CommonUtil.ConvertIntToString(i)+".doc_count").Num)
|
|
|
|
studentCountRxnfArr = append(studentCountRxnfArr, studentCountRxnf)
|
|
}
|
|
return studentCountRxnfArr, nil
|
|
}
|
|
|
|
/**
|
|
功能:根据市或区的编码(也可传单位ID)获取学生总数
|
|
*/
|
|
func GetStudentTotalCount(areaCode string) (int, error) {
|
|
var areaCodeTerm *elastic.TermQuery
|
|
if len(areaCode) == 6 {
|
|
if strings.HasSuffix(areaCode, "00") { //市
|
|
areaCodeTerm = elastic.NewTermQuery("city_code", areaCode)
|
|
} else { //区
|
|
areaCodeTerm = elastic.NewTermQuery("district_code", areaCode)
|
|
}
|
|
} else {
|
|
areaCodeTerm = elastic.NewTermQuery("org_id", areaCode)
|
|
}
|
|
enableFlagTerm := elastic.NewTermQuery("enable_flag", 1)
|
|
delFlagTerm := elastic.NewTermQuery("del_flag", 0)
|
|
bUseTerm := elastic.NewTermQuery("data_content.b_use", 1)
|
|
|
|
boolQuery := elastic.NewBoolQuery().Must(areaCodeTerm, enableFlagTerm, delFlagTerm, bUseTerm)
|
|
|
|
result, err := esClient.Search().
|
|
Index("user_student").
|
|
Query(boolQuery).
|
|
TrackTotalHits(true).
|
|
Do(CTX)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return int(result.TotalHits()), nil
|
|
}
|
|
|
|
/**
|
|
功能:根据市或区的编码(也可传单位ID)获取学生当前学期新增数
|
|
*/
|
|
func GetStudentCurrentTermAddCount(areaCode string) (int, error) {
|
|
endYearStr := CommonUtil.ConvertIntToString(time.Now().Year())
|
|
month := CommonUtil.ConvertStringToInt32(time.Now().Format("1"))
|
|
monthStr := "08"
|
|
if month >= 3 && month <= 7 {
|
|
monthStr = "03"
|
|
}
|
|
|
|
createTime := endYearStr + "/" + monthStr + "/01 00:00:01"
|
|
|
|
var areaCodeTerm *elastic.TermQuery
|
|
if len(areaCode) == 6 {
|
|
if strings.HasSuffix(areaCode, "00") { //市
|
|
areaCodeTerm = elastic.NewTermQuery("city_code", areaCode)
|
|
} else { //区
|
|
areaCodeTerm = elastic.NewTermQuery("district_code", areaCode)
|
|
}
|
|
} else {
|
|
areaCodeTerm = elastic.NewTermQuery("org_id", areaCode)
|
|
}
|
|
enableFlagTerm := elastic.NewTermQuery("enable_flag", 1)
|
|
delFlagTerm := elastic.NewTermQuery("del_flag", 0)
|
|
bUseTerm := elastic.NewTermQuery("data_content.b_use", 1)
|
|
createTimeRang := elastic.NewRangeQuery("data_content.create_time").Gte(createTime)
|
|
|
|
boolQuery := elastic.NewBoolQuery().Must(areaCodeTerm, enableFlagTerm, delFlagTerm, bUseTerm, createTimeRang)
|
|
|
|
result, err := esClient.Search().
|
|
Index("user_student").
|
|
Query(boolQuery).
|
|
TrackTotalHits(true).
|
|
Do(CTX)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return int(result.TotalHits()), nil
|
|
}
|