Commit faf4a49c by tyfanchz

APP端功能开发

1 parent db9bae72
......@@ -2,6 +2,7 @@ package com.glsx.smart.controller;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartProductType;
import com.glsx.smart.exception.BusinessException;
......@@ -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 产品类型
......
......@@ -2,6 +2,7 @@ package com.glsx.smart.dao.mapper;
import java.util.List;
import com.glsx.smart.dao.entity.SmartProductType;
import com.glsx.smart.dao.entity.SmartSysCode;
import org.apache.ibatis.annotations.Mapper;
/**
......@@ -15,4 +16,10 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SmartProductTypeMapper extends tk.mybatis.mapper.common.Mapper<SmartProductType> {
List<SmartProductType> getList(SmartProductType req);
int countByProductTypeName(String typeName);
List<SmartSysCode> getTypeCategoryList();
List<SmartProductType> getAllList();
}
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();
}
}
......@@ -13,10 +13,12 @@ public enum ResultEnum {
PARAM_ERROR(1001, "参数错误"),
ID_NOT_FOUND(1002, "ID不存在"),
PRODUCT_NOT_FOUND(2001, "产品ID未找到"),
PRODUCT_NAME_DUPLICATE(2002, "产品名称重复"),
PRODUCT_TYPE_NOT_FOUND(3001, "产品类型ID未找到"),
PRODUCT_TYPE_NAME_DUPLICATE(3002, "产品类型名称重复"),
;
private final Integer code;
......
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);
}
}
package com.glsx.smart.service;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartProductType;
......@@ -17,6 +18,10 @@ public interface SmartProductTypeService {
SmartProductType getById(Long id);
JSONObject getTypeAndCategory();
JSONObject getTypeAndCategory2();
void save(SmartProductType productType);
List<String> getShopTagList();
......
......@@ -71,12 +71,11 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
Date now = new Date();
product.setUpdateTime(now);
int nameCount = productMapper.countByProductName(product.getProductName());
if (nameCount > 0) {
throw new BusinessException(ResultEnum.PRODUCT_NAME_DUPLICATE);
}
if (product.getId() == null) {
int nameCount = productMapper.countByProductName(product.getProductName());
if (nameCount > 0) {
throw new BusinessException(ResultEnum.PRODUCT_NAME_DUPLICATE);
}
product.setCreateTime(now);
productMapper.insertSelective(product);
} else {
......
......@@ -3,9 +3,15 @@ package com.glsx.smart.service.impl;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
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.glsx.smart.dao.entity.SmartProductType;
import com.glsx.smart.dao.entity.SmartSysCode;
import com.glsx.smart.dao.mapper.SmartProductTypeMapper;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
......@@ -42,12 +48,98 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
return pageInfo;
}
/**
* 根据 id 获取产品类型。
*
* @param id 产品类型 id
* @return 结果
*/
@Override
public SmartProductType getById(Long 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 产品类型
......@@ -59,6 +151,10 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
productType.setUpdateTime(now);
if (productType.getId() == null) {
int nameCount = productTypeMapper.countByProductTypeName(productType.getTypeName());
if (nameCount > 0) {
throw new BusinessException(ResultEnum.PRODUCT_TYPE_NAME_DUPLICATE);
}
productType.setCreateTime(now);
productTypeMapper.insertSelective(productType);
} else {
......@@ -72,7 +168,7 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
}
/**
* 获取商城标签
* 获取商城标签。
*
* @return 结果
*/
......
......@@ -32,4 +32,26 @@
</where>
order by spt.create_time desc
</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>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!