Przeglądaj źródła

服务国家树形结构展示,测试通过上传

Sakana 2 tygodni temu
rodzic
commit
596d3f5c25

+ 1 - 1
edu-travel-remote/edu-travel-remote-country/src/main/java/edu/travel/dto/BaseCountryDto.java

@@ -42,7 +42,7 @@ public class BaseCountryDto extends PagePO {
     /**
      * 国家区号
      */
-    private Integer areaCode;
+    private String areaCode;
     /**
      * 创建时间
      */

+ 13 - 0
edu-travel-remote/edu-travel-remote-commodity/src/main/java/edu/travel/dto/BaseCountryServeDto.java → edu-travel-remote/edu-travel-remote-country/src/main/java/edu/travel/dto/BaseCountryServeDto.java

@@ -1,11 +1,13 @@
 package edu.travel.dto;
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import edu.travel.po.PagePO;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import java.math.BigDecimal;
+import java.util.Date;
 
 /**
  * 服务国家表
@@ -64,4 +66,15 @@ public class BaseCountryServeDto extends PagePO {
      * 货币表id
      */
     private Long currencyId;
+    /**
+     * 创建时间
+     */
+    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date createTime;
+
+    /**
+     * 更新时间
+     */
+    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date updateTime;
 }

+ 0 - 3
edu-travel-remote/edu-travel-remote-commodity/src/main/java/edu/travel/remote/BaseCountryServeRemoteController.java → edu-travel-remote/edu-travel-remote-country/src/main/java/edu/travel/remote/BaseCountryServeRemoteController.java

@@ -2,11 +2,8 @@ package edu.travel.remote;
 
 import edu.travel.dto.BaseCountryServeDto;
 import edu.travel.remote.base.RemoteBaseController;
