package com.platform.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.map.MapUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.platform.mapper.UserMapper; import com.platform.model.dto.UserDTO; import com.platform.model.entity.UserAccountEntity; import com.platform.model.entity.UserEntity; import com.platform.model.entity.UserRelationEntity; import com.platform.model.vo.UserVO; import com.platform.service.UserAccountService; import com.platform.service.UserRelationService; import com.platform.service.UserService; import com.platform.utils.DozerUtils; import com.platform.utils.IdCardUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; import javax.annotation.Resource; import java.util.List; import java.util.Map; @Service public class UserServiceImpl extends ServiceImpl implements UserService { @Resource private UserAccountService userAccountService; @Resource private UserRelationService userRelationService; @Override public UserVO getDetail(String userName) { UserEntity userEntity = baseMapper.selectOne(new LambdaQueryWrapper() .eq(UserEntity::getUserName, userName)); UserVO userVO = DozerUtils.convertBean(userEntity, UserVO.class); if (userVO != null) { //查看用户账户列表 List userAccountEntityList = userAccountService.queryListByUserName(userName); userVO.setUserAccountList(userAccountEntityList); } List relationShipList = userRelationService.list(new LambdaQueryWrapper().eq(UserRelationEntity::getUserId, userEntity.getId())); if(!ObjectUtils.isEmpty(relationShipList)){ userVO.setRelationShipList(relationShipList); } return userVO; } @Override public boolean updateUser(UserDTO userDTO) { List relationShipList = userDTO.getRelationShipList(); if(!ObjectUtils.isEmpty(relationShipList)){ userRelationService.saveOrUpdateBatch(relationShipList); } updateById(userDTO); //先删除账户信息 userAccountService.deleteByUserName(userDTO.getUserName()); //保存新的账户信息 List userAccountEntityList = userDTO.getUserAccountList(); if (CollUtil.isNotEmpty(userAccountEntityList)) { userAccountEntityList.forEach(userAccountEntity -> userAccountEntity.setUserName(userDTO.getUserName())); userAccountService.updateBatchById(userAccountEntityList); } return Boolean.TRUE; } @Override @Transactional public Boolean add(UserDTO userDTO) { //解析身份证号 Map idCardInfo = IdCardUtils.parseIdCardInfo(userDTO.getIdCard()); if(MapUtil.isNotEmpty(idCardInfo)){ userDTO.setAge(Integer.parseInt(idCardInfo.get("age"))); userDTO.setSex(idCardInfo.get("sex")); userDTO.setIdType("身份证"); } save(userDTO); List relationShipList = userDTO.getRelationShipList(); for (UserRelationEntity userRelationEntity : relationShipList) { userRelationEntity.setOtherId(userDTO.getId()); } userRelationService.saveBatch(relationShipList); for (UserRelationEntity userRelationEntity : relationShipList) { userRelationEntity.setOtherId(userRelationEntity.getUserId()); userRelationEntity.setUserId(userDTO.getId()); userRelationEntity.setId(null); } userRelationService.saveBatch(relationShipList); return Boolean.TRUE; } }