Search in sources :

Example 1 with MappingTrainingCategory

use of top.hcode.hoj.pojo.entity.training.MappingTrainingCategory in project HOJ by HimitZH.

the class TrainingServiceImpl method getAdminTrainingDto.

@Override
public CommonResult getAdminTrainingDto(Long tid, HttpServletRequest request) {
    // 获取本场训练的信息
    Training training = trainingMapper.selectById(tid);
    if (training == null) {
        // 查询不存在
        return CommonResult.errorResponse("查询失败:该训练不存在,请检查参数tid是否准确!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和训练拥有者才能操作
    if (!isRoot && !userRolesVo.getUsername().equals(training.getAuthor())) {
        return CommonResult.errorResponse("对不起,你无权限操作!", CommonResult.STATUS_FORBIDDEN);
    }
    TrainingDto trainingDto = new TrainingDto();
    trainingDto.setTraining(training);
    QueryWrapper<MappingTrainingCategory> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tid", tid);
    MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryMapper.selectOne(queryWrapper);
    TrainingCategory trainingCategory = null;
    if (mappingTrainingCategory != null) {
        trainingCategory = trainingCategoryService.getById(mappingTrainingCategory.getCid());
    }
    trainingDto.setTrainingCategory(trainingCategory);
    return CommonResult.successResponse(trainingDto, "查询成功!");
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingDto(top.hcode.hoj.pojo.dto.TrainingDto) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory)

Example 2 with MappingTrainingCategory

use of top.hcode.hoj.pojo.entity.training.MappingTrainingCategory in project HOJ by HimitZH.

the class TrainingServiceImpl method updateTraining.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateTraining(TrainingDto trainingDto) {
    Training training = trainingDto.getTraining();
    Training oldTraining = trainingMapper.selectById(training.getId());
    trainingMapper.updateById(training);
    // 私有训练 修改密码 需要清空之前注册训练的记录
    if (training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        if (!Objects.equals(training.getPrivatePwd(), oldTraining.getPrivatePwd())) {
            UpdateWrapper<TrainingRegister> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId());
            trainingRegisterMapper.delete(updateWrapper);
        }
    }
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getId() == null) {
        try {
            trainingCategoryService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryService.getOne(queryWrapper, false);
        }
    }
    MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryMapper.selectOne(new QueryWrapper<MappingTrainingCategory>().eq("tid", training.getId()));
    if (mappingTrainingCategory == null) {
        mappingTrainingCategoryMapper.insert(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
    } else {
        if (!mappingTrainingCategory.getCid().equals(trainingCategory.getId())) {
            UpdateWrapper<MappingTrainingCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId()).set("cid", trainingCategory.getId());
            int update = mappingTrainingCategoryMapper.update(null, updateWrapper);
            return update > 0;
        }
    }
    return true;
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRegister(top.hcode.hoj.pojo.entity.training.TrainingRegister) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with MappingTrainingCategory

use of top.hcode.hoj.pojo.entity.training.MappingTrainingCategory in project HOJ by HimitZH.

the class AdminTrainingManager method addTraining.

@Transactional(rollbackFor = Exception.class)
public void addTraining(TrainingDto trainingDto) throws StatusFailException {
    Training training = trainingDto.getTraining();
    trainingEntityService.save(training);
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getId() == null) {
        try {
            trainingCategoryEntityService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryEntityService.getOne(queryWrapper, false);
        }
    }
    boolean isOk = mappingTrainingCategoryEntityService.save(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
    if (!isOk) {
        throw new StatusFailException("添加失败!");
    }
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with MappingTrainingCategory

use of top.hcode.hoj.pojo.entity.training.MappingTrainingCategory in project HOJ by HimitZH.

the class TrainingServiceImpl method addTraining.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean addTraining(TrainingDto trainingDto) {
    Training training = trainingDto.getTraining();
    trainingMapper.insert(training);
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getId() == null) {
        try {
            trainingCategoryService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryService.getOne(queryWrapper, false);
        }
    }
    int insert = mappingTrainingCategoryMapper.insert(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
    return insert > 0;
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with MappingTrainingCategory

use of top.hcode.hoj.pojo.entity.training.MappingTrainingCategory in project HOJ by HimitZH.

the class AdminTrainingManager method updateTraining.

@Transactional(rollbackFor = Exception.class)
public void updateTraining(TrainingDto trainingDto) throws StatusForbiddenException, StatusFailException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和训练拥有者才能操作
    if (!isRoot && !userRolesVo.getUsername().equals(trainingDto.getTraining().getAuthor())) {
        throw new StatusForbiddenException("对不起,你无权限操作!");
    }
    Training training = trainingDto.getTraining();
    Training oldTraining = trainingEntityService.getById(training.getId());
    trainingEntityService.updateById(training);
    // 私有训练 修改密码 需要清空之前注册训练的记录
    if (training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        if (!Objects.equals(training.getPrivatePwd(), oldTraining.getPrivatePwd())) {
            UpdateWrapper<TrainingRegister> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId());
            trainingRegisterEntityService.remove(updateWrapper);
        }
    }
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getId() == null) {
        try {
            trainingCategoryEntityService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryEntityService.getOne(queryWrapper, false);
        }
    }
    MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryEntityService.getOne(new QueryWrapper<MappingTrainingCategory>().eq("tid", training.getId()), false);
    if (mappingTrainingCategory == null) {
        mappingTrainingCategoryEntityService.save(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
        adminTrainingRecordManager.checkSyncRecord(trainingDto.getTraining());
    } else {
        if (!mappingTrainingCategory.getCid().equals(trainingCategory.getId())) {
            UpdateWrapper<MappingTrainingCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId()).set("cid", trainingCategory.getId());
            boolean isOk = mappingTrainingCategoryEntityService.update(null, updateWrapper);
            if (isOk) {
                adminTrainingRecordManager.checkSyncRecord(trainingDto.getTraining());
            } else {
                throw new StatusFailException("修改失败");
            }
        }
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRegister(top.hcode.hoj.pojo.entity.training.TrainingRegister) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)6 MappingTrainingCategory (top.hcode.hoj.pojo.entity.training.MappingTrainingCategory)6 Training (top.hcode.hoj.pojo.entity.training.Training)6 TrainingCategory (top.hcode.hoj.pojo.entity.training.TrainingCategory)6 Transactional (org.springframework.transaction.annotation.Transactional)4 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)3 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)3 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)3 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)2 Session (org.apache.shiro.session.Session)2 TrainingDto (top.hcode.hoj.pojo.dto.TrainingDto)2 TrainingRegister (top.hcode.hoj.pojo.entity.training.TrainingRegister)2 HttpSession (javax.servlet.http.HttpSession)1