Commit 2a8b2fb7 by 张洪涛

接口优化,添加异常处理

parent 8015819f
......@@ -15,9 +15,10 @@ import lombok.Data;
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目的
*/
public enum ResponseCode {
RESPONSE_CODE_100("100","响应成功"),
RESPONSE_CODE_100("100","成功"),
RESPONSE_CODE_101("101","请求参数不正确"),
RESPONSE_CODE_102("102","请求失败");
RESPONSE_CODE_102("102","请求失败"),
RESPONSE_CODE_ERROR_PARAM("104","参数不对");
private String code;
private String msg;
......
......@@ -26,42 +26,35 @@ import java.util.List;
@Controller
@Api("订单sku Controller")
@RequestMapping("order")
public class OrdersController {
@Autowired
private OrdersService ordersService;
@ApiOperation("获取订单的商品信息")
@GetMapping("/getOrder")
@GetMapping("/detail")
@ResponseBody
public BaseResponse getOrder(@RequestParam("orderId") String orderId){
if (orderId == null){
return new BaseResponse(ResponseCode.RESPONSE_CODE_101.getCode(),ResponseCode.RESPONSE_CODE_101.getMsg(),"1","请求参数为空");
}
List<OrderProductDTO> list = ordersService.getOrderProductDTOByOrderId(orderId);
if (list.size() == 0){
return new BaseResponse(ResponseCode.RESPONSE_CODE_102.getCode(),ResponseCode.RESPONSE_CODE_102.getMsg(),"1","查询不到订单,请输入正确的订单号");
}
return new BaseResponse(ResponseCode.RESPONSE_CODE_100.getCode(),ResponseCode.RESPONSE_CODE_100.getMsg(),"1",list);
return ordersService.getOrderProductDTOByOrderId(orderId);
}
@ApiOperation("根据正确的sku进行补推")
@PostMapping("/getRightSkuAndOrderPush")
@PostMapping("/fixSkuAndPush")
@ResponseBody
public BaseResponse getRightSkuAndOrderPush(@RequestBody OrderProductDTO orderProductDTO){
String sku = orderProductDTO.getSku();
String id = orderProductDTO.getId();
if (sku == null || id == null){
if (orderProductDTO.getId() == null || orderProductDTO.getSku() == null || orderProductDTO.getSku().equals("") || orderProductDTO.getId().equals("")){
return new BaseResponse(ResponseCode.RESPONSE_CODE_101.getCode(),ResponseCode.RESPONSE_CODE_101.getMsg(),"1","请求参数为空");
}
// 修复sku
boolean isMod = ordersService.RepairByRightSku(orderProductDTO);
if (isMod){
return new BaseResponse(ResponseCode.RESPONSE_CODE_100.getCode(),ResponseCode.RESPONSE_CODE_100.getMsg(),"1","修复成功");
} else {
return new BaseResponse(ResponseCode.RESPONSE_CODE_102.getCode(),ResponseCode.RESPONSE_CODE_102.getMsg(),"1","修复失败");
if (orderProductDTO.getSku().length() != 8){
return new BaseResponse(ResponseCode.RESPONSE_CODE_ERROR_PARAM.getCode(), ResponseCode.RESPONSE_CODE_ERROR_PARAM.getMsg(), "1", "输入的sku格式不对");
}
// 修复sku
return ordersService.RepairByRightSku(orderProductDTO);
}
}
......@@ -13,6 +13,7 @@
package com.freemud.Service.Impl;
import com.alibaba.fastjson.JSON;
import com.freemud.Constant.ResponseCode;
import com.freemud.Mapper.OrderProductMapper;
import com.freemud.Service.OrdersService;
import com.freemud.dto.BaseResponse;
......@@ -25,6 +26,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.io.File;
......@@ -52,14 +54,18 @@ public class OrdersServiceImpl implements OrdersService {
@Override
public List<OrderProductDTO> getOrderProductDTOByOrderId(String orderId) {
public BaseResponse getOrderProductDTOByOrderId(String orderId) {
List<OrderProductDTO> orderProductDTO = orderProductMapper.getOrderProductDTOByOrderId(orderId);
log.info("查询结果:{}", JSON.toJSONString(orderProductDTO));
return orderProductDTO;
if (orderProductDTO.size() == 0 || orderProductDTO == null){
return new BaseResponse(ResponseCode.RESPONSE_CODE_102.getCode(),ResponseCode.RESPONSE_CODE_102.getMsg(),"1","查询不到订单,请输入正确的订单号");
}
return new BaseResponse(ResponseCode.RESPONSE_CODE_100.getCode(),ResponseCode.RESPONSE_CODE_100.getMsg(),"1",orderProductDTO);
}
@Override
public Boolean RepairByRightSku(OrderProductDTO orderProductDTO) {
public BaseResponse RepairByRightSku(OrderProductDTO orderProductDTO) {
// 请求修复接口
String url = RequestUrl+"/order/repairByRightSku";
String titles[] = {"订单联合表主键","正确的sku"};
......@@ -73,14 +79,20 @@ public class OrdersServiceImpl implements OrdersService {
FileSystemResource resource = new FileSystemResource(file.getPath());
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("file",resource);
String code = restTemplate.postForObject(url,param,String.class);
String code = "";
try {
code = restTemplate.postForObject(url,param,String.class);
} catch (RestClientException e) {
log.error("調用修復sku接口出錯");
return new BaseResponse(ResponseCode.RESPONSE_CODE_102.getCode(),ResponseCode.RESPONSE_CODE_102.getMsg(),"1","調用修復接口出錯");
}
log.info("调用接口{}返回的code:{}",url,code);
if (code.equals(returnCode)){
log.info("修改sku成功{}",JSON.toJSONString(orderProductDTO));
return true;
return new BaseResponse(ResponseCode.RESPONSE_CODE_100.getCode(),ResponseCode.RESPONSE_CODE_100.getMsg(),"1","修复成功");
}
log.info("修改sku失败:{}",JSON.toJSONString(orderProductDTO));
return false;
return new BaseResponse(ResponseCode.RESPONSE_CODE_102.getCode(),ResponseCode.RESPONSE_CODE_102.getMsg(),"1","修复失败");
}
......
......@@ -12,6 +12,7 @@
*/
package com.freemud.Service;
import com.freemud.dto.BaseResponse;
import com.freemud.dto.OrderProductDTO;
import java.util.List;
......@@ -23,8 +24,8 @@ public interface OrdersService {
* @param orderId
* @return
*/
List<OrderProductDTO> getOrderProductDTOByOrderId(String orderId);
BaseResponse getOrderProductDTOByOrderId(String orderId);
Boolean RepairByRightSku(OrderProductDTO orderProductDTO);
BaseResponse RepairByRightSku(OrderProductDTO orderProductDTO);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment