Commit 323f9748 by tyfanchz

调整接口返回

1 parent 76618edb
package com.glsx.smart.controller;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct;
......@@ -13,8 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>Description: 产品列表业务
* <p>Project: chejia-smart
......@@ -84,9 +83,9 @@ public class ProductController {
*/
@RequestMapping(value = "/getProductAndCategory",
method = {RequestMethod.GET, RequestMethod.POST})
public String getProductAndCategory() {
public JSONArray getProductAndCategory() {
log.info("$$$$ /product/getProductAndCategory...");
return productService.getProductAndCategory();
return productService.getProductTree();
}
/**
......
......@@ -6,7 +6,6 @@ import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
......@@ -32,7 +31,7 @@ public class SmartProduct extends BaseDTO {
private String iconUrl;
/**
* 图标 url
* 二级分类 key
*/
@Transient
private String categoryKey;
......
package com.glsx.smart.dao.entity;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
......@@ -10,6 +7,8 @@ import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
/**
* <p>Description:
......@@ -27,6 +26,19 @@ import java.util.List;
@Table(name = "smart_product_type")
public class SmartProductType extends BaseDTO {
/**
* 内容列表
*/
@Transient
private List<SmartDeviceOrder> contentList;
/**
* 条件列表
*/
@Transient
private List<SmartScenceSupstatus> conditionList;
/**
* 标识ID
*/
@Id
......@@ -72,20 +84,4 @@ public class SmartProductType extends BaseDTO {
* 更新人
*/
private Long updateBy;
/**
* 图标 url
*/
@Transient
private String iconUrl;
/**
* 内容列表
*/
private List<SmartDeviceOrder> contentList;
/**
* 条件列表
*/
private List<SmartScenceSupstatus> conditionList;
}
package com.glsx.smart.service;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct;
......@@ -23,7 +23,9 @@ public interface SmartProductService {
SmartProduct getById(Long id);
String getProductAndCategory();
JSONArray getProductAndCategory();
JSONArray getProductTree();
Boolean save(SmartProduct product);
......
......@@ -7,7 +7,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder;
......@@ -15,7 +15,6 @@ import com.glsx.smart.dao.entity.SmartProduct;
import com.glsx.smart.dao.entity.SmartSysCode;
import com.glsx.smart.dao.mapper.SmartDeviceOrderMapper;
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.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
......@@ -46,7 +45,7 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
private SmartDeviceOrderMapper deviceOrderMapper;
@Autowired
private SmartSysCodeMapper smartSysCodeMapper;
private SmartSysCodeMapper sysCodeMapper;
@Autowired
private DjApiInvoker djApiInvoker;
......@@ -90,52 +89,74 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
}
/**
* 获取结构化的产品数据。
* 数据格式为:
* {
* "车智能": ["产品类型1-1", "产品类型1-2"],
* "产品类型1-1": ["产品名称1", "产品名称2"],
*
* "家智能": ["产品类型2-1"],
* "产品类型2-1": ["产品名称3", "产品名称4", "产品名称5"]
* }
* <p>获取树状的产品数据。
*
* @return 结果
*/
@Override
public String getProductAndCategory() {
SmartSysCode smartSysCode=new SmartSysCode();
smartSysCode.setDictType("smart_device");
List<SmartSysCode> list = smartSysCodeMapper.getList(smartSysCode);
public JSONArray getProductAndCategory() {
SmartSysCode example = new SmartSysCode();
example.setDictType("smart_device");
List<SmartSysCode> categoryList = sysCodeMapper.getList(example);
List<SmartProduct> allProductList = productMapper.getAllProductList();
List<Map<String,Object>> objectMap=new ArrayList<>();
if(CollectionUtils.isNotEmpty(list)) {
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());
firstlist.forEach(index->{
Map<String,Object> map=new HashMap<>();
map.put("title",index.getDictValue());
List<Map<String,Object>> mapArrayList=new ArrayList<>();
List<SmartSysCode> collect = twolist.stream().filter(inx -> inx.getParentKey().equals(index.getDictKey())).collect(Collectors.toList());
collect.forEach(in->{
Map<String,Object> stringObjectMap=new HashMap<>();
stringObjectMap.put("title",in.getDictValue());
List<SmartProduct> smartProducts = allProductList.stream().filter(pro -> pro.getCategoryKey().equals(in.getDictKey())).collect(Collectors.toList());
List<Map<String, Object>> converlist = smartProducts.stream().map(sma -> {
Map<String, Object> converMap = new HashMap<>();
converMap.put("name", sma.getProductName());
converMap.put("icon", sma.getIconUrl());
converMap.put("typeId", sma.getTypeId());
return converMap;
}).collect(Collectors.toList());
stringObjectMap.put("list", converlist);
mapArrayList.add(stringObjectMap);
JSONArray resultList = new JSONArray();
if (CollectionUtils.isNotEmpty(categoryList)) {
// 筛选一级分类
List<SmartSysCode> firstCategoryList = categoryList.stream()
.filter(index -> index.getLevel() == 0).collect(Collectors.toList());
// 筛选二级分类
List<SmartSysCode> secondCategoryList = categoryList.stream()
.filter(index -> index.getLevel() == 1).collect(Collectors.toList());
// 一级分类数据
firstCategoryList.forEach(firstCategory -> {
JSONObject firstObj = new JSONObject();
firstObj.put("title", firstCategory.getDictValue());
List<JSONObject> firstItemList = new ArrayList<>();
// 获取此分类下的子分类
List<SmartSysCode> subCategoryList = secondCategoryList.stream()
.filter(inx -> inx.getParentKey().equals(firstCategory.getDictKey()))
.collect(Collectors.toList());
// 子分类数据
subCategoryList.forEach(subCategory -> {
JSONObject subObj = new JSONObject();
subObj.put("title", subCategory.getDictValue());
// 获取此分类下的产品
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);
objectMap.add(map);
firstObj.put("list", firstItemList);
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
success = productMapper.updateByPrimaryKeySelective(product) > 0;
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!