feat:权限功能

This commit is contained in:
lulz1 2024-05-16 17:02:47 +08:00
parent 93c1d3f346
commit 0d054de1e8
34 changed files with 650 additions and 100 deletions

View File

@ -14,7 +14,6 @@
<name>Route</name>
<description>Route</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,15 @@
package com.chint.application.commands;
import com.chint.domain.aggregates.user.User;
import com.chint.infrastructure.echo_framework.command.Command;
import lombok.Data;
@Data
public class UserRoleCommand extends Command {
private User user;
public UserRoleCommand user(User user) {
this.user = user;
return this;
}
}

View File

@ -1,8 +1,10 @@
package com.chint.application.dtos.response;
import com.chint.domain.value_object.system.SystemOrganizationVO;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import java.util.List;
@Data
public class UserRes {
@ -15,10 +17,8 @@ public class UserRes {
private String gender;
private String phoneNumber;
private String password;
@Transient
private String rankCode;
@Transient
private String companyCode;
@Transient
private String workStatus;
private List<SystemOrganizationVO> roleOrgList;
}

View File

@ -0,0 +1,15 @@
package com.chint.application.dtos.system;
import lombok.Data;
import java.util.List;
@Data
public class RoleOrgParam {
private Long roleId;
private String roleName;
private String roleDesc;
private Long userId;
private List<Long> orgIdList;
private List<Long> roleIdList;
}

View File

@ -0,0 +1,4 @@
package com.chint.application.dtos.system;
public class RoleParam {
}

View File

@ -1,4 +1,4 @@
package com.chint.application.dtos;
package com.chint.application.dtos.system;
import lombok.Data;

View File

@ -1,7 +1,5 @@
package com.chint.application.dtos;
package com.chint.application.dtos.system;
import cn.hutool.core.bean.BeanUtil;
import com.chint.domain.aggregates.system.SystemAnnouncement;
import lombok.Data;
import java.time.LocalDateTime;

View File

@ -0,0 +1,8 @@
package com.chint.application.dtos.system;
import lombok.Data;
@Data
public class SystemOrganizationParam {
private Long id;
}

View File

@ -133,6 +133,4 @@ public class OrderOutController {
public Result<PageResult<RouteOrderFiledVO>> queryRoutOrderField(@RequestBody RoutOrderFieldParam param) {
return Result.Success(SUCCESS, orderQuery.queryRouteOrderFiled(param));
}
}

View File

@ -1,12 +1,16 @@
package com.chint.application.system;
import com.chint.application.dtos.OrganizationTicketClerk;
import com.chint.application.dtos.SystemAnnouncementDTO;
import com.chint.application.dtos.SystemAnnouncementVO;
import com.chint.application.dtos.system.SystemAnnouncementDTO;
import com.chint.application.dtos.system.SystemAnnouncementVO;
import com.chint.application.dtos.system.SystemOrganizationParam;
import com.chint.domain.aggregates.system.SystemAnnouncement;
import com.chint.domain.aggregates.system.SystemOrganization;
import com.chint.domain.exceptions.NotFoundException;
import com.chint.domain.repository.SystemAnnouncementRepository;
import com.chint.domain.repository.SystemOrganizationRepository;
import com.chint.domain.service.SystemDomainService;
import com.chint.domain.value_object.system.SystemOrganizationVO;
import com.chint.infrastructure.util.Result;
import com.chint.interfaces.rest.data_center.org.OrgRequest;
import io.swagger.annotations.ApiOperation;
@ -36,6 +40,9 @@ public class SystemController {
@Autowired
private SystemDomainService systemDomainService;
@Autowired
private SystemOrganizationRepository systemOrganizationRepository;
@Autowired
private OrgRequest orgRequest;
@ -127,4 +134,25 @@ public class SystemController {
organizationTicketClerk.getExtension());
return Result.Success(SUCCESS);
}
@ApiOperation("查询所有公司信息")
@PostMapping("/organization/all/company")
public Result<List<SystemOrganizationVO>> queryAllCompanyInfo() {
List<SystemOrganizationVO> systemOrganizationVOList = systemOrganizationRepository.findByOrgLevelIsNull()
.stream()
.map(SystemOrganization::mapToVO)
.toList();
return Result.Success(SUCCESS, systemOrganizationVOList);
}
@ApiOperation("查询子部门信息")
@PostMapping("/organization/sub/org")
public Result<List<SystemOrganizationVO>> querySubOrgInfo(@RequestBody SystemOrganizationParam systemOrganizationParam) {
List<SystemOrganizationVO> systemOrganizationVOList = systemOrganizationRepository
.findByParentId(systemOrganizationParam.getId())
.stream()
.map(SystemOrganization::mapToVO)
.toList();
return Result.Success(SUCCESS, systemOrganizationVOList);
}
}

