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 faf4a49c
authored
Mar 18, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
APP端功能开发
1 parent
db9bae72
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
200 additions
and
45 deletions
src/main/java/com/glsx/smart/controller/ProductTypeController.java
src/main/java/com/glsx/smart/dao/mapper/SmartProductTypeMapper.java
src/main/java/com/glsx/smart/exception/GlobalExceptionHandler.java
src/main/java/com/glsx/smart/exception/ResultEnum.java
src/main/java/com/glsx/smart/filter/CorsFilter.java
src/main/java/com/glsx/smart/service/SmartProductTypeService.java
src/main/java/com/glsx/smart/service/impl/SmartProductServiceImpl.java
src/main/java/com/glsx/smart/service/impl/SmartProductTypeServiceImpl.java
src/main/resources/mapper/SmartProductTypeMapper.xml
src/main/java/com/glsx/smart/controller/ProductTypeController.java
View file @
faf4a49
...
@@ -2,6 +2,7 @@ package com.glsx.smart.controller;
...
@@ -2,6 +2,7 @@ package com.glsx.smart.controller;
import
java.util.List
;
import
java.util.List
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.pagehelper.PageInfo
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.exception.BusinessException
;
import
com.glsx.smart.exception.BusinessException
;
...
@@ -56,6 +57,17 @@ public class ProductTypeController {
...
@@ -56,6 +57,17 @@ public class ProductTypeController {
}
}
/**
/**
* 获取结构化的产品类型。
*
* @return 结果
*/
@RequestMapping
(
value
=
"/getTypeAndCategory"
,
method
=
{
RequestMethod
.
GET
,
RequestMethod
.
POST
})
public
JSONObject
getProductTypeTree
()
{
log
.
info
(
"$$$$ /productType/getTypeAndCategory..."
);
return
productTypeService
.
getTypeAndCategory
();
}
/**
* 保存产品类型。
* 保存产品类型。
*
*
* @param productType 产品类型
* @param productType 产品类型
...
...
src/main/java/com/glsx/smart/dao/mapper/SmartProductTypeMapper.java
View file @
faf4a49
...
@@ -2,6 +2,7 @@ package com.glsx.smart.dao.mapper;
...
@@ -2,6 +2,7 @@ package com.glsx.smart.dao.mapper;
import
java.util.List
;
import
java.util.List
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.dao.entity.SmartSysCode
;
import
org.apache.ibatis.annotations.Mapper
;
import
org.apache.ibatis.annotations.Mapper
;
/**
/**
...
@@ -15,4 +16,10 @@ import org.apache.ibatis.annotations.Mapper;
...
@@ -15,4 +16,10 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
@Mapper
public
interface
SmartProductTypeMapper
extends
tk
.
mybatis
.
mapper
.
common
.
Mapper
<
SmartProductType
>
{
public
interface
SmartProductTypeMapper
extends
tk
.
mybatis
.
mapper
.
common
.
Mapper
<
SmartProductType
>
{
List
<
SmartProductType
>
getList
(
SmartProductType
req
);
List
<
SmartProductType
>
getList
(
SmartProductType
req
);
int
countByProductTypeName
(
String
typeName
);
List
<
SmartSysCode
>
getTypeCategoryList
();
List
<
SmartProductType
>
getAllList
();
}
}
src/main/java/com/glsx/smart/exception/GlobalExceptionHandler.java
deleted
100644 → 0
View file @
db9bae7
package
com
.
glsx
.
smart
.
exception
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.validation.BindException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.ServletRequestBindingException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-16
* @deprecated 已废弃,需要抛出异常让上游服务感知
*/
@Slf4j
//@RestControllerAdvice
@Deprecated
public
class
GlobalExceptionHandler
{
@ExceptionHandler
({
BusinessException
.
class
})
public
String
handleBusinessException
(
BusinessException
e
)
{
log
.
error
(
"!!!! 业务异常"
,
e
);
return
e
.
getMessage
();
}
@ExceptionHandler
({
ServletRequestBindingException
.
class
,
MethodArgumentNotValidException
.
class
,
BindException
.
class
})
public
String
handleBindException
(
Exception
e
)
{
log
.
error
(
"!!!! 参数错误"
,
e
);
return
ResultEnum
.
PARAM_ERROR
.
getMessage
();
}
@ExceptionHandler
(
Exception
.
class
)
public
String
handleException
(
Exception
e
)
{
log
.
error
(
"!!!! 未知异常"
,
e
);
return
ResultEnum
.
ERROR
.
getMessage
();
}
}
src/main/java/com/glsx/smart/exception/ResultEnum.java
View file @
faf4a49
...
@@ -13,10 +13,12 @@ public enum ResultEnum {
...
@@ -13,10 +13,12 @@ public enum ResultEnum {
PARAM_ERROR
(
1001
,
"参数错误"
),
PARAM_ERROR
(
1001
,
"参数错误"
),
ID_NOT_FOUND
(
1002
,
"ID不存在"
),
ID_NOT_FOUND
(
1002
,
"ID不存在"
),
PRODUCT_NOT_FOUND
(
2001
,
"产品ID未找到"
),
PRODUCT_NOT_FOUND
(
2001
,
"产品ID未找到"
),
PRODUCT_NAME_DUPLICATE
(
2002
,
"产品名称重复"
),
PRODUCT_NAME_DUPLICATE
(
2002
,
"产品名称重复"
),
PRODUCT_TYPE_NOT_FOUND
(
3001
,
"产品类型ID未找到"
),
PRODUCT_TYPE_NOT_FOUND
(
3001
,
"产品类型ID未找到"
),
PRODUCT_TYPE_NAME_DUPLICATE
(
3002
,
"产品类型名称重复"
),
;
;
private
final
Integer
code
;
private
final
Integer
code
;
...
...
src/main/java/com/glsx/smart/filter/CorsFilter.java
0 → 100644
View file @
faf4a49
package
com
.
glsx
.
smart
.
filter
;
import
javax.servlet.Filter
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.util.Enumeration
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.stereotype.Component
;
/**
* <p>Description: 跨域配置
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-18
*/
@Component
public
class
CorsFilter
implements
Filter
{
@Override
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
HttpServletRequest
httpRequest
=
(
HttpServletRequest
)
request
;
HttpServletResponse
httpResponse
=
(
HttpServletResponse
)
response
;
StringBuffer
headersSb
=
new
StringBuffer
();
Enumeration
<
String
>
headerNames
=
httpRequest
.
getHeaderNames
();
while
(
headerNames
.
hasMoreElements
())
{
headersSb
.
append
(
headerNames
.
nextElement
()).
append
(
", "
);
}
if
(!
headersSb
.
toString
().
toLowerCase
().
contains
(
"authorization"
))
{
headersSb
.
append
(
"authorization"
);
}
httpResponse
.
addHeader
(
HttpHeaders
.
ACCESS_CONTROL_ALLOW_HEADERS
,
headersSb
.
toString
().
replaceAll
(
", +$"
,
""
));
httpResponse
.
addHeader
(
HttpHeaders
.
ACCESS_CONTROL_ALLOW_CREDENTIALS
,
"true"
);
httpResponse
.
addHeader
(
HttpHeaders
.
ACCESS_CONTROL_ALLOW_ORIGIN
,
httpRequest
.
getHeader
(
HttpHeaders
.
ORIGIN
));
httpResponse
.
addHeader
(
HttpHeaders
.
ACCESS_CONTROL_ALLOW_METHODS
,
httpRequest
.
getMethod
());
httpResponse
.
addHeader
(
HttpHeaders
.
ACCESS_CONTROL_EXPOSE_HEADERS
,
"*"
);
httpResponse
.
addHeader
(
HttpHeaders
.
ACCESS_CONTROL_MAX_AGE
,
"18000L"
);
chain
.
doFilter
(
request
,
response
);
}
}
src/main/java/com/glsx/smart/service/SmartProductTypeService.java
View file @
faf4a49
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.github.pagehelper.PageInfo
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.dao.entity.SmartProductType
;
...
@@ -17,6 +18,10 @@ public interface SmartProductTypeService {
...
@@ -17,6 +18,10 @@ public interface SmartProductTypeService {
SmartProductType
getById
(
Long
id
);
SmartProductType
getById
(
Long
id
);
JSONObject
getTypeAndCategory
();
JSONObject
getTypeAndCategory2
();
void
save
(
SmartProductType
productType
);
void
save
(
SmartProductType
productType
);
List
<
String
>
getShopTagList
();
List
<
String
>
getShopTagList
();
...
...
src/main/java/com/glsx/smart/service/impl/SmartProductServiceImpl.java
View file @
faf4a49
...
@@ -71,12 +71,11 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
...
@@ -71,12 +71,11 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
Date
now
=
new
Date
();
Date
now
=
new
Date
();
product
.
setUpdateTime
(
now
);
product
.
setUpdateTime
(
now
);
int
nameCount
=
productMapper
.
countByProductName
(
product
.
getProductName
());
if
(
nameCount
>
0
)
{
throw
new
BusinessException
(
ResultEnum
.
PRODUCT_NAME_DUPLICATE
);
}
if
(
product
.
getId
()
==
null
)
{
if
(
product
.
getId
()
==
null
)
{
int
nameCount
=
productMapper
.
countByProductName
(
product
.
getProductName
());
if
(
nameCount
>
0
)
{
throw
new
BusinessException
(
ResultEnum
.
PRODUCT_NAME_DUPLICATE
);
}
product
.
setCreateTime
(
now
);
product
.
setCreateTime
(
now
);
productMapper
.
insertSelective
(
product
);
productMapper
.
insertSelective
(
product
);
}
else
{
}
else
{
...
...
src/main/java/com/glsx/smart/service/impl/SmartProductTypeServiceImpl.java
View file @
faf4a49
...
@@ -3,9 +3,15 @@ package com.glsx.smart.service.impl;
...
@@ -3,9 +3,15 @@ package com.glsx.smart.service.impl;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Set
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.TypeReference
;
import
com.github.pagehelper.PageInfo
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.dao.entity.SmartProductType
;
import
com.glsx.smart.dao.entity.SmartSysCode
;
import
com.glsx.smart.dao.mapper.SmartProductTypeMapper
;
import
com.glsx.smart.dao.mapper.SmartProductTypeMapper
;
import
com.glsx.smart.exception.BusinessException
;
import
com.glsx.smart.exception.BusinessException
;
import
com.glsx.smart.exception.ResultEnum
;
import
com.glsx.smart.exception.ResultEnum
;
...
@@ -42,12 +48,98 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
...
@@ -42,12 +48,98 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
return
pageInfo
;
return
pageInfo
;
}
}
/**
* 根据 id 获取产品类型。
*
* @param id 产品类型 id
* @return 结果
*/
@Override
@Override
public
SmartProductType
getById
(
Long
id
)
{
public
SmartProductType
getById
(
Long
id
)
{
return
productTypeMapper
.
selectByPrimaryKey
(
id
);
return
productTypeMapper
.
selectByPrimaryKey
(
id
);
}
}
/**
/**
* 获取结构化的产品类型。
* 数据格式为:
* {
* "车智能": ["产品类型1-1", "产品类型1-2"],
* "产品类型1-1": ["产品名称1", "产品名称2"],
*
* "家智能": ["产品类型2-1"],
* "产品类型2-1": ["产品名称3", "产品名称4", "产品名称5"]
* }
*
* @return 结果
*/
@Override
public
JSONObject
getTypeAndCategory
()
{
JSONObject
result
=
new
JSONObject
();
List
<
SmartProductType
>
productTypeList
=
productTypeMapper
.
getAllList
();
for
(
SmartProductType
productType
:
productTypeList
)
{
// 一级分类放在备注字段中
String
parentCategoryKey
=
productType
.
getRemark
();
// 二级分类
String
categoryKey
=
productType
.
getCategoryKey
();
// 根据一级分类将二级分类分组
if
(!
result
.
containsKey
(
parentCategoryKey
))
{
result
.
put
(
parentCategoryKey
,
new
HashSet
<>());
}
Set
<
Object
>
set
=
result
.
getObject
(
parentCategoryKey
,
new
TypeReference
<
Set
<
Object
>>()
{});
set
.
add
(
categoryKey
);
result
.
put
(
parentCategoryKey
,
set
);
// 根据二级分类将产品类型分组
if
(!
result
.
containsKey
(
categoryKey
))
{
result
.
put
(
categoryKey
,
new
JSONArray
());
}
result
.
getJSONArray
(
categoryKey
).
add
(
productType
);
}
return
result
;
}
/**
* 获取结构化的产品类型。
* 数据格式为:
* {
* "车智能": ["产品类型1-1", "产品类型1-2"],
* "产品类型1-1": ["产品名称1", "产品名称2"],
*
* "家智能": ["产品类型2-1"],
* "产品类型2-1": ["产品名称3", "产品名称4", "产品名称5"]
* }
*
* @return 结果
*/
@Override
@Deprecated
public
JSONObject
getTypeAndCategory2
()
{
JSONObject
result
=
new
JSONObject
();
// 获取所有产品类型,并按照二级分类来分组
List
<
SmartProductType
>
productTypeList
=
productTypeMapper
.
selectAll
();
for
(
SmartProductType
productType
:
productTypeList
)
{
String
categoryKey
=
productType
.
getCategoryKey
();
if
(!
result
.
containsKey
(
categoryKey
))
{
result
.
put
(
categoryKey
,
new
JSONArray
());
}
result
.
getJSONArray
(
categoryKey
).
add
(
productType
);
}
// 获取产品类型对应的字典集,并按照产品的二级分类来分组
List
<
SmartSysCode
>
categoryList
=
productTypeMapper
.
getTypeCategoryList
();
for
(
SmartSysCode
sysCode
:
categoryList
)
{
String
parentKey
=
sysCode
.
getParentKey
();
String
dictKey
=
sysCode
.
getDictKey
();
if
(!
result
.
containsKey
(
parentKey
))
{
result
.
put
(
parentKey
,
new
JSONArray
());
}
result
.
getJSONArray
(
parentKey
).
add
(
dictKey
);
}
return
result
;
}
/**
* 保存产品类型。
* 保存产品类型。
*
*
* @param productType 产品类型
* @param productType 产品类型
...
@@ -59,6 +151,10 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
...
@@ -59,6 +151,10 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
productType
.
setUpdateTime
(
now
);
productType
.
setUpdateTime
(
now
);
if
(
productType
.
getId
()
==
null
)
{
if
(
productType
.
getId
()
==
null
)
{
int
nameCount
=
productTypeMapper
.
countByProductTypeName
(
productType
.
getTypeName
());
if
(
nameCount
>
0
)
{
throw
new
BusinessException
(
ResultEnum
.
PRODUCT_TYPE_NAME_DUPLICATE
);
}
productType
.
setCreateTime
(
now
);
productType
.
setCreateTime
(
now
);
productTypeMapper
.
insertSelective
(
productType
);
productTypeMapper
.
insertSelective
(
productType
);
}
else
{
}
else
{
...
@@ -72,7 +168,7 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
...
@@ -72,7 +168,7 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
}
}
/**
/**
* 获取商城标签
* 获取商城标签
。
*
*
* @return 结果
* @return 结果
*/
*/
...
...
src/main/resources/mapper/SmartProductTypeMapper.xml
View file @
faf4a49
...
@@ -32,4 +32,26 @@
...
@@ -32,4 +32,26 @@
</where>
</where>
order by spt.create_time desc
order by spt.create_time desc
</select>
</select>
<select
id=
"countByProductTypeName"
resultType=
"int"
>
select count(0)
from smart_product_type
where type_name = #{typeName,jdbcType=VARCHAR}
</select>
<select
id=
"getTypeCategoryList"
resultType=
"com.glsx.smart.dao.entity.SmartSysCode"
>
select distinct ssc.id, ssc.dict_type dictType, ssc.dict_key dictKey,
ssc.dict_value dictValue, ssc.parent_key parentKey, ssc.sort
from smart_product_type spt
left join smart_sys_code ssc on ssc.dict_key = spt.category_key
order by ssc.sort
</select>
<select
id=
"getAllList"
resultMap=
"BaseResultMap"
>
select spt.id, spt.type_name, spt.category_key, spt.shop_tag,
ssc.parent_key remark
from smart_product_type spt
left join smart_sys_code ssc on ssc.dict_key = spt.category_key
order by ssc.sort
</select>
</mapper>
</mapper>
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