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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.yiboshi.science.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiboshi.arch.exception.BusinessException;
import com.yiboshi.science.base.BaseServiceImpl;
import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.dao.ComProjectAssignDAO;
import com.yiboshi.science.entity.AssignCount;
import com.yiboshi.science.entity.ComProject;
import com.yiboshi.science.entity.ComProjectAssign;
import com.yiboshi.science.entity.SystemParameter;
import com.yiboshi.science.enumeration.CommonEnum;
import com.yiboshi.science.param.dto.ComExpertSpecDTO;
import com.yiboshi.science.param.dto.ComProjectAssignDTO;
import com.yiboshi.science.param.dto.ComProjectGroupDTO;
import com.yiboshi.science.param.query.ComProjectAssignQueryVO;
import com.yiboshi.science.service.*;
import lombok.AllArgsConstructor;
import net.bytebuddy.asm.Advice;
import org.mockito.internal.matchers.Find;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@Service
@AllArgsConstructor
public class ComProjectAssignServiceImpl extends BaseServiceImpl<ComProjectAssignDAO, ComProjectAssignQueryVO, ComProjectAssignDTO, ComProjectAssign> implements ComProjectAssignService {
@Autowired
private ComProjectAssignDAO comProjectAssignDAO;
@Autowired
private ComBatchService comBatchService;
@Autowired
private ComExpertSpecService comExpertSpecService;
@Autowired
private ComProjectService comProjectService;
@Autowired
private SystemParameterService systemParameterService;
@Override
protected void setCriteriaForQuery(ComProjectAssignQueryVO vo, QueryWrapper<ComProjectAssignQueryVO> criteria) {
if (Objects.nonNull(vo.getExpertId())) {
criteria.eq("a.expert_id", vo.getExpertId());
}
if (Objects.nonNull(vo.getProjId())) {
criteria.eq("a.proj_id", vo.getProjId());
}
if (Objects.nonNull(vo.getGroupId())) {
criteria.eq("k.id", vo.getGroupId());
}
if (Objects.nonNull(vo.getAssignYear())) {
criteria.eq("assign_year", vo.getAssignYear());
}
if (Objects.nonNull(vo.getTotalScore())) {
criteria.eq("total_score", vo.getTotalScore());
}
if (Objects.nonNull(vo.getAuditState())) {
criteria.eq("a.audit_state", vo.getAuditState());
}
if (Objects.nonNull(vo.getCertId())) {
criteria.like("c.cert_id", vo.getCertId());
}
if (Objects.nonNull(vo.getPersonName())) {
criteria.like("c.person_name", vo.getPersonName());
}
if (Objects.nonNull(vo.getSex())) {
criteria.eq("c.sex", vo.getSex());
}
if (Objects.nonNull(vo.getProjName())) {
criteria.like("f.proj_name", vo.getProjName());
}
if (Objects.nonNull(vo.getProjType())) {
criteria.eq("f.proj_type", vo.getProjType());
}
if (Objects.nonNull(vo.getProjNo())) {
criteria.like("f.proj_no", vo.getProjNo());
}
if (Objects.nonNull(vo.getAppUnitName())) {
criteria.like("i.unit_name", vo.getAppUnitName());
}
}
@Override
public Pagination<ComProjectAssignDTO> getListByPage(ComProjectAssignQueryVO vo) {
QueryWrapper criteria = new QueryWrapper();
setCriteriaForQuery(vo, criteria);
Page<ComProjectAssignQueryVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
List<ComProjectAssignDTO> dtoList = comProjectAssignDAO.getListByPage(page, criteria).getRecords();
dtoList.forEach(e -> {
e.setSpecList(comExpertSpecService.getListByExpertId(e.getExpertId()));
});
return new Pagination<>(dtoList, page.getTotal(), vo.getPageSize());
}
@Transactional
public String assignProject(Map<String, Object> map) {
List<String> projects = null;
List<String> experts = null;
if (!map.containsKey("type"))
throw new BusinessException("参数错误");
if (!map.containsKey("projects"))
throw new BusinessException("参数错误");
if (!map.containsKey("projType"))
throw new BusinessException("参数错误");
Integer projType = CommonEnum.projType.num.getCode();
if (map.containsKey("projType")) {
projType = (int) map.get("projType");
}
experts = (List<String>) map.get("experts");
projects = (List<String>) map.get("projects");
Integer reportYear = comBatchService.getReportYear(projType, null);
if (null == projects || projects.size() == 0 || null == experts || experts.size() == 0)
throw new BusinessException("参数错误");
return this.assignProject(reportYear, projects, experts);
}
public String assignProject(Integer year, List<String> projects, List<String> experts) {
AtomicInteger allCount = new AtomicInteger(0);
projects.forEach(e -> {
AtomicInteger assignCount = new AtomicInteger(0);
experts.forEach(f -> {
if (!isDistribute(e, f)) {
ComProjectAssign assign = new ComProjectAssign();
assign.setProjId(e);
assign.setExpertId(f);
assign.setExpertType(comExpertSpecService.getExpertTypeByExpertId(f));
assign.setAssignYear(year);
this.insert(assign);
assignCount.incrementAndGet();
allCount.incrementAndGet();
}
});
if (assignCount.get() > 0)
updateAssignState(e);
});
if (allCount.get() == 0)
throw new BusinessException("项目分配失败或专家已分配,请检查!");
return allCount.getAndIncrement() + "";
}
public String deleteAssignExpert(String id) {
ComProjectAssign comProjectAssign = this.getById(id);
if (null == comProjectAssign)
throw new BusinessException("分配记录已删除!");
if (Objects.nonNull(comProjectAssign.getAuditState()) || !comProjectAssign.getAuditState().equals(0))
throw new BusinessException("专家已评审,不允许删除!");
this.deleteById(id);
this.updateAssignState(comProjectAssign.getProjId());
return id;
}
public boolean isAssignByExpertId(String expertId) {
ComProjectAssign model = new ComProjectAssign();
model.setExpertId(expertId);
List<ComProjectAssign> list = this.entityList(model);
if (null != list && list.size() > 0) {
return true;
} else
return false;
}
@Transactional
public String expertEvaluation(ComProjectAssignDTO dto) {
ComProjectAssign comProjectAssign = this.getById(dto.getId());
if (null == comProjectAssign)
throw new BusinessException("分配记录不存在或已删除!");
ComProjectAssign entity = convert2Entity(dto);
this.update(entity);
if (Objects.nonNull(dto.getAuditState()) && dto.getAuditState().equals(2)) {
this.updateAssignState(entity.getProjId());
}
return entity.getId();
}
public ComProjectAssignDTO getAssignExpertById(String id) {
ComProjectAssignDTO dto = comProjectAssignDAO.getAssignExpertById(id);
if (null != dto) {
dto.setSpecList(comExpertSpecService.getListByExpertId(dto.getExpertId()));
}
return dto;
}
public List<AssignCount> getAssignCount(String expertId) {
return comProjectAssignDAO.getAssignCount(expertId);
}
public List<ComProjectAssignDTO> getAssignExpertList(String projectId) {
return comProjectAssignDAO.getAssignExpertList(projectId);
}
private boolean isDistribute(String projectId, String expertId) {
ComProjectAssign comProjectAssign = new ComProjectAssign();
comProjectAssign.setProjId(projectId);
comProjectAssign.setExpertId(expertId);
comProjectAssign = this.getEntity(comProjectAssign);
if (comProjectAssign != null)
return true;
else
return false;
}
public void updateAssignState(String projectId) {
Integer assignState;
Integer completed;
BigDecimal totalScore = new BigDecimal(0);
BigDecimal calculateScore = new BigDecimal(0);
BigDecimal technologyScore = new BigDecimal(0);
BigDecimal technologyAverageScore = new BigDecimal(0);
BigDecimal economyScore = new BigDecimal(0);
BigDecimal economyAverageScore = new BigDecimal(0);
List<ComProjectAssignDTO> list = this.getAssignExpertList(projectId);
if (null == list || list.size() == 0) {
completed = 0;
assignState = 0;
} else {
int technologyCount = 0;
int economyCount = 0;
int personCount = 0;
for (ComProjectAssignDTO obj : list) {
if (Objects.nonNull(obj.getAuditState()) && obj.getAuditState().equals(2)) {
personCount++;
if (Objects.nonNull(obj.getTotalScore())) {
if (obj.getExpertType().equals(1)) {
technologyCount++;
technologyScore = technologyScore.add(obj.getTotalScore());
} else {
economyCount++;
economyScore = economyScore.add(obj.getTotalScore());
}
}
}
}
DecimalFormat df1 = new DecimalFormat("0.00");
if (technologyCount > 0) {
technologyAverageScore = technologyScore.divide(new BigDecimal(technologyCount), 2, BigDecimal.ROUND_HALF_UP);
technologyScore = new BigDecimal(df1.format(technologyScore));
}
if (economyCount > 0) {
economyAverageScore = economyScore.divide(new BigDecimal(economyCount), 2, BigDecimal.ROUND_HALF_UP);
economyScore = new BigDecimal(df1.format(economyScore));
}
totalScore = technologyScore.add(economyScore);
calculateScore = technologyAverageScore.multiply(new BigDecimal(1)).add(economyAverageScore.multiply(new BigDecimal(1)));
calculateScore = new BigDecimal(df1.format(calculateScore));
if (list.size() > personCount)
completed = 0;
else
completed = 1;
assignState = 1;
}
this.updateAssignState(projectId, assignState, completed, totalScore, technologyScore, technologyAverageScore, economyScore, economyAverageScore, calculateScore);
}
public void updateAssignState(String projectId, Integer assignState, Integer completed, BigDecimal totalScore, BigDecimal technologyScore, BigDecimal technologyAverageScore, BigDecimal economyScore, BigDecimal economyAverageScore, BigDecimal calculateScore) {
ComProject comProject = new ComProject();
comProject.setId(projectId);
comProject.setAssignState(assignState);
comProject.setCompleted(completed);
comProject.setTotalScore(totalScore);
comProject.setTechnologyScore(technologyScore);
comProject.setTechnologyAverageScore(technologyAverageScore);
comProject.setEconomyScore(economyScore);
comProject.setEconomyAverageScore(economyAverageScore);
comProject.setCalculateScore(calculateScore);
comProject.setUpdated(new Date());
comProjectService.update(comProject);
}
public List<ComProjectGroupDTO> getProjectGroupAssignById(String id, String expertId) {
return comProjectAssignDAO.getProjectGroupAssignById(id, expertId);
}
}