View File

@ -1,9 +1,14 @@
package com.chint.application.out;
package com.chint.application.system;
import cn.hutool.core.bean.BeanUtil;
import com.chint.application.dtos.response.UserRes;
import com.chint.application.dtos.system.RoleOrgParam;
import com.chint.domain.aggregates.user.Role;
import com.chint.domain.aggregates.user.RoleUser;
import com.chint.domain.aggregates.user.User;
import com.chint.domain.aggregates.user.UserDepartmentInfo;
import com.chint.domain.repository.RoleRepository;
import com.chint.domain.repository.RoleUserRepository;
import com.chint.domain.repository.UserRepository;
import com.chint.infrastructure.util.BaseContext;
import com.chint.infrastructure.util.Result;
@ -30,13 +35,41 @@ public class UserController {
@Autowired
private UserHttpRequest userHttpRequest;
@Autowired
private RoleUserRepository roleUserRepository;
@Autowired
private RoleRepository roleRepository;
@ApiOperation("根据Id查询用户信息")
@PostMapping("/query")
public Result<UserRes> getUserByEmployeeNo() {
User currentUser = BaseContext.getCurrentUser();
User currentUser = BaseContext.getCurrentUser().loadRoleOrg();
return Result.Success(SUCCESS, BeanUtil.copyProperties(currentUser, UserRes.class));
}
@ApiOperation("根据用户ID添加角色权限")
@PostMapping("/add/role")
public Result<String> addRoleUser(@RequestBody RoleOrgParam roleOrgParam) {
roleOrgParam.getRoleIdList().stream()
.map(it -> RoleUser.of(roleOrgParam.getUserId(), it))
.toList()
.forEach(it -> roleUserRepository.save(it));
return Result.Success();
}
@ApiOperation("编辑角色组织权限")
@PostMapping("/edit/role")
public Result<String> editRole(@RequestBody RoleOrgParam roleOrgParam) {
roleRepository.findById(roleOrgParam.getRoleId())
.ifPresentOrElse(
role -> roleRepository.save(role.updateWithParam(roleOrgParam)),
() -> roleRepository.save(Role.buildWithParam(roleOrgParam)
));
return Result.Success();
}
@ApiOperation("根据Id查询用户信息")
@PostMapping("/sync/public")

View File

@ -142,9 +142,6 @@ public class OrderDetail implements Serializable {
return null;
}
return this.orderEventList.isEmpty() ? null : this.orderEventList.get(this.orderEventList.size() - 1);
// .stream()
// .max(Comparator.comparingLong(OrderEvent::getOrderEventId))
// .orElseThrow(() -> new NotFoundException(NOT_FOUND));
}
public Optional<OrderEvent> getETAEvent() {

View File

@ -1,8 +1,8 @@
package com.chint.domain.aggregates.system;
import cn.hutool.core.bean.BeanUtil;
import com.chint.application.dtos.SystemAnnouncementDTO;
import com.chint.application.dtos.SystemAnnouncementVO;
import com.chint.application.dtos.system.SystemAnnouncementDTO;
import com.chint.application.dtos.system.SystemAnnouncementVO;
import com.chint.domain.aggregates.user.User;
import com.chint.infrastructure.util.BaseContext;
import com.chint.infrastructure.util.DateTimeUtil;

View File

@ -1,6 +1,8 @@
package com.chint.domain.aggregates.system;
import cn.hutool.core.bean.BeanUtil;
import com.chint.domain.value_object.system.SystemOrganizationVO;
import com.chint.interfaces.rest.data_center.org.OrgSfResponse;
import lombok.Data;
import org.springframework.data.annotation.Id;
@ -81,4 +83,8 @@ public class SystemOrganization implements Serializable {
});
return this;
}
public SystemOrganizationVO mapToVO() {
return BeanUtil.copyProperties(this, SystemOrganizationVO.class);
}
}

View File

@ -0,0 +1,56 @@
package com.chint.domain.aggregates.user;
import com.chint.application.dtos.system.RoleOrgParam;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.Table;
import java.util.ArrayList;
import java.util.List;
@Data
@Table("role")
public class Role {
@Id
private Long id;
private String roleName;
private String roleDesc;
@MappedCollection(idColumn = "role_id", keyColumn = "role_key")
private List<RoleOrganization> roleOrganizationList;
public Role addRoleOrganization(RoleOrganization roleOrganization) {
if (roleOrganizationList == null) {
roleOrganizationList = new ArrayList<>();
}
for (RoleOrganization organization : roleOrganizationList) {
if (organization.getOrgId().equals(roleOrganization.getOrgId())) {
return this;
}
}
roleOrganizationList.add(roleOrganization);
return this;
}
public Role updateWithParam(RoleOrgParam roleOrgParam) {
roleOrgParam.getOrgIdList().stream()
.map(it -> RoleOrganization.of(roleOrgParam.getRoleId(), it))
.toList()
.forEach(this::addRoleOrganization);
return this;
}
public static Role buildWithParam(RoleOrgParam roleOrgParam) {
Role role = new Role();
role.setRoleName(roleOrgParam.getRoleName());
role.setRoleDesc(roleOrgParam.getRoleDesc());
if(roleOrgParam.getRoleIdList() != null){
roleOrgParam.getOrgIdList().stream()
.map(it -> RoleOrganization.of(roleOrgParam.getRoleId(), it))
.toList()
.forEach(role::addRoleOrganization);
}
return role;
}
}

View File

@ -0,0 +1,22 @@
package com.chint.domain.aggregates.user;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Table("role_organization")
public class RoleOrganization {
@Id
private Long id;
private Long roleId;
private Long orgId;
public static RoleOrganization of(Long roleId, Long orgId) {
RoleOrganization roleOrganization = new RoleOrganization();
roleOrganization.setRoleId(roleId);
roleOrganization.setOrgId(orgId);
return roleOrganization;
}
}

View File

@ -0,0 +1,22 @@
package com.chint.domain.aggregates.user;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Table("role_user")
public class RoleUser {
@Id
private Long id;
private Long userId;
private Long roleId;
public static RoleUser of(Long userId, Long roleId) {
RoleUser roleUser = new RoleUser();
roleUser.setUserId(userId);
roleUser.setRoleId(roleId);
return roleUser;
}
}

View File

