fix:随机一题

上级 2c2601dd
package com.kwan.springbootkwan.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springbootkwan.entity.AlgorithmicProblem;
import com.kwan.springbootkwan.entity.Result;
import com.kwan.springbootkwan.entity.dto.AlgorithmicProblemDTO;
import com.kwan.springbootkwan.service.AlgorithmicProblemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 算法题(AlgorithmicProblem)表控制层
*
* @author makejava
* @since 2023-10-07 09:15:45
*/
@RestController
@RequestMapping("algorithmicProblem")
public class AlgorithmicProblemController {
/**
* 服务对象
*/
@Resource
private AlgorithmicProblemService algorithmicProblemService;
/**
* 获取面试题的种类的数量
*
* @return
*/
@GetMapping("/questionType")
public Result questionType() {
return Result.ok(this.algorithmicProblemService.questionType());
}
/**
* 分页查询所有数据
*
* @return 所有数据
*/
@GetMapping("/page")
public Result selectAll(@RequestParam Integer page
, @RequestParam Integer pageSize
, @RequestParam String questionName
, @RequestParam Integer questionType) {
Page<AlgorithmicProblem> pageParm = new Page<>();
pageParm.setCurrent(page);
pageParm.setSize(pageSize);
QueryWrapper<AlgorithmicProblem> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id");
if (questionType != 0) {
wrapper.eq("question_type", questionType);
}
wrapper.eq("is_delete", 0);
if (StringUtils.isNotEmpty(questionName)) {
wrapper.like("question_name", questionName);
}
return Result.ok(AlgorithmicProblemDTO.Converter.INSTANCE.from(this.algorithmicProblemService.page(pageParm, wrapper)));
}
/**
* 随机一题
*/
@GetMapping("/random")
public Result random() {
QueryWrapper<AlgorithmicProblem> wrapper = new QueryWrapper<>();
wrapper.eq("is_delete", 0);
wrapper.orderByAsc("rand()").last("limit 1");
return Result.ok(AlgorithmicProblemDTO.Converter.INSTANCE.from(this.algorithmicProblemService.getOne(wrapper)));
}
}
package com.kwan.springbootkwan.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.util.Date;
/**
* 算法题(AlgorithmicProblem)表实体类
*
* @author makejava
* @since 2023-10-07 09:15:47
*/
@Data
public class AlgorithmicProblem extends Model<AlgorithmicProblem> {
/**
* 主键id
*/
private Integer id;
/**
* 问题名称
*/
private String questionName;
/**
* 0:'全部';
* 1: '链表';
* 2: '数组';
* 3: 'JVM';
* 4: '并发编程';
* 5:'MySql';
* 6: 'Redis';
* 7: '中间件';
* 8: 'Spring';
* 9: '微服务';
* 10:'分布式';
* 11:'项目';
* 12:'算法';
* 99: '其他';
*/
private Integer questionType;
/**
* 1~10的分值
*/
private Integer degreeOfImportance;
/**
* 1:简单;2:中等;3:困难
*/
private Integer degreeOfDifficulty;
/**
* 困难指数
*/
private Integer difficultyOfScore;
/**
* 力扣的问题号
*/
private Integer leetcodeNumber;
/**
* 力扣的问题链接
*/
private String leetcodeLink;
/**
* 创建时间
*/
private Date createTime;
//逻辑删除,0未删除,1已删除
private Integer isDelete;
}
\ No newline at end of file
package com.kwan.springbootkwan.entity.dto;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.kwan.springbootkwan.entity.AlgorithmicProblem;
import com.kwan.springbootkwan.mapstruct.FromConverter;
import lombok.Data;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import java.util.Date;
/**
* 面试题(InterviewQuestion)表实体类
*
* @author makejava
* @since 2023-09-08 16:31:53
*/
@Data
@SuppressWarnings("serial")
public class AlgorithmicProblemDTO extends Model<AlgorithmicProblemDTO> {
/**
* 主键id
*/
private Integer id;
/**
* 面试问题
*/
private String questionName;
/**
* 类型
*/
private Integer questionType;
/**
* 1~10的分值
*/
private Integer degreeOfImportance;
/**
* 1:简单;2:中等;3:困难
*/
private Integer degreeOfDifficulty;
/**
* 困难指数
*/
private Integer difficultyOfScore;
/**
* 力扣的问题号
*/
private Integer leetcodeNumber;
/**
* 力扣的问题链接
*/
private String leetcodeLink;
/**
* 创建时间
*/
private Date createTime;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Converter extends FromConverter<AlgorithmicProblemDTO, AlgorithmicProblem> {
AlgorithmicProblemDTO.Converter INSTANCE = Mappers.getMapper(AlgorithmicProblemDTO.Converter.class);
}
}
\ No newline at end of file
package com.kwan.springbootkwan.entity.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AlgorithmicQuestionTypeDTO {
/**
* 问题类型编码
*/
private Integer questionType;
/**
* 问题类型名称
*/
private String name;
/**
* 问题类型数量
*/
private Integer typeSize;
}
package com.kwan.springbootkwan.enums;
import lombok.Getter;
import lombok.ToString;
@Getter
@ToString
public enum AlgorithmicProblemTypeEnum {
/**
* 全部
*/
type_00(0, "全部"),
/**
* 链表
*/
type_01(1, "链表"),
/**
* 数组
*/
type_02(2, "数组"),
/**
* JVM
*/
type_03(3, "JVM"),
/**
* 并发编程
*/
type_04(4, "并发编程"),
/**
* MySql
*/
type_05(5, "MySql"),
/**
* Redis
*/
type_06(6, "Redis"),
/**
* 中间件
*/
type_07(7, "中间件"),
/**
* Spring
*/
type_08(8, "Spring"),
/**
* 微服务
*/
type_09(9, "微服务"),
/**
* 分布式
*/
type_10(10, "分布式"),
/**
* 项目
*/
type_11(11, "项目"),
/**
* 算法
*/
type_12(12, "算法"),
/**
* 反问环节
*/
type_13(13, "反问环节"),
/**
* 设计模式
*/
type_14(14, "设计模式"),
/**
* 其他
*/
type_99(99, "其他");
private Integer code;
private String name;
AlgorithmicProblemTypeEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
/**
* 通过code获取name
*
* @param code
* @return
*/
public static String getNameByCode(Integer code) {
for (AlgorithmicProblemTypeEnum enums : AlgorithmicProblemTypeEnum.values()) {
if (enums.getCode().equals(code)) {
return enums.getName();
}
}
return null;
}
/**
* 通过value取枚举
*
* @param value
* @return
*/
public static AlgorithmicProblemTypeEnum getBrandDetailNoByValue(String value) {
if (null == value) {
return null;
}
for (AlgorithmicProblemTypeEnum enums : AlgorithmicProblemTypeEnum.values()) {
if (enums.getCode().equals(value)) {
return enums;
}
}
return null;
}
}
package com.kwan.springbootkwan.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kwan.springbootkwan.entity.AlgorithmicProblem;
import com.kwan.springbootkwan.entity.dto.AlgorithmicQuestionTypeDTO;
import java.util.LinkedList;
/**
* 算法题(AlgorithmicProblem)表数据库访问层
*
* @author makejava
* @since 2023-10-07 09:15:45
*/
public interface AlgorithmicProblemMapper extends BaseMapper<AlgorithmicProblem> {
LinkedList<AlgorithmicQuestionTypeDTO> questionType();
}
package com.kwan.springbootkwan.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kwan.springbootkwan.entity.AlgorithmicProblem;
import com.kwan.springbootkwan.entity.dto.AlgorithmicQuestionTypeDTO;
import java.util.List;
/**
* 算法题(AlgorithmicProblem)表服务接口
*
* @author makejava
* @since 2023-10-07 09:15:47
*/
public interface AlgorithmicProblemService extends IService<AlgorithmicProblem> {
List<AlgorithmicQuestionTypeDTO> questionType();
}
package com.kwan.springbootkwan.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springbootkwan.entity.AlgorithmicProblem;
import com.kwan.springbootkwan.entity.dto.AlgorithmicQuestionTypeDTO;
import com.kwan.springbootkwan.enums.AlgorithmicProblemTypeEnum;
import com.kwan.springbootkwan.mapper.AlgorithmicProblemMapper;
import com.kwan.springbootkwan.service.AlgorithmicProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;
/**
* 算法题(AlgorithmicProblem)表服务实现类
*
* @author makejava
* @since 2023-10-07 09:15:47
*/
@Service("algorithmicProblemService")
public class AlgorithmicProblemServiceImpl extends ServiceImpl<AlgorithmicProblemMapper, AlgorithmicProblem> implements AlgorithmicProblemService {
@Autowired
private AlgorithmicProblemMapper interviewQuestionMapper;
@Override
public List<AlgorithmicQuestionTypeDTO> questionType() {
//获取种类,并按数量排序
LinkedList<AlgorithmicQuestionTypeDTO> types = interviewQuestionMapper.questionType();
types.addFirst(new AlgorithmicQuestionTypeDTO(0, "全部", 0));
for (AlgorithmicQuestionTypeDTO algorithmicQuestionTypeDTO : types) {
//数据库存的是问题类型的编码
algorithmicQuestionTypeDTO.setName(AlgorithmicProblemTypeEnum.getNameByCode(algorithmicQuestionTypeDTO.getQuestionType()));
}
return types;
}
}
<?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.kwan.springbootkwan.mapper.AlgorithmicProblemMapper">
<select id="questionType" resultType="com.kwan.springbootkwan.entity.dto.AlgorithmicQuestionTypeDTO">
SELECT DISTINCT question_type AS questionType, COUNT(*) AS typeSize
FROM algorithmic_problem
WHERE 1 = 1
AND is_delete = 0
AND question_type != 0
GROUP BY question_type
ORDER BY typeSize DESC
</select>
</mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册