代码优化
This commit is contained in:
parent
e0f7ec883e
commit
949b32b603
|
|
@ -1,11 +1,11 @@
|
||||||
package com.bonus.base.basic.mapper;
|
package com.bonus.base.basic.mapper;
|
||||||
|
|
||||||
import com.bonus.base.basic.domain.TbPeople;
|
import com.bonus.base.basic.domain.TbPeople;
|
||||||
import com.bonus.base.basic.domain.TbPeopleDto;
|
|
||||||
import com.bonus.base.screen.vo.PeoplePositionVo;
|
import com.bonus.base.screen.vo.PeoplePositionVo;
|
||||||
import com.bonus.system.api.domain.SysUser;
|
import com.bonus.system.api.domain.SysUser;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -80,5 +80,12 @@ public interface TbPeopleMapper {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
SysUser getUserById(Long userId);
|
SysUser getUserById(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据身份证号查询人员信息
|
||||||
|
* @param idCards
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<TbPeople> queryByIdCards(@Param("list") ArrayList<String> idCards);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -382,45 +382,51 @@ public class TbPeopleServiceImpl implements TbPeopleService {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
// 使用一个 Set 来记录已经处理过的 idCard,避免重复处理相同的人员数据
|
// 使用一个 Set 来记录已经处理过的 idCard,避免重复处理相同的人员数据
|
||||||
Set<String> processedIdCards = new HashSet<>();
|
Set<String> processedIdCards = new HashSet<>();
|
||||||
|
// 提前收集所有的 idCard,减少数据库查询次数
|
||||||
|
Set<String> allIdCards = new HashSet<>();
|
||||||
for (TbPeopleDto tbPeople : tbPeopleList) {
|
for (TbPeopleDto tbPeople : tbPeopleList) {
|
||||||
// 如果当前记录已经处理过,跳过
|
allIdCards.add(tbPeople.getIdCard());
|
||||||
|
}
|
||||||
|
// 批量查询数据库中所有的 idCard 对应的人员
|
||||||
|
List<TbPeople> existingPeopleList = tbPeopleDao.queryByIdCards(new ArrayList<>(allIdCards));
|
||||||
|
// 创建一个 Map 来缓存已查询的人员信息,以 idCard 为 key
|
||||||
|
Map<String, TbPeople> existingPeopleMap = new HashMap<>();
|
||||||
|
for (TbPeople people : existingPeopleList) {
|
||||||
|
existingPeopleMap.put(Sm4Utils.decode(people.getIdCard()), people);
|
||||||
|
}
|
||||||
|
// 遍历 tbPeopleList 进行处理
|
||||||
|
for (TbPeopleDto tbPeople : tbPeopleList) {
|
||||||
|
// 如果该 idCard 已处理,则跳过
|
||||||
if (processedIdCards.contains(tbPeople.getIdCard())) {
|
if (processedIdCards.contains(tbPeople.getIdCard())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
List<TbPeople> peopleList = tbPeopleDao.queryByName(tbPeople);
|
// 查找是否已有此人员数据
|
||||||
if (CollectionUtils.isNotEmpty(peopleList)) {
|
TbPeople existingPeople = existingPeopleMap.get(tbPeople.getIdCard());
|
||||||
for (TbPeople people : peopleList) {
|
if (existingPeople != null) {
|
||||||
if (StringUtils.isNotBlank(people.getIdCard()) && StringUtils.isNotBlank(tbPeople.getIdCard())) {
|
// 如果找到相应的人员并且 idCard 匹配,执行更新操作
|
||||||
if (Objects.equals(Sm4Utils.decode(people.getIdCard()), tbPeople.getIdCard())) {
|
TbPeople dto = new TbPeople();
|
||||||
//进行更新操作
|
dto.setId(existingPeople.getId());
|
||||||
TbPeople dto = new TbPeople();
|
dto.setUpdateUser(SecurityUtils.getUserId());
|
||||||
dto.setId(people.getId());
|
dto.setDelFlag(0);
|
||||||
dto.setUpdateUser(SecurityUtils.getUserId());
|
dto.setRelPhone(Sm4Utils.encode(tbPeople.getRelPhone()));
|
||||||
dto.setDelFlag(0);
|
dto.setIdCard(Sm4Utils.encode(tbPeople.getIdCard()));
|
||||||
dto.setRelPhone(Sm4Utils.encode(tbPeople.getRelPhone()));
|
dto.setSex(tbPeople.getGender());
|
||||||
dto.setIdCard(Sm4Utils.encode(tbPeople.getIdCard()));
|
dto.setRelName(tbPeople.getRelName());
|
||||||
dto.setSex(tbPeople.getGender());
|
dto.setPostCode(tbPeople.getPostCode());
|
||||||
dto.setRelName(tbPeople.getRelName());
|
result += tbPeopleDao.update(dto);
|
||||||
dto.setPostCode(tbPeople.getPostCode());
|
} else {
|
||||||
result += tbPeopleDao.update(dto);
|
// 否则进行新增操作
|
||||||
// 标记该 idCard 已处理
|
TbPeople dto = new TbPeople();
|
||||||
processedIdCards.add(tbPeople.getIdCard());
|
dto.setCreateUser(SecurityUtils.getUserId());
|
||||||
} else {
|
dto.setRelPhone(Sm4Utils.encode(tbPeople.getRelPhone()));
|
||||||
//新增操作
|
dto.setIdCard(Sm4Utils.encode(tbPeople.getIdCard()));
|
||||||
TbPeople dto = new TbPeople();
|
dto.setSex(tbPeople.getGender());
|
||||||
dto.setCreateUser(SecurityUtils.getUserId());
|
dto.setRelName(tbPeople.getRelName());
|
||||||
dto.setRelPhone(Sm4Utils.encode(tbPeople.getRelPhone()));
|
dto.setPostCode(tbPeople.getPostCode());
|
||||||
dto.setIdCard(Sm4Utils.encode(tbPeople.getIdCard()));
|
result += tbPeopleDao.insert(dto);
|
||||||
dto.setSex(tbPeople.getGender());
|
|
||||||
dto.setRelName(tbPeople.getRelName());
|
|
||||||
dto.setPostCode(tbPeople.getPostCode());
|
|
||||||
result += tbPeopleDao.insert(dto);
|
|
||||||
// 标记该 idCard 已处理
|
|
||||||
processedIdCards.add(tbPeople.getIdCard());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// 标记该 idCard 已处理
|
||||||
|
processedIdCards.add(tbPeople.getIdCard());
|
||||||
}
|
}
|
||||||
if (result > 0) {
|
if (result > 0) {
|
||||||
return AjaxResult.success(ExceptionEnum.SUCCESS.getMsg(), result);
|
return AjaxResult.success(ExceptionEnum.SUCCESS.getMsg(), result);
|
||||||
|
|
|
||||||
|
|
@ -157,5 +157,21 @@
|
||||||
select user_id as userId, user_name as userName, password as password from sys_user where user_id = #{userId}
|
select user_id as userId, user_name as userName, password as password from sys_user where user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="queryByIdCards" resultType="com.bonus.base.basic.domain.TbPeople">
|
||||||
|
select
|
||||||
|
id as id,
|
||||||
|
team_id as teamId,
|
||||||
|
rel_name as relName,
|
||||||
|
rel_phone as relPhone,
|
||||||
|
id_card as idCard,
|
||||||
|
post_code as postCode,
|
||||||
|
sex as sex
|
||||||
|
from tb_people
|
||||||
|
where id_card in
|
||||||
|
<foreach collection="list" item="item" separator="," open="(" close=")">
|
||||||
|
#{item}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue