package com.xiaopeng.account.service.impl;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.xiaopeng.account.mapper.XpOperationsRoleMapper;
|
import com.xiaopeng.account.mapper.XpOperationsUserMapper;
|
import com.xiaopeng.account.model.dto.XpOperationsUserDTO;
|
import com.xiaopeng.account.model.query.XpOperationsUserPageQuery;
|
import com.xiaopeng.account.service.IXpOperationsUserService;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.xiaopeng.api.feignClient.maintenance.XpClockInClient;
|
import com.xiaopeng.api.feignClient.vehicleConvoy.XpFleetClient;
|
import com.xiaopeng.api.feignClient.vehicleConvoy.XpFleetOperationsClient;
|
import com.xiaopeng.api.model.account.XpOperationsUser;
|
import com.xiaopeng.api.model.vehicleConvoy.XpFleet;
|
import com.xiaopeng.api.vo.account.FleetInfoVO;
|
import com.xiaopeng.api.vo.account.XpOperationsUserProfileVO;
|
import com.xiaopeng.api.vo.account.XpOperationsUserWithRoleVO;
|
import com.xiaopeng.common.core.domain.R;
|
import com.xiaopeng.common.core.exception.ServiceException;
|
import com.xiaopeng.common.core.web.page.PageInfo;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 运维人员 服务实现类
|
* </p>
|
*
|
* @author mitao
|
* @since 2026-02-03
|
*/
|
@Slf4j
|
@Service
|
public class XpOperationsUserServiceImpl extends ServiceImpl<XpOperationsUserMapper, XpOperationsUser> implements IXpOperationsUserService {
|
|
@Autowired
|
private XpFleetOperationsClient xpFleetOperationsClient;
|
|
@Autowired
|
private XpFleetClient xpFleetClient;
|
|
@Autowired
|
private XpOperationsRoleMapper xpOperationsRoleMapper;
|
|
@Autowired
|
private XpClockInClient xpClockInClient;
|
|
@Override
|
public List<XpOperationsUser> listByName(String nickName) {
|
if (StringUtils.isBlank(nickName)) {
|
return Collections.emptyList();
|
}
|
return lambdaQuery().like(XpOperationsUser::getNickName, nickName).list();
|
}
|
|
@Override
|
public PageInfo<XpOperationsUserWithRoleVO> selectUserPageByConditions(XpOperationsUserPageQuery query) {
|
Page<XpOperationsUserWithRoleVO> page = baseMapper.selectUserPageByConditions(new Page<>(query.getPageNum(), query.getPageSize()), query);
|
PageInfo<XpOperationsUserWithRoleVO> pageInfo = new PageInfo<>();
|
pageInfo.setRecords(page.getRecords());
|
pageInfo.setTotal(page.getTotal());
|
return pageInfo;
|
}
|
|
@Override
|
public XpOperationsUserProfileVO getUserProfile(Long userId) {
|
// 1. 查询运维人员基础信息
|
XpOperationsUser user = this.getById(userId);
|
if (user == null) {
|
throw new ServiceException("运维人员不存在");
|
}
|
|
XpOperationsUserProfileVO vo = new XpOperationsUserProfileVO();
|
BeanUtils.copyProperties(user, vo);
|
|
// 2. 查询关联的车队ID列表(一个运维人员只能关联一个车队)
|
R<List<Long>> fleetIdsResult = xpFleetOperationsClient.getFleetIdsByUserId(userId);
|
List<Long> fleetIds = fleetIdsResult.getData();
|
|
if (!CollectionUtils.isEmpty(fleetIds)) {
|
// 3. 获取第一个车队ID(因为只能关联一个车队)
|
Long fleetId = fleetIds.get(0);
|
|
// 4. 查询车队信息
|
R<List<XpFleet>> fleetsResult = xpFleetClient.listByIds(Collections.singletonList(fleetId));
|
List<XpFleet> fleets = fleetsResult.getData();
|
|
if (!CollectionUtils.isEmpty(fleets)) {
|
XpFleet fleet = fleets.get(0);
|
|
// 5. 组装车队信息VO
|
FleetInfoVO fleetVO = new FleetInfoVO();
|
fleetVO.setFleetId(fleet.getId());
|
fleetVO.setFleetName(fleet.getFleetName());
|
fleetVO.setProvinceName(fleet.getProvinceName());
|
fleetVO.setCityName(fleet.getCityName());
|
fleetVO.setDistrictName(fleet.getDistrictName());
|
|
// 6. 查询并填充车队队长信息
|
if (fleet.getOperationsUserId() != null) {
|
XpOperationsUser captain = this.getById(fleet.getOperationsUserId());
|
if (captain != null) {
|
fleetVO.setCaptainId(captain.getId());
|
fleetVO.setFleetLeaderName(captain.getNickName());
|
fleetVO.setFleetLeaderAvatar(captain.getAvatar());
|
}
|
}
|
|
vo.setFleet(fleetVO);
|
}
|
}
|
|
// 7. 查询是否上班中
|
try {
|
R<Boolean> workStatusResult = xpClockInClient.checkWorkStatus(userId);
|
if (workStatusResult != null && workStatusResult.getCode() == 200) {
|
vo.setWorkFlag(workStatusResult.getData());
|
} else {
|
// 调用失败,默认 false
|
vo.setWorkFlag(false);
|
log.warn("查询用户上班状态失败, userId={}, result={}", userId, workStatusResult);
|
}
|
} catch (Exception e) {
|
// 异常情况,默认 false
|
vo.setWorkFlag(false);
|
log.error("查询用户上班状态异常, userId={}", userId, e);
|
}
|
|
// 8. 查询角色信息
|
List<XpOperationsUserWithRoleVO> userWithRoles = xpOperationsRoleMapper.selectUsersWithRoleByIds(Collections.singletonList(userId));
|
if (!CollectionUtils.isEmpty(userWithRoles)) {
|
List<XpOperationsUserProfileVO.RoleInfoVO> roleVOs = userWithRoles.stream()
|
.filter(r -> r.getRoleId() != null)
|
.map(r -> {
|
XpOperationsUserProfileVO.RoleInfoVO roleVO = new XpOperationsUserProfileVO.RoleInfoVO();
|
roleVO.setRoleId(r.getRoleId());
|
roleVO.setRoleName(r.getRoleName());
|
return roleVO;
|
})
|
.collect(Collectors.toList());
|
vo.setRoles(roleVOs);
|
}
|
|
return vo;
|
}
|
|
@Override
|
public void saveCertificate(XpOperationsUserDTO dto) {
|
XpOperationsUser xpOperationsUser = BeanUtil.copyProperties(dto, XpOperationsUser.class);
|
updateById(xpOperationsUser);
|
}
|
}
|