|
@@ -1,5 +1,7 @@
|
|
|
package edu.travel.warehouse.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
@@ -7,6 +9,8 @@ import edu.travel.adapter.service.country.CountryAdapter;
|
|
|
import edu.travel.adapter.service.tenant.SysRoleAdapter;
|
|
|
import edu.travel.adapter.service.tenant.TenantAdapter;
|
|
|
import edu.travel.dto.ShopWarehouseDto;
|
|
|
+import edu.travel.dto.ShopWarehouseSaveDto;
|
|
|
+import edu.travel.dto.ShopWarehouseStaffDto;
|
|
|
import edu.travel.remote.feign.mode.vo.tenant.EduTenantVo;
|
|
|
import edu.travel.remote.feign.mode.vo.tenant.SysRoleVo;
|
|
|
import edu.travel.rpc.RPCBaseResponse;
|
|
@@ -21,7 +25,10 @@ import edu.travel.warehouse.mapper.ShopWarehouseStaffMapper;
|
|
|
import edu.travel.warehouse.service.ShopWarehouseService;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -40,13 +47,6 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
private ShopWarehouseStaffMapper shopWarehouseStaffMapper;
|
|
|
/**
|
|
|
* 树形结构查询(连表)
|
|
|
- * @param dto
|
|
|
- * @return
|
|
|
- */
|
|
|
- /**
|
|
|
- * 树形结构查询(连表)
|
|
|
- * @param dto
|
|
|
- * @return
|
|
|
*/
|
|
|
@Override
|
|
|
public RPCBaseResponse<List<ShopWarehouseVo>> getWarehouseCountryForm(ShopWarehouseDto dto) {
|
|
@@ -82,6 +82,9 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
return new RPCBaseResponse<>(200, "SUCCESS", tree);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 分页动态查询(连表)
|
|
|
+ */
|
|
|
@Override
|
|
|
public RPCBaseResponse<IPage<ShopWarehouseVo>> getWarehouseCountryPageForm(ShopWarehouseDto dto) {
|
|
|
// 创建分页对象
|
|
@@ -93,7 +96,7 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
dto.getStatus(),
|
|
|
dto.getParentId(),
|
|
|
dto.getPageSize(),
|
|
|
- (int) ((dto.getCurrentPage() - 1) * dto.getPageSize())
|
|
|
+ (dto.getCurrentPage() - 1) * dto.getPageSize()
|
|
|
);
|
|
|
int total = shopWarehouseMapper.countWarehouseWithCountry(dto.getWarehouseName(), dto.getStatus(), dto.getParentId());
|
|
|
|
|
@@ -134,6 +137,9 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
return new RPCBaseResponse<>(200, "SUCCESS", pageVoLink);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 提取国家数据
|
|
|
+ */
|
|
|
private Map<String, BaseCountryServeVo> fetchCountryData(Set<String> countryServeIds) {
|
|
|
Map<String, BaseCountryServeVo> countryMap = new HashMap<>();
|
|
|
if (!countryServeIds.isEmpty()) {
|
|
@@ -147,6 +153,9 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
return countryMap;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 提取仓库数据
|
|
|
+ */
|
|
|
private Map<Long, List<ShopWarehouseStaffVo>> fetchWarehouseTenantRoles(Set<Long> ids) {
|
|
|
Map<Long, List<ShopWarehouseStaffVo>> staffMap = new HashMap<>();
|
|
|
if (!ids.isEmpty()) {
|
|
@@ -160,6 +169,9 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
return staffMap;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *生成map
|
|
|
+ */
|
|
|
private ShopWarehouseVo mapToWarehouseVo(ShopWarehouse shopWarehouse,
|
|
|
Map<String, BaseCountryServeVo> countryMap,
|
|
|
Map<Long, List<ShopWarehouseStaffVo>> staffMap) {
|
|
@@ -171,6 +183,7 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
shopWarehouseVo.setLatitude(shopWarehouse.getLatitude());
|
|
|
shopWarehouseVo.setDetailedAddress(shopWarehouse.getDetailedAddress());
|
|
|
shopWarehouseVo.setStatus(shopWarehouse.getStatus());
|
|
|
+ shopWarehouseVo.setProject(shopWarehouse.getProject());
|
|
|
shopWarehouseVo.setParentId(shopWarehouse.getParentId());
|
|
|
shopWarehouseVo.setDeleteFlag(shopWarehouse.getDeleteFlag());
|
|
|
shopWarehouseVo.setMap(new HashMap<>());
|
|
@@ -184,6 +197,9 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
return shopWarehouseVo;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建树
|
|
|
+ */
|
|
|
private void addToTree(List<ShopWarehouseVo> tree, ShopWarehouseVo warehouseVo, Map<Long, ShopWarehouseVo> warehouseVoMap) {
|
|
|
if (warehouseVo.getParentId() == null || warehouseVo.getParentId() == 0) {
|
|
|
tree.add(warehouseVo);
|
|
@@ -201,7 +217,6 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
|
|
|
/**
|
|
|
* 库房关联人员(角色)查询,通过库房id,查询用户以及角色
|
|
|
- * @return
|
|
|
*/
|
|
|
@Override
|
|
|
public RPCBaseResponse<List<ShopWarehouseStaffVo>> getWarehouseTenantRole(String id) {
|
|
@@ -278,4 +293,143 @@ public class ShopWarehouseServiceImpl extends SysServiceImpl<ShopWarehouseMapper
|
|
|
return new RPCBaseResponse<>(200, "SUCCESS", resultList);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 新增连表
|
|
|
+ * @param entity
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional // 确保方法在事务中执行
|
|
|
+ public RPCBaseResponse<ShopWarehouseVo> saveAllForm(ShopWarehouseSaveDto entity) {
|
|
|
+ // 创建一个新的 ShopWarehouse 实体
|
|
|
+ ShopWarehouse warehouse = new ShopWarehouse();
|
|
|
+
|
|
|
+ // 获取当前用户 ID
|
|
|
+ Long currentUserId = getCurrentUserId(); // 获取当前用户 ID
|
|
|
+
|
|
|
+ // 将 DTO 中的属性复制到实体对象中
|
|
|
+ copyWarehouseProperties(entity, warehouse);
|
|
|
+ warehouse.setCreateUserId(String.valueOf(currentUserId)); // 填充创建人 ID
|
|
|
+
|
|
|
+ // 插入仓库信息到数据库
|
|
|
+ shopWarehouseMapper.insert(warehouse);
|
|
|
+
|
|
|
+ // 如果存在员工列表,则保存员工角色信息
|
|
|
+ if (entity.getStaffList() != null && !entity.getStaffList().isEmpty()) {
|
|
|
+ saveStaffRoles(entity.getStaffList(), warehouse.getId(), currentUserId); // 传入用户 ID
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建并返回响应对象
|
|
|
+ ShopWarehouseVo warehouseVo = new ShopWarehouseVo();
|
|
|
+ warehouseVo.setId(warehouse.getId());
|
|
|
+ warehouseVo.setWarehouseName(warehouse.getWarehouseName());
|
|
|
+ warehouseVo.setProject(warehouse.getProject());
|
|
|
+ warehouseVo.setCreateTime(warehouse.getCreateTime());
|
|
|
+ warehouseVo.setCreateUserId(warehouse.getCreateUserId());
|
|
|
+ warehouseVo.setUpdateTime(warehouse.getUpdateTime());
|
|
|
+ warehouseVo.setUpdateUserId(warehouse.getUpdateUserId());
|
|
|
+ warehouseVo.setDeleteFlag(warehouse.getDeleteFlag());
|
|
|
+ warehouseVo.setParentId(warehouse.getParentId());
|
|
|
+ warehouseVo.setLongitude(warehouse.getLongitude());
|
|
|
+ warehouseVo.setLatitude(warehouse.getLatitude());
|
|
|
+ warehouseVo.setCountryServeId(warehouse.getCountryServeId());
|
|
|
+ warehouseVo.setDetailedAddress(warehouse.getDetailedAddress());
|
|
|
+ warehouseVo.setStatus(warehouse.getStatus());
|
|
|
+ // 其他字段映射...
|
|
|
+
|
|
|
+ // 返回成功的响应
|
|
|
+ return new RPCBaseResponse<>(200, "SUCCESS", warehouseVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional // 确保方法在事务中执行
|
|
|
+ public RPCBaseResponse<ShopWarehouseVo> updateWarehouse(ShopWarehouseSaveDto entity) {
|
|
|
+ // 根据 ID 查找现有的仓库信息
|
|
|
+ ShopWarehouse existingWarehouse = shopWarehouseMapper.selectById(entity.getId());
|
|
|
+
|
|
|
+ // 如果找不到该仓库,返回错误响应
|
|
|
+ if (existingWarehouse == null) {
|
|
|
+ return new RPCBaseResponse<>(404, "NULL", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新现有仓库的属性
|
|
|
+ copyWarehouseProperties(entity, existingWarehouse);
|
|
|
+
|
|
|
+ // 获取当前用户 ID
|
|
|
+ Long currentUserId = getCurrentUserId(); // 获取当前用户 ID
|
|
|
+ existingWarehouse.setUpdateUserId(String.valueOf(currentUserId)); // 填充更新人 ID
|
|
|
+
|
|
|
+ // 更新仓库信息到数据库
|
|
|
+ shopWarehouseMapper.updateById(existingWarehouse);
|
|
|
+
|
|
|
+ // 更新员工角色信息
|
|
|
+ if (entity.getStaffList() != null) {
|
|
|
+ // 先删除原有员工信息
|
|
|
+ shopWarehouseStaffMapper.deleteByWarehouseId(existingWarehouse.getId());
|
|
|
+ // 然后插入新的员工角色信息
|
|
|
+ saveStaffRoles(entity.getStaffList(), existingWarehouse.getId(), currentUserId); // 传入用户 ID
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建并返回响应对象
|
|
|
+ ShopWarehouseVo warehouseVo = new ShopWarehouseVo();
|
|
|
+ warehouseVo.setId(existingWarehouse.getId());
|
|
|
+ warehouseVo.setWarehouseName(existingWarehouse.getWarehouseName());
|
|
|
+ warehouseVo.setProject(existingWarehouse.getProject());
|
|
|
+ warehouseVo.setCreateTime(existingWarehouse.getCreateTime());
|
|
|
+ warehouseVo.setCreateUserId(existingWarehouse.getCreateUserId());
|
|
|
+ warehouseVo.setUpdateTime(existingWarehouse.getUpdateTime());
|
|
|
+ warehouseVo.setUpdateUserId(existingWarehouse.getUpdateUserId());
|
|
|
+ warehouseVo.setDeleteFlag(existingWarehouse.getDeleteFlag());
|
|
|
+ warehouseVo.setParentId(existingWarehouse.getParentId());
|
|
|
+ warehouseVo.setLongitude(existingWarehouse.getLongitude());
|
|
|
+ warehouseVo.setLatitude(existingWarehouse.getLatitude());
|
|
|
+ warehouseVo.setCountryServeId(existingWarehouse.getCountryServeId());
|
|
|
+ warehouseVo.setDetailedAddress(existingWarehouse.getDetailedAddress());
|
|
|
+ warehouseVo.setStatus(existingWarehouse.getStatus());
|
|
|
+ // 其他字段映射...
|
|
|
+
|
|
|
+ // 返回成功的响应
|
|
|
+ return new RPCBaseResponse<>(200, "更新成功", warehouseVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 复制仓库属性的方法
|
|
|
+ private void copyWarehouseProperties(ShopWarehouseSaveDto source, ShopWarehouse target) {
|
|
|
+ target.setParentId(source.getParentId());
|
|
|
+ target.setWarehouseName(source.getWarehouseName());
|
|
|
+ target.setLongitude(source.getLongitude());
|
|
|
+ target.setLatitude(source.getLatitude());
|
|
|
+ target.setCountryServeId(source.getCountryServeId());
|
|
|
+ target.setDetailedAddress(source.getDetailedAddress());
|
|
|
+ target.setStatus(source.getStatus());
|
|
|
+ if (source.getProject() != null) {
|
|
|
+ target.setProject(source.getProject());
|
|
|
+ }
|
|
|
+ // 其他需要设置的字段...
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当前用户 ID 的方法
|
|
|
+ private Long getCurrentUserId() {
|
|
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ Object principal = authentication.getPrincipal();
|
|
|
+ Long userId = null;
|
|
|
+
|
|
|
+ if (!(principal instanceof String && "anonymousUser".equals(principal))) {
|
|
|
+ String jsonString = JSON.toJSONString(principal);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(jsonString);
|
|
|
+ userId = jsonObject.getLong("id");
|
|
|
+ }
|
|
|
+
|
|
|
+ return userId;
|
|
|
+ }
|
|
|
+ // 保存员工角色信息的方法
|
|
|
+ private void saveStaffRoles(List<ShopWarehouseStaffDto> staffList, Long warehouseId, Long currentUserId) {
|
|
|
+ for (ShopWarehouseStaffDto staffDto : staffList) {
|
|
|
+ ShopWarehouseStaff staff = new ShopWarehouseStaff();
|
|
|
+ staff.setWarehouseId(warehouseId);
|
|
|
+ staff.setUserId(staffDto.getUserId());
|
|
|
+ staff.setRoleId(staffDto.getRoleId());
|
|
|
+ staff.setCreateUserId(String.valueOf(currentUserId)); // 设置创建用户 ID
|
|
|
+ shopWarehouseStaffMapper.insert(staff);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|