添加全局异常处理

20230922-ljl-fixBug
qiushengjie 1 year ago
parent edcd5c17d5
commit 22f78366d2

@ -1,49 +1,31 @@
package com.ms.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinritemai.cloud.base.api.BaseRequest;
import com.jinritemai.cloud.base.api.BaseResponse;
import com.jinritemai.cloud.base.api.ExtensionService;
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
import com.ms.api.common.E;
import com.ms.api.common.R;
import com.ms.api.common.Ret;
import com.ms.api.common.SPIBaseService;
import com.ms.api.dto.ItemDTO;
import com.ms.api.tool.DsJsonRequestTemplate;
import com.ms.api.tool.SecurityTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
@ExtensionService("testSpi")
@Slf4j
public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, JSONObject> {
@Autowired
private DsJsonRequestTemplate dsJsonRequestTemplate;
public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, String> {
@Override
public BaseResponse<JSONObject> handle(BaseRequest<ItemDTO> req) {
initHandle(req);
getAuthCode();
HashMap<String, Object> params = new HashMap<>();
params.put("productId",req.getData().getTitle());
params.put("authCode",authCode);
String res = null;
JSONObject resObj = null;
try {
res = dsJsonRequestTemplate.execute("/micro_move/get_product_info", params);
resObj = JSON.parseObject(res);
log.info(res);
} catch (Exception e) {
public BaseResponse<String> handle(BaseRequest<ItemDTO> req) {
try{
initHandle(req);
// do something
E.throwMSException("这里有全局异常抛出");
return R.ok("这里执行不到");
}catch (Throwable e){
e.printStackTrace();
log.error(e.getMessage());
return R.ok(e.getMessage());
}
return R.ok(resObj);
}
}

@ -0,0 +1,14 @@
package com.ms.api.common;
/**
*
*/
public class E {
public static void throwMSException(String msg) throws MSException {
throw new MSException(msg);
}
public static void throwMSException(String msg,Throwable e) throws MSException {
throw new MSException(msg, e);
}
}

@ -0,0 +1,16 @@
package com.ms.api.common;
/**
*
*/
public class MSException extends RuntimeException {
public MSException(String message) {
super(message);
}
public MSException(String message, Throwable cause) {
super(message, cause);
}
}

@ -24,6 +24,7 @@ import com.ms.dal.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@ -55,58 +56,63 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
@Override
public BaseResponse<Ret> handle(BaseRequest<SaveBatchMoveTaskRequestDTO> req) {
initHandle(req);
// ----参数校验----
SaveBatchMoveTaskRequestDTO fields = req.getData();
// ----逻辑校验----
// ----业务处理----
// 检查和校验整理输入
log.info("shopId " + shopId + " start saveTask");
List<MoveProductDTO> productSimpleDetails = fields.getProducts();
String copyType = MoveConst.Collect_DS_COPY;
log.info("shopId " + shopId + " start checkAndFormatData");
Ret checkAndFormatRet = checkAndFormatData(shopId, productSimpleDetails);
log.info("shopId " + shopId + " end checkAndFormatData");
if (CommonTool.isFailRet(checkAndFormatRet)) {
return R.ok(checkAndFormatRet);
}
List<MoveProductDTO> curProductDetails = (List<MoveProductDTO>) checkAndFormatRet.getData().get("curProductDetails");
MoveConditionBO shopConditions = (MoveConditionBO) checkAndFormatRet.getData().get("shopConditions");
log.info("shopId " + shopId + " start allocShopProductsByAuthShop");
List<MoveTaskProductBO> shopProducts = allocShopProductsByAuthShop(curProductDetails);
// 保存任务
log.info("shopId " + shopId + " start saveTaskByAuthShop");
Ret saveRet = saveTaskByAuthShop(shopId, copyType, shopConditions, shopProducts);
if (CommonTool.isFailRet(saveRet)) {
return R.ok(saveRet);
}
try {
initHandle(req);
// ----参数校验----
SaveBatchMoveTaskRequestDTO fields = req.getData();
// ----逻辑校验----
// ----业务处理----
// 检查和校验整理输入
log.info("shopId " + shopId + " start saveTask");
List<MoveProductDTO> productSimpleDetails = fields.getProducts();
String copyType = MoveConst.Collect_DS_COPY;
log.info("shopId " + shopId + " start checkAndFormatData");
Ret checkAndFormatRet = checkAndFormatData(shopId, productSimpleDetails);
log.info("shopId " + shopId + " end checkAndFormatData");
if (CommonTool.isFailRet(checkAndFormatRet)) {
return R.ok(checkAndFormatRet);
}
List<MoveProductDTO> curProductDetails = (List<MoveProductDTO>) checkAndFormatRet.getData().get("curProductDetails");
MoveConditionBO shopConditions = (MoveConditionBO) checkAndFormatRet.getData().get("shopConditions");
log.info("shopId " + shopId + " start allocShopProductsByAuthShop");
List<MoveTaskProductBO> shopProducts = allocShopProductsByAuthShop(curProductDetails);
// 保存任务
log.info("shopId " + shopId + " start saveTaskByAuthShop");
Ret saveRet = saveTaskByAuthShop(shopId, copyType, shopConditions, shopProducts);
if (CommonTool.isFailRet(saveRet)) {
return R.ok(saveRet);
}
// 检查商品任务有效数量
int shopTaskId = (Integer) saveRet.getData().get("taskId");
log.info("shopId " + shopId + " session shopId saveTask success, shopTaskId" + shopTaskId);
int submitCnt = shopProducts.size();
if (submitCnt == 0) {
Ret ret = CommonTool.failResult();
ret.setReason("提交商品数为0请检查是否正常提交商品");
// 检查商品任务有效数量
int shopTaskId = (Integer) saveRet.getData().get("taskId");
log.info("shopId " + shopId + " session shopId saveTask success, shopTaskId" + shopTaskId);
int submitCnt = shopProducts.size();
if (submitCnt == 0) {
Ret ret = CommonTool.failResult();
ret.setReason("提交商品数为0请检查是否正常提交商品");
StrObjMap retData = new StrObjMap();
retData.put("taskId", shopTaskId);
retData.put("submitCnt", submitCnt);
ret.setData(retData);
return R.ok(ret);
}
// ----结果返回----
Ret ret = new Ret();
ret.setResult(CommonConst.SUCCESS);
StrObjMap retData = new StrObjMap();
retData.put("taskId", shopTaskId);
retData.put("submitCnt", submitCnt);
ret.setData(retData);
return R.ok(ret);
} catch (Throwable e) {
e.printStackTrace();
return R.ok(CommonTool.failResult(e.getMessage()));
}
// ----结果返回----
Ret ret = new Ret();
ret.setResult(CommonConst.SUCCESS);
StrObjMap retData = new StrObjMap();
retData.put("taskId", shopTaskId);
retData.put("submitCnt", submitCnt);
ret.setData(retData);
return R.ok(ret);
}
private Ret checkAndFormatData(Long shopId, List<MoveProductDTO> curProductDetails) {
@ -133,24 +139,24 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
MoveShopSetting shopMoveCondition = moveShopSettingService.getDetailByShopId(shopId);
if (Objects.isNull(shopMoveCondition)) {
Ret ret = CommonTool.failResult();
ret.setReason("shopId:"+shopId+",搬家配置数据不完整,提交失败。请联系客服!");
ret.setReason("shopId:" + shopId + ",搬家配置数据不完整,提交失败。请联系客服!");
return ret;
}
// 检查搬家条件
Ret conditionRet = getTaskCondition(shopMoveCondition);
if (CommonTool.isFailRet(conditionRet)){
if (CommonTool.isFailRet(conditionRet)) {
Ret ret = CommonTool.failResult();
ret.setReason("shopId:"+shopId+",搬家配置数据错误,提交失败。请联系客服!");
ret.setReason("shopId:" + shopId + ",搬家配置数据错误,提交失败。请联系客服!");
return ret;
}
MoveConditionBO moveCondition = (MoveConditionBO)conditionRet.getData().get("condition");
MoveConditionBO moveCondition = (MoveConditionBO) conditionRet.getData().get("condition");
// 店铺过期检查
boolean shopIsExpire = checkShopIsExpire(shopId);
if (!shopIsExpire) {
Ret ret = CommonTool.failResult();
ret.setReason("店铺【"+shopInfo.getShopName()+"】,已过期,请联系客服续费!");
ret.setReason("店铺【" + shopInfo.getShopName() + "】,已过期,请联系客服续费!");
return ret;
}
@ -163,7 +169,7 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
if (productTotal > moveConfig.getAvailableTotal()) { // TODO 原先没有availableTotal字段
Shop shopInfo = shopService.getDetailById(shopId);
Ret ret = CommonTool.failResult();
ret.setReason("店铺【"+shopInfo.getShopName()+"】,搬家次数不足,请联系客服!");
ret.setReason("店铺【" + shopInfo.getShopName() + "】,搬家次数不足,请联系客服!");
return ret;
}
@ -188,12 +194,13 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
/**
*
*
* @param setting
* @return
*/
private Ret getTaskCondition(MoveShopSetting setting) {
MoveConditionBO condition = new MoveConditionBO();
MovePriceBO priceBO = new MovePriceBO();
MovePriceBO priceBO = new MovePriceBO();
condition.setMovePriceBO(priceBO);
if (!Objects.isNull(setting.getDeliveryDelayDay())) {
condition.setDeliveryDelayDay(setting.getDeliveryDelayDay());
@ -205,15 +212,15 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
condition.setNoConsignPriceType(setting.getNoConsignPriceType());
if(setting.getPriceTail() >= 0){
if (setting.getPriceTail() >= 0) {
priceBO.setPriceTail(setting.getPriceTail());
}
if(setting.getMoveOnsaleType() > 0){
if (setting.getMoveOnsaleType() > 0) {
condition.setMoveOnsaleTyp(setting.getMoveOnsaleType());
}
if(!Objects.isNull(setting.getFreightTemplateId()) && setting.getFreightTemplateId() > 0){
if (!Objects.isNull(setting.getFreightTemplateId()) && setting.getFreightTemplateId() > 0) {
condition.setFreightTemplateId(setting.getFreightTemplateId());
}
@ -225,7 +232,7 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
condition.setSupply7dayReturn(setting.getSupply7dayReturn());
}
return CommonTool.successResult("condition",condition);
return CommonTool.successResult("condition", condition);
}
public Map<Long, MoveProductDTO> getDsDetails(List<MoveProductDTO> products) {
@ -247,25 +254,17 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
return addRet;
}
private Ret addShopCollectTask(long shopId, List<MoveTaskProductBO> shopProductsList, String name, String copyType, MoveConditionBO shopConditions) {
// TODO 添加事务
// int oldErrorMode = db.setErrorMode(ZcDb.ERROR_MODE_EXCEPTION);
// boolean transStatus = db.startTransaction();
@Transactional(rollbackFor = Exception.class)
public Ret addShopCollectTask(long shopId, List<MoveTaskProductBO> shopProductsList, String name, String copyType, MoveConditionBO shopConditions) {
int taskId = 0;
String cnaliDsDetailMap = "";
try {
Ret addRet = this.addCollectTask(shopId, copyType, name, shopConditions, shopProductsList);
if (CommonTool.isSuccessRet(addRet)) {
taskId = (Integer) addRet.getData().get("taskId");
cnaliDsDetailMap = (String)addRet.getData().get("cnaliDsDetailMap");
cnaliDsDetailMap = (String) addRet.getData().get("cnaliDsDetailMap");
}
// db.commit(transStatus);
// db.setErrorMode(oldErrorMode);
} catch (Exception ex) {
// moveLog.error(ex.getMessage());
// db.rollback(transStatus);
// db.setErrorMode(oldErrorMode);
ex.printStackTrace();
return CommonTool.failResult("添加任务数据失败");
}
@ -288,15 +287,15 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
}
Ret checkAndGetConfigIdRet = checkAvailableCopyAndGetMoveConfigId(shopId, productTotal);
if (CommonTool.isFailRet(checkAndGetConfigIdRet)) {
throw new RuntimeException("shopId " +shopId+ checkAndGetConfigIdRet.getReason());
throw new RuntimeException("shopId " + shopId + checkAndGetConfigIdRet.getReason());
}
int moveShopConfigId = (Integer)checkAndGetConfigIdRet.getData().get("moveShopConfigId");
int moveShopConfigId = (Integer) checkAndGetConfigIdRet.getData().get("moveShopConfigId");
// 商品复制次数扣减
if (productTotal > 0) {
Ret deductRet = deductMoveCopyCount(shopId, productTotal, moveShopConfigId);
if (CommonTool.isFailRet(deductRet)) {
throw new RuntimeException("shopId " +shopId+ deductRet.getReason());
throw new RuntimeException("shopId " + shopId + deductRet.getReason());
}
}
@ -376,10 +375,10 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
}
private Map<Long, MoveTaskProductBO> getDsTaskDetails(List<MoveTaskProductBO> products) {
Map<Long,MoveTaskProductBO> dsDetails = new HashMap<>();
Map<Long, MoveTaskProductBO> dsDetails = new HashMap<>();
products.forEach(product -> {
if (product.isFromDs() && StringUtils.isBlank(product.getCnaliDsDetailId())) { // TODO
dsDetails.put(product.getSourceItemId(),product);
dsDetails.put(product.getSourceItemId(), product);
}
});

Loading…
Cancel
Save