|
@@ -1,39 +1,55 @@
|
|
|
package edu.travel.commodity.web;
|
|
|
|
|
|
+import edu.travel.adapter.service.country.CountryAdapter;
|
|
|
+import edu.travel.adapter.service.upload.UploadAdapter;
|
|
|
import edu.travel.commodity.entity.ShopCategory;
|
|
|
import edu.travel.commodity.service.ShopCategoryService;
|
|
|
+import edu.travel.remote.commodity.ShopCategoryRemoteController;
|
|
|
import edu.travel.remote.dto.BaseDto;
|
|
|
+import edu.travel.remote.dto.ShopCategoryDto;
|
|
|
import edu.travel.remote.dto.ShopTypeDto;
|
|
|
import edu.travel.remote.feign.mode.vo.tenant.AddMenuVo;
|
|
|
+import edu.travel.remote.vo.ShopCategoryVo;
|
|
|
+import edu.travel.remote.vo.ShopTypeVo;
|
|
|
import edu.travel.resp.BaseResponse;
|
|
|
import edu.travel.rpc.RPCBaseResponse;
|
|
|
-import edu.travel.remote.vo.ShopTypeVo;
|
|
|
+import edu.travel.vo.BaseCountryServeVo;
|
|
|
+import edu.travel.vo.BaseCountryVo;
|
|
|
import edu.travel.web.BaseController;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
-
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
import static edu.travel.rpc.RPCBaseResponse.success;
|
|
|
|
|
|
/**
|
|
|
-* 商品类型(shop_category)表控制层
|
|
|
-*
|
|
|
-* @author xxxxx
|
|
|
-*/
|
|
|
+ * 商品类型(shop_category)表控制层
|
|
|
+ *
|
|
|
+ * @author xxxxx
|
|
|
+ */
|
|
|
@RestController
|
|
|
@RequestMapping("/shopCategory")
|
|
|
-public class ShopCategoryController extends BaseController<ShopCategory> {
|
|
|
-/**
|
|
|
-* 服务对象
|
|
|
-*/
|
|
|
+public class ShopCategoryController extends BaseController<ShopCategory> implements ShopCategoryRemoteController {
|
|
|
+ /**
|
|
|
+ * 服务对象
|
|
|
+ */
|
|
|
@Autowired
|
|
|
private ShopCategoryService shopCategoryService;
|
|
|
+ @Autowired
|
|
|
+ private UploadAdapter uploadAdapter;
|
|
|
+ @Autowired
|
|
|
+ private CountryAdapter countryAdapter;
|
|
|
|
|
|
/**
|
|
|
- * 获取商品类型
|
|
|
+ * 获取商品类型
|
|
|
+ *
|
|
|
* @return {@link BaseResponse }<{@link AddMenuVo }>
|
|
|
*/
|
|
|
@GetMapping("/getShopType")
|
|
@@ -42,7 +58,8 @@ public class ShopCategoryController extends BaseController<ShopCategory> {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取二级热门商品类型
|
|
|
+ * 获取二级热门商品类型
|
|
|
+ *
|
|
|
* @return {@link BaseResponse }<{@link AddMenuVo }>
|
|
|
*/
|
|
|
@GetMapping("/getLevelToShopType")
|
|
@@ -50,4 +67,152 @@ public class ShopCategoryController extends BaseController<ShopCategory> {
|
|
|
return success(shopCategoryService.getLevelToShopType(param));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * icon上传
|
|
|
+ */
|
|
|
+ @PostMapping("/uploadIcon")
|
|
|
+ public RPCBaseResponse<String> uploadIcon(MultipartFile file) {
|
|
|
+ return shopCategoryService.uploadIcon(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过id获取商品类型
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @GetMapping("/getFormId")
|
|
|
+ public RPCBaseResponse<ShopCategoryVo> getFormId(String id) {
|
|
|
+ RPCBaseResponse<ShopCategory> shopCurrencyRPCBaseResponse = super.getId(id);
|
|
|
+ RPCBaseResponse<ShopCategoryVo> shopCurrencyVoRPCBaseResponse = new RPCBaseResponse<>();
|
|
|
+ BeanUtils.copyProperties(shopCurrencyRPCBaseResponse, shopCurrencyVoRPCBaseResponse);
|
|
|
+ return shopCurrencyVoRPCBaseResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过id更新商品类型
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @PostMapping("/updateTargetFormId")
|
|
|
+ public RPCBaseResponse<ShopCategoryVo> updateTargetFormId(ShopCategoryDto entity) {
|
|
|
+ ShopCategory shopCurrency = new ShopCategory();
|
|
|
+ BeanUtils.copyProperties(entity, shopCurrency);
|
|
|
+ //调用接口获取地址,填入ImageUrl
|
|
|
+ // 检查 iconId 是否为空,
|
|
|
+ if (entity.getIconId() != null) {
|
|
|
+ shopCurrency.setImageUrl(uploadAdapter.getFormId(entity.getIconId()).toString());
|
|
|
+ }
|
|
|
+ RPCBaseResponse<ShopCategory> shopCurrencyRPCBaseResponse = super.updateTargetById(shopCurrency);
|
|
|
+ RPCBaseResponse<ShopCategoryVo> shopCurrencyVoRPCBaseResponse = new RPCBaseResponse<>();
|
|
|
+ BeanUtils.copyProperties(shopCurrencyRPCBaseResponse, shopCurrencyVoRPCBaseResponse);
|
|
|
+ return shopCurrencyVoRPCBaseResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增商品类型
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @PostMapping("/saveFormTarget")
|
|
|
+ public RPCBaseResponse<ShopCategoryVo> saveFormTarget(@RequestBody ShopCategoryDto entity) {
|
|
|
+ ShopCategory shopCurrency = new ShopCategory();
|
|
|
+ BeanUtils.copyProperties(entity, shopCurrency);
|
|
|
+ //调用接口获取地址,填入ImageUrl
|
|
|
+ // 检查 iconId 是否为空,避免 NullPointerException
|
|
|
+ if (entity.getIconId() != null) {
|
|
|
+ shopCurrency.setImageUrl(uploadAdapter.getFormId(entity.getIconId()).toString());
|
|
|
+ } else {
|
|
|
+ shopCurrency.setImageUrl(null);
|
|
|
+ }
|
|
|
+ RPCBaseResponse<ShopCategory> shopCurrencyRPCBaseResponse = super.saveTarget(shopCurrency);
|
|
|
+ RPCBaseResponse<ShopCategoryVo> shopCurrencyVoRPCBaseResponse = new RPCBaseResponse<>();
|
|
|
+ BeanUtils.copyProperties(shopCurrencyRPCBaseResponse, shopCurrencyVoRPCBaseResponse);
|
|
|
+ return shopCurrencyVoRPCBaseResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除商品类型
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @PostMapping("/deleteTargetFormId")
|
|
|
+ public RPCBaseResponse<ShopCategoryVo> deleteTargetFormId(List<String> ids) {
|
|
|
+ RPCBaseResponse<ShopCategory> shopCurrencyRPCBaseResponse = super.deleteTargetById(ids);
|
|
|
+ RPCBaseResponse<ShopCategoryVo> shopCurrencyVoRPCBaseResponse = new RPCBaseResponse<>();
|
|
|
+ BeanUtils.copyProperties(shopCurrencyRPCBaseResponse, shopCurrencyVoRPCBaseResponse);
|
|
|
+ return shopCurrencyVoRPCBaseResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有商品类型(树形结构)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @GetMapping("/getAllForm")
|
|
|
+ public RPCBaseResponse<List<ShopCategoryVo>> getAllForm() {
|
|
|
+ RPCBaseResponse<List<ShopCategory>> shopCurrencyRPCBaseResponse = super.listAll();
|
|
|
+
|
|
|
+ // 创建一个用于存储每个节点的映射
|
|
|
+ Map<Long, ShopCategoryVo> categoryMap = new HashMap<>();
|
|
|
+ List<ShopCategoryVo> result = new ArrayList<>();
|
|
|
+ // 创建一个 Map,用于存储 countryId 对应的数据
|
|
|
+ Map<String, BaseCountryVo> countryDataMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 将所有 ShopCategory 转换为 ShopCategoryVo,并存储在 map 中
|
|
|
+ if (shopCurrencyRPCBaseResponse.getData() != null) {
|
|
|
+ for (ShopCategory category : shopCurrencyRPCBaseResponse.getData()) {
|
|
|
+ ShopCategoryVo categoryVo = new ShopCategoryVo();
|
|
|
+ BeanUtils.copyProperties(category, categoryVo);
|
|
|
+ categoryMap.put(category.getId(), categoryVo);
|
|
|
+
|
|
|
+ // 利用 countryId 查找相关数据并存储到 countryDataMap 中
|
|
|
+ if (category.getCountryId() != null) {
|
|
|
+ // 假设 countryAdapter.getFormId 是 RPCBaseResponse<BaseCountryServeVo> 类型
|
|
|
+ RPCBaseResponse<BaseCountryServeVo> countryResponse = countryAdapter.getFormId(category.getCountryId());
|
|
|
+
|
|
|
+ // 确保 countryResponse 不为 null,并提取数据
|
|
|
+ if (countryResponse != null && countryResponse.getData() != null) {
|
|
|
+ BaseCountryServeVo serveVo = countryResponse.getData();
|
|
|
+ BaseCountryVo countryData = convertToBaseCountryVo(serveVo); // 需要实现此方法进行转换
|
|
|
+ countryDataMap.put(category.getCountryId(), countryData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建树结构
|
|
|
+ for (ShopCategoryVo categoryVo : categoryMap.values()) {
|
|
|
+ if (categoryVo.getParentId() == null || categoryVo.getParentId() == 0) {
|
|
|
+ result.add(categoryVo);
|
|
|
+ } else {
|
|
|
+ ShopCategoryVo parent = categoryMap.get(categoryVo.getParentId());
|
|
|
+ if (parent != null) {
|
|
|
+ if (parent.getChildren() == null) {
|
|
|
+ parent.setChildren(new ArrayList<>());
|
|
|
+ }
|
|
|
+ parent.getChildren().add(categoryVo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有对应的 countryData,将其添加到 categoryVo 的 map 属性中
|
|
|
+ if (countryDataMap.containsKey(categoryVo.getCountryId())) {
|
|
|
+ Map<String, Object> countryDataMapEntry = new HashMap<>();
|
|
|
+ countryDataMapEntry.put("countryData", countryDataMap.get(categoryVo.getCountryId()));
|
|
|
+ categoryVo.setMap(countryDataMapEntry);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 创建返回的 RPCBaseResponse
|
|
|
+ RPCBaseResponse<List<ShopCategoryVo>> shopCurrencyVoRPCBaseResponse = new RPCBaseResponse<>();
|
|
|
+ BeanUtils.copyProperties(shopCurrencyRPCBaseResponse, shopCurrencyVoRPCBaseResponse);
|
|
|
+ shopCurrencyVoRPCBaseResponse.setData(result); // 设置树形结构的根节点
|
|
|
+
|
|
|
+ return shopCurrencyVoRPCBaseResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 实现 BaseCountryServeVo 到 BaseCountryVo 的转换方法
|
|
|
+ private BaseCountryVo convertToBaseCountryVo(BaseCountryServeVo serveVo) {
|
|
|
+ BaseCountryVo countryVo = new BaseCountryVo();
|
|
|
+ // 假设 serveVo 有与 countryVo 对应的字段
|
|
|
+ countryVo.setId(serveVo.getId());
|
|
|
+ countryVo.setCountryNameZh(serveVo.getCountryNameZh());
|
|
|
+ countryVo.setCountryNameEn(serveVo.getCountryNameEn());
|
|
|
+ countryVo.setAreaCode(serveVo.getAreaCode());
|
|
|
+ return countryVo;
|
|
|
+ }
|
|
|
+
|
|
|
}
|