PersonInfoServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package com.ruoyi.web.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.json.JSONUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.ruoyi.common.enums.BusinessType;
  11. import com.ruoyi.common.exception.ServiceException;
  12. import com.ruoyi.common.utils.SecureSensitiveUtils;
  13. import com.ruoyi.common.utils.bean.BeanUtils;
  14. import com.ruoyi.web.domain.dto.person.PersonInfoAddRequest;
  15. import com.ruoyi.web.domain.dto.person.PersonInfoEditRequest;
  16. import com.ruoyi.web.domain.dto.person.PersonInfoListQueryRequest;
  17. import com.ruoyi.web.domain.dto.person.PersonInfoQueryRequest;
  18. import com.ruoyi.web.domain.entity.HouseInfo;
  19. import com.ruoyi.web.domain.entity.HouseholdInfo;
  20. import com.ruoyi.web.domain.entity.PersonInfo;
  21. import com.ruoyi.web.domain.vo.PersonInfoVO;
  22. import com.ruoyi.web.mapper.HouseInfoMapper;
  23. import com.ruoyi.web.mapper.PersonInfoMapper;
  24. import com.ruoyi.web.service.HouseholdInfoService;
  25. import com.ruoyi.web.service.PersonInfoService;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import java.util.Arrays;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.Objects;
  32. import java.util.stream.Collectors;
  33. /**
  34. * 人员信息服务
  35. */
  36. @Service
  37. public class PersonInfoServiceImpl extends ServiceImpl<PersonInfoMapper, PersonInfo>
  38. implements PersonInfoService {
  39. @Autowired
  40. private HouseInfoMapper houseInfoMapper;
  41. @Autowired
  42. private HouseholdInfoService householdInfoService;
  43. @Override
  44. public Integer addPersonInfo(PersonInfoAddRequest personInfoAddRequest) {
  45. if (personInfoAddRequest == null) {
  46. throw new ServiceException("请求参数为空");
  47. }
  48. try {
  49. PersonInfo personInfo = new PersonInfo();
  50. BeanUtil.copyProperties(personInfoAddRequest, personInfo);
  51. // 将人口标签 List 转为 String
  52. personInfo.setPopulationTags(JSONUtil.toJsonStr(personInfoAddRequest.getPopulationTags()));
  53. // 数据校验 todo
  54. validPersonInfo(personInfo, BusinessType.INSERT);
  55. // // 身份证加密
  56. // String idCard = personInfo.getIdCard();
  57. // personInfo.setIdCard(this.encryptIdCard(idCard));
  58. // 保存到数据库
  59. this.save(personInfo);
  60. return personInfo.getId();
  61. } catch (Exception e) {
  62. log.error("添加人员失败", e);
  63. throw new ServiceException("添加人员失败");
  64. }
  65. }
  66. @Override
  67. public boolean deletePersonInfo(String ids) {
  68. if (StrUtil.isBlank(ids)) {
  69. throw new ServiceException("id不能为空或id异常");
  70. }
  71. // 2. 分割ID字符串为List<Integer>
  72. List<Long> idList = Arrays.stream(ids.split(","))
  73. .map(String::trim)
  74. .filter(StrUtil::isNotBlank)
  75. .map(Long::parseLong)
  76. .collect(Collectors.toList());
  77. if (idList.isEmpty()) {
  78. throw new ServiceException("ID格式异常");
  79. }
  80. // 3. 构建删除条件
  81. QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
  82. queryWrapper.in("id", idList);
  83. // 4. 执行删除(返回是否删除成功)
  84. return remove(queryWrapper);
  85. }
  86. @Override
  87. public void editPersonInfo(PersonInfoEditRequest personInfoEditRequest) {
  88. // 判断是否存在
  89. Integer id = personInfoEditRequest.getId();
  90. PersonInfo oldPersonInfo = this.getById(id);
  91. if (oldPersonInfo == null) {
  92. throw new ServiceException("没有找到人员");
  93. }
  94. PersonInfo personInfo = new PersonInfo();
  95. BeanUtil.copyProperties(personInfoEditRequest, personInfo);
  96. // 将人口标签 List 转为 String
  97. personInfo.setPopulationTags(JSONUtil.toJsonStr(personInfoEditRequest.getPopulationTags()));
  98. personInfo.setUpdateTime(new Date());
  99. // 数据校验
  100. validPersonInfo(personInfo, BusinessType.UPDATE);
  101. // 操作数据库
  102. boolean result = this.updateById(personInfo);
  103. if (!result) {
  104. throw new ServiceException("修改人员操作失败");
  105. }
  106. }
  107. /**
  108. * 校验人员
  109. *
  110. * @param personInfo
  111. */
  112. public void validPersonInfo(PersonInfo personInfo, BusinessType type) {
  113. if (ObjectUtil.isEmpty(personInfo)) {
  114. throw new ServiceException("数据为空");
  115. }
  116. Integer id = personInfo.getId();
  117. String realname = personInfo.getRealname();
  118. String idCard = personInfo.getIdCard();
  119. String ethnic = personInfo.getEthnic();
  120. Integer age = personInfo.getAge();
  121. Integer gender = personInfo.getGender();
  122. Date birthDate = personInfo.getBirthDate();
  123. Integer populationCategory = personInfo.getPopulationCategory();
  124. String phone = personInfo.getPhone();
  125. String populationTags = personInfo.getPopulationTags();
  126. String occupation = personInfo.getOccupation();
  127. String grid = personInfo.getGrid();
  128. String educationLevel = personInfo.getEducationLevel();
  129. Integer maritalStatus = personInfo.getMaritalStatus();
  130. String politicalStatus = personInfo.getPoliticalStatus();
  131. String religion = personInfo.getReligion();
  132. String photoUrl = personInfo.getPhotoUrl();
  133. Integer householdId = personInfo.getHouseholdId();
  134. String householdRelation = personInfo.getHouseholdRelation();
  135. String specialIdentity = personInfo.getSpecialIdentity();
  136. Date createTime = personInfo.getCreateTime();
  137. String createBy = personInfo.getCreateBy();
  138. Date updateTime = personInfo.getUpdateTime();
  139. String updateBy = personInfo.getUpdateBy();
  140. String delFlag = personInfo.getDelFlag();
  141. // 修改数据时,id 不能为空 todo
  142. if (type != BusinessType.INSERT && ObjectUtil.isEmpty(id)) {
  143. throw new ServiceException("id不能为空");
  144. }
  145. if (StrUtil.isBlank(realname)) {
  146. throw new ServiceException("姓名不能为空");
  147. }
  148. }
  149. /**
  150. * 根据id获取人员包装类
  151. *
  152. * @param id
  153. * @return
  154. */
  155. @Override
  156. public PersonInfoVO getPersonInfoById(int id) {
  157. if (id <= 0 || ObjectUtil.isEmpty(id)) {
  158. throw new ServiceException("id不能为空或id异常");
  159. }
  160. PersonInfo personInfo = this.getById(id);
  161. return getPersonInfoVO(personInfo);
  162. }
  163. /**
  164. * 根据 系统用户id 获取人员包装类列表
  165. *
  166. * @param userIds
  167. * @return
  168. */
  169. @Override
  170. public List<PersonInfoVO> getPersonInfoVOListByUserIds(List<Integer> userIds) {
  171. if (ObjectUtil.isEmpty(userIds)) {
  172. throw new ServiceException("系统用户id不能为空或id异常");
  173. }
  174. // 1. 批量查询 PersonInfo
  175. QueryWrapper<PersonInfo> wrapper = new QueryWrapper<>();
  176. wrapper.in("user_id", userIds);
  177. List<PersonInfo> personInfoList = this.list(wrapper);
  178. // 2. 转换为 VO 列表
  179. return personInfoList.stream()
  180. .map(this::getPersonInfoVO)
  181. .collect(Collectors.toList());
  182. }
  183. /**
  184. * 分页获取人员列表
  185. *
  186. * @param personInfoQueryRequest
  187. * @return
  188. */
  189. @Override
  190. public Page<PersonInfoVO> getListPersonInfoByPage(PersonInfoQueryRequest personInfoQueryRequest) {
  191. long current = personInfoQueryRequest.getPageNum();
  192. long size = personInfoQueryRequest.getPageSize();
  193. Page<PersonInfo> personInfoPage = this.page(new Page<>(current, size),
  194. getQueryWrapper(personInfoQueryRequest));
  195. // 2. 补充关联数据
  196. Page<PersonInfoVO> resultPage = new Page<>();
  197. BeanUtils.copyProperties(personInfoPage, resultPage, "records");
  198. // 3. 处理每条记录
  199. List<PersonInfoVO> viewList = personInfoPage.getRecords().stream().map(personInfo -> {
  200. return getPersonInfoVO(personInfo);
  201. }).collect(Collectors.toList());
  202. resultPage.setRecords(viewList);
  203. return resultPage;
  204. }
  205. /**
  206. * 获取人员列表
  207. *
  208. * @param personInfoListQueryRequest
  209. * @return
  210. */
  211. @Override
  212. public List<PersonInfoVO> getPersonInfoVOList(PersonInfoListQueryRequest personInfoListQueryRequest) {
  213. QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
  214. String realname = personInfoListQueryRequest.getRealname();
  215. String idCard = personInfoListQueryRequest.getIdCard();
  216. String phone = personInfoListQueryRequest.getPhone();
  217. // 姓名、身份证、电话 模糊查询
  218. queryWrapper.like(StrUtil.isNotBlank(realname), "realname", realname);
  219. queryWrapper.like(StrUtil.isNotBlank(idCard), "idCard", idCard);
  220. queryWrapper.like(StrUtil.isNotBlank(phone), "phone", phone);
  221. List<PersonInfo> list = this.list(queryWrapper);
  222. // 3. 处理每条记录
  223. return list.stream().map(this::getPersonInfoVO).collect(Collectors.toList());
  224. }
  225. /**
  226. * 获取人员包装类
  227. *
  228. * @param personInfo
  229. * @return
  230. */
  231. @Override
  232. public PersonInfoVO getPersonInfoVO(PersonInfo personInfo) {
  233. if (ObjectUtil.isEmpty(personInfo)) {
  234. throw new ServiceException("请求参数不存在");
  235. }
  236. PersonInfoVO personInfoVO = new PersonInfoVO();
  237. BeanUtils.copyProperties(personInfo, personInfoVO);
  238. // // 获取身份证号 解密
  239. // String idCard = personInfoVO.getIdCard();
  240. // idCard = this.decryptIdCard(idCard);
  241. // personInfoVO.setIdCard(idCard);
  242. // 联表查询补充数据
  243. if (personInfo.getHouseholdId() != null) {
  244. // 查询户籍信息
  245. HouseholdInfo household = householdInfoService.getById(personInfo.getHouseholdId());
  246. if (household != null) {
  247. //户主姓名
  248. int householdHeadId = household.getHouseholdHeadId();
  249. PersonInfo householdHead = this.getById(householdHeadId);
  250. //String householdHead = household.getHouseholdHead();
  251. if (householdHead != null) {
  252. // 户主姓名
  253. personInfoVO.setHouseHoldHead(householdHead.getRealname());
  254. }
  255. // 查询房屋信息
  256. if (StrUtil.isNotBlank(household.getHouseCode())) {
  257. HouseInfo house = houseInfoMapper.selectOne(
  258. new QueryWrapper<HouseInfo>()
  259. .eq("house_code", household.getHouseCode())
  260. .last("LIMIT 1"));
  261. if (house != null) {
  262. // 房屋编号
  263. personInfoVO.setHouseCode(house.getHouseCode());
  264. // 门牌号
  265. personInfoVO.setDoorplateNumber(house.getDoorplateNumber());
  266. // 房主姓名
  267. Integer houseOwnerId = house.getHouseOwnerId();
  268. PersonInfo houseOwner;
  269. String houseOwnerName = null;
  270. if (houseOwnerId != null) {
  271. houseOwner = this.getById(houseOwnerId);
  272. houseOwnerName = houseOwner.getRealname();
  273. }
  274. personInfoVO.setHouseOwnerName(houseOwnerName);
  275. }
  276. }
  277. }
  278. }
  279. return personInfoVO;
  280. }
  281. /**
  282. * 获取查询条件
  283. *
  284. * @param personInfoQueryRequest
  285. * @return
  286. */
  287. @Override
  288. public QueryWrapper<PersonInfo> getQueryWrapper(PersonInfoQueryRequest personInfoQueryRequest) {
  289. QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
  290. if (personInfoQueryRequest == null) {
  291. return queryWrapper;
  292. }
  293. // 从对象中取值
  294. Integer id = personInfoQueryRequest.getId();
  295. String realname = personInfoQueryRequest.getRealname();
  296. String ethnic = personInfoQueryRequest.getEthnic();
  297. String doorplateNumber = personInfoQueryRequest.getDoorplateNumber();
  298. String specialIdentity = personInfoQueryRequest.getSpecialIdentity();
  299. List<String> populationTags = personInfoQueryRequest.getPopulationTags();
  300. String sortField = personInfoQueryRequest.getSortField();
  301. String sortOrder = personInfoQueryRequest.getSortOrder();
  302. // 姓名、民族、房屋门牌号、特殊身份、人口标签
  303. queryWrapper.eq(ObjectUtil.isNotEmpty(id), "id", id);
  304. queryWrapper.like(StrUtil.isNotBlank(realname), "realname", realname);
  305. queryWrapper.eq(StrUtil.isNotBlank(ethnic), "ethnic", ethnic);
  306. queryWrapper.eq(StrUtil.isNotBlank(specialIdentity), "special_identity", specialIdentity);
  307. // 门牌号查询人员列表
  308. if (StrUtil.isNotBlank(doorplateNumber)) {
  309. String subQuery = "SELECT hd.id FROM household_info hd " +
  310. "JOIN house_info hi ON hd.house_code = hi.house_code " +
  311. "WHERE hi.doorplate_number = '" + doorplateNumber + "'";
  312. queryWrapper.inSql("household_id", subQuery);
  313. }
  314. // 人口标签 JSON 数组查询
  315. if (CollUtil.isNotEmpty(populationTags)) {
  316. for (String tag : populationTags) {
  317. queryWrapper.like("population_tags", "\"" + tag + "\"");
  318. }
  319. }
  320. // 排序
  321. queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), "ascend".equals(sortOrder), sortField);
  322. return queryWrapper;
  323. }
  324. /**
  325. * 解密身份证
  326. * @param idCard
  327. * @return
  328. */
  329. @Override
  330. public String decryptIdCard(String idCard) {
  331. try {
  332. idCard = SecureSensitiveUtils.decrypt(idCard, SecureSensitiveUtils.generateAesKey());
  333. } catch (Exception e) {
  334. throw new RuntimeException(e);
  335. }
  336. return idCard;
  337. }
  338. /**
  339. * 加密身份证
  340. * @param idCard
  341. * @return
  342. */
  343. @Override
  344. public String encryptIdCard(String idCard) {
  345. try {
  346. idCard = SecureSensitiveUtils.encrypt(idCard, SecureSensitiveUtils.generateAesKey());
  347. } catch (Exception e) {
  348. throw new RuntimeException(e);
  349. }
  350. return idCard;
  351. }
  352. /**
  353. * 根据人口id列表 获取 系统用户 id列表
  354. *
  355. * @param personIds
  356. * @return
  357. */
  358. @Override
  359. public List<Integer> getUserIdListByPersonIds(List<Integer> personIds) {
  360. if (ObjectUtil.isEmpty(personIds)) {
  361. throw new ServiceException("参数为空");
  362. }
  363. // 2. 查询数据库获取user_id列表
  364. QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
  365. queryWrapper.select("user_id")
  366. .in("id", personIds)
  367. .isNotNull("user_id");
  368. List<PersonInfo> personInfoList = this.list(queryWrapper);
  369. // 3. 提取user_id并去重
  370. return personInfoList.stream()
  371. .map(PersonInfo::getUserId)
  372. .filter(Objects::nonNull) // 过滤掉null值
  373. .distinct() // 去重
  374. .collect(Collectors.toList());
  375. }
  376. }