DrawLotsGroupItemsServiceImpl.java
6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
}
}