DrawLotsGroupJudgesServiceImpl.java 10.3 KB
package com.zhongzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongzhi.common.constant.RoleType;
import com.zhongzhi.common.constant.TeachType;
import com.zhongzhi.common.exception.HttpException;
import com.zhongzhi.common.utils.ConstantUtils;
import com.zhongzhi.dao.DrawLotGroupDictMapper;
import com.zhongzhi.dao.DrawLotsGroupItemsMapper;
import com.zhongzhi.dao.MatchDictDAO;
import com.zhongzhi.dto.drawlots.AddJudgeDTO;
import com.zhongzhi.dto.drawlots.DrawLoginDTO;
import com.zhongzhi.dto.drawlots.JudgeScoreScreenDTO;
import com.zhongzhi.dto.judge.MatchJudgesDTO;
import com.zhongzhi.model.*;
import com.zhongzhi.dao.DrawLotsGroupJudgesMapper;
import com.zhongzhi.service.DrawLotsGroupJudgesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongzhi.vo.drawlots.*;
import com.zhongzhi.vo.judge.JudgeInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.zhongzhi.service.impl.DrawLotsGroupItemsServiceImpl.findUnselectedElements;

/**
 * <p>
 * 赛组评委表 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2025-06-19
 */
@Service
public class DrawLotsGroupJudgesServiceImpl extends ServiceImpl<DrawLotsGroupJudgesMapper, DrawLotsGroupJudgesDO> implements DrawLotsGroupJudgesService {

    @Autowired
    private DrawLotsGroupItemsMapper drawLotsGroupItemsMapper;

    @Autowired
    private DrawLotGroupDictMapper drawLotGroupDictMapper;

    @Override
    public IPage<MatchJudgesVO> matchJudges(MatchJudgesDTO matchJudgesDTO) {
        Page page = new Page(matchJudgesDTO.getPageNum(), matchJudgesDTO.getPageSize());
        return this.baseMapper.matchJudges(page, matchJudgesDTO.getSceneId(), matchJudgesDTO.getUserName(), matchJudgesDTO.getExpectorType(), matchJudgesDTO.getRoleType());
    }

    @Override
    public IPage<SelectJudgesVO> selectJudges(MatchJudgesDTO matchJudgesDTO) {
        Page page = new Page(matchJudgesDTO.getPageNum(), matchJudgesDTO.getPageSize());
        return this.baseMapper.selectJudges(page, matchJudgesDTO.getSceneId(), matchJudgesDTO.getUserName());
    }

