|
|
@@ -0,0 +1,189 @@
|
|
|
+package space.anyi.serve.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import space.anyi.serve.entity.problem.*;
|
|
|
+import space.anyi.serve.entity.user.User;
|
|
|
+import space.anyi.serve.mapper.ProblemCaseMapper;
|
|
|
+import space.anyi.serve.mapper.ProblemCategoryRelMapper;
|
|
|
+import space.anyi.serve.mapper.ProblemMapper;
|
|
|
+import space.anyi.serve.mapper.ProblemTestMapper;
|
|
|
+import space.anyi.serve.service.ProblemService;
|
|
|
+import space.anyi.serve.service.UserService;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem> implements ProblemService {
|
|
|
+
|
|
|
+ private final ProblemCaseMapper problemCaseMapper;
|
|
|
+ private final ProblemTestMapper problemTestMapper;
|
|
|
+ private final ProblemCategoryRelMapper problemCategoryRelMapper;
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ public ProblemServiceImpl(ProblemCaseMapper problemCaseMapper,
|
|
|
+ ProblemTestMapper problemTestMapper,
|
|
|
+ ProblemCategoryRelMapper problemCategoryRelMapper,
|
|
|
+ UserService userService) {
|
|
|
+ this.problemCaseMapper = problemCaseMapper;
|
|
|
+ this.problemTestMapper = problemTestMapper;
|
|
|
+ this.problemCategoryRelMapper = problemCategoryRelMapper;
|
|
|
+ this.userService = userService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<ProblemVo> queryByPage(String title, String difficulty, Long categoryId,
|
|
|
+ Integer isPublic, boolean onlyPublic, Page<Problem> page) {
|
|
|
+ LambdaQueryWrapper<Problem> wrapper = new LambdaQueryWrapper<Problem>()
|
|
|
+ .like(StringUtils.hasText(title), Problem::getTitle, title)
|
|
|
+ .eq(StringUtils.hasText(difficulty), Problem::getDifficulty, difficulty)
|
|
|
+ .eq(isPublic != null, Problem::getIsPublic, isPublic)
|
|
|
+ .orderByDesc(Problem::getId);
|
|
|
+ if (onlyPublic) {
|
|
|
+ wrapper.eq(Problem::getIsPublic, 1);
|
|
|
+ }
|
|
|
+ if (categoryId != null) {
|
|
|
+ List<ProblemCategoryRel> rels = problemCategoryRelMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<ProblemCategoryRel>()
|
|
|
+ .eq(ProblemCategoryRel::getCategoryId, categoryId));
|
|
|
+ List<Long> problemIds = rels.stream().map(ProblemCategoryRel::getProblemId).toList();
|
|
|
+ if (problemIds.isEmpty()) {
|
|
|
+ Page<ProblemVo> empty = new Page<>(page.getCurrent(), page.getSize(), 0);
|
|
|
+ return empty;
|
|
|
+ }
|
|
|
+ wrapper.in(Problem::getId, problemIds);
|
|
|
+ }
|
|
|
+ Page<Problem> problemPage = page(page, wrapper);
|
|
|
+ Page<ProblemVo> result = new Page<>(problemPage.getCurrent(), problemPage.getSize(), problemPage.getTotal());
|
|
|
+ result.setRecords(ProblemVo.from(problemPage.getRecords()));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProblemDetailVo getDetail(Long id) {
|
|
|
+ Problem problem = getById(id);
|
|
|
+ if (problem == null) {
|
|
|
+ throw new IllegalArgumentException("题目不存在");
|
|
|
+ }
|
|
|
+ ProblemDetailVo vo = ProblemDetailVo.from(problem);
|
|
|
+ vo.setCategories(problemCategoryRelMapper.selectCategoriesByProblemId(id));
|
|
|
+ List<ProblemCase> cases = problemCaseMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<ProblemCase>()
|
|
|
+ .eq(ProblemCase::getProblemId, id)
|
|
|
+ .orderByAsc(ProblemCase::getSortOrder));
|
|
|
+ vo.setCases(ProblemCaseVo.from(cases));
|
|
|
+ if (problem.getCreatedBy() != null) {
|
|
|
+ User creator = userService.getById(problem.getCreatedBy());
|
|
|
+ if (creator != null) {
|
|
|
+ vo.setCreatedByUsername(creator.getUsername());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Boolean insert(ProblemDto dto, Long createdBy) {
|
|
|
+ Problem problem = new Problem();
|
|
|
+ BeanUtils.copyProperties(dto, problem);
|
|
|
+ problem.setCreatedBy(createdBy);
|
|
|
+ boolean saved = save(problem);
|
|
|
+ if (saved && dto.getCategoryIds() != null) {
|
|
|
+ for (Long categoryId : dto.getCategoryIds()) {
|
|
|
+ problemCategoryRelMapper.upsert(problem.getId(), categoryId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Boolean update(ProblemDto dto) {
|
|
|
+ Problem problem = getById(dto.getId());
|
|
|
+ if (problem == null) {
|
|
|
+ throw new IllegalArgumentException("题目不存在");
|
|
|
+ }
|
|
|
+ problem.setTitle(dto.getTitle());
|
|
|
+ problem.setDescription(dto.getDescription());
|
|
|
+ problem.setTimeLimitMs(dto.getTimeLimitMs());
|
|
|
+ problem.setMemoryLimitMb(dto.getMemoryLimitMb());
|
|
|
+ problem.setIsPublic(dto.getIsPublic());
|
|
|
+ problem.setDifficulty(dto.getDifficulty());
|
|
|
+ boolean updated = updateById(problem);
|
|
|
+ problemCategoryRelMapper.delete(new LambdaQueryWrapper<ProblemCategoryRel>()
|
|
|
+ .eq(ProblemCategoryRel::getProblemId, dto.getId()));
|
|
|
+ if (dto.getCategoryIds() != null) {
|
|
|
+ for (Long categoryId : dto.getCategoryIds()) {
|
|
|
+ problemCategoryRelMapper.upsert(dto.getId(), categoryId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return updated;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Boolean deleteByIds(List<Long> ids) {
|
|
|
+ if (ids == null || ids.isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ problemCategoryRelMapper.delete(new LambdaQueryWrapper<ProblemCategoryRel>()
|
|
|
+ .in(ProblemCategoryRel::getProblemId, ids));
|
|
|
+ problemCaseMapper.delete(new LambdaQueryWrapper<ProblemCase>()
|
|
|
+ .in(ProblemCase::getProblemId, ids));
|
|
|
+ problemTestMapper.delete(new LambdaQueryWrapper<ProblemTest>()
|
|
|
+ .in(ProblemTest::getProblemId, ids));
|
|
|
+ return removeByIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean addCase(ProblemCaseDto dto) {
|
|
|
+ ProblemCase problemCase = new ProblemCase();
|
|
|
+ BeanUtils.copyProperties(dto, problemCase);
|
|
|
+ return problemCaseMapper.insert(problemCase) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateCase(ProblemCaseDto dto) {
|
|
|
+ ProblemCase problemCase = new ProblemCase();
|
|
|
+ BeanUtils.copyProperties(dto, problemCase);
|
|
|
+ return problemCaseMapper.updateById(problemCase) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deleteCase(Long id) {
|
|
|
+ return problemCaseMapper.deleteById(id) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean addTest(ProblemTestDto dto) {
|
|
|
+ ProblemTest problemTest = new ProblemTest();
|
|
|
+ BeanUtils.copyProperties(dto, problemTest);
|
|
|
+ return problemTestMapper.insert(problemTest) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateTest(ProblemTestDto dto) {
|
|
|
+ ProblemTest problemTest = new ProblemTest();
|
|
|
+ BeanUtils.copyProperties(dto, problemTest);
|
|
|
+ return problemTestMapper.updateById(problemTest) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deleteTest(Long id) {
|
|
|
+ return problemTestMapper.deleteById(id) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ProblemTestVo> queryTests(Long problemId) {
|
|
|
+ List<ProblemTest> tests = problemTestMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<ProblemTest>()
|
|
|
+ .eq(ProblemTest::getProblemId, problemId)
|
|
|
+ .orderByAsc(ProblemTest::getSortOrder));
|
|
|
+ return ProblemTestVo.from(tests);
|
|
|
+ }
|
|
|
+}
|