Commit 52d2ed89 by wnb0128

Merge branch 'dev' of 192.168.3.233:open-source/chejia-smart-service into dev

2 parents 840a3851 a68c0e66
package com.glsx.smart.controller;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.service.SmartProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -49,6 +47,7 @@ public class ProductController {
*/
@RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
public SmartProduct getById(Long id) {
log.info("$$$$ /product/getById param: {}", id);
return productService.getById(id);
}
......@@ -56,11 +55,12 @@ public class ProductController {
* 保存产品。无则插入,有则更新。
*
* @param product 产品
* @return 是否成功
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
public void save(@RequestBody @Validated SmartProduct product) {
public Boolean save(@RequestBody SmartProduct product) {
log.info("$$$$ /product/save param: {}", JSON.toJSONString(product));
productService.save(product);
return productService.save(product);
}
/**
......@@ -69,25 +69,20 @@ public class ProductController {
* @param id 产品 id
*/
@RequestMapping(value = "/getCommandSetById", method = {RequestMethod.GET, RequestMethod.POST})
public void getCommandSetById(Long id) {
public List<SmartDeviceOrder> getCommandSetById(Long id) {
log.info("$$$$ /product/getCommandSetById param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
productService.getCommandSetById(id);
return productService.getCommandSetById(id);
}
/**
* 校验商城商品 id。
*
* @param shopProductId 商品 id
* @return 商品 id 是否合法
*/
@RequestMapping(value = "/checkShopProductId", method = {RequestMethod.GET, RequestMethod.POST})
public boolean checkShopProductId(String shopProductId) {
public Boolean checkShopProductId(String shopProductId) {
log.info("$$$$ /product/checkShopProductId param: {}", shopProductId);
if (StringUtils.isBlank(shopProductId)) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
return productService.checkShopProductId(shopProductId);
}
......@@ -95,13 +90,11 @@ public class ProductController {
* 上下架产品。
*
* @param id 产品 id
* @return 是否成功
*/
@RequestMapping(value = "/putOnOff", method = {RequestMethod.GET, RequestMethod.POST})
public void putOnOff(Long id) {
public Boolean putOnOff(Long id) {
log.info("$$$$ /product/putOnOff param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
productService.putOnOff(id);
return productService.putOnOff(id);
}
}
......@@ -2,12 +2,12 @@ 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.service.SmartProductTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -39,24 +39,42 @@ public class ProductTypeController {
return productTypeService.getList(req);
}
/**
* 根据 id 获取产品类型。
*
* @param id 产品类型 id
* @return 结果
*/
@RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
public SmartProductType getById(Long id) {
return productTypeService.getById(id);
}
/**
* 获取结构化的产品类型。
*
* @return 结果
*/
@RequestMapping(value = "/getTypeAndCategory", method = {RequestMethod.GET, RequestMethod.POST})
public JSONObject getProductTypeTree() {
log.info("$$$$ /productType/getTypeAndCategory...");
return productTypeService.getTypeAndCategory();
}
/**
* 保存产品类型。
*
* @param productType 产品类型
* @return 是否成功
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
public void save(@RequestBody @Validated SmartProductType productType) {
public Boolean save(@RequestBody SmartProductType productType) {
log.info("$$$$ /productType/save param: {}", JSON.toJSONString(productType));
productTypeService.save(productType);
return productTypeService.save(productType);
}
/**
* 获取商城标签
* 获取商城标签。
*
* @return 结果
*/
......
......@@ -5,7 +5,6 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
......@@ -48,7 +47,6 @@ public class SmartProduct extends BaseDTO {
/**
* 产品名称
*/
@NotBlank
private String productName;
/**
......
......@@ -4,7 +4,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import com.glsx.smart.pojo.BaseDTO;
import lombok.Data;
......@@ -34,7 +33,6 @@ public class SmartProductType extends BaseDTO {
/**
* 类型名称
*/
@NotBlank
private String typeName;
/**
......
package com.glsx.smart.dao.mapper;
import java.util.List;
import com.glsx.smart.dao.entity.SmartProduct;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <p>Description:
......@@ -16,18 +14,9 @@ import java.util.List;
@Mapper
public interface SmartProductMapper extends tk.mybatis.mapper.common.Mapper<SmartProduct> {
List<SmartProduct> getList(SmartProduct product);
List<SmartProduct> getList(SmartProduct req);
SmartProduct getById(Long id);
int countByProductName(String productName);
int insertOrUpdate(SmartProduct record);
int insertOrUpdateSelective(SmartProduct record);
int updateBatch(List<SmartProduct> list);
int updateBatchSelective(List<SmartProduct> list);
int batchInsert(@Param("list") List<SmartProduct> list);
}
......@@ -3,7 +3,6 @@ package com.glsx.smart.dao.mapper;
import java.util.List;
import com.glsx.smart.dao.entity.SmartProductType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* <p>Description:
......@@ -15,16 +14,11 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public interface SmartProductTypeMapper extends tk.mybatis.mapper.common.Mapper<SmartProductType> {
List<SmartProductType> getList(SmartProductType req);
List<SmartProductType> getList(SmartProductType productType);
SmartProductType getById(Long id);
int insertOrUpdate(SmartProductType record);
int countByProductTypeName(String typeName);
int insertOrUpdateSelective(SmartProductType record);
int updateBatch(List<SmartProductType> list);
int updateBatchSelective(List<SmartProductType> list);
int batchInsert(@Param("list") List<SmartProductType> list);
List<SmartProductType> getAllList();
}
......@@ -6,7 +6,6 @@ import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* <p>Description:
......@@ -14,27 +13,28 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
*
* @author wbh
* @date 2021-03-16
* @deprecated 抛出异常,让上游服务捕获信息
*/
@Slf4j
@RestControllerAdvice
//@RestControllerAdvice
@Deprecated
public class GlobalExceptionHandler {
@ExceptionHandler({BusinessException.class})
public R handleBusinessException(BusinessException e) {
log.error("业务异常:{}", e);
log.error("业务异常", e);
return R.error(e.getMessage());
}
@ExceptionHandler({ServletRequestBindingException.class, MethodArgumentNotValidException.class,
BindException.class})
public R handleBindException(Exception e) {
log.error("参数错误:{}", e);
log.error("参数错误", e);
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error("未知异常:{}", e);
log.error("未知异常", e);
return R.error(e.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.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct;
/**
......@@ -12,15 +14,15 @@ import com.glsx.smart.dao.entity.SmartProduct;
*/
public interface SmartProductService {
PageInfo<SmartProduct> getList(SmartProduct req);
PageInfo<SmartProduct> getList(SmartProduct product);
SmartProduct getById(Long id);
void save(SmartProduct product);
Boolean save(SmartProduct product);
void getCommandSetById(Long id);
List<SmartDeviceOrder> getCommandSetById(Long id);
boolean checkShopProductId(String shopProductId);
Boolean checkShopProductId(String shopProductId);
void putOnOff(Long id);
Boolean putOnOff(Long id);
}
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;
......@@ -13,11 +14,13 @@ import com.glsx.smart.dao.entity.SmartProductType;
*/
public interface SmartProductTypeService {
PageInfo<SmartProductType> getList(SmartProductType req);
PageInfo<SmartProductType> getList(SmartProductType productType);
SmartProductType getById(Long id);
void save(SmartProductType productType);
JSONObject getTypeAndCategory();
Boolean save(SmartProductType productType);
List<String> getShopTagList();
}
......@@ -5,13 +5,16 @@ import java.util.Date;
import java.util.List;
import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartDeviceOrder;
import com.glsx.smart.dao.entity.SmartProduct;
import com.glsx.smart.dao.mapper.SmartDeviceOrderMapper;
import com.glsx.smart.dao.mapper.SmartProductMapper;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.service.BaseService;
import com.glsx.smart.service.SmartProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -28,16 +31,19 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
@Resource
private SmartProductMapper productMapper;
@Autowired
private SmartDeviceOrderMapper deviceOrderMapper;
/**
* 获取产品列表。
*
* @param req 请求体
* @param product 请求体
* @return 结果
*/
@Override
public PageInfo<SmartProduct> getList(SmartProduct req) {
super.startPagination(req);
List<SmartProduct> productList = productMapper.getList(req);
public PageInfo<SmartProduct> getList(SmartProduct product) {
super.startPagination(product);
List<SmartProduct> productList = productMapper.getList(product);
PageInfo<SmartProduct> pageInfo = PageInfo.of(productList);
return pageInfo;
}
......@@ -50,7 +56,10 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
*/
@Override
public SmartProduct getById(Long id) {
SmartProduct product = productMapper.selectByPrimaryKey(id);
SmartProduct product = productMapper.getById(id);
if (product == null) {
throw new BusinessException(ResultEnum.PRODUCT_NOT_FOUND);
}
return product;
}
......@@ -58,39 +67,47 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
* 保存产品。无则插入,有则更新。
*
* @param product 产品
* @return 是否成功
*/
@Override
@Transactional
public void save(SmartProduct product) {
public Boolean save(SmartProduct product) {
boolean success;
Date now = new Date();
product.setUpdateTime(now);
int nameCount = productMapper.countByProductName(product.getProductName());
if (nameCount > 0) {
throw new BusinessException(ResultEnum.PRODUCT_NAME_DUPLICATE);
}
product.setUpdateBy(0L);
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);
product.setCreateBy(0L);
success = productMapper.insertSelective(product) > 0;
} else {
SmartProduct record = productMapper.selectByPrimaryKey(product.getId());
if (record != null) {
productMapper.updateByPrimaryKeySelective(product);
success = productMapper.updateByPrimaryKeySelective(product) > 0;
} else {
throw new BusinessException(ResultEnum.PRODUCT_NOT_FOUND);
}
}
return success;
}
/**
* 根据产品 id 获取指令集。
*
* @param id 产品 id
* @return 结果
*/
@Override
public void getCommandSetById(Long id) {
public List<SmartDeviceOrder> getCommandSetById(Long id) {
SmartDeviceOrder example = new SmartDeviceOrder();
example.setProductId(String.valueOf(id));
return deviceOrderMapper.select(example);
}
/**
......@@ -100,7 +117,7 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
* @return 商品 id 是否合法
*/
@Override
public boolean checkShopProductId(String shopProductId) {
public Boolean checkShopProductId(String shopProductId) {
return RandomUtil.randomBoolean();
}
......@@ -108,14 +125,18 @@ public class SmartProductServiceImpl extends BaseService implements SmartProduct
* 上下架产品。
*
* @param id 产品 id
* @return 是否成功
*/
@Override
public void putOnOff(Long id) {
@Transactional
public Boolean putOnOff(Long id) {
boolean success;
SmartProduct product = productMapper.selectByPrimaryKey(id);
if (product == null) {
throw new BusinessException(ResultEnum.PRODUCT_NOT_FOUND);
}
product.setStatus((byte) (product.getStatus() ^ 0b00000001));
productMapper.updateByPrimaryKeySelective(product);
success = productMapper.updateByPrimaryKeySelective(product) > 0;
return success;
}
}
......@@ -3,7 +3,12 @@ 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.mapper.SmartProductTypeMapper;
......@@ -31,48 +36,109 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
/**
* 获取产品类型列表。
*
* @param req 请求
* @param productType 请求
* @return 结果
*/
@Override
public PageInfo<SmartProductType> getList(SmartProductType req) {
super.startPagination(req);
List<SmartProductType> productTypeList = productTypeMapper.getList(req);
public PageInfo<SmartProductType> getList(SmartProductType productType) {
super.startPagination(productType);
List<SmartProductType> productTypeList = productTypeMapper.getList(productType);
PageInfo<SmartProductType> pageInfo = PageInfo.of(productTypeList);
return pageInfo;
}
/**
* 根据 id 获取产品类型。
*
* @param id 产品类型 id
* @return 结果
*/
@Override
public SmartProductType getById(Long id) {
return productTypeMapper.selectByPrimaryKey(id);
SmartProductType productType = productTypeMapper.getById(id);
if (productType == null) {
throw new BusinessException(ResultEnum.PRODUCT_TYPE_NOT_FOUND);
}
return productType;
}
/**
* 获取结构化的产品类型。
* 数据格式为:
* {
* "车智能": ["产品类型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;
}
/**
* 保存产品类型。
*
* @param productType 产品类型
* @return 是否成功
*/
@Override
@Transactional
public void save(SmartProductType productType) {
public Boolean save(SmartProductType productType) {
boolean success;
Date now = new Date();
productType.setUpdateTime(now);
productType.setUpdateBy(0L);
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);
productType.setCreateBy(0L);
success = productTypeMapper.insertSelective(productType) > 0;
} else {
SmartProductType record = productTypeMapper.selectByPrimaryKey(productType.getId());
if (record != null) {
productTypeMapper.updateByPrimaryKeySelective(productType);
success = productTypeMapper.updateByPrimaryKeySelective(productType) > 0;
} else {
throw new BusinessException(ResultEnum.PRODUCT_TYPE_NOT_FOUND);
}
}
return success;
}
/**
* 获取商城标签
* 获取商城标签。
*
* @return 结果
*/
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!