@ -3,11 +3,13 @@ package com.chint.domain.aggregates.user;
import cn.hutool.core.bean.BeanUtil;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.chint.application.commands.UserRoleCommand;
import com.chint.application.commands.UserSFCommand;
import com.chint.domain.aggregates.system.FsscSystem;
import com.chint.domain.exceptions.NotFoundException;
import com.chint.domain.value_object.UserLoginParam;
import com.chint.domain.value_object.UserVO;
import com.chint.domain.value_object.system.SystemOrganizationVO;
import com.chint.infrastructure.constant.AuthMessageConstant;
import com.chint.infrastructure.echo_framework.command.Command;
import com.chint.infrastructure.util.BaseContext;
@ -76,6 +78,10 @@ public class User implements Serializable {
private String workStatus;
@Transient
private UserLoginParam userLoginParam;
@Transient
private List<String> roleOrgCodeList;
@Transient
private List<SystemOrganizationVO> roleOrgList;
public User loadInfoFromDept() {
if (this.userDepartmentInfoList == null || this.userDepartmentInfoList.isEmpty()) {
@ -383,4 +389,9 @@ public class User implements Serializable {
this.setUserDepartmentInfoList(result);
return this;
}
public User loadRoleOrg() {
Command.of(UserRoleCommand.class).user(this).sendToQueue();
return this;
}
}

View File

@ -0,0 +1,13 @@
package com.chint.domain.repository;
import com.chint.domain.aggregates.user.Role;
import javax.swing.text.html.Option;
import java.util.Optional;
public interface RoleRepository {
Optional<Role> findById(Long id);
Role save(Role role);
}

View File

@ -0,0 +1,11 @@
package com.chint.domain.repository;
import com.chint.domain.aggregates.user.RoleUser;
import java.util.List;
public interface RoleUserRepository {
List<RoleUser> findByUserId(Long userId);
RoleUser save(RoleUser roleUser);
}

View File

@ -13,9 +13,14 @@ public interface SystemOrganizationRepository {
List<SystemOrganization> findAll();
List<SystemOrganization> findByIdIn(List<Long> ids);
List<SystemOrganization> findByOrgLevelIsNull();
Optional<SystemOrganization> findByOrgCodeContaining(String orgCode);
List<SystemOrganization> findByOrgCodeList(List<String> orgCode);
List<SystemOrganization> findByParentId(Long parentId);
}

View File

@ -28,12 +28,12 @@ import com.chint.interfaces.rest.ctrip.dto.search.car.OrderFee;
import com.chint.interfaces.rest.ctrip.dto.search.flight.*;
import com.chint.interfaces.rest.ctrip.dto.search.hotel.HotelOrderInfoEntity;
import com.chint.interfaces.rest.ctrip.dto.search.hotel.RefundInfo;
import com.chint.interfaces.rest.data_center.user.UserHttpRequest;
import com.chint.interfaces.rest.ly.LYSearchRequest;
import com.chint.interfaces.rest.ly.dto.search.response.car.CarDetailResponse;
import com.chint.interfaces.rest.ly.dto.search.response.filght.FlightOrderResponse;
import com.chint.interfaces.rest.ly.dto.search.response.hotel.HotelDetailResponse;
import com.chint.interfaces.rest.ly.dto.search.response.train.TrainDetailResponse;
import com.chint.interfaces.rest.data_center.user.UserHttpRequest;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -59,12 +59,6 @@ public class OrderDomainService {
@Autowired
private RouteRepository routeRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private LocationRepository locationRepository;
@Autowired
private RouteOrderFactory routeOrderFactory;
@ -74,18 +68,6 @@ public class OrderDomainService {
@Autowired
private OrderDetailRepository orderDetailRepository;
@Autowired
private UserHttpRequest userHttpRequest;
@Autowired
private BPMParamFactory bpmParamFactory;
@Autowired
private BPMRequest bpmRequest;
@Autowired
private LegRepository legRepository;
@Autowired
private LegEventFactory legEventFactory;
@ -98,12 +80,6 @@ public class OrderDomainService {
@Autowired
private LYSearchRequest lySearchRequest;
@Autowired
private JdbcOrderDetailRepository jdbcOrderDetailRepository;
@Autowired
private JdbcLegRepository jdbcLegRepository;
@Autowired
private BPMOrderDomainService bpmOrderDomainService;
@ -183,23 +159,42 @@ public class OrderDomainService {
OrderDetail orderDetail = command.getOrderDetail();
String outStatus = command.getOutStatus();
Integer orderEventType = command.getOrderEventType();
OrderEvent event = orderDetailFactory.createEvent(orderEventType,
outStatus);
if (orderEventType.equals(ORDER_EVENT_ETA) ||
orderEventType.equals(ORDER_EVENT_CHANGE) ||
orderEventType.equals(ORDER_EVENT_REFUND)) {
OrderEvent event = orderDetailFactory.createEvent(orderEventType, outStatus);
if (isSpecialEventType(orderEventType)) {
event.setExtension(command.getExtension());
}
orderDetail.addOrderEvent(event);
reCheckOrderETA(event, orderDetail);
orderDetailRepository.save(orderDetail);
if (orderEventType.equals(ORDER_EVENT_ETA) ||
if (isSpecialEventType(orderEventType)) {
handleSpecialEvent(orderDetail, command, orderEventType);
}
}
private boolean isSpecialEventType(Integer orderEventType) {
return orderEventType.equals(ORDER_EVENT_ETA) ||
orderEventType.equals(ORDER_EVENT_CHANGE) ||
orderEventType.equals(ORDER_EVENT_REFUND)) {
//这里如果发现事件的状态为ORDER_EVENT_ETA
//需要根据发送审批给bpm
//根据订单类型获取不同的订单扩展明细
orderEventType.equals(ORDER_EVENT_REFUND);
}
private void handleSpecialEvent(OrderDetail orderDetail, OrderStatusChangeCommand command, Integer orderEventType) {
orderDetail.getExtensionDataByProductType();
sendBPMAuditCommand(orderDetail, command, orderEventType);
String supplierName = orderDetail.getSupplierName();
Integer productType = orderDetail.getProductType();
if (SUPPLIER_C_TRIP.equals(supplierName)) {
handleCTripEvents(orderDetail, orderEventType, productType);
} else if (SUPPLIER_L_Y.equals(supplierName)) {
handleLYEvents(orderDetail, orderEventType, productType);
}
}
private void sendBPMAuditCommand(OrderDetail orderDetail, OrderStatusChangeCommand command, Integer orderEventType) {
Command.of(BPMAuditCommand.class)
.operationType(orderEventType)
.orderNo(orderDetail.getOrderNo())
@ -207,30 +202,33 @@ public class OrderDomainService {
.extensionData(orderDetail)
.reason(command.getExtension())
.sendToQueue();
}
if (orderDetail.getSupplierName().equals(SUPPLIER_C_TRIP)) {
if (!orderEventType.equals(ORDER_EVENT_ETA) && orderDetail.getProductType().equals(LEG_TYPE_AIRPLANE)) {
cTripFlightRefundAndChangeOrder(orderDetail.getOrderNo());
private void handleCTripEvents(OrderDetail orderDetail, Integer orderEventType, Integer productType) {
String orderNo = orderDetail.getOrderNo();
if (ORDER_EVENT_ETA != orderEventType) {
if (LEG_TYPE_AIRPLANE == productType) {
cTripFlightRefundAndChangeOrder(orderNo);
} else if (LEG_TYPE_TAXI == productType) {
cTripCarRefundChangeOrder(orderNo);
}
if (orderDetail.getProductType().equals(LEG_TYPE_HOTEL)) {
cTripHotelRefundAndChangeOrder(orderDetail.getOrderNo());
}
if (!orderEventType.equals(ORDER_EVENT_ETA) && orderDetail.getProductType().equals(LEG_TYPE_TAXI)) {
cTripCarRefundChangeOrder(orderDetail.getOrderNo());
if (LEG_TYPE_HOTEL == productType) {
cTripHotelRefundAndChangeOrder(orderNo);
}
}
if (orderDetail.getSupplierName().equals(SUPPLIER_L_Y)) {
if (!orderEventType.equals(ORDER_EVENT_ETA) && orderDetail.getProductType().equals(LEG_TYPE_TRAIN)) {
lyTrainRefundGenerate(orderDetail.getOrderNo());
}
if (!orderEventType.equals(ORDER_EVENT_ETA) && orderDetail.getProductType().equals(LEG_TYPE_HOTEL)) {
lyHotelRefundGenerate(orderDetail.getOrderNo());
}
if (!orderEventType.equals(ORDER_EVENT_ETA) && orderDetail.getProductType().equals(LEG_TYPE_TAXI)) {
lyCarRefundGenerate(orderDetail.getOrderNo());
private void handleLYEvents(OrderDetail orderDetail, Integer orderEventType, Integer productType) {
String orderNo = orderDetail.getOrderNo();
if (ORDER_EVENT_ETA != (orderEventType)) {
if (LEG_TYPE_TRAIN == productType) {
lyTrainRefundGenerate(orderNo);
} else if (LEG_TYPE_TAXI == productType) {
lyCarRefundGenerate(orderNo);
}
}
if (LEG_TYPE_HOTEL == productType) {
lyHotelRefundGenerate(orderNo);
}
}
@ -431,4 +429,45 @@ public class OrderDomainService {
}
return routeOrder;
}
private OrderDetail reCheckOrderETA(OrderEvent orderEvent, OrderDetail orderDetail) {
if (!orderEvent.getEventType().equals(ORDER_EVENT_ETA)) {
return orderDetail;
}
Integer productType = orderDetail.getProductType();
switch (productType) {
case LEG_TYPE_TRAIN -> {
TrainOrderDetail trainOrderDetail = orderDetail.getTrainOrderDetail();
if (trainOrderDetail != null) {
trainOrderDetail.setOverStandard("");
trainOrderDetail.setOverStandardReason(orderEvent.getExtension());
}
}
case LEG_TYPE_AIRPLANE -> {
FlightOrderDetail flightOrderDetail = orderDetail.getFlightOrderDetail();
if (flightOrderDetail != null) {
flightOrderDetail.setOverStandard("");
flightOrderDetail.setOverStandardReason(orderEvent.getExtension());
}
}
case LEG_TYPE_HOTEL -> {
HotelOrderDetail hotelOrderDetail = orderDetail.getHotelOrderDetail();
if (hotelOrderDetail != null) {
hotelOrderDetail.setOverStandard("");
hotelOrderDetail.setOverStandardReason(orderEvent.getExtension());
}
}
case LEG_TYPE_TAXI -> {
CarOrderDetail carOrderDetail = orderDetail.getCarOrderDetail();
if (carOrderDetail != null) {
carOrderDetail.setOverStandard("");
carOrderDetail.setOverStandardReason(orderEvent.getExtension());
}
}
default -> {
}
}
return orderDetail;
}
}

View File

@ -1,18 +1,22 @@
package com.chint.domain.service;
import com.chint.application.commands.UserRoleCommand;
import com.chint.application.dtos.OrganizationTicketClerk;
import com.chint.domain.aggregates.order.order_record.OrderBaseRecord;
import com.chint.domain.aggregates.system.AccountCompany;
import com.chint.domain.aggregates.system.SystemCode;
import com.chint.domain.aggregates.system.SystemOrganization;
import com.chint.domain.aggregates.system.SystemOrganizationExtension;
import com.chint.domain.aggregates.user.RoleOrganization;
import com.chint.domain.aggregates.user.RoleUser;
import com.chint.domain.aggregates.user.User;
import com.chint.domain.exceptions.NotFoundException;
import com.chint.domain.repository.AccountCompanyRepository;
import com.chint.domain.repository.SystemCodeRepository;
import com.chint.domain.repository.SystemOrganizationRepository;
import com.chint.domain.repository.*;
import com.chint.infrastructure.echo_framework.annotation.ListenTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@ -33,6 +37,12 @@ public class SystemDomainService {
@Autowired
private SystemOrganizationRepository systemOrganizationRepository;
@Autowired
private RoleUserRepository roleUserRepository;
@Autowired
private RoleRepository roleRepository;
public boolean checkSystemCode(String sysCode) {
boolean b = systemCodeRepository.findBySysCode(sysCode) != null;
if (!b) {
@ -147,4 +157,46 @@ public class SystemDomainService {
.findFirst()
.orElse(null);
}
@ListenTo(command = "UserRoleCommand", order = 0)
public void loadUserRole(UserRoleCommand command) {
User user = command.getUser();
List<RoleUser> byUserId = roleUserRepository.findByUserId(user.getUserId());
List<Long> orgListId = byUserId.stream().flatMap(it ->
roleRepository
.findById(it.getRoleId())
.stream()
.flatMap(role -> role.getRoleOrganizationList().stream())
.map(RoleOrganization::getOrgId)
).distinct().toList();
// 获取所有的 SystemOrganization
List<SystemOrganization> rootOrganizations = systemOrganizationRepository.findByIdIn(orgListId);
// 展开所有的 SystemOrganization
List<SystemOrganization> allOrganizations = new ArrayList<>();
rootOrganizations.forEach(rootOrg -> expandOrganizations(rootOrg, allOrganizations));
// 设置用户的角色组织列表
user.setRoleOrgCodeList(
allOrganizations.stream()
.map(SystemOrganization::getOrgShortCode)
.distinct()
.toList()
);
user.setRoleOrgList(
allOrganizations.stream()
.map(SystemOrganization::mapToVO)
.distinct()
.toList()
);
}
private void expandOrganizations(SystemOrganization org, List<SystemOrganization> allOrganizations) {
queryChild(org); // 查询子组织
allOrganizations.add(org); // 添加当前组织到列表
for (SystemOrganization child : org.getChildOrganizationList()) {
expandOrganizations(child, allOrganizations); // 递归展开子组织
}
}
}

View File

@ -0,0 +1,18 @@
package com.chint.domain.value_object.system;
import lombok.Data;
@Data
public class SystemOrganizationVO {
private Long id;
private String orgCode;
private String orgShortCode;
private String orgName;
private String orgEnName;
private Long parentId;
private String parentOrgCode;
private String parentOrgName;
private String parentOrgEnName;
private String costCenter;
private String orgLevel;
}

View File

@ -0,0 +1,42 @@
package com.chint.infrastructure.repository;
import com.chint.domain.aggregates.user.Role;
import com.chint.domain.repository.RoleRepository;
import com.chint.infrastructure.repository.cache.CacheRoleRepository;
import com.chint.infrastructure.repository.jdbc.JdbcRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public class RoleRepositoryImpl implements RoleRepository {
@Autowired
private JdbcRoleRepository jdbcRoleRepository;
@Autowired
private CacheRoleRepository cacheRoleRepository;
@Override
public Optional<Role> findById(Long id) {
if (id == null) {
return Optional.empty();
}
Optional<Role> fromCache = cacheRoleRepository.getFromCache(id, Optional.empty());
if (fromCache.isPresent()) {
return fromCache;
} else {
return cacheRoleRepository.getFromCache(id, jdbcRoleRepository.findById(id));
}
}
@Override
public Role save(Role role) {
Long id = role.getId();
if(id != null){
cacheRoleRepository.evict(id);
}
return jdbcRoleRepository.save(role);
}
}

View File

@ -0,0 +1,30 @@
package com.chint.infrastructure.repository;
import com.chint.domain.aggregates.user.RoleUser;
import com.chint.domain.repository.RoleUserRepository;
import com.chint.infrastructure.repository.jdbc.JdbcRoleUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class RoleUserRepositoryImpl implements RoleUserRepository {
@Autowired
private JdbcRoleUserRepository jdbcRoleUserRepository;
@Cacheable(value = "RoleUser", key = "#userId")
@Override
public List<RoleUser> findByUserId(Long userId) {
return jdbcRoleUserRepository.findByUserId(userId);
}
@CacheEvict(value = "RoleUser", key = "#roleUser.userId")
@Override
public RoleUser save(RoleUser roleUser) {
return jdbcRoleUserRepository.save(roleUser);
}
}

View File

@ -2,6 +2,7 @@ package com.chint.infrastructure.repository;
import com.chint.domain.aggregates.system.SystemOrganization;
import com.chint.domain.repository.SystemOrganizationRepository;
import com.chint.infrastructure.repository.cache.CacheSystemOrganizationRepository;
import com.chint.infrastructure.repository.jdbc.JdbcSystemOrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@ -16,6 +17,9 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos
@Autowired
private JdbcSystemOrganizationRepository jdbcSystemOrganizationRepository;
@Autowired
private CacheSystemOrganizationRepository cacheSystemOrganizationRepository;
@Override
public void saveAll(List<SystemOrganization> systemOrganizationList) {
for (SystemOrganization systemOrganization : systemOrganizationList) {
@ -30,7 +34,10 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos
@Override
public SystemOrganization save(SystemOrganization systemOrganization) {
return jdbcSystemOrganizationRepository.save(systemOrganization);
cacheSystemOrganizationRepository.cacheEvict(systemOrganization); // 先清理旧缓存
jdbcSystemOrganizationRepository.save(systemOrganization);
cacheSystemOrganizationRepository.cacheEvictIdsByParentId(systemOrganization.getParentId());
return cacheSystemOrganizationRepository.cache(systemOrganization);
}
@Override
@ -41,20 +48,55 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos
return systemOrganizationList;
}
@Override
public List<SystemOrganization> findByIdIn(List<Long> ids) {
List<SystemOrganization> fromCacheByIdIn = cacheSystemOrganizationRepository.getFromCacheByIdIn(ids);
List<Long> fromCacheOrg = fromCacheByIdIn.stream().map(SystemOrganization::getId).toList();
List<Long> list = ids.stream().filter(it -> !fromCacheOrg.contains(it)).toList();
if (!list.isEmpty()) {
List<SystemOrganization> fromDBByIdIn = jdbcSystemOrganizationRepository.findByIdIn(list);
fromDBByIdIn.forEach(it -> cacheSystemOrganizationRepository.cache(it)); // 更新缓存
fromCacheByIdIn.addAll(fromDBByIdIn);
}
return fromCacheByIdIn;
}
@Override
public List<SystemOrganization> findByOrgLevelIsNull() {
return jdbcSystemOrganizationRepository.findByOrgLevelIsNullOrOrgLevel("").stream()
.peek(systemOrganization -> cacheSystemOrganizationRepository.cache(systemOrganization)) // 更新缓存
.toList();
}
@Override
public Optional<SystemOrganization> findByOrgCodeContaining(String orgCode) {
return jdbcSystemOrganizationRepository.findByOrgCodeContaining(orgCode).stream().findFirst();
return jdbcSystemOrganizationRepository.findByOrgCodeContaining(orgCode).stream()
.peek(systemOrganization -> cacheSystemOrganizationRepository.cache(systemOrganization)) // 更新缓存
.findFirst();
}
@Override
public List<SystemOrganization> findByOrgCodeList(List<String> orgCode) {
return jdbcSystemOrganizationRepository.findByOrgShortCodeIn(orgCode);
return jdbcSystemOrganizationRepository.findByOrgShortCodeIn(orgCode).stream()
.peek(systemOrganization -> cacheSystemOrganizationRepository.cache(systemOrganization)) // 更新缓存
.toList();
}
@Override
public List<SystemOrganization> findByParentId(Long parentId) {
return jdbcSystemOrganizationRepository.findByParentId(parentId);
List<Long> idsFromCacheByParentId = cacheSystemOrganizationRepository.getIdsFromCacheByParentId(parentId, null);
List<SystemOrganization> list;
if (idsFromCacheByParentId == null) {
list = jdbcSystemOrganizationRepository.findByParentId(parentId).stream()
.peek(systemOrganization -> cacheSystemOrganizationRepository.cache(systemOrganization)) // 更新缓存
.toList();
cacheSystemOrganizationRepository.cacheEvictIdsByParentId(parentId);
List<Long> ids = list.stream().map(SystemOrganization::getId).toList();
cacheSystemOrganizationRepository.getIdsFromCacheByParentId(parentId, ids);
} else {
list = cacheSystemOrganizationRepository.getFromCacheByIdIn(idsFromCacheByParentId);
}
return list;
}
}

View File

@ -0,0 +1,22 @@
package com.chint.infrastructure.repository.cache;
import com.chint.domain.aggregates.user.Role;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public class CacheRoleRepository {
@Cacheable(value = "Role", key = "#id")
public Optional<Role> getFromCache(Long id, Optional<Role> role) {
return role;
}
@CacheEvict(value = "Role", key = "#id")
public void evict(Long id) {
}
}

View File

@ -0,0 +1,43 @@
package com.chint.infrastructure.repository.cache;
import com.chint.domain.aggregates.system.SystemOrganization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class CacheSystemOrganizationRepository {
@Autowired
private RedisTemplate<String, SystemOrganization> redisTemplate;
public List<SystemOrganization> getFromCacheByIdIn(List<Long> ids) {
List<String> routeOrderKeys = ids.stream().map(id -> "SystemOrganization::" + id).toList();
return redisTemplate.opsForValue().multiGet(routeOrderKeys);
}
@Cacheable(value = "SystemOrganization", key = "#systemOrganization.id")
public SystemOrganization cache(SystemOrganization systemOrganization) {
return systemOrganization;
}
@CacheEvict(value = "SystemOrganization", key = "#systemOrganization.id")
public void cacheEvict(SystemOrganization systemOrganization) {
}
@Cacheable(value = "SystemOrganization::ParentId", key = "#parentId")
// 新增方法根据 parentId 从缓存中获取子组织的 ID 列表
public List<Long> getIdsFromCacheByParentId(Long parentId, List<Long> ids) {
return ids;
}
// 新增方法根据 parentId 清除缓存中子组织的 ID 列表
@CacheEvict(value = "SystemOrganization::ParentId", key = "#parentId")
public void cacheEvictIdsByParentId(Long parentId) {
}
}

View File

@ -0,0 +1,10 @@
package com.chint.infrastructure.repository.jdbc;
import com.chint.domain.aggregates.standards.Ranks;
import com.chint.domain.aggregates.user.Role;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface JdbcRoleRepository extends CrudRepository<Role, Long> {
}

View File

@ -0,0 +1,13 @@
package com.chint.infrastructure.repository.jdbc;
import com.chint.domain.aggregates.user.RoleUser;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface JdbcRoleUserRepository extends CrudRepository<RoleUser, Long> {
List<RoleUser> findByUserId(Long userId);
}

View File

@ -12,5 +12,7 @@ public interface JdbcSystemOrganizationRepository extends CrudRepository<SystemO
List<SystemOrganization> findByOrgCodeContaining(String orgCode);
List<SystemOrganization> findByOrgCode(String orgCode);
List<SystemOrganization> findByParentId(Long parentId);
List<SystemOrganization> findByOrgLevelIsNullOrOrgLevel(String orgLevel);
List<SystemOrganization> findByOrgShortCodeIn(Collection<String> orgCode);
List<SystemOrganization> findByIdIn(Collection<Long> id);
}

View File

@ -246,7 +246,7 @@ public class CTripTest {
@Test
void search() {
BaseContext.setCurrentUser(user);
SearchOrderResponse response = orderSearchRequest.searchOrderResponseByOrderId("31900510524");
SearchOrderResponse response = orderSearchRequest.searchOrderResponseByOrderId("31791272980");
System.out.println(response);
}

View File

@ -291,7 +291,7 @@ class RouteApplicationTests {
@Test
void loginSign() {
String sfno = "0001018592";
String sfno = "060726001";
String syscode = "FSSC";
String billcode = "CLSQ240225000099";
String companycode = "正泰集团股份有限公司";
@ -302,7 +302,7 @@ class RouteApplicationTests {
@Test
void loginSignProd() {
String sfno = "180605039";
String sfno = "060726001";
String syscode = "FSSC";
String billcode = "CLSQ240225000099";
String companycode = "正泰集团股份有限公司";
@ -310,10 +310,6 @@ class RouteApplicationTests {
String key = "ZhengTaiRoute";
String s = Digest.md5(sfno + syscode + billcode + companycode + key + timespan);
System.out.println(s);
// log.info("info");
// log.trace("trace");
// log.trace("trace");
// log.trace("trace");
}
// @Test