    @Override
    public String removeJudge(DrawLotsGroupJudgesDO drawLotsGroupJudgesDO) {
        this.baseMapper.deleteById(drawLotsGroupJudgesDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    @Override
    public String updateJudge(DrawLotsGroupJudgesDO drawLotsGroupJudgesDO) {
        this.baseMapper.updateById(drawLotsGroupJudgesDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    @Override
    public String addJudge(AddJudgeDTO addJudgeDTO) {

        List<Long> judgeIds = addJudgeDTO.getJudgeIds();
        for (Long judgeId : judgeIds) {
            DrawLotsGroupJudgesDO drawLotsGroupJudgesDO = new DrawLotsGroupJudgesDO();
            drawLotsGroupJudgesDO.setJudgeId(judgeId);
            drawLotsGroupJudgesDO.setSceneId(addJudgeDTO.getSceneId());
            this.baseMapper.insert(drawLotsGroupJudgesDO);
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    @Override
    public List<DrawLotsGroupJudgesDO> selectNoDrawLotsJudge(DrawLotsGroupJudgesDO drawLotsGroupJudgesDO) {
        return this.baseMapper.selectList(new QueryWrapper<DrawLotsGroupJudgesDO>()
                .lambda()
                .eq(DrawLotsGroupJudgesDO::getSceneId, drawLotsGroupJudgesDO.getSceneId())
                .eq(DrawLotsGroupJudgesDO::getRoleType, drawLotsGroupJudgesDO.getRoleType())
                .isNull(DrawLotsGroupJudgesDO::getGroupId));
    }


    @Override
    public String assignJudge(AddJudgeDTO addJudgeDTO) {

        List<Long> ids = addJudgeDTO.getIds();
        for (Long id : ids) {
            DrawLotsGroupJudgesDO drawLotsGroupJudgesDO = this.baseMapper.selectById(id);
            drawLotsGroupJudgesDO.setGroupId(addJudgeDTO.getGroupId());
            drawLotsGroupJudgesDO.setExpectorType(addJudgeDTO.getExpectorType());
            drawLotsGroupJudgesDO.setRoleType(addJudgeDTO.getRoleType());
            this.baseMapper.updateById(drawLotsGroupJudgesDO);
        }

        return ConstantUtils.SUCCESS_UPDATE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DrawlotsVO drawlots(DrawLotsGroupJudgesDO drawLotsGroupJudgesDO) {

        DrawlotsVO drawlotsVO = new DrawlotsVO();

        //判断是否已经抽过了
        int count = this.baseMapper.selectCount(new QueryWrapper<DrawLotsGroupJudgesDO>()
                .lambda()
                .eq(DrawLotsGroupJudgesDO::getSceneId, drawLotsGroupJudgesDO.getSceneId())
                .eq(DrawLotsGroupJudgesDO::getJudgeId, drawLotsGroupJudgesDO.getJudgeId())
                .isNotNull(DrawLotsGroupJudgesDO::getGroupId));

        if (count > 0) {
            throw new HttpException(10038);
        }

        //当前老师是什么角色
        DrawLotsGroupJudgesDO judge = this.baseMapper.selectOne(new QueryWrapper<DrawLotsGroupJudgesDO>()
        .lambda()
        .eq(DrawLotsGroupJudgesDO::getSceneId,drawLotsGroupJudgesDO.getSceneId())
        .eq(DrawLotsGroupJudgesDO::getJudgeId,drawLotsGroupJudgesDO.getJudgeId()));

        List<DrawLotGroupDictDO> drawLotGroupDictDOS = new ArrayList<>();
        //判断是组长抽还是组员抽
        if (RoleType.TEAMLEADER.equals(judge.getRoleType())) {
            //返回还没有组长的组
            drawLotGroupDictDOS = this.baseMapper.noLeaderGroup(drawLotsGroupJudgesDO.getSceneId());
        } else {
            //返回专家类型还没有满员的组   expectorType
            drawLotGroupDictDOS = this.baseMapper.noMemberGroup(drawLotsGroupJudgesDO.getSceneId(), judge.getExpectorType());
        }

        if (drawLotGroupDictDOS.size() == 0) {
            throw new HttpException(10036);
        }

        //随机一个组别
        DrawLotGroupDictDO drawLotGroupDictDO = drawLotGroupDictDOS.get(new Random().nextInt(drawLotGroupDictDOS.size()));


        //这个组别所有的编号
        List<Integer> allNum = this.baseMapper.allNum(drawLotGroupDictDO.getId());

        //这个组已经随机的编号
        List<Integer> exists = this.baseMapper.existNum(drawLotGroupDictDO.getId());

        List<Integer> unselectedElements = findUnselectedElements(allNum, exists);

        judge.setGroupNum(unselectedElements.get(new Random().nextInt(unselectedElements.size())));
        judge.setGroupId(drawLotGroupDictDO.getId());
        this.baseMapper.updateById(judge);

        drawlotsVO.setGroupName(drawLotGroupDictDO.getGroupName());
        drawlotsVO.setGroupNum(judge.getGroupNum());
        drawlotsVO.setExpectorType(judge.getExpectorType());
        drawlotsVO.setRoleType(judge.getRoleType());

        return drawlotsVO;
    }

    @Override
    public List<JudgeScreenVO> judgeScreen(DrawLotsGroupJudgesDO drawLotsGroupJudgesDO) {

        List<JudgeScreenVO> judgeScreenVOS = new ArrayList<>();
        // 查每个组别
        List<DrawLotGroupDictDO> drawLotGroupDictDOS = drawLotGroupDictMapper.selectList(new QueryWrapper<DrawLotGroupDictDO>()
                .lambda()
                .eq(DrawLotGroupDictDO::getSceneId, drawLotsGroupJudgesDO.getSceneId())
                .orderByAsc(DrawLotGroupDictDO::getId));

        for (DrawLotGroupDictDO dlgd : drawLotGroupDictDOS) {
            JudgeScreenVO judgeScreenVO = new JudgeScreenVO();
            judgeScreenVO.setId(dlgd.getId());
            judgeScreenVO.setGroupName(dlgd.getGroupName());
            //组别下的评委
            List<DrawJudgeInfoVO> drawJudgeInfoVOS = this.baseMapper.drawJudgeInfo(dlgd.getId());
            judgeScreenVO.setGroupProjectInfoVOS(drawJudgeInfoVOS);
            judgeScreenVOS.add(judgeScreenVO);
        }
        return judgeScreenVOS;
    }

    @Override
    public LoginVO login(DrawLoginDTO drawLoginDTO) {
        return this.baseMapper.login(drawLoginDTO.getName());
    }

    @Override
    public List<JudgeScoreScreenVO> judgeScoreScreen(JudgeScoreScreenDTO judgeScoreScreenDTO) {

        List<JudgeScoreScreenVO> result = new ArrayList<>();
        //项目编号
        List<Integer> integers = judgeScoreScreenDTO.getGroupNums();

        DrawLotGroupDictDO drawLotGroupDictDO = drawLotGroupDictMapper.selectById(judgeScoreScreenDTO.getGroupId());

        for (Integer integer : integers) {
            JudgeScoreScreenVO judgeScoreScreenVO = new JudgeScoreScreenVO();
            judgeScoreScreenVO.setGroupName(drawLotGroupDictDO.getGroupName());
            judgeScoreScreenVO.setGroupNum(integer);
            List<ScoreScreenVO> scoreScreenVOS = this.baseMapper.judgeScoreScreen(judgeScoreScreenDTO.getGroupId(), integer);
            judgeScoreScreenVO.setScoreScreenVOS(scoreScreenVOS);

            DrawLotsGroupItemsDO drawLotsGroupItemsDO = drawLotsGroupItemsMapper.selectOne(new QueryWrapper<DrawLotsGroupItemsDO>()
                    .lambda()
                    .eq(DrawLotsGroupItemsDO::getGroupId, judgeScoreScreenDTO.getGroupId())
                    .eq(DrawLotsGroupItemsDO::getGroupNum, integer));
            judgeScoreScreenVO.setTotalScore(drawLotsGroupItemsDO.getTotalScore());
            result.add(judgeScoreScreenVO);
        }

        return result;
    }

    @Override
    public LeaderFullVO leaderFull(DrawLotGroupDictDO drawLotGroupDictDO) {

        LeaderFullVO leaderFullVO = new LeaderFullVO();
        //判断组长有没有抽齐
        int cnt = this.baseMapper.selectCount(new QueryWrapper<DrawLotsGroupJudgesDO>()
                .lambda()
                .eq(DrawLotsGroupJudgesDO::getSceneId, drawLotGroupDictDO.getSceneId())
                .eq(DrawLotsGroupJudgesDO::getRoleType, RoleType.TEAMLEADER)
                .isNull(DrawLotsGroupJudgesDO::getGroupId));

        if (cnt > 0) {
            leaderFullVO.setFullFlag(false);
        }else {
            leaderFullVO.setFullFlag(true);
        }
        return leaderFullVO;
    }

    @Override
    public JudgeFullInfoVO judgeInfo(DrawLotsJudgeNumDO drawLotsJudgeNumDO) {
        return this.baseMapper.judgeInfo(drawLotsJudgeNumDO.getGroupId(),drawLotsJudgeNumDO.getGroupNum());
    }

}