Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
chejia-smart-service
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 323f9748
authored
Mar 24, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
调整接口返回
1 parent
76618edb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
134 additions
and
69 deletions
src/main/java/com/glsx/smart/controller/ProductController.java
src/main/java/com/glsx/smart/dao/entity/SmartProduct.java
src/main/java/com/glsx/smart/dao/entity/SmartProductType.java
src/main/java/com/glsx/smart/service/SmartProductService.java
src/main/java/com/glsx/smart/service/impl/SmartProductServiceImpl.java
src/main/java/com/glsx/smart/controller/ProductController.java
View file @
323f974
package
com
.
glsx
.
smart
.
controller
;
import
java.util.List
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
Object
;
import
com.alibaba.fastjson.JSON
Array
;
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
.
getProduct
AndCategory
();
return
productService
.
getProduct
Tree
();
}
/**
...
...
src/main/java/com/glsx/smart/dao/entity/SmartProduct.java
View file @
323f974
...
...
@@ -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
;
...
...
src/main/java/com/glsx/smart/dao/entity/SmartProductType.java
View file @
323f974
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
;
}
src/main/java/com/glsx/smart/service/SmartProductService.java
View file @
323f974
package
com
.
glsx
.
smart
.
service
;
import
java.util.List
;
import
com.alibaba.fastjson.JSON
Object
;
import
com.alibaba.fastjson.JSON
Array
;
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
);
...
...
src/main/java/com/glsx/smart/service/impl/SmartProductServiceImpl.java
View file @
323f974
...
...
@@ -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
s
martS
ysCodeMapper
;
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
();
smartSysCod
e
.
setDictType
(
"smart_device"
);
List
<
SmartSysCode
>
list
=
smartSysCodeMapper
.
getList
(
smartSysCod
e
);
public
JSONArray
getProductAndCategory
()
{
SmartSysCode
example
=
new
SmartSysCode
();
exampl
e
.
setDictType
(
"smart_device"
);
List
<
SmartSysCode
>
categoryList
=
sysCodeMapper
.
getList
(
exampl
e
);
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"
,
mapArray
List
);
objectMap
.
add
(
map
);
firstObj
.
put
(
"list"
,
firstItem
List
);
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
;
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment