diff --git a/api/errcode/errcode.go b/api/errcode/errcode.go index 8f44644a..4a0efe65 100644 --- a/api/errcode/errcode.go +++ b/api/errcode/errcode.go @@ -223,8 +223,9 @@ var ErrCodeMsgMap = map[ErrCode]string{ ErrCodeHvyogoSrvFail: "慧用工接口服务错误", - ErrCodeRavenIQTestSrvFail: "瑞文智商测试表服务错误", - ErrCodeRavenIQTestNotExist: "瑞文智商测试表不存在", + ErrCodeRavenIQTestSrvFail: "瑞文智商测试表服务错误", + ErrCodeRavenIQTestNotExist: "瑞文智商测试表不存在", + ErrCodeRavenIQTestQuestionNotExist: "瑞文智商测试表题目不存在", } const ( @@ -546,8 +547,8 @@ const ( ErrCodeHandleWsFail ErrCode = -100001 // 长链连接失败 // RavenIQTest: 101xxx - ErrCodeRavenIQTestSrvOk ErrCode = ErrCodeOk - ErrCodeRavenIQTestSrvFail ErrCode = -101001 // 瑞文智商测试表服务错误 - ErrCodeRavenIQTestNotExist ErrCode = -101002 // 瑞文智商测试表不存在 - + ErrCodeRavenIQTestSrvOk ErrCode = ErrCodeOk + ErrCodeRavenIQTestSrvFail ErrCode = -101001 // 瑞文智商测试表服务错误 + ErrCodeRavenIQTestNotExist ErrCode = -101002 // 瑞文智商测试表不存在 + ErrCodeRavenIQTestQuestionNotExist ErrCode = -101003 // 瑞文智商测试表题目不存在 ) diff --git a/api/proto/Raven_IQ_test/proto/Raven_IQ_test_vo_api.go b/api/proto/Raven_IQ_test/proto/Raven_IQ_test_vo_api.go index 0eb58447..d481f205 100644 --- a/api/proto/Raven_IQ_test/proto/Raven_IQ_test_vo_api.go +++ b/api/proto/Raven_IQ_test/proto/Raven_IQ_test_vo_api.go @@ -24,6 +24,7 @@ func (vo *ApiListVO) CopyRavenIQTest(test *dbstruct.RavenIQTest) *ApiListVO { if test == nil { return vo } + vo.IQResult = &apollostruct.IQResult{} vo.Id = test.GetId() vo.UserId = test.GetUserId() vo.Age = test.GetAge() diff --git a/api/proto/Raven_IQ_test/proto/not_null_def_api.go b/api/proto/Raven_IQ_test/proto/not_null_def_api.go index 3259bcad..bcc08211 100644 --- a/api/proto/Raven_IQ_test/proto/not_null_def_api.go +++ b/api/proto/Raven_IQ_test/proto/not_null_def_api.go @@ -8,6 +8,7 @@ import ( func (p *ApiCreateReq) ProvideNotNullValue() (params []*validator.JsonParam) { params = make([]*validator.JsonParam, 0) params = append(params, validator.NewInt64PtrParam("请提供您的年龄!", p.Age)) + params = append(params, validator.NewStructSliceParam("请提供您的答案!", len(p.RavenIQTest.AnswerList))) return params } diff --git a/apollostruct/RavenIQtest.go b/apollostruct/RavenIQtest.go index ec2ba71d..d1641058 100644 --- a/apollostruct/RavenIQtest.go +++ b/apollostruct/RavenIQtest.go @@ -1,7 +1,8 @@ package apollostruct type QuestionMapCfg struct { - Map map[int64]*RavenIQTestQuestion `json:"map"` + Map map[int64]*RavenIQTestQuestion `json:"map"` + ClassList []int64 `json:"class_list"` } type RavenIQTestQuestion struct { @@ -24,7 +25,7 @@ type AgeScore2IQResultMapCfg struct { } type ClassScore2ClassResultMapCfg struct { - ClassId2ClassScoreBlockListMap map[int64][]int64 `json:"class_id_2_class_score_block_list_map" bson:"class_id_2_class_score_block_list_map"` // 大类Id到大类分数分布区域的映射map + Map map[int64][]int64 `json:"map" bson:"map"` // 大类Id到大类分数分布区域的映射map } type IQResultMapCfg struct { diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 29f17d6b..f6e053b5 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -5617,7 +5617,7 @@ func (m *Mongo) GetRavenIQTestById(ctx *gin.Context, id int64) (*dbstruct.RavenI err := col.Find(ctx, query).One(test) if err == qmgo.ErrNoSuchDocuments { err = nil - return test, err + return nil, err } return test, err } diff --git a/app/mix/service/apiservice.go b/app/mix/service/apiservice.go index 97461d85..5be910ce 100644 --- a/app/mix/service/apiservice.go +++ b/app/mix/service/apiservice.go @@ -3592,7 +3592,7 @@ func (s *Service) ApiHvyogoWorkerUpdate(ctx *gin.Context, req *hvyogoproto.ApiWo func (s *Service) ApiCreateRavenIQTest(ctx *gin.Context, req *Raven_IQ_testproto.ApiCreateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeRavenIQTestSrvOk - // 计算总得分 + // 缓存数据准备 questionMapcfg := apollostruct.QuestionMapCfg{} err := apollo.GetJson(consts.QuestionMapKey, &questionMapcfg, apollo.ApolloOpts().SetNamespace("Raven_IQ_test")) if err != nil { @@ -3600,25 +3600,6 @@ func (s *Service) ApiCreateRavenIQTest(ctx *gin.Context, req *Raven_IQ_testproto return errcode.ErrCodeApolloReadFail } - classScoreMap := make(map[int64]int64) - for _, answer := range req.AnswerList { - if answer.GetSelectedOption() == questionMapcfg.Map[answer.GetQuestionId()].CorrectOption { - classScoreMap[answer.GetQuestionId()] += 1 - } - } - - req.ClassScoreList = make([]*dbstruct.RavenIQTestClassScore, 0) - totalScore := float64(0) - for classId, score := range classScoreMap { - req.ClassScoreList = append(req.ClassScoreList, &dbstruct.RavenIQTestClassScore{ - ClassId: goproto.Int64(classId), - Score: goproto.Int64(score), - }) - totalScore += float64(score) - } - req.TotalScore = goproto.Float64(totalScore) - - // 换算智商及百分位 ageScore2IQResultMapCfg := apollostruct.AgeScore2IQResultMapCfg{} err = apollo.GetJson(consts.AgeScore2IQResultMapKey, &ageScore2IQResultMapCfg, apollo.ApolloOpts().SetNamespace("Raven_IQ_test")) if err != nil { @@ -3635,15 +3616,42 @@ func (s *Service) ApiCreateRavenIQTest(ctx *gin.Context, req *Raven_IQ_testproto return } + // 计算总得分 + classScoreMap := make(map[int64]int64) + for _, classId := range questionMapcfg.ClassList { + classScoreMap[classId] = 0 + } + for _, answer := range req.AnswerList { + question, ok := questionMapcfg.Map[answer.GetQuestionId()] + if !ok { + return errcode.ErrCodeRavenIQTestQuestionNotExist + } + if answer.GetSelectedOption() == question.CorrectOption { + classScoreMap[answer.GetQuestionId()] += 1 + } + } + + req.ClassScoreList = make([]*dbstruct.RavenIQTestClassScore, 0) + totalScore := float64(0) + for classId, score := range classScoreMap { + req.ClassScoreList = append(req.ClassScoreList, &dbstruct.RavenIQTestClassScore{ + ClassId: goproto.Int64(classId), + Score: goproto.Int64(score), + }) + totalScore += float64(score) + } + req.TotalScore = goproto.Float64(totalScore) + + // 换算智商及百分位 ageBlockIdx := util.GetLastLessOrEqualForInt64(ageScore2IQResultMapCfg.AgeBlockList, req.GetAge()) list := ageScore2IQResultMapCfg.AgeBlockIndex2ScoreBlockListMap[ageBlockIdx] scoreBlockIdx := util.GetLastLessOrEqualForFloat64(list, req.GetTotalScore()) IQBlockId := ageScore2IQResultMapCfg.ScoreBlockIndex2IQBlockIdMap[scoreBlockIdx] for _, classScore := range req.ClassScoreList { - classScoreBlockList := classScore2ClassResultMapCfg.ClassId2ClassScoreBlockListMap[classScore.GetClassId()] + classScoreBlockList := classScore2ClassResultMapCfg.Map[classScore.GetClassId()] classScoreBlockIdx := util.GetLastLessOrEqualForInt64(classScoreBlockList, classScore.GetScore()) - classScore.ClassBlockId = goproto.Int64(int64(classScoreBlockIdx)) + classScore.ClassBlockId = goproto.Int64(classScore.GetClassId()*100 + int64(classScoreBlockIdx)) // 100 * 大类id + 偏移量 } req.IQBlockId = goproto.Int64(IQBlockId)