Commit d785f1c0 by tyfanchz

1、删除无用代码

2、增加feign调用异常捕捉
3、车家智能后台接口开发
1 parent 411cd5e8
package com.glsx.admin.controller;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.exception.BusinessException;
import com.glsx.admin.exception.ResultEnum;
import com.glsx.admin.pojo.request.SmartProductReq;
import com.glsx.admin.pojo.response.SmartDeviceOrderResp;
import com.glsx.admin.pojo.response.SmartProductResp;
import com.glsx.admin.remote.ProductFeignClient;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
......@@ -39,7 +42,7 @@ public class ProductController {
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public R getList(SmartProductReq req) {
log.info("$$$$ /product/getList param: {}", JSON.toJSONString(req));
PageInfo<SmartProductReq> pageInfo = productFeignClient.getList(req);
PageInfo<SmartProductResp> pageInfo = productFeignClient.getList(req);
return R.ok().putPageData(pageInfo);
}
......@@ -50,18 +53,25 @@ public class ProductController {
* @return 结果
*/
@RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
public SmartProductReq getById(Long id) {
return null;
public R getById(Long id) {
log.info("$$$$ /product/getById param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
SmartProductResp resp = productFeignClient.getById(id);
return R.ok().data(resp);
}
/**
* 保存产品。无则插入,有则更新。
*
* @param product 产品
* @param req 产品
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
public void save(@RequestBody @Validated SmartProductReq product) {
log.info("$$$$ /product/save param: {}", JSON.toJSONString(product));
public R save(@RequestBody @Validated SmartProductReq req) {
log.info("$$$$ /product/save param: {}", JSON.toJSONString(req));
productFeignClient.save(req);
return R.ok();
}
/**
......@@ -70,8 +80,13 @@ public class ProductController {
* @param id 产品 id
*/
@RequestMapping(value = "/getCommandSetById", method = {RequestMethod.GET, RequestMethod.POST})
public void getCommandSetById(Long id) {
public R getCommandSetById(String id) {
log.info("$$$$ /product/getCommandSetById param: {}", id);
if (StringUtils.isBlank(id)) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
List<SmartDeviceOrderResp> respList = productFeignClient.getCommandSetById(id);
return R.ok().data(respList);
}
/**
......@@ -80,12 +95,13 @@ public class ProductController {
* @param shopProductId 商品 id
*/
@RequestMapping(value = "/checkShopProductId", method = {RequestMethod.GET, RequestMethod.POST})
public Boolean checkShopProductId(String shopProductId) {
public R checkShopProductId(String shopProductId) {
log.info("$$$$ /product/checkShopProductId param: {}", shopProductId);
if (StringUtils.isBlank(shopProductId)) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
return null;
Boolean productIdValid = productFeignClient.checkShopProductId(shopProductId);
return R.ok().data(productIdValid);
}
/**
......@@ -94,10 +110,12 @@ public class ProductController {
* @param id 产品 id
*/
@RequestMapping(value = "/putOnOff", method = {RequestMethod.GET, RequestMethod.POST})
public void putOnOff(Long id) {
public R putOnOff(Long id) {
log.info("$$$$ /product/putOnOff param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
productFeignClient.putOnOff(id);
return R.ok();
}
}
package com.glsx.admin.controller;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.exception.BusinessException;
import com.glsx.admin.exception.ResultEnum;
import com.glsx.admin.pojo.request.SmartProductTypeReq;
import com.glsx.admin.pojo.response.SmartProductTypeResp;
import com.glsx.admin.remote.ProductTypeFeignClient;
import com.glsx.plat.core.web.R;
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;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-17
*/
@Slf4j
@RestController
@RequestMapping("/productType")
public class ProductTypeController {
@Autowired
private ProductTypeFeignClient productTypeFeignClient;
/**
* 获取产品类型列表。
*
* @param req 请求
* @return 结果
*/
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public R getList(SmartProductTypeReq req) {
log.info("$$$$ /productType/getList param: {}", JSON.toJSONString(req));
PageInfo<SmartProductTypeResp> pageInfo = productTypeFeignClient.getList(req);
return R.ok().putPageData(pageInfo);
}
/**
* 根据 id 获取产品类型。
*
* @param id 产品类型 id
* @return 结果
*/
@RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
public R getById(Long id) {
log.info("$$$$ /productType/getById param: {}", id);
if (id == null) {
throw new BusinessException(ResultEnum.PARAM_ERROR);
}
SmartProductTypeResp resp = productTypeFeignClient.getById(id);
return R.ok().data(resp);
}
/**
* 保存产品类型。
*
* @param req 产品类型
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
public R save(@RequestBody @Validated SmartProductTypeReq req) {
log.info("$$$$ /productType/save param: {}", JSON.toJSONString(req));
productTypeFeignClient.save(req);
return R.ok();
}
/**
* 获取商城标签
*
* @return 结果
*/
@RequestMapping(value = "/getShopTagList", method = {RequestMethod.GET, RequestMethod.POST})
public R getShopTagList() {
log.info("$$$$ /productType/getShopTagList ...");
List<String> shopTagList = productTypeFeignClient.getShopTagList();
return R.ok().data(shopTagList);
}
}
package com.glsx.admin.exception;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
......@@ -18,21 +24,44 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({BusinessException.class})
public String handleBusinessException(BusinessException e) {
public R handleBusinessException(BusinessException e) {
log.error("!!!! 业务异常", e);
return e.getMessage();
return R.error(e.getCode(), e.getMessage());
}
@ExceptionHandler({ServletRequestBindingException.class, MethodArgumentNotValidException.class,
BindException.class})
public String handleBindException(Exception e) {
public R handleBindException(Exception e) {
log.error("!!!! 参数错误", e);
return ResultEnum.PARAM_ERROR.getMessage();
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
}
@ExceptionHandler({HystrixRuntimeException.class})
public R handleHystrixException(HystrixRuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof FeignException) {
FeignException feignException = (FeignException) cause;
return this.handleFeignException(feignException);
} else {
log.error("!!!! Hystrix熔断错误", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
}
}
@ExceptionHandler({FeignException.class})
public R handleFeignException(FeignException e) {
log.error("!!!! 远程服务错误", e);
JSONObject respObj = JSON.parseObject(e.contentUTF8());
String respMessage = respObj.getString("message");
int code = ResultEnum.REMOTE_SERVICE_ERROR.getCode();
String message = StringUtils.isBlank(respMessage)
? ResultEnum.REMOTE_SERVICE_ERROR.getMessage() : respMessage;
return R.error(code, message);
}
@ExceptionHandler(Exception.class)
public String handleException(Exception e) {
public R handleException(Exception e) {
log.error("!!!! 未知异常", e);
return ResultEnum.ERROR.getMessage();
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
}
}
......@@ -17,6 +17,8 @@ public enum ResultEnum {
PRODUCT_NAME_DUPLICATE(2002, "产品名称重复"),
PRODUCT_TYPE_NOT_FOUND(3001, "产品类型ID未找到"),
REMOTE_SERVICE_ERROR(9001, "服务调用错误"),
;
private final Integer code;
......
package com.glsx.admin.pojo.request;
import lombok.Data;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-17
*/
@Data
public class SmartDeviceOrderReq {
private Integer id;
/**
* 产品id
*/
private String productId;
private String orderCode;
private String orderValue;
/**
* 设备类型 1.非固定值不需要查询 2.固定值需要查询
*/
private String orderType;
}
package com.glsx.admin.pojo.request;
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.admin.pojo.BaseDTO;
......@@ -22,13 +18,10 @@ import lombok.Data;
* 车家智能产品数据表
*/
@Data
@Table(name = "smart_product")
public class SmartProductReq extends BaseDTO {
/**
* 标识ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
......
......@@ -9,23 +9,16 @@ package com.glsx.admin.pojo.request;
*/
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
/**
* 车家智能产品类型参数表
*/
@Data
@Table(name = "smart_product_type_param")
public class SmartProductTypeParamReq {
/**
* 标识ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
......
package com.glsx.admin.pojo.request;
import javax.persistence.GeneratedValue;
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.admin.pojo.BaseDTO;
......@@ -23,26 +18,10 @@ import lombok.Data;
* 车家智能产品类型表
*/
@Data
@Table(name = "smart_product_type")
public class SmartProductTypeReq extends BaseDTO {
/**
* 一级分类名
*/
@Transient
private String categoryName1;
/**
* 二级分类名
*/
@Transient
private String categoryName2;
/**
* 标识ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
......
package com.glsx.admin.pojo.response;
import lombok.Data;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-17
*/
@Data
public class SmartDeviceOrderResp {
private Integer id;
/**
* 产品id
*/
private String productId;
private String orderCode;
private String orderValue;
/**
* 设备类型 1.非固定值不需要查询 2.固定值需要查询
*/
private String orderType;
}
package com.glsx.admin.pojo.response;
import java.util.Date;
import com.glsx.admin.pojo.BaseDTO;
import lombok.Data;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-15
*/
/**
* 车家智能产品数据表
*/
@Data
public class SmartProductResp extends BaseDTO {
/**
* 标识ID
*/
private Long id;
/**
* 产品名称
*/
private String productName;
/**
* 图片ID 关联smart_sys_file中id
*/
private Long icon;
/**
* 产品类型ID 关联smart_product_type中id
*/
private Long typeId;
/**
* 商城产品id
*/
private String shopProductId;
/**
* 接入平台key 关联smart_sys_code中dict_key
*/
private String platformKey;
/**
* 接入平台产品id
*/
private String platformProductId;
/**
* 配网方式key 关联smart_sys_code中dict_key
*/
private String networkKey;
/**
* 状态:0下线 1上线
*/
private Byte status;
/**
* 备注
*/
private String remark;
/**
* 创建时间
*/
private Date createTime;
/**
* 创建人
*/
private Long createBy;
/**
* 更新时间
*/
private Date updateTime;
/**
* 更新人
*/
private Long updateBy;
}
package com.glsx.admin.pojo.response;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import com.glsx.admin.pojo.BaseDTO;
import lombok.Data;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-15
*/
/**
* 车家智能产品类型表
*/
@Data
public class SmartProductTypeResp extends BaseDTO {
/**
* 标识ID
*/
private Long id;
/**
* 类型名称
*/
@NotBlank
private String typeName;
/**
* 类别key 关联smart_sys_code中dict_key
*/
private String categoryKey;
/**
* 商城标签
*/
private String shopTag;
/**
* 备注
*/
private String remark;
/**
* 创建时间
*/
private Date createTime;
/**
* 创建人
*/
private Long createBy;
/**
* 更新时间
*/
private Date updateTime;
/**
* 更新人
*/
private Long updateBy;
}
package com.glsx.admin.remote;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.pojo.request.SmartProductReq;
import com.glsx.admin.pojo.response.SmartDeviceOrderResp;
import com.glsx.admin.pojo.response.SmartProductResp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
......@@ -18,5 +24,25 @@ import org.springframework.web.bind.annotation.ResponseBody;
public interface ProductFeignClient {
@RequestMapping(value = "/product/getList", method = {RequestMethod.GET})
@ResponseBody
PageInfo<SmartProductReq> getList(SmartProductReq req);
PageInfo<SmartProductResp> getList(@SpringQueryMap SmartProductReq req);
@RequestMapping(value = "/product/getById", method = {RequestMethod.GET})
@ResponseBody
SmartProductResp getById(@RequestParam Long id);
@RequestMapping(value = "/product/save", method = {RequestMethod.POST})
@ResponseBody
void save(@RequestBody SmartProductReq req);
@RequestMapping("/product/getCommandSetById")
@ResponseBody
List<SmartDeviceOrderResp> getCommandSetById(@RequestParam String id);
@RequestMapping(value = "/product/checkShopProductId", method = {RequestMethod.GET})
@ResponseBody
Boolean checkShopProductId(@RequestParam String shopProductId);
@RequestMapping(value = "/product/putOnOff", method = {RequestMethod.POST})
@ResponseBody
void putOnOff(@RequestParam Long id);
}
package com.glsx.admin.remote;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.pojo.request.SmartProductTypeReq;
import com.glsx.admin.pojo.response.SmartProductTypeResp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-17
*/
@FeignClient(name = "${feign.server.name}",url = "${feign.server.url}")
public interface ProductTypeFeignClient {
@RequestMapping(value = "/productType/getList", method = {RequestMethod.GET})
@ResponseBody
PageInfo<SmartProductTypeResp> getList(@SpringQueryMap SmartProductTypeReq req);
@RequestMapping(value = "/productType/getById", method = {RequestMethod.GET})
@ResponseBody
SmartProductTypeResp getById(@RequestParam Long id);
@RequestMapping(value = "/productType/save", method = {RequestMethod.POST})
@ResponseBody
void save(@RequestBody SmartProductTypeReq req);
@RequestMapping(value = "/productType/getShopTagList", method = {RequestMethod.GET})
@ResponseBody
List<String> getShopTagList();
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!