文章目录
1.提交答题情况 1.PracticeDetailController.java 2.PracticeDetailService.java 3.PracticeDetailServiceImpl.java 4.PracticeDetailDao.java 5.PracticeDetailDao.xml 6.req SubmitSubjectDetailReq.java
7.dto 1.SubjectDetailDTO.java 2.SubjectDTO.java 3.SubjectOptionDTO.java
8.测试 1.接口设计 2.db 1.更新提交时间和用时 practice_info 2.练习细节如果有的话就更新记录 practice_detail 3.测试遇到一个bug,即使题目正确也会插入答案状态为0,状态设置的时候有问题
2.答案解析-每题得分 1.GetScoreDetailReq.java 2.ScoreDetailVO.java 3.PracticeDetailController.java 4.PracticeDetailService.java 5.PracticeDetailServiceImpl.java 6.PracticeDetailDao.xml 7.测试
1.提交答题情况
1.PracticeDetailController.java
@PostMapping ( value = "/submitSubject" )
public Result < Boolean > submitSubject ( @RequestBody SubmitSubjectDetailReq req) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "练习提交题目入参{}" , JSON . toJSONString ( req) ) ;
}
Preconditions . checkArgument ( ! Objects . isNull ( req) , "参数不能为空!" ) ;
Preconditions . checkArgument ( ! Objects . isNull ( req. getPracticeId ( ) ) , "练习id不能为空!" ) ;
Preconditions . checkArgument ( ! Objects . isNull ( req. getSubjectId ( ) ) , "题目id不能为空!" ) ;
Preconditions . checkArgument ( ! Objects . isNull ( req. getSubjectType ( ) ) , "题目类型不能为空!" ) ;
Preconditions . checkArgument ( ! StringUtils . isBlank ( req. getTimeUse ( ) ) , "用时不能为空!" ) ;
Boolean result = practiceDetailService. submitSubject ( req) ;
log. info ( "练习提交题目出参{}" , result) ;
return Result . ok ( result) ;
} catch ( IllegalArgumentException e) {
log. error ( "参数异常!错误原因{}" , e. getMessage ( ) , e) ;
return Result . fail ( e. getMessage ( ) ) ;
} catch ( Exception e) {
log. error ( "练习提交题目异常!错误原因{}" , e. getMessage ( ) , e) ;
return Result . fail ( "练习提交题目异常!" ) ;
}
}
2.PracticeDetailService.java
package com. sunxiansheng. practice. server. service ;
import com. sunxiansheng. practice. api. req. SubmitSubjectDetailReq ;
public interface PracticeDetailService {
Boolean submitSubject ( SubmitSubjectDetailReq req) ;
}
3.PracticeDetailServiceImpl.java
package com. sunxiansheng. practice. server. service. impl ;
import com. sunxiansheng. practice. api. enums. SubjectInfoTypeEnum ;
import com. sunxiansheng. practice. api. req. SubmitSubjectDetailReq ;
import com. sunxiansheng. practice. server. dao. * ;
import com. sunxiansheng. practice. server. entity. dto. SubjectDTO ;
import com. sunxiansheng. practice. server. entity. po. PracticeDetailPO ;
import com. sunxiansheng. practice. server. entity. po. PracticePO ;
import com. sunxiansheng. practice. server. entity. po. SubjectMultiplePO ;
import com. sunxiansheng. practice. server. entity. po. SubjectRadioPO ;
import com. sunxiansheng. practice. server. service. PracticeDetailService ;
import com. sunxiansheng. practice. server. util. LoginUtil ;
import lombok. extern. slf4j. Slf4j ;
import org. apache. commons. lang3. StringUtils ;
import org. springframework. stereotype. Service ;
import org. springframework. transaction. annotation. Transactional ;
import org. springframework. util. CollectionUtils ;
import javax. annotation. Resource ;
import java. util. Collections ;
import java. util. Date ;
import java. util. List ;
import java. util. Objects ;
@Service
@Slf4j
public class PracticeDetailServiceImpl implements PracticeDetailService {
@Resource
private PracticeDao practiceDao;
@Resource
private SubjectDao subjectDao;
@Resource
private SubjectRadioDao subjectRadioDao;
@Resource
private SubjectMultipleDao subjectMultipleDao;
@Resource
private PracticeDetailDao practiceDetailDao;
@Override
@Transactional ( rollbackFor = Exception . class )
public Boolean submitSubject ( SubmitSubjectDetailReq req) {
Long practiceId = req. getPracticeId ( ) ;
Long subjectId = req. getSubjectId ( ) ;
List < Integer > answerContents = req. getAnswerContents ( ) ;
Integer subjectType = req. getSubjectType ( ) ;
String timeUse = req. getTimeUse ( ) ;
if ( timeUse. equals ( "0" ) ) {
timeUse = "000000" ;
}
String hour = timeUse. substring ( 0 , 2 ) ;
String minute = timeUse. substring ( 2 , 4 ) ;
String second = timeUse. substring ( 4 , 6 ) ;
PracticePO practicePO = new PracticePO ( ) ;
practicePO. setId ( practiceId) ;
practicePO. setTimeUse ( hour + ":" + minute + ":" + second) ;
practicePO. setSubmitTime ( new Date ( ) ) ;
practiceDao. update ( practicePO) ;
PracticeDetailPO practiceDetailPO = new PracticeDetailPO ( ) ;
practiceDetailPO. setPracticeId ( practiceId) ;
practiceDetailPO. setSubjectId ( subjectId) ;
practiceDetailPO. setSubjectType ( subjectType) ;
String loginId = LoginUtil . getLoginId ( ) ;
practiceDetailPO. setCreatedBy ( loginId) ;
practiceDetailPO. setCreatedTime ( new Date ( ) ) ;
practiceDetailPO. setIsDeleted ( 0 ) ;
String answerContent = getAnswerContent ( answerContents) ;
practiceDetailPO. setAnswerContent ( answerContent) ;
SubjectDTO subjectDTO = new SubjectDTO ( ) ;
subjectDTO. setSubjectId ( req. getSubjectId ( ) ) ;
subjectDTO. setSubjectType ( req. getSubjectType ( ) ) ;
Integer answerStatus = 0 ;
StringBuffer correctAnswer = new StringBuffer ( ) ;
extracted ( subjectType, subjectId, correctAnswer) ;
if ( Objects . equals ( correctAnswer. toString ( ) , answerContent) ) {
answerStatus = 1 ;
}
practiceDetailPO. setAnswerStatus ( answerStatus) ;
PracticeDetailPO existDetail = practiceDetailDao. selectDetail ( practiceId, subjectId, loginId) ;
if ( Objects . isNull ( existDetail) ) {
practiceDetailDao. insertSingle ( practiceDetailPO) ;
} else {
practiceDetailPO. setId ( existDetail. getId ( ) ) ;
practiceDetailDao. update ( practiceDetailPO) ;
}
return true ;
}
private void extracted ( Integer subjectType, Long subjectId, StringBuffer correctAnswer) {
if ( subjectType == SubjectInfoTypeEnum . RADIO . getCode ( ) ) {
List < SubjectRadioPO > subjectRadioPOS = subjectRadioDao. selectBySubjectId ( subjectId) ;
subjectRadioPOS. forEach (
radio -> {
if ( Objects . equals ( radio. getIsCorrect ( ) , 1 ) ) {
correctAnswer. append ( radio. getOptionType ( ) ) ;
}
}
) ;
}
if ( subjectType == SubjectInfoTypeEnum . MULTIPLE . getCode ( ) ) {
List < SubjectMultiplePO > subjectMultiplePOS = subjectMultipleDao. selectBySubjectId ( subjectId) ;
subjectMultiplePOS. forEach (
multiple -> {
if ( Objects . equals ( multiple. getIsCorrect ( ) , 1 ) ) {
correctAnswer. append ( multiple. getOptionType ( ) ) . append ( "," ) ;
}
}
) ;
if ( correctAnswer. length ( ) > 0 ) {
correctAnswer. deleteCharAt ( correctAnswer. length ( ) - 1 ) ;
}
}
if ( subjectType == SubjectInfoTypeEnum . JUDGE . getCode ( ) ) {
List < SubjectMultiplePO > subjectMultiplePOS = subjectMultipleDao. selectBySubjectId ( subjectId) ;
Integer isCorrect = subjectMultiplePOS. get ( 0 ) . getIsCorrect ( ) ;
correctAnswer. append ( isCorrect) ;
}
}
private static String getAnswerContent ( List < Integer > answerContents) {
String answerContent = "" ;
if ( ! CollectionUtils . isEmpty ( answerContents) ) {
Collections . sort ( answerContents) ;
answerContent = StringUtils . join ( answerContents, "," ) ;
}
return answerContent;
}
}
4.PracticeDetailDao.java
int update ( PracticeDetailPO practiceDetailPO) ;
5.PracticeDetailDao.xml
< update id = " update" >
update practice_detail
< set>
< if test = " answerStatus != null" >
answer_status = #{answerStatus},
</ if>
< if test = " answerContent != null" >
answer_content = #{answerContent},
</ if>
</ set>
where id = #{id,jdbcType=BIGINT}
</ update>
6.req
SubmitSubjectDetailReq.java
package com. sunxiansheng. practice. api. req ;
import lombok. Data ;
import java. io. Serializable ;
import java. util. List ;
@Data
public class SubmitSubjectDetailReq implements Serializable {
private Long practiceId;
private Long subjectId;
private List < Integer > answerContents;
private Integer subjectType;
private String timeUse;
}
7.dto
1.SubjectDetailDTO.java
package com. sunxiansheng. practice. server. entity. dto ;
import lombok. Data ;
import java. io. Serializable ;
import java. util. List ;
@Data
public class SubjectDetailDTO implements Serializable {
private Long id;
private String subjectName;
private Integer isCorrect;
private String subjectParse;
private List < SubjectOptionDTO > optionList;
}
2.SubjectDTO.java
package com. sunxiansheng. practice. server. entity. dto ;
import lombok. Data ;
import java. io. Serializable ;
@Data
public class SubjectDTO implements Serializable {
private Long id;
private Long subjectId;
private String subjectName;
private Integer subjectType;
}
3.SubjectOptionDTO.java
package com. sunxiansheng. practice. server. entity. dto ;
import lombok. Data ;
import java. io. Serializable ;
@Data
public class SubjectOptionDTO implements Serializable {
private Integer optionType;
private String optionContent;
private Integer isCorrect;
}
8.测试
1.接口设计
2.db
1.更新提交时间和用时 practice_info
2.练习细节如果有的话就更新记录 practice_detail
3.测试遇到一个bug,即使题目正确也会插入答案状态为0,状态设置的时候有问题
2.答案解析-每题得分
1.GetScoreDetailReq.java
package com. sunxiansheng. practice. api. req ;
import lombok. Data ;
import java. io. Serializable ;
@Data
public class GetScoreDetailReq implements Serializable {
private Long practiceId;
}
2.ScoreDetailVO.java
package com. sunxiansheng. practice. api. vo ;
import lombok. Data ;
import java. io. Serializable ;
@Data
public class ScoreDetailVO implements Serializable {
private Long subjectId;
private Integer subjectType;
private Integer isCorrect;
}
3.PracticeDetailController.java
@PostMapping ( value = "/getScoreDetail" )
public Result < List < ScoreDetailVO > > getScoreDetail ( @RequestBody GetScoreDetailReq req) {
try {
if ( log. isInfoEnabled ( ) ) {
log. info ( "每题得分入参{}" , JSON . toJSONString ( req) ) ;
}
Preconditions . checkArgument ( ! Objects . isNull ( req) , "参数不能为空!" ) ;
Preconditions . checkArgument ( ! Objects . isNull ( req. getPracticeId ( ) ) , "练习id不能为空!" ) ;
List < ScoreDetailVO > list = practiceDetailService. getScoreDetail ( req) ;
if ( log. isInfoEnabled ( ) ) {
log. info ( "每题得分出参{}" , JSON . toJSONString ( list) ) ;
}
return Result . ok ( list) ;
} catch ( IllegalArgumentException e) {
log. error ( "参数异常!错误原因{}" , e. getMessage ( ) , e) ;
return Result . fail ( e. getMessage ( ) ) ;
} catch ( Exception e) {
log. error ( "每题得分异常!错误原因{}" , e. getMessage ( ) , e) ;
return Result . fail ( "每题得分异常!" ) ;
}
}
4.PracticeDetailService.java
List < ScoreDetailVO > getScoreDetail ( GetScoreDetailReq req) ;
5.PracticeDetailServiceImpl.java
@Override
public List < ScoreDetailVO > getScoreDetail ( GetScoreDetailReq req) {
Long practiceId = req. getPracticeId ( ) ;
List < PracticeDetailPO > practiceDetailPOList = practiceDetailDao. selectByPracticeId ( practiceId) ;
if ( CollectionUtils . isEmpty ( practiceDetailPOList) ) {
return Collections . emptyList ( ) ;
}
List < ScoreDetailVO > res = practiceDetailPOList. stream ( ) . map (
po -> {
ScoreDetailVO scoreDetailVO = new ScoreDetailVO ( ) ;
scoreDetailVO. setSubjectId ( po. getSubjectId ( ) ) ;
scoreDetailVO. setSubjectType ( po. getSubjectType ( ) ) ;
scoreDetailVO. setIsCorrect ( po. getAnswerStatus ( ) ) ;
return scoreDetailVO;
}
) . collect ( Collectors . toList ( ) ) ;
return res;
}
6.PracticeDetailDao.xml
< select id = " selectByPracticeId" resultType = " com.sunxiansheng.practice.server.entity.po.PracticeDetailPO" >
select subject_id as subjectId, subject_type as subjectType, answer_status as answerStatus
from practice_detail
where practice_id = #{practiceId}
and is_deleted = 0
</ select>
7.测试