package cn.fw.morax.rpc.ehr; import cn.fw.data.base.domain.common.Message; import cn.fw.ehr.sdk.api.ManagerApi; import cn.fw.ehr.sdk.api.PostApi; import cn.fw.ehr.sdk.api.StaffApi; import cn.fw.ehr.sdk.api.result.*; import cn.fw.morax.common.utils.PublicUtil; import cn.fw.morax.common.utils.StringUtils; import cn.fw.morax.rpc.ehr.dto.*; import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.cache.annotation.Cacheable; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * @author : kurisu * @className : EhrRpcService * @description : 人事 * @date: 2021-01-19 17:34 */ @Slf4j @RequiredArgsConstructor @Service public class EhrRpcService { private final ManagerApi managerApi; private final StaffApi staffApi; private final PostApi postApi; @Cacheable(cacheNames = "rpc_ehr", key = "':rpc:ehr:user:'+ #userId", unless = "#result == null") @Nullable public StaffInfoDTO queryStaffInfo(final Long userId) { if (userId == null) { return null; } try { final Message msg = staffApi.getStaffInfoById(userId); log.info("StaffApi.queryStaffInfo: param:[{}] msg.code={}, msg.result={}, msg.data={}", userId, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { final StaffInfoDTO staffInfoDTO = new StaffInfoDTO(); BeanUtils.copyProperties(msg.getData(), staffInfoDTO); return staffInfoDTO; } } catch (Exception e) { e.printStackTrace(); } return null; } @Nullable public List queryStaffInfos(final List userIds) { if (userIds == null) { return null; } try { final Message> msg = staffApi.getBatchStaffBaseInfoList(userIds); log.info("StaffApi.getBatchStaffBaseInfoList: param:[{}] msg.code={}, msg.result={}, msg.data={}", userIds, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { List staffInfoDTOS = PublicUtil.copyList(msg.getData(), StaffInfoDTO.class); return staffInfoDTOS; } } catch (Exception e) { e.printStackTrace(); } return null; } @Cacheable(cacheNames = "rpc_ehr", key = "':rpc:ehr:user_base:'+ #userId", unless = "#result == null") @Nullable public StaffBaseInfoDTO queryStaffBaseInfo(final Long userId) { if (userId == null) { return null; } try { final Message msg = staffApi.getStaffBaseInfo(userId); log.info("StaffApi.StaffBaseInfo: param:[{}] msg.code={}, msg.result={}, msg.data={}", userId, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTO(msg.getData()); } } catch (Exception e) { e.printStackTrace(); } return null; } @Cacheable(cacheNames = "rpc_ehr", key = "':rpc:ehr:mobile:'+ #mobile", unless = "#result == null") @Nullable public StaffInfoDTO queryStaffInfoByMobile(final String mobile) { if (StringUtils.isEmpty(mobile)) { return null; } try { final Message msg = staffApi.getStaffInfoByMobile(mobile); log.info("StaffApi.queryStaffInfoByMobile: param:[{}] msg.code={}, msg.result={}, msg.data={}", mobile, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { final StaffInfoDTO staffInfoDTO = new StaffInfoDTO(); BeanUtils.copyProperties(msg.getData(), staffInfoDTO); return staffInfoDTO; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询门店岗位下的员工 * * @param shopIds * @param postId * @return */ @Nullable public List queryShopPostStaffs(final List shopIds, final Long postId, Class c) { if (StringUtils.isEmpty(postId) || StringUtils.isEmpty(shopIds)) { return new ArrayList<>(); } try { final Message> msg = staffApi.getStaffListByShopPost(shopIds, postId); log.info("StaffApi.getStaffListByShopPost: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { final List staffBaseInfoVos = PublicUtil.copyList(msg.getData(), c); return staffBaseInfoVos; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询管理范围下的员工 * * @param userId * @return */ @Nullable public List queryManageStaffs(final Long userId) { if (PublicUtil.isEmpty(userId)) { return new ArrayList<>(); } try { final Message msg = managerApi.getRealTimeManagerScope(null, userId, true); log.info("ManagerApi.getRealTimeManagerScope: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTOs(msg.getData().getManageStaffList()); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询管理范围下的员工 * * @param userId * @return */ @Nullable public List queryManageShops(final Long userId) { if (PublicUtil.isEmpty(userId)) { return new ArrayList<>(); } try { final Message msg = managerApi.getRealTimeManagerScope(null, userId, false); log.info("ManagerApi.getRealTimeManagerScope: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertShopIds(msg.getData().getScopeList()); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询门店实时管理者 * * @param shopIds * @return */ @Nullable public List getRealTimeShopManager(final List shopIds) { if (PublicUtil.isEmpty(shopIds)) { return new ArrayList<>(); } final Message> msg = managerApi.getRealTimeShopManager(shopIds, true); try { log.info("ManagerApi.getRealTimeShopManager: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { List managerDTOS = new ArrayList<>(msg.getData().size()); ManagerDTO managerDTO = null; for (ManagerVo managerVo : msg.getData()) { managerDTO = PublicUtil.copy(managerVo, ManagerDTO.class); if (PublicUtil.isNotEmpty(managerVo.getManageStaffList())) { managerDTO.setManageStaffList(PublicUtil.copyList(managerVo.getManageStaffList(), StaffBaseInfoDTO.class)); } List scopeList = new ArrayList<>(); for (ManagerStaffVo staffVo : managerVo.getScopeList()) { ManagerStaffDTO managerStaffDTO = PublicUtil.copy(staffVo, ManagerStaffDTO.class); if (PublicUtil.isNotEmpty(staffVo.getRoleList())) { managerStaffDTO.setRoleList(PublicUtil.copyList(staffVo.getRoleList(), ManagerStaffRoleDTO.class)); } if (PublicUtil.isNotEmpty(staffVo.getShopList())) { managerStaffDTO.setShopList(PublicUtil.copyList(staffVo.getShopList(), ManagerStaffShopDTO.class)); } scopeList.add(managerStaffDTO); } managerDTO.setScopeList(scopeList); managerDTOS.add(managerDTO); } return managerDTOS; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询管理范围下的离职员工 * * @param staffId * @return */ @Nullable public List queryLeaveManageStaffs(final Long staffId, Date startLeaveTime, Date endLeaveTime) { if (PublicUtil.isEmpty(staffId)) { return new ArrayList<>(); } try { final Message msg = managerApi.getRealTimeManagerLeaveScope(null, staffId, startLeaveTime.getTime(), endLeaveTime.getTime()); log.info("ManagerApi.getRealTimeManagerLeaveScope: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTOs(msg.getData().getManageStaffList()); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询门店、岗位下的员工 * * @param postId * @param shopIds * @param staffName * @return */ @Nullable public List queryKpiStaffByName(final Long postId, List shopIds, String staffName) { if (PublicUtil.isEmpty(postId) || PublicUtil.isEmpty(shopIds)) { return new ArrayList<>(); } try { final Message> msg = staffApi.getPerformanceStaff(postId, shopIds); log.info("StaffApi.getPerformanceStaff: param:[postId:{},shopIds:{}] msg.code={}, msg.result={}, msg.data={}", postId, shopIds, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { List staffDTOS = new ArrayList<>(msg.getData().size()); PerformanceStaffDTO staffDTO = null; for (PerformanceStaffVo staffVo : msg.getData()) { if (PublicUtil.isNotEmpty(staffName) && (! staffVo.getName().contains(staffName))) { continue; } staffDTO = PublicUtil.copy(staffVo, PerformanceStaffDTO.class); ZoneId zoneId = ZoneId.systemDefault(); if (PublicUtil.isNotEmpty(staffVo.getEntryDate())) { staffDTO.setEntryDate(staffVo.getEntryDate().toInstant().atZone(zoneId).toLocalDate()); } if (PublicUtil.isNotEmpty(staffVo.getRegularDate())) { staffDTO.setRegularDate(staffVo.getRegularDate().toInstant().atZone(zoneId).toLocalDate()); } if (PublicUtil.isNotEmpty(staffVo.getGainPostTime())) { staffDTO.setGainPostTime(staffVo.getGainPostTime().toInstant().atZone(zoneId).toLocalDate()); } staffDTOS.add(staffDTO); } return staffDTOS; } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<>(); } /** * 查询门店、岗位下的员工 * * @param postId * @param shopIds * @return */ @Nullable public List queryKpiStaff(final Long postId, List shopIds) { return this.queryKpiStaffByName(postId, shopIds, null); } /** * 查询员工试用期工资 * * @param userId * @return */ @Nullable public BigDecimal queryProbationerSalary(final Long userId, final LocalDate localDate) { if (userId == null) { return null; } try { List staffIds = new ArrayList(){{add(userId);}}; Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); final Message> msg = staffApi.getProbationSalary(staffIds, date.getTime()); log.info("StaffApi.getProbationSalary: param:[userId:{}, date:{}] msg.code={}, msg.result={}, msg.data={}", JSON.toJSONString(staffIds), date, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { final List probationStaffSalaryVos = msg.getData(); if ((probationStaffSalaryVos.size() == 0) || PublicUtil.isEmpty(probationStaffSalaryVos.get(0).getSalary())) { return BigDecimal.ZERO; } return probationStaffSalaryVos.get(0).getSalary(); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取岗位信息 * * @param postIds * @return */ @Nullable public List getPostInfoList(final List postIds) { if (PublicUtil.isEmpty(postIds)) { return new ArrayList<>(); } try { final Message> msg = postApi.getPostList(postIds); log.info("PostApi.getPostList: param:[{}] msg.code={}, msg.result={}, msg.data={}", postIds, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { List postInfos = Lists.newArrayListWithCapacity(msg.getData().size()); for (PostInfo postInfo : msg.getData()) { PostInfoDTO postInfoDTO = PublicUtil.copy(postInfo, PostInfoDTO.class); if (PublicUtil.isNotEmpty(postInfo.getRoleList())) { List postRoleDTOS = PublicUtil.copyList(postInfo.getRoleList(), PostRoleDTO.class); postInfoDTO.setRoleList(postRoleDTOS); } else { postInfoDTO.setRoleList(new ArrayList<>()); } postInfos.add(postInfoDTO); } return postInfos; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询门店下离职员工 * * @param shopIds * @param firstDayOfMonth * @param endDayOfMonth * @return */ @Nullable public List queryShopLeaveStaff(final List shopIds, Date firstDayOfMonth, Date endDayOfMonth) { if (PublicUtil.isEmpty(shopIds)) { return new ArrayList<>(); } try { final Message> msg = staffApi.getLeaveStaff(shopIds, firstDayOfMonth.getTime(), endDayOfMonth.getTime()); log.info("ManagerApi.getRealTimeManagerScope: param:[shopIds:{},firstDayOfMonth:{}.endDayOfMonth:{}] " + "msg.code={}, msg.result={}, msg.data={}", shopIds, firstDayOfMonth, endDayOfMonth, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTOs(msg.getData()); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 查询员工基础信息 * * @param staffIds * @return */ @Nullable public List queryStaffBaseInfo(final List staffIds) { if (PublicUtil.isEmpty(staffIds)) { return new ArrayList<>(); } try { final Message> msg = staffApi.getBatchStaffBaseInfoList(staffIds); log.info("ManagerApi.getBatchStaffBaseInfoList: param:[staffIds:{}], msg.code={}, msg.result={}, msg.data={}", staffIds, msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTOs(msg.getData()); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 转换为dto * @param staffBaseInfo * @return */ public StaffBaseInfoDTO convertStaffBaseDTO(StaffBaseInfo staffBaseInfo) { StaffBaseInfoDTO staffBaseInfoDTO = PublicUtil.copy(staffBaseInfo, StaffBaseInfoDTO.class); List staffShopList = new ArrayList<>(); if (PublicUtil.isNotEmpty(staffBaseInfo.getStaffShopList())) { staffShopList = PublicUtil.copyList(staffBaseInfo.getStaffShopList(), StaffShopInfoDTO.class); } staffBaseInfoDTO.setStaffShopList(staffShopList); return staffBaseInfoDTO; } /** * 转换为dto * @param staffBaseInfos * @return */ public List convertStaffBaseDTOs(List staffBaseInfos) { if (PublicUtil.isEmpty(staffBaseInfos)) { return new ArrayList<>(); } final List staffBaseInfoVos = Lists.newArrayListWithCapacity(staffBaseInfos.size()); StaffBaseInfoDTO staffBaseInfoDTO = null; List staffShopList = null; for (StaffBaseInfo staffBaseInfo :staffBaseInfos) { staffShopList = new ArrayList<>(); staffBaseInfoDTO = PublicUtil.copy(staffBaseInfo, StaffBaseInfoDTO.class); if (PublicUtil.isNotEmpty(staffBaseInfo.getStaffShopList())) { staffShopList = PublicUtil.copyList(staffBaseInfo.getStaffShopList(), StaffShopInfoDTO.class); } staffBaseInfoDTO.setStaffShopList(staffShopList); staffBaseInfoVos.add(staffBaseInfoDTO); } return staffBaseInfoVos; } /** * 转换为dto * @param scopes * @return */ public List convertShopIds(List scopes) { if (PublicUtil.isEmpty(scopes)) { return new ArrayList<>(); } List shopIds = scopes.stream().flatMap(managerStaffVo -> managerStaffVo.getShopList().stream()) .map(ManagerStaffShopVo::getShopId).distinct() .collect(Collectors.toList()); return shopIds; } /** * 根据身份证批量查询员工基础信息 * * @param groupId * @param idNumbers * @return */ @Nullable public List queryStaffInfoByIdNumbers(final Long groupId, final List idNumbers) { if (PublicUtil.isEmpty(idNumbers)) { return new ArrayList<>(); } try { final Message> msg = staffApi.getStaffInfoByIdNumbers(groupId, idNumbers); log.info("ManagerApi.getBatchStaffBaseInfoList: param:[groupId:{},idNumbers:{}], msg.code={}, msg.result={}, msg.data={}", groupId, JSON.toJSONString(idNumbers), msg.getCode(), msg.getResult(), msg.getData()); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { final List staffInfoVos = Lists.newArrayListWithCapacity(msg.getData().size()); StaffInfoDTO staffInfoDTO = null; for (StaffInfo staffInfo :msg.getData()) { staffInfoDTO = PublicUtil.copy(staffInfo, StaffInfoDTO.class); staffInfoVos.add(staffInfoDTO); } return staffInfoVos; } } catch (Exception e) { e.printStackTrace(); } return null; } /*** * 通过角色编码+管理人员id+门店列表id 获取管管理人员的所有员工列表 * @param roleCode 角色编码 * @param staffId 管理人员用户id * @param shopIdList 门店id列表 * @return */ public List getManagerScopeStaffs(String roleCode, Long staffId, List shopIdList) { if (PublicUtil.isEmpty(roleCode) || PublicUtil.isEmpty(staffId) || PublicUtil.isEmpty(shopIdList)) { return new ArrayList<>(); } try { Message> msg = staffApi.getManagerScopeStaffList(roleCode, staffId, shopIdList); log.info("获取ehr系统员工信息 staffApi.managerScopeStaffList 传入参数:roleCode:{}, staffId:{}, shopIdList:{} 返回结果:{}", roleCode, staffId, JSON.toJSONString(shopIdList), JSON.toJSONString(msg)); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTOs(msg.getData()); } } catch (Exception e) { log.error("获取ehr系统员工信息 staffApi.getManagerScopeStaffList异常"); e.printStackTrace(); } return new ArrayList<>(); } /*** * 通过门店集合下岗所有员工列表 * @param shopIdList 门店id列表 * @return */ public List getStaffListByShop(List shopIdList) { if (PublicUtil.isEmpty(shopIdList)) { return new ArrayList<>(); } try { Message> msg = staffApi.getStaffListByShopPost(shopIdList, null); log.info("获取ehr系统员工信息 staffApi.getStaffListByShopPost 传入参数: shopIdList:{} 返回结果:{}", JSON.toJSONString(shopIdList), JSON.toJSONString(msg)); if (msg.isSuccess() && !Objects.isNull(msg.getData())) { return this.convertStaffBaseDTOs(msg.getData()); } } catch (Exception e) { log.error("获取ehr系统员工信息 staffApi.getStaffListByShopPost异常"); e.printStackTrace(); } return new ArrayList<>(); } }