Commit 323f9748 by tyfanchz

调整接口返回

1 parent 76618edb
package com.glsx.smart.controller; package com.glsx.smart.controller;
import java.util.List;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder; import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct; import com.glsx.smart.dao.entity.SmartProduct;
...@@ -13,8 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -13,8 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/** /**
* <p>Description: 产品列表业务 * <p>Description: 产品列表业务
* <p>Project: chejia-smart * <p>Project: chejia-smart
...@@ -84,9 +83,9 @@ public class ProductController { ...@@ -84,9 +83,9 @@ public class ProductController {
*/ */
@RequestMapping(value = "/getProductAndCategory", @RequestMapping(value = "/getProductAndCategory",
method = {RequestMethod.GET, RequestMethod.POST}) method = {RequestMethod.GET, RequestMethod.POST})
public String getProductAndCategory() { public JSONArray getProductAndCategory() {
log.info("$$$$ /product/getProductAndCategory..."); log.info("$$$$ /product/getProductAndCategory...");
return productService.getProductAndCategory(); return productService.getProductTree();
} }
/** /**
......
...@@ -6,7 +6,6 @@ import javax.persistence.Id; ...@@ -6,7 +6,6 @@ import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import java.util.Date; import java.util.Date;
import com.glsx.smart.pojo.BaseDTO; import com.glsx.smart.pojo.BaseDTO;
import lombok.Data; import lombok.Data;
...@@ -32,7 +31,7 @@ public class SmartProduct extends BaseDTO { ...@@ -32,7 +31,7 @@ public class SmartProduct extends BaseDTO {
private String iconUrl; private String iconUrl;
/** /**
* 图标 url * 二级分类 key
*/ */
@Transient @Transient
private String categoryKey; private String categoryKey;
......
package com.glsx.smart.dao.entity; package com.glsx.smart.dao.entity;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
...@@ -10,6 +7,8 @@ import javax.persistence.Table; ...@@ -10,6 +7,8 @@ import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
/** /**
* <p>Description: * <p>Description:
...@@ -27,6 +26,19 @@ import java.util.List; ...@@ -27,6 +26,19 @@ import java.util.List;
@Table(name = "smart_product_type") @Table(name = "smart_product_type")
public class SmartProductType extends BaseDTO { public class SmartProductType extends BaseDTO {
/** /**
* 内容列表
*/
@Transient
private List<SmartDeviceOrder> contentList;
/**
* 条件列表
*/
@Transient
private List<SmartScenceSupstatus> conditionList;
/**
* 标识ID * 标识ID
*/ */
@Id @Id
...@@ -72,20 +84,4 @@ public class SmartProductType extends BaseDTO { ...@@ -72,20 +84,4 @@ public class SmartProductType extends BaseDTO {
* 更新人 * 更新人
*/ */
private Long updateBy; private Long updateBy;
/**
* 图标 url
*/
@Transient
private String iconUrl;
/**
* 内容列表
*/
private List<SmartDeviceOrder> contentList;
/**
* 条件列表
*/
private List<SmartScenceSupstatus> conditionList;
} }
package com.glsx.smart.service; package com.glsx.smart.service;
import java.util.List; import java.util.List;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder; import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct; import com.glsx.smart.dao.entity.SmartProduct;
...@@ -23,7 +23,9 @@ public interface SmartProductService { ...@@ -23,7 +23,9 @@ public interface SmartProductService {
SmartProduct getById(Long id); SmartProduct getById(Long id);
String getProductAndCategory(); JSONArray getProductAndCategory();
JSONArray getProductTree();
Boolean save(SmartProduct product); Boolean save(SmartProduct product);
......
...@@ -7,7 +7,7 @@ import java.util.HashMap; ...@@ -7,7 +7,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder; import com.glsx.smart.dao.entity.SmartDeviceOrder;
...@@ -15,7 +15,6 @@ import com.glsx.smart.dao.entity.SmartProduct; ...@@ -15,7 +15,6 @@ import com.glsx.smart.dao.entity.SmartProduct;
import com.glsx.smart.dao.entity.SmartSysCode; import com.glsx.smart.dao.entity.SmartSysCode;
import com.glsx.smart.dao.mapper.SmartDeviceOrderMapper; import com.glsx.smart.dao.mapper.SmartDeviceOrderMapper;
import com.glsx.smart.dao.mapper.SmartProductMapper; import com.glsx.smart.dao.mapper.SmartProductMapper;
import com.glsx.smart.dao.mapper.SmartProductTypeMapper;
import com.glsx.smart.dao.mapper.SmartSysCodeMapper; import com.glsx.smart.dao.mapper.SmartSysCodeMapper;
import com.glsx.smart.exception.BusinessException; import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum; import com.glsx.smart.exception.ResultEnum;
...@@ -46,7 +45,7 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct ...@@ -46,7 +45,7 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
private SmartDeviceOrderMapper deviceOrderMapper; private SmartDeviceOrderMapper deviceOrderMapper;
@Autowired @Autowired
private SmartSysCodeMapper smartSysCodeMapper; private SmartSysCodeMapper sysCodeMapper;
@Autowired @Autowired
private DjApiInvoker djApiInvoker; private DjApiInvoker djApiInvoker;
...@@ -90,52 +89,74 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct ...@@ -90,52 +89,74 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
} }
/** /**
* 获取结构化的产品数据。 * <p>获取树状的产品数据。
* 数据格式为:
* {
* "车智能": ["产品类型1-1", "产品类型1-2"],
* "产品类型1-1": ["产品名称1", "产品名称2"],
*
* "家智能": ["产品类型2-1"],
* "产品类型2-1": ["产品名称3", "产品名称4", "产品名称5"]
* }
* *
* @return 结果 * @return 结果
*/ */
@Override @Override
public String getProductAndCategory() { public JSONArray getProductAndCategory() {
SmartSysCode smartSysCode=new SmartSysCode(); SmartSysCode example = new SmartSysCode();
smartSysCode.setDictType("smart_device"); example.setDictType("smart_device");
List<SmartSysCode> list = smartSysCodeMapper.getList(smartSysCode); List<SmartSysCode> categoryList = sysCodeMapper.getList(example);
List<SmartProduct> allProductList = productMapper.getAllProductList(); List<SmartProduct> allProductList = productMapper.getAllProductList();
List<Map<String,Object>> objectMap=new ArrayList<>(); JSONArray resultList = new JSONArray();
if(CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(categoryList)) {
List<SmartSysCode> firstlist = list.stream().filter(index -> index.getLevel() == 0).collect(Collectors.toList()); // 筛选一级分类
List<SmartSysCode> twolist = list.stream().filter(index -> index.getLevel() == 1).collect(Collectors.toList()); List<SmartSysCode> firstCategoryList = categoryList.stream()
firstlist.forEach(index->{ .filter(index -> index.getLevel() == 0).collect(Collectors.toList());
Map<String,Object> map=new HashMap<>(); // 筛选二级分类
map.put("title",index.getDictValue()); List<SmartSysCode> secondCategoryList = categoryList.stream()
List<Map<String,Object>> mapArrayList=new ArrayList<>(); .filter(index -> index.getLevel() == 1).collect(Collectors.toList());
List<SmartSysCode> collect = twolist.stream().filter(inx -> inx.getParentKey().equals(index.getDictKey())).collect(Collectors.toList()); // 一级分类数据
collect.forEach(in->{ firstCategoryList.forEach(firstCategory -> {
Map<String,Object> stringObjectMap=new HashMap<>(); JSONObject firstObj = new JSONObject();
stringObjectMap.put("title",in.getDictValue()); firstObj.put("title", firstCategory.getDictValue());
List<SmartProduct> smartProducts = allProductList.stream().filter(pro -> pro.getCategoryKey().equals(in.getDictKey())).collect(Collectors.toList()); List<JSONObject> firstItemList = new ArrayList<>();
List<Map<String, Object>> converlist = smartProducts.stream().map(sma -> { // 获取此分类下的子分类
Map<String, Object> converMap = new HashMap<>(); List<SmartSysCode> subCategoryList = secondCategoryList.stream()
converMap.put("name", sma.getProductName()); .filter(inx -> inx.getParentKey().equals(firstCategory.getDictKey()))
converMap.put("icon", sma.getIconUrl()); .collect(Collectors.toList());
converMap.put("typeId", sma.getTypeId()); // 子分类数据
return converMap; subCategoryList.forEach(subCategory -> {
}).collect(Collectors.toList()); JSONObject subObj = new JSONObject();
stringObjectMap.put("list", converlist); subObj.put("title", subCategory.getDictValue());
mapArrayList.add(stringObjectMap); // 获取此分类下的产品
List<SmartProduct> productList = allProductList.stream()
.filter(pro -> pro.getCategoryKey().equals(subCategory.getDictKey()))
.collect(Collectors.toList());
// 映射产品信息
List<JSONObject> productInfoList = productList.stream()
.map(product -> {
JSONObject productObj = new JSONObject();
productObj.put("name", product.getProductName());
productObj.put("iconUrl", product.getIconUrl());
productObj.put("typeId", product.getTypeId());
productObj.put("shopProductId", product.getShopProductId());
return productObj;
}).collect(Collectors.toList());
subObj.put("list", productInfoList);
firstItemList.add(subObj);
}); });
map.put("list",mapArrayList); firstObj.put("list", firstItemList);
objectMap.add(map); resultList.add(firstObj);
}); });
} }
return JSONObject.toJSONString(objectMap); return resultList;
}
/**
* <p>获取树状的产品数据。
*
* @return 结果
*/
@Override
public JSONArray getProductTree() {
SmartSysCode example = new SmartSysCode();
example.setDictType("smart_device");
example.setLevel((byte) 0);
List<SmartSysCode> sysCodeList = sysCodeMapper.getTree(example);
List<SmartProduct> allProductList = productMapper.getAllProductList();
return this.matchProductAndCategory(sysCodeList, allProductList);
} }
/** /**
...@@ -219,4 +240,52 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct ...@@ -219,4 +240,52 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
success = productMapper.updateByPrimaryKeySelective(product) > 0; success = productMapper.updateByPrimaryKeySelective(product) > 0;
return success; return success;
} }
/**
* 将产品和分类匹配成树状数据结构。
*
* @param sysCodeList 分类
* @param productList 产品
* @return 匹配完成的树状结构数据
*/
private JSONArray matchProductAndCategory(List<SmartSysCode> sysCodeList,
List<SmartProduct> productList) {
JSONArray parentArray = new JSONArray();
for (SmartSysCode sysCode : sysCodeList) {
List<SmartSysCode> subCodeList = sysCode.getSubCodeList();
// 判断是否有下级分类
if (CollectionUtils.isNotEmpty(subCodeList)) {
// 如果还有下级分类:
// 将本级分类的分类名存储,并递归查找到的下级分类存储到 list 中
JSONObject subItem = new JSONObject();
subItem.put("title", sysCode.getDictValue());
subItem.put("list", this.matchProductAndCategory(subCodeList, productList));
parentArray.add(subItem);
} else {
// 如果没有下级分类:
// 将产品和分类匹配,筛选匹配到的产品
List<SmartProduct> matchedProductList = productList.stream()
.filter(product -> product.getCategoryKey().equals(sysCode.getDictKey()))
.collect(Collectors.toList());
// 存储必要产品信息到对象中
JSONArray productInfoArray = new JSONArray();
for (SmartProduct product : matchedProductList) {
JSONObject productInfo = new JSONObject();
//productInfo.put("id", product.getId());
productInfo.put("name", product.getProductName());
productInfo.put("icon", product.getIconUrl());
productInfo.put("typeId", product.getTypeId());
productInfo.put("shopProductId", product.getShopProductId());
productInfoArray.add(productInfo);
}
// 将本级分类名和匹配到的产品类型放到 list 中
JSONObject subItem = new JSONObject();
subItem.put("title", sysCode.getDictValue());
subItem.put("list", productInfoArray);
parentArray.add(subItem);
}
}
return parentArray;
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!