|
@@ -0,0 +1,107 @@
|
|
|
+package edu.travel.country.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+
|
|
|
+import edu.travel.country.entity.BaseCountry;
|
|
|
+import edu.travel.country.entity.BaseCountryServe;
|
|
|
+import edu.travel.country.mapper.BaseCountryMapper;
|
|
|
+import edu.travel.country.mapper.BaseCountryServeMapper;
|
|
|
+import edu.travel.country.service.BaseCountryServeService;
|
|
|
+import edu.travel.dto.BaseCountryServeDto;
|
|
|
+import edu.travel.rpc.RPCBaseResponse;
|
|
|
+import edu.travel.service.SysServiceImpl;
|
|
|
+import edu.travel.vo.BaseCountryServeVo;
|
|
|
+
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class BaseCountryServeServiceImpl extends SysServiceImpl<BaseCountryServeMapper, BaseCountryServe> implements BaseCountryServeService {
|
|
|
+ @Autowired
|
|
|
+ private BaseCountryMapper baseCountryMapper;
|
|
|
+ //分页
|
|
|
+ @Override
|
|
|
+ public RPCBaseResponse<IPage<BaseCountryServeVo>> getCountryServePage(BaseCountryServeDto dto) {
|
|
|
+ Page<BaseCountryServe> baseCountryServePage = new Page<>(dto.getCurrentPage(), dto.getPageSize());
|
|
|
+ IPage<BaseCountryServe> pageLink = super.getPageLink(new LambdaQueryWrapper<BaseCountryServe>(), baseCountryServePage);
|
|
|
+ IPage<BaseCountryServeVo> result = new Page<>();
|
|
|
+ BeanUtils.copyProperties(pageLink, result);
|
|
|
+ return new RPCBaseResponse<>(200,"SUCCESS",result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 服务国家树
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public RPCBaseResponse<List<BaseCountryServeVo>> getCountryServeTree() {
|
|
|
+ // 查询所有国家服务数据
|
|
|
+ LambdaQueryWrapper<BaseCountryServe> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ List<BaseCountryServe> baseCountryServeList = super.getListLink(queryWrapper);
|
|
|
+
|
|
|
+
|
|
|
+ // 构建树形结构
|
|
|
+ List<BaseCountryServeVo> baseCountryServeTree = buildCountryServeTree(baseCountryServeList);
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ return new RPCBaseResponse<>(200, "SUCCESS", baseCountryServeTree);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 树构建方法
|
|
|
+ private List<BaseCountryServeVo> buildCountryServeTree(List<BaseCountryServe> baseCountryServeList) {
|
|
|
+ Map<Long, BaseCountryServeVo> serveMap = new HashMap<>();
|
|
|
+ List<BaseCountryServeVo> rootCountries = new ArrayList<>();
|
|
|
+
|
|
|
+ // 构建每个国家服务VO对象
|
|
|
+ for (BaseCountryServe serve : baseCountryServeList) {
|
|
|
+ BaseCountryServeVo serveVo = new BaseCountryServeVo();
|
|
|
+ BeanUtils.copyProperties(serve, serveVo);
|
|
|
+
|
|
|
+ // 关联国家信息
|
|
|
+ BaseCountry country = baseCountryMapper.selectById(serve.getCountryId());
|
|
|
+ if (country != null) {
|
|
|
+ serveVo.setImageUrl(country.getImageUrl());
|
|
|
+ serveVo.setAreaCode(String.valueOf(country.getAreaCode()));
|
|
|
+ }
|
|
|
+
|
|
|
+ serveMap.put(serve.getId(), serveVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建树形结构
|
|
|
+ for (BaseCountryServe serve : baseCountryServeList) {
|
|
|
+ Long parentId = serve.getParentId();
|
|
|
+ BaseCountryServeVo serveVo = serveMap.get(serve.getId());
|
|
|
+
|
|
|
+ if (serveVo == null) {
|
|
|
+ System.err.println("服务对象为 null,服务ID: " + serve.getId());
|
|
|
+ continue; // 跳过这个服务
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentId == null || parentId == 0) {
|
|
|
+ rootCountries.add(serveVo);
|
|
|
+ } else {
|
|
|
+ BaseCountryServeVo parentServeVo = serveMap.get(parentId);
|
|
|
+ if (parentServeVo != null) {
|
|
|
+ // 确保 children 列表被初始化
|
|
|
+ if (parentServeVo.getChildren() == null) {
|
|
|
+ parentServeVo.setChildren(new ArrayList<>());
|
|
|
+ }
|
|
|
+ parentServeVo.getChildren().add(serveVo);
|
|
|
+ } else {
|
|
|
+ // 日志记录未找到父节点
|
|
|
+ System.err.println("未找到父节点,parentId: " + parentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return rootCountries;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|