Commit fc58fc18 by 刘鑫

fix(工作队): 工作队添加修改人员时错误问题, 废弃原有写法 先移除工作队人员关系再保存新的人员关系

1 parent 00ac4a0a
......@@ -7,7 +7,6 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
......@@ -36,4 +35,12 @@ public interface OrgTeamEngineerDao extends JpaRepository<OrgTeamEngineerEntity,
void updateStatusByEngineerCodes(String teamId, List<String> engineerCodes, int status);
List<OrgTeamEngineerEntity> findByEngineerCode(String engineerCode);
/**
* 移除工作队下的所有人员关联关系
*
* @param teamId 工作队
* @return 影响行数
*/
Integer deleteByTeamId(String teamId);
}
......@@ -40,9 +40,7 @@ import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -210,18 +208,31 @@ public class BusinessTeamServiceImpl implements BusinessTeamService {
orgTeamDao.save(entity);
// 技术员列表
if (req.getEngineerCodes() != null && !req.getEngineerCodes().isEmpty()) {
updateTeamEngineers(entity, req.getEngineerCodes());
List<String> engineerCodes = req.getEngineerCodes();
if (engineerCodes != null && !engineerCodes.isEmpty()) {
final String targetTeamId = String.valueOf(entity.getTeamId());
List<String> targetTeamWorkDayOfWeek = List.of(entity.getWorkdays().split(","));
judgeTeam(engineerCodes, targetTeamId, targetTeamWorkDayOfWeek);
//移除原有关系映射
orgTeamEngineerDao.deleteByTeamId(targetTeamId);
//新增新的关系映射
List<OrgTeamEngineerEntity> newRelation = engineerCodes.stream().map(t -> {
OrgTeamEngineerEntity relation = new OrgTeamEngineerEntity();
relation.setTeamId(targetTeamId);
relation.setEngineerCode(t);
relation.setCreateTime(LocalDateTime.now());
relation.setUpdateTime(LocalDateTime.now());
relation.setStatus(1);
return relation;
}).collect(Collectors.toList());
orgTeamEngineerDao.saveAll(newRelation);
}
return Result.success(null);
}
public void updateTeamEngineers(OrgTeamEntity targetTeam, List<String> engineerCodes) {
// 获取现有的工作队和技术员之间的所有关系
final String targetTeamId = String.valueOf(targetTeam.getTeamId());
List<String> targetTeamWorkDayOfWeek = List.of(targetTeam.getWorkdays().split(","));
List<OrgTeamEngineerEntity> existingRelations = orgTeamEngineerDao.findAllByTeamId(targetTeamId);
private void judgeTeam(List<String> engineerCodes, String targetTeamId, List<String> targetTeamWorkDayOfWeek) {
// 查询工程师在其它工作队的工作时间以及当前工作队的工作时间
final String message = "工号为{0}的工程师在{1}工作队的周{2}已有工作日安排";
List<String> warnMessage = new ArrayList<>();
......@@ -247,48 +258,6 @@ public class BusinessTeamServiceImpl implements BusinessTeamService {
if (!CollectionUtils.isEmpty(warnMessage)) {
throw new BusinessException(StringUtils.join(warnMessage, ";"));
}
// 构建新的关系映射
Map<String, OrgTeamEngineerEntity> newRelationsMap = new HashMap<>();
for (String engineerCode : engineerCodes) {
OrgTeamEngineerEntity relation = new OrgTeamEngineerEntity();
relation.setTeamId(targetTeamId);
relation.setEngineerCode(engineerCode);
relation.setCreateTime(LocalDateTime.now());
relation.setUpdateTime(LocalDateTime.now());
relation.setStatus(1);
newRelationsMap.put(engineerCode, relation);
}
// 遍历现有的关系列表,更新关系的最后时间和状态
for (OrgTeamEngineerEntity relation : existingRelations) {
String engineerCode = relation.getEngineerCode();
if (newRelationsMap.containsKey(engineerCode)) {
OrgTeamEngineerEntity newRelation = newRelationsMap.get(engineerCode);
newRelation.setId(relation.getId());
newRelation.setUpdateTime(LocalDateTime.now());
newRelationsMap.put(engineerCode, newRelation);
} else {
// 新的关系不再,设置为0(删除)
relation.setStatus(0);
orgTeamEngineerDao.save(relation);
}
}
// 遍历新的关系映射,创建新的关系
for (OrgTeamEngineerEntity newRelation : newRelationsMap.values()) {
if (!existingRelations.contains(newRelation)) {
// 检查是否存在,已经存在的,只需要set status=1就可以了
OrgTeamEngineerEntity exist = orgTeamEngineerDao.findByTeamIdAndEngineerCode(newRelation.getTeamId(),
newRelation.getEngineerCode());
if (null != exist && exist.getId() != null) {
exist.setStatus(1);
orgTeamEngineerDao.save(exist);
} else {
orgTeamEngineerDao.save(newRelation);
}
}
}
}
@Override
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!