| | |
| | | |
| | | import com.rongyichuang.judge.dto.request.JudgeInput; |
| | | import com.rongyichuang.judge.dto.response.JudgeResponse; |
| | | import com.rongyichuang.judge.dto.response.JudgeStatsResponse; |
| | | import com.rongyichuang.judge.entity.Judge; |
| | | import com.rongyichuang.tag.entity.Tag; |
| | | import com.rongyichuang.judge.repository.JudgeRepository; |
| | |
| | | import com.rongyichuang.common.service.MediaService; |
| | | import com.rongyichuang.common.exception.BusinessException; |
| | | import com.rongyichuang.common.enums.MediaTargetType; |
| | | import com.rongyichuang.common.util.UserContextUtil; |
| | | import com.rongyichuang.tag.repository.TagRepository; |
| | | import com.rongyichuang.user.service.UserService; |
| | | import com.rongyichuang.user.entity.User; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import java.util.Optional; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.util.StringUtils; |
| | | import org.slf4j.Logger; |
| | |
| | | private final TagRepository tagRepository; |
| | | private final MediaRepository mediaRepository; |
| | | private final MediaService mediaService; |
| | | private final UserService userService; |
| | | private final JdbcTemplate jdbcTemplate; |
| | | private final UserContextUtil userContextUtil; |
| | | |
| | | @Value("${app.media-url:${app.media.url:}}") |
| | | @Value("${app.media-url}") |
| | | private String mediaBaseUrl; |
| | | |
| | | public JudgeService(JudgeRepository judgeRepository, TagRepository tagRepository, |
| | | MediaRepository mediaRepository, MediaService mediaService) { |
| | | MediaRepository mediaRepository, MediaService mediaService, |
| | | UserService userService, JdbcTemplate jdbcTemplate, |
| | | UserContextUtil userContextUtil) { |
| | | this.judgeRepository = judgeRepository; |
| | | this.tagRepository = tagRepository; |
| | | this.mediaRepository = mediaRepository; |
| | | this.mediaService = mediaService; |
| | | this.userService = userService; |
| | | this.jdbcTemplate = jdbcTemplate; |
| | | this.userContextUtil = userContextUtil; |
| | | } |
| | | |
| | | public List<JudgeResponse> findAll() { |
| | |
| | | .orElse(null); |
| | | } |
| | | |
| | | /** |
| | | * 根据用户ID获取评委信息 |
| | | */ |
| | | public Judge findByUserId(Long userId) { |
| | | Optional<Judge> judge = judgeRepository.findByUserId(userId); |
| | | return judge.orElse(null); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前评委的统计数据 |
| | | */ |
| | | public JudgeStatsResponse getJudgeStats() { |
| | | log.info("获取评委统计数据"); |
| | | |
| | | Long currentJudgeId = userContextUtil.getCurrentJudgeId(); |
| | | if (currentJudgeId == null) { |
| | | throw new RuntimeException("当前用户不是评委,无法查询评审统计"); |
| | | } |
| | | |
| | | // 查询我未评审的数量 |
| | | String unReviewedSql = "SELECT COUNT(*) FROM t_activity_player ap " + |
| | | "WHERE EXISTS (" + |
| | | " SELECT 1 FROM t_activity_judge aj " + |
| | | " WHERE ap.activity_id = aj.activity_id " + |
| | | " AND ap.stage_id = aj.stage_id " + |
| | | " AND aj.judge_id = ? " + |
| | | ") " + |
| | | "AND NOT EXISTS (" + |
| | | " SELECT 1 FROM t_activity_player_rating apr " + |
| | | " WHERE ap.id = apr.activity_player_id " + |
| | | " AND apr.judge_id = ? " + |
| | | " AND apr.state = 1 " + |
| | | ") " + |
| | | "AND ap.state = 1"; |
| | | Integer unReviewedCount = jdbcTemplate.queryForObject(unReviewedSql, Integer.class, currentJudgeId, currentJudgeId); |
| | | |
| | | // 查询我已评审的数量 |
| | | String reviewedSql = "SELECT COUNT(*) FROM t_activity_player ap " + |
| | | "WHERE EXISTS (" + |
| | | " SELECT 1 FROM t_activity_judge aj " + |
| | | " WHERE ap.activity_id = aj.activity_id " + |
| | | " AND ap.stage_id = aj.stage_id " + |
| | | " AND aj.judge_id = ? " + |
| | | ") " + |
| | | "AND EXISTS (" + |
| | | " SELECT 1 FROM t_activity_player_rating apr " + |
| | | " WHERE ap.id = apr.activity_player_id " + |
| | | " AND apr.judge_id = ? " + |
| | | " AND apr.state = 1 " + |
| | | ") " + |
| | | "AND ap.state = 1"; |
| | | Integer reviewedCount = jdbcTemplate.queryForObject(reviewedSql, Integer.class, currentJudgeId, currentJudgeId); |
| | | |
| | | // 计算总数 |
| | | Integer totalCount = (unReviewedCount != null ? unReviewedCount : 0) + (reviewedCount != null ? reviewedCount : 0); |
| | | |
| | | log.info("评委统计数据 - 待评审: {}, 已评审: {}, 总数: {}", unReviewedCount, reviewedCount, totalCount); |
| | | |
| | | return new JudgeStatsResponse( |
| | | unReviewedCount != null ? unReviewedCount : 0, |
| | | reviewedCount != null ? reviewedCount : 0, |
| | | totalCount |
| | | ); |
| | | } |
| | | |
| | | @Transactional |
| | | public JudgeResponse save(JudgeInput input) { |
| | | Judge judge; |
| | | User user; |
| | | |
| | | // 处理User表逻辑 |
| | | if (input.getPassword() != null && !input.getPassword().trim().isEmpty() && !"••••••••".equals(input.getPassword())) { |
| | | // 有密码且不是占位符时,创建或更新用户(包含密码) |
| | | user = userService.findOrCreateUserByPhone(input.getPhone(), input.getName(), input.getPassword()); |
| | | } else { |
| | | // 无密码或是占位符时,只更新用户基本信息(不更新密码) |
| | | user = userService.findOrCreateUserByPhone(input.getPhone(), input.getName(), null); |
| | | } |
| | | |
| | | if (input.getId() != null) { |
| | | judge = judgeRepository.findById(input.getId()) |
| | | .orElseThrow(() -> new BusinessException("评委不存在")); |
| | |
| | | judge.setTitle(input.getTitle()); |
| | | judge.setCompany(input.getCompany()); |
| | | judge.setIntroduction(input.getIntroduction()); |
| | | judge.setUserId(user.getId()); // 设置关联的用户ID |
| | | // 头像信息通过t_media表的target_type和target_id关联 |
| | | |
| | | // 设置专业标签 |
| | |
| | | * 保存媒体信息 |
| | | */ |
| | | public MediaResponse saveMediaInfo(MediaInput input) { |
| | | Media media = mediaService.saveMedia( |
| | | input.getName(), |
| | | input.getPath(), |
| | | input.getFileSize(), |
| | | input.getFileExt(), |
| | | input.getMediaType(), |
| | | input.getTargetType(), |
| | | input.getTargetId() |
| | | ); |
| | | Media media; |
| | | |
| | | // 如果有缩略图路径,使用支持缩略图的方法 |
| | | if (input.getThumbPath() != null && !input.getThumbPath().trim().isEmpty()) { |
| | | media = mediaService.saveMedia( |
| | | input.getName(), |
| | | input.getPath(), |
| | | input.getFileSize(), |
| | | input.getFileExt(), |
| | | input.getMediaType(), |
| | | input.getTargetType(), |
| | | input.getTargetId(), |
| | | input.getThumbPath() |
| | | ); |
| | | } else { |
| | | media = mediaService.saveMedia( |
| | | input.getName(), |
| | | input.getPath(), |
| | | input.getFileSize(), |
| | | input.getFileExt(), |
| | | input.getMediaType(), |
| | | input.getTargetType(), |
| | | input.getTargetId() |
| | | ); |
| | | } |
| | | |
| | | MediaResponse response = new MediaResponse(); |
| | | response.setId(media.getId()); |
| | |
| | | response.setMediaType(media.getMediaType()); |
| | | response.setTargetType(media.getTargetType()); |
| | | response.setTargetId(media.getTargetId()); |
| | | response.setThumbPath(media.getThumbPath()); |
| | | response.setDuration(media.getDuration()); |
| | | response.setDescription(media.getDescription()); |
| | | |
| | | return response; |
| | | } |