DrawLotsGroupItemsServiceImpl.java 6.82 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.ProjectDAO;
import com.zhongzhi.dto.drawlots.AddProjectToSceneDTO;
import com.zhongzhi.dto.drawlots.AllGroupItemsDTO;
import com.zhongzhi.dto.drawlots.CollegeProjectsDTO;
import com.zhongzhi.dto.drawlots.MatchProjectsDTO;
import com.zhongzhi.model.DrawLotGroupDictDO;
import com.zhongzhi.model.DrawLotsGroupItemsDO;
import com.zhongzhi.model.ProjectDO;
import com.zhongzhi.service.DrawLotsGroupItemsService;
import com.zhongzhi.vo.drawlots.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2025-06-19
 */
@Service
public class DrawLotsGroupItemsServiceImpl extends ServiceImpl<DrawLotsGroupItemsMapper, DrawLotsGroupItemsDO> implements DrawLotsGroupItemsService {

    @Autowired
    private DrawLotGroupDictMapper drawLotGroupDictMapper;

    @Autowired
    private ProjectDAO projectDAO;

    @Override
    public IPage<AllGroupItemsVO> allGroupItems(AllGroupItemsDTO allGroupItemsDTO) {
        Page page = new Page(allGroupItemsDTO.getPageNum(), allGroupItemsDTO.getPageSize());
        return this.baseMapper.allGroupItems(page,allGroupItemsDTO.getSceneId(), allGroupItemsDTO.getProjectName(), allGroupItemsDTO.getCollege());
    }

    @Override
    public String deleteGroupItem(DrawLotsGroupItemsDO drawLotsGroupItemsDO) {
        this.baseMapper.deleteById(drawLotsGroupItemsDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    @Override
    public List<MatchProjectsVO> matchProjects(MatchProjectsDTO matchProjectsDTO) {
        return this.baseMapper.matchProjects(matchProjectsDTO.getMatchId(), matchProjectsDTO.getProjectSchedule());
    }

    @Override
    public IPage<CollegeProjectsVO> collegeProjects(CollegeProjectsDTO collegeProjectsDTO) {
        Page page = new Page(collegeProjectsDTO.getPageNum(), collegeProjectsDTO.getPageSize());
        return this.baseMapper.collegeProjects(page, collegeProjectsDTO.getSceneId(), collegeProjectsDTO.getCollege(), collegeProjectsDTO.getProjectSchedule(), collegeProjectsDTO.getProjectProgress());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DrawlotsVO drawlots(DrawLotsGroupItemsDO drawLotsGroupItemsDO) {

        DrawlotsVO drawlotsVO = new DrawlotsVO();
        //判断是否已经抽过了
        DrawLotsGroupItemsDO dlgid = this.baseMapper.selectById(drawLotsGroupItemsDO.getProjectId());

        ProjectDO projectDO = projectDAO.selectById(dlgid.getProjectId());

        if (null != dlgid.getGroupId()){
            throw new HttpException(10037);
        }

        //开始抽签
        Integer integer = this.baseMapper.sceneCollege(drawLotsGroupItemsDO.getSceneId(),projectDO.getCollege());

        //找到还剩余的坑位--组别
        List<DrawLotGroupDictDO> drawLotGroupDictDOS = this.baseMapper.noFullGroup(drawLotsGroupItemsDO.getSceneId(),projectDO.getCollege(),integer);

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

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

        List<Integer> array = new ArrayList<>();
        for (int i = 1 ; i <= drawLotGroupDictDO.getProjectCnt() ; i++){
            array.add(i);
        }

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

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

        DrawLotGroupDictDO groupDictDO = drawLotGroupDictMapper.selectById(drawLotGroupDictDO.getId());
        drawlotsVO.setGroupName(groupDictDO.getGroupName());
        drawlotsVO.setGroupNum(unselectedElements.get(new Random().nextInt(unselectedElements.size())));
        DrawLotsGroupItemsDO drawLotsGroupItemsDO1 = this.baseMapper.selectById(drawLotsGroupItemsDO.getProjectId());

        drawLotsGroupItemsDO1.setGroupId(drawLotGroupDictDO.getId());
        drawLotsGroupItemsDO1.setGroupNum(drawlotsVO.getGroupNum());
        this.baseMapper.updateById(drawLotsGroupItemsDO1);

        return drawlotsVO;
    }

    @Override
    public List<GroupProjectScreenVO> groupProjectScreen(DrawLotsGroupItemsDO drawLotsGroupItemsDO) {

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

        for (DrawLotGroupDictDO dlgd : drawLotGroupDictDOS) {
            GroupProjectScreenVO groupProjectScreenVO = new GroupProjectScreenVO();
            groupProjectScreenVO.setId(dlgd.getId());
            groupProjectScreenVO.setGroupName(dlgd.getGroupName());
            //组别下的每个项目
            List<GroupProjectInfoVO> groupProjectInfoVOS = this.baseMapper.groupProjectInfo(dlgd.getId());
            groupProjectScreenVO.setGroupProjectInfoVOS(groupProjectInfoVOS);
            groupProjectScreenVOS.add(groupProjectScreenVO);
        }
        return groupProjectScreenVOS;
    }

    @Override
    public String addProjectToScene(AddProjectToSceneDTO addProjectToSceneDTO) {
        List<Long> ids = addProjectToSceneDTO.getProjectIds();

        for (Long id : ids) {
            DrawLotsGroupItemsDO drawLotsGroupItemsDO = new DrawLotsGroupItemsDO();
            drawLotsGroupItemsDO.setProjectId(id);
            drawLotsGroupItemsDO.setSceneId(addProjectToSceneDTO.getSceneId());
            this.baseMapper.insert(drawLotsGroupItemsDO);
        }
        return ConstantUtils.ADD_SUCCESS;
    }


    public static <T> List<T> findUnselectedElements(List<T> array, List<T> selected) {
        // 使用集合来记录已选择的元素,提升查找效率
        Set<T> selectedSet = new HashSet<>(selected);

        // 存储未被选中的元素
        List<T> result = new ArrayList<>();

        // 遍历数组,找出未被选中的元素
        for (T element : array) {
            if (!selectedSet.contains(element)) {
                result.add(element);
            }
        }

        return result;
    }

}