文章目录
1.sun-club-practice-api
1.enums
1.CompleteStatusEnum.java
package com.sunxiansheng.practice.api.enums;
public enum CompleteStatusEnum {
/**
* 未完成
*/
NO_COMPLETE(0, "未完成"),
/**
* 已完成
*/
COMPLETE(1, "已完成");
final private int code;
final private String desc;
CompleteStatusEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
}
2.req
1.GetPracticeSubjectsReq.java
package com.sunxiansheng.practice.api.req;
import lombok.Data;
import java.io.Serializable;
/**
* Description:
* @Author sun
* @Create 2024/6/28 13:03
* @Version 1.0
*/
@Data
public class GetPracticeSubjectsReq implements Serializable {
/**
* 套题id
*/
private Long setId;
/**
* 练习id
*/
private Long practiceId;
}
3.vo
1.PracticeSubjectListVO.java
package com.sunxiansheng.practice.api.vo;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class PracticeSubjectListVO implements Serializable {
/**
* 练习标题(套卷名字)
*/
private String title;
/**
* 题目列表
*/
private List<PracticeSubjectDetailVO> subjectList;
/**
* 练习id
*/
private Long practiceId;
/**
* 用时
*/
private String timeUse;
}
2.sun-club-practice-server
1.PracticeSetController.java
/**
* 获取题目详情
*/
@PostMapping(value = "/getPracticeSubject")
public Result<PracticeSubjectVO> getPracticeSubject(@RequestBody GetPracticeSubjectReq req) {
if (log.isInfoEnabled()) {
log.info("获取练习题详情入参{}", JSON.toJSONString(req));
}
try {
Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空!");
Preconditions.checkArgument(!Objects.isNull(req.getSubjectId()), "题目id不能为空!");
Preconditions.checkArgument(!Objects.isNull(req.getSubjectType()), "题目类型不能为空!");
PracticeSubjectDTO dto = new PracticeSubjectDTO();
dto.setSubjectId(req.getSubjectId());
dto.setSubjectType(req.getSubjectType());
PracticeSubjectVO vo = practiceSetService.getPracticeSubject(dto);
if (log.isInfoEnabled()) {
log.info("获取练习题目详情出参{}", JSON.toJSONString(vo));
}
return Result.ok(vo);
} 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.service
1.PracticeSetServiceImpl.java
/**
*
* @param req setId practiceId
* @return
*/
public PracticeSubjectListVO getSubjects(GetPracticeSubjectsReq req) {
// 获取req中的setId
Long setId = req.getSetId();
// 构建一个返回的vo
PracticeSubjectListVO vo = new PracticeSubjectListVO();
// 根据setId查询出所有题目的id和type
List<PracticeSetDetailPO> practiceSetDetailPOS = practiceSetDetailDao.selectBySetId(setId);
// 判空
if (CollectionUtils.isEmpty(practiceSetDetailPOS)) {
return vo;
}
// 从请求中获取练习id,以此来判断用户是否练习过
Long practiceId = req.getPracticeId();
// 获取loginId
String loginId = LoginUtil.getLoginId();
// 组装List<PracticeSubjectDetailVO>
List<PracticeSubjectDetailVO> practiceSubjectDetailVOList = practiceSetDetailPOS.stream().map(
// 练习套卷的subjectId和subjectTpye
practiceSetDetailPO -> {
// 将每一个题目的subjectId和subjectTpye映射到PracticeSubjectDetailVO
PracticeSubjectDetailVO practiceSubjectDetailVO = new PracticeSubjectDetailVO();
practiceSubjectDetailVO.setSubjectId(practiceSetDetailPO.getSubjectId());
practiceSubjectDetailVO.setSubjectType(practiceSetDetailPO.getSubjectType());
// 当练题id不为空的时候,根据练习id是否为空来设置每个题目是否已经作答
if (Objects.nonNull(practiceId)) {
// 查询出当前题目的详情
PracticeDetailPO practiceDetailPO = practiceDetailDao.selectDetail(practiceId, practiceSetDetailPO.getSubjectId(), loginId);
// 根据这条题目的答案内容是否为空,做出不同决策
if (Objects.nonNull(practiceDetailPO) && StringUtils.isNotBlank(practiceDetailPO.getAnswerContent())) {
// 答案如果不为空,则设置这个题目为已回答
practiceSubjectDetailVO.setIsAnswer(1);
} else {
// 未回答
practiceSubjectDetailVO.setIsAnswer(0);
}
}
return practiceSubjectDetailVO;
}
).collect(Collectors.toList());
// 查询套卷名字
PracticeSetPO practiceSetPO = practiceSetDao.selectById(setId);
// 组装PracticeSubjectListVO
vo.setTitle(practiceSetPO.getSetName());
vo.setSubjectList(practiceSubjectDetailVOList);
// 如果练习id为空,添加逻辑
if (Objects.isNull(practiceId)) {
// 根据套卷id新增一个练习
Long newPracticeId = insertUnCompletePractice(setId);
// 返回
vo.setPracticeId(newPracticeId);
} else {
// 更新获取练习的时间
updateUnCompletePractice(practiceId);
PracticePO practicePO = practiceDao.selectById(practiceId);
// 返回用时和练习id
vo.setTimeUse(practicePO.getTimeUse());
vo.setPracticeId(practiceId);
}
return vo;
}
/**
* 新增练习并返回id
* @param practiceSetId
* @return
*/
private Long insertUnCompletePractice(Long practiceSetId) {
PracticePO practicePO = new PracticePO();
practicePO.setSetId(practiceSetId);
practicePO.setCompleteStatus(CompleteStatusEnum.NO_COMPLETE.getCode());
practicePO.setTimeUse("00:00:00");
practicePO.setSubmitTime(new Date());
practicePO.setCorrectRate(new BigDecimal("0.00"));
practicePO.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
practicePO.setCreatedBy(LoginUtil.getLoginId());
practicePO.setCreatedTime(new Date());
practiceDao.insert(practicePO);
return practicePO.getId();
}
/**
* 更新获取练习的时间(重新计时)
* @param practiceId
*/
private void updateUnCompletePractice(Long practiceId) {
PracticePO practicePO = new PracticePO();
practicePO.setId(practiceId);
practicePO.setSubmitTime(new Date());
practiceDao.update(practicePO);
}
3.dao
1.PracticeDao.java
package com.sunxiansheng.practice.server.dao;
import com.sunxiansheng.practice.server.entity.po.PracticePO;
/**
* Description:
* @Author sun
* @Create 2024/6/28 16:40
* @Version 1.0
*/
public interface PracticeDao {
/**
* 根据id查询练习信息
* @param practiceId
* @return
*/
PracticePO selectById(Long practiceId);
/**
* 插入练习信息
* @param practicePO
*/
void insert(PracticePO practicePO);
/**
* 更新练习信息
* @param practicePO
*/
void update(PracticePO practicePO);
}
2.PracticeDetailDao.java
package com.sunxiansheng.practice.server.dao;
import com.sunxiansheng.practice.server.entity.po.PracticeDetailPO;
import org.apache.ibatis.annotations.Param;
/**
* Description:
* @Author sun
* @Create 2024/6/28 16:07
* @Version 1.0
*/
public interface PracticeDetailDao {
/**
* 根据练习的id和题目id还有创建人来查询练习细节
* @param practiceId
* @param subjectId
* @param loginId
* @return
*/
PracticeDetailPO selectDetail(@Param("practiceId") Long practiceId, @Param("subjectId") Long subjectId, @Param("loginId") String loginId);
}
4.mapper
1.PracticeDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sunxiansheng.practice.server.dao.PracticeDao">
<select id="selectById" resultType="com.sunxiansheng.practice.server.entity.po.PracticePO">
select set_id as setId, time_use as timeUse, submit_time as submitTime, correct_rate as correctRate
from practice_info
where id = #{id}
and is_deleted = 0
</select>
<insert id="insert">
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO practice_info(set_id, complete_status, time_use, submit_time, correct_rate, is_deleted,
created_by, created_time)
values (#{setId,jdbcType=BIGINT},
#{completeStatus,jdbcType=INTEGER},
#{timeUse,jdbcType=VARCHAR},
#{submitTime,jdbcType=TIMESTAMP},
#{correctRate,jdbcType=VARCHAR},
#{isDeleted,jdbcType=INTEGER},
#{createdBy,jdbcType=VARCHAR},
#{createdTime,jdbcType=TIMESTAMP})
</insert>
<update id="update">
update practice_info
<set>
<if test="submitTime != null">
submit_time = #{submitTime},
</if>
<if test="timeUse != null">
time_use = #{timeUse},
</if>
<if test="completeStatus != null">
complete_status = #{completeStatus},
</if>
<if test="correctRate != null">
correct_rate = #{correctRate},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
2.PracticeDetailDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sunxiansheng.practice.server.dao.PracticeDetailDao">
<select id="selectDetail" resultType="com.sunxiansheng.practice.server.entity.po.PracticeDetailPO">
select id,
subject_id as subjectId,
subject_type as subjectType,
answer_status as answerStatus,
answer_content as answerContent
from practice_detail
where is_deleted = 0
and practice_id = #{practiceId}
and subject_id = #{subjectId}
and created_by = #{loginId}
</select>
</mapper>
5.entity
1.po
1.PracticeDetailPO.java
package com.sunxiansheng.practice.server.entity.po;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class PracticeDetailPO implements Serializable {
/**
* 主键
*/
private Long id;
/**
* 练题id
*/
private Long practiceId;
/**
* 题目id
*/
private Long subjectId;
/**
* 题目类型
*/
private Integer subjectType;
/**
* 是否正确 1正确 0错误
*/
private Integer answerStatus;
/**
* 答案内容
*/
private String answerContent;
/**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdTime;
/**
* 更新人
*/
private String updateBy;
private Integer isDeleted;
/**
* 更新时间
*/
private Date updateTime;
}
2.PracticePO.java
package com.sunxiansheng.practice.server.entity.po;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class PracticePO implements Serializable {
/**
* 主键
*/
private Long id;
/**
* 套题id
*/
private Long setId;
/**
* 完成情况 1完成 0未完成
*/
private Integer completeStatus;
/**
* 所用时间
*/
private String timeUse;
/**
* 交卷时间
*/
private Date submitTime;
/**
* 正确率
*/
private BigDecimal correctRate;
/**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private Date createdTime;
/**
* 更新人
*/
private String updateBy;
private Integer isDeleted;
/**
* 更新时间
*/
private Date updateTime;
}