From 22f78366d22d1af6e4c80fe89cd4c5ab7fa88d60 Mon Sep 17 00:00:00 2001 From: qiushengjie Date: Wed, 6 Sep 2023 18:25:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/ms/api/TestSpiService.java | 42 ++--- ms-biz/src/main/java/com/ms/api/common/E.java | 14 ++ .../java/com/ms/api/common/MSException.java | 16 ++ .../spi/move/SaveBatchMoveTaskService.java | 143 +++++++++--------- 4 files changed, 113 insertions(+), 102 deletions(-) create mode 100644 ms-biz/src/main/java/com/ms/api/common/E.java create mode 100644 ms-biz/src/main/java/com/ms/api/common/MSException.java diff --git a/ms-biz/src/main/java/com/ms/api/TestSpiService.java b/ms-biz/src/main/java/com/ms/api/TestSpiService.java index e3d4842b..0e3f49f3 100644 --- a/ms-biz/src/main/java/com/ms/api/TestSpiService.java +++ b/ms-biz/src/main/java/com/ms/api/TestSpiService.java @@ -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 { - @Autowired - private DsJsonRequestTemplate dsJsonRequestTemplate; +public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler { + @Override - public BaseResponse handle(BaseRequest req) { - initHandle(req); - getAuthCode(); - HashMap 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 handle(BaseRequest 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); } } diff --git a/ms-biz/src/main/java/com/ms/api/common/E.java b/ms-biz/src/main/java/com/ms/api/common/E.java new file mode 100644 index 00000000..f8558d35 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/common/E.java @@ -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); + } +} diff --git a/ms-biz/src/main/java/com/ms/api/common/MSException.java b/ms-biz/src/main/java/com/ms/api/common/MSException.java new file mode 100644 index 00000000..73ceefc9 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/common/MSException.java @@ -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); + } +} + diff --git a/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java b/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java index ea19d3b1..85db1dae 100644 --- a/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java +++ b/ms-biz/src/main/java/com/ms/api/spi/move/SaveBatchMoveTaskService.java @@ -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 handle(BaseRequest req) { - initHandle(req); - // ----参数校验---- - SaveBatchMoveTaskRequestDTO fields = req.getData(); - - // ----逻辑校验---- - - - // ----业务处理---- - // 检查和校验整理输入 - log.info("shopId " + shopId + " start saveTask"); - List 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 curProductDetails = (List) checkAndFormatRet.getData().get("curProductDetails"); - MoveConditionBO shopConditions = (MoveConditionBO) checkAndFormatRet.getData().get("shopConditions"); - log.info("shopId " + shopId + " start allocShopProductsByAuthShop"); - List 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 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 curProductDetails = (List) checkAndFormatRet.getData().get("curProductDetails"); + MoveConditionBO shopConditions = (MoveConditionBO) checkAndFormatRet.getData().get("shopConditions"); + log.info("shopId " + shopId + " start allocShopProductsByAuthShop"); + List 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 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 getDsDetails(List products) { @@ -247,25 +254,17 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio return addRet; } - private Ret addShopCollectTask(long shopId, List 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 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 getDsTaskDetails(List products) { - Map dsDetails = new HashMap<>(); + Map dsDetails = new HashMap<>(); products.forEach(product -> { if (product.isFromDs() && StringUtils.isBlank(product.getCnaliDsDetailId())) { // TODO - dsDetails.put(product.getSourceItemId(),product); + dsDetails.put(product.getSourceItemId(), product); } });