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, "查询成功!");
}
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;
}
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("添加失败!");
}
}
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;
}
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("修改失败");
}
}
}
}
Aggregations