-import edu.travel.rpc.RPCBaseResponse;
 import edu.travel.vo.BaseCountryServeVo;
 import org.springframework.cloud.openfeign.FeignClient;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestBody;
 
 @FeignClient(path = "/baseCountryServe",name = "commodity")
 public interface BaseCountryServeRemoteController extends RemoteBaseController<BaseCountryServeVo, BaseCountryServeDto> {

+ 8 - 0
edu-travel-remote/edu-travel-remote-commodity/src/main/java/edu/travel/vo/BaseCountryServeVo.java → edu-travel-remote/edu-travel-remote-country/src/main/java/edu/travel/vo/BaseCountryServeVo.java

@@ -67,6 +67,14 @@ public class BaseCountryServeVo extends BaseEntity {
      */
     private Long currencyId;
     /**
+     * 图片url
+     */
+    private String imageUrl;
+    /**
+     * 区号
+     */
+    private String areaCode;
+    /**
      * 子类
      */
     private List<BaseCountryServeVo> children = new ArrayList<>();

+ 1 - 1
edu-travel-remote/edu-travel-remote-country/src/main/java/edu/travel/vo/BaseCountryVo.java

@@ -44,7 +44,7 @@ public class BaseCountryVo extends BaseEntity {
     /**
      * 国家区号
      */
-    private Integer areaCode;
+    private String areaCode;
     /**
      * 子类
      */

+ 1 - 1
edu-travel-service/edu-travel-service-commodity/src/main/java/edu/travel/commodity/entity/BaseCountryServe.java → edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/entity/BaseCountryServe.java

@@ -1,4 +1,4 @@
-package edu.travel.commodity.entity;
+package edu.travel.country.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;

+ 3 - 2
edu-travel-service/edu-travel-service-commodity/src/main/java/edu/travel/commodity/mapper/BaseCountryServeMapper.java → edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/mapper/BaseCountryServeMapper.java

@@ -1,7 +1,8 @@
-package edu.travel.commodity.mapper;
+package edu.travel.country.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import edu.travel.commodity.entity.BaseCountryServe;
+import edu.travel.country.entity.BaseCountryServe;
+
 
 public interface BaseCountryServeMapper extends BaseMapper<BaseCountryServe> {
 }

+ 7 - 3
edu-travel-service/edu-travel-service-commodity/src/main/java/edu/travel/commodity/service/BaseCountryServeService.java → edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/service/BaseCountryServeService.java

@@ -1,14 +1,18 @@
-package edu.travel.commodity.service;
+package edu.travel.country.service;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
-import edu.travel.commodity.entity.BaseCountryServe;
 import com.baomidou.mybatisplus.extension.service.IService;
+import edu.travel.country.entity.BaseCountryServe;
 import edu.travel.dto.BaseCountryServeDto;
 import edu.travel.rpc.RPCBaseResponse;
 import edu.travel.vo.BaseCountryServeVo;
 
-public interface BaseCountryServeService extends IService<BaseCountryServe>{
+import java.util.List;
 
+public interface BaseCountryServeService extends IService<BaseCountryServe>{
 
+    //分页
     RPCBaseResponse<IPage<BaseCountryServeVo>> getCountryServePage(BaseCountryServeDto dto);
+//服务国家树
+    RPCBaseResponse<List<BaseCountryServeVo>> getCountryServeTree();
 }

+ 107 - 0
edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/service/impl/BaseCountryServeServiceImpl.java

@@ -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;
+    }
+
+}

+ 1 - 1
edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/service/impl/BaseCountryServiceImpl.java

@@ -61,7 +61,7 @@ public class BaseCountryServiceImpl extends SysServiceImpl<BaseCountryMapper, Ba
             baseCountryVo.setCountryNameZh(baseCountry.getCountryNameZh());
             baseCountryVo.setCountryNameEn(baseCountry.getCountryNameEn());
             baseCountryVo.setCountryNameLocal(baseCountry.getCountryNameLocal());
-            baseCountryVo.setAreaCode(baseCountry.getAreaCode());
+            baseCountryVo.setAreaCode(String.valueOf(baseCountry.getAreaCode()));
             countryMap.put(baseCountry.getId(),baseCountryVo);
         }
 

+ 12 - 5
edu-travel-service/edu-travel-service-commodity/src/main/java/edu/travel/commodity/web/BaseCountryServeController.java → edu-travel-service/edu-travel-service-country/src/main/java/edu/travel/country/web/BaseCountryServeController.java

@@ -1,9 +1,9 @@
-package edu.travel.commodity.web;
+package edu.travel.country.web;
 
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
-import edu.travel.commodity.entity.BaseCountryServe;
-import edu.travel.commodity.service.BaseCountryServeService;
+import edu.travel.country.entity.BaseCountryServe;
+import edu.travel.country.service.BaseCountryServeService;
 import edu.travel.dto.BaseCountryServeDto;
 import edu.travel.remote.BaseCountryServeRemoteController;
 import edu.travel.rpc.RPCBaseResponse;
@@ -21,7 +21,7 @@ import java.util.List;
 * @author xxxxx
 */
 @RestController
-@RequestMapping("/base_country_serve")
+@RequestMapping("/baseCountryServe")
 public class BaseCountryServeController  extends BaseController<BaseCountryServe> implements BaseCountryServeRemoteController {
 
     /**
@@ -41,7 +41,14 @@ public class BaseCountryServeController  extends BaseController<BaseCountryServe
     public RPCBaseResponse<IPage<BaseCountryServeVo>> getCountryServePage(BaseCountryServeDto dto) {
         return baseCountryServeService.getCountryServePage(dto);
     }
-
+/**
+ * 服务国家树
+ */
+    @GetMapping("/getCountryServeTree")
+ public RPCBaseResponse<List<BaseCountryServeVo>> getCountryServeTree(){
+        System.out.println("获取服务国家树");
+        return baseCountryServeService.getCountryServeTree();
+    }
     /**
      * 获取服务国家信息
      * @param id

+ 2 - 3
edu-travel-service/edu-travel-service-commodity/src/main/resources/mapper/BaseCountryServeMapper.xml → edu-travel-service/edu-travel-service-country/src/main/resources/mapper/BaseCountryServeMapper.xml

@@ -1,7 +1,6 @@
-<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="edu.travel.commodity.mapper.BaseCountryServeMapper">
-  <resultMap id="BaseResultMap" type="edu.travel.commodity.entity.BaseCountryServe">
+<mapper namespace="edu.travel.country.mapper.BaseCountryServeMapper">
+  <resultMap id="BaseResultMap" type="edu.travel.country.entity.BaseCountryServe">
     <!--@mbg.generated-->
     <!--@Table base_country_serve-->
     <id column="id" jdbcType="BIGINT" property="id" />