|
|
@@ -8,6 +8,7 @@ import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
@@ -52,6 +53,7 @@ import com.sckw.order.model.vo.res.*;
|
|
|
import com.sckw.order.model.vo.res.GoodsInfoDetailRes;
|
|
|
import com.sckw.order.model.vo.res.OrderDetailRes;
|
|
|
import com.sckw.order.model.vo.res.UnitInfoDetailRes;
|
|
|
+import com.sckw.order.repository.KwoTradeOrderUnitRepository;
|
|
|
import com.sckw.payment.api.dubbo.PayCenterDubboService;
|
|
|
import com.sckw.payment.api.dubbo.PaymentDubboService;
|
|
|
import com.sckw.payment.api.model.WalletFreeze;
|
|
|
@@ -142,6 +144,7 @@ public class KwoTradeOrderService {
|
|
|
private final KwoTradeOrderGoodsUnitService kwoTradeOrderGoodsUnitService;
|
|
|
private final KwoTradeOrderAmountService tradeOrderAmountService;
|
|
|
private final KwoTradeOrderTransportService kwoTradeOrderTransportService;
|
|
|
+ private final KwoTradeOrderUnitRepository kwoTradeOrderUnitRepository;
|
|
|
@Value("${url.order.list.valet.pc}")
|
|
|
private String pcValetListUrl;
|
|
|
|
|
|
@@ -1811,6 +1814,66 @@ public class KwoTradeOrderService {
|
|
|
* @Param params:
|
|
|
* @return: java.util.List<com.sckw.report.service.param.TradeOrderListExport>
|
|
|
*/
|
|
|
+
|
|
|
+
|
|
|
+ public TradeOrderCountResp countStatistic() {
|
|
|
+ Long entId = LoginUserHolder.getEntId();
|
|
|
+ Set<Long> tradeOrderIds = kwoTradeOrderUnitRepository
|
|
|
+ .queryByEntIdAndUnitType(entId, OrderUnitTypeEnum.PURCHASE.getType())
|
|
|
+ .stream()
|
|
|
+ .map(KwoTradeOrderUnit::getTOrderId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ Long totalCount = 0L;
|
|
|
+ Map<Integer, Long> statusCountMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
|
|
|
+ if (CollUtil.isNotEmpty(tradeOrderIds)) {
|
|
|
+ QueryWrapper<KwoTradeOrder> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.select("status", "COUNT(1) AS statusCount")
|
|
|
+ .eq("del_flag", Global.NO)
|
|
|
+ .in("id", tradeOrderIds)
|
|
|
+ .groupBy("status")
|
|
|
+ .orderByAsc("status");
|
|
|
+
|
|
|
+ List<Map<String, Object>> statisticMaps = kwoTradeOrderMapper.selectMaps(queryWrapper);
|
|
|
+ if (CollUtil.isNotEmpty(statisticMaps)) {
|
|
|
+ statusCountMap = statisticMaps.stream()
|
|
|
+ .filter(item -> Objects.nonNull(item.get("status")))
|
|
|
+ .collect(Collectors.toMap(item -> Integer.parseInt(String.valueOf(item.get("status"))),
|
|
|
+ item -> Long.parseLong(String.valueOf(item.get("statusCount"))),
|
|
|
+ Long::sum));
|
|
|
+ }
|
|
|
+
|
|
|
+ totalCount = Optional.ofNullable(kwoTradeOrderMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<KwoTradeOrder>()
|
|
|
+ .eq(KwoTradeOrder::getDelFlag, Global.NO)
|
|
|
+ .in(KwoTradeOrder::getId, tradeOrderIds))).orElse(0L);
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<Integer> statuses = new TreeSet<>();
|
|
|
+ TradeOrderStatusEnum.getSortList().stream().map(TradeOrderStatusEnum::getCode).forEach(statuses::add);
|
|
|
+ statuses.addAll(statusCountMap.keySet());
|
|
|
+
|
|
|
+ List<TradeOrderCountResp.TradeOrderCountItem> items = new ArrayList<>(statuses.size());
|
|
|
+ for (Integer status : statuses) {
|
|
|
+ TradeOrderCountResp.TradeOrderCountItem item = new TradeOrderCountResp.TradeOrderCountItem();
|
|
|
+ item.setStatus(status)
|
|
|
+ .setStatusDesc(getTradeOrderStatusDesc(status))
|
|
|
+ .setStatusCount(statusCountMap.getOrDefault(status, 0L));
|
|
|
+ items.add(item);
|
|
|
+ }
|
|
|
+ return new TradeOrderCountResp().setTotalCount(totalCount).setItems(items);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTradeOrderStatusDesc(Integer status) {
|
|
|
+ String statusDesc = TradeOrderStatusEnum.getMsg(status);
|
|
|
+ if (StrUtil.isNotBlank(statusDesc)) {
|
|
|
+ return statusDesc;
|
|
|
+ }
|
|
|
+ statusDesc = OrderStatusEnum.getMsg(status);
|
|
|
+ return StrUtil.isNotBlank(statusDesc) ? statusDesc : "unknown";
|
|
|
+ }
|
|
|
+
|
|
|
public List<TradeOrderListExport> export(TradeOrderListExportParam params) {
|
|
|
TradeOrderListSelectDTO dto = new TradeOrderListSelectDTO();
|
|
|
List<Long> ids = new ArrayList<>();
|
|
|
@@ -1948,6 +2011,39 @@ public class KwoTradeOrderService {
|
|
|
KwpGoods goodsById = goodsInfoService.getGoodsById(tradeOrderParam.getGoodsId());
|
|
|
|
|
|
TradeContractResDto tradeContractResDto = checkPara(tradeOrderParam, order, goodsById);
|
|
|
+
|
|
|
+ // ====== 下单前校验 start ======
|
|
|
+ // 1. 验证线下钱包预付余额是否足够,不足则提示"线下钱包预付余额不足"
|
|
|
+ // TODO: 后续对接外部系统,验证线下钱包预付余额
|
|
|
+ // validateOfflineWalletBalance(buyEntId, order.getPrice());
|
|
|
+
|
|
|
+ // 2. 验证商品是否开启库存(amount不为null代表已开启库存,null代表无限库存无需验证)
|
|
|
+ if (goodsById.getAmount() != null && goodsById.getAmount().compareTo(tradeOrderParam.getAmount()) < 0) {
|
|
|
+ if (!Boolean.TRUE.equals(tradeOrderParam.getConfirmInsufficientStock())) {
|
|
|
+ throw new CustomPromptException(HttpStatus.STOCK_INSUFFICIENT_CONFIRM_CODE, "当前库存可能不足,是否依旧下单?");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 线下钱包扣减预付余额、增加冻结金额
|
|
|
+ // TODO: 后续对接外部系统,执行线下钱包扣减预付余额、增加冻结金额
|
|
|
+ // deductOfflineWalletAndFreeze(buyEntId, order.getPrice());
|
|
|
+
|
|
|
+ // 4. 记录钱包金额账数据,类型为冻结,备注为"贸易订单号:xxx 下单冻结"
|
|
|
+ // TODO: 后续对接外部系统,记录钱包流水
|
|
|
+ // recordWalletTransaction(buyEntId, order.getPrice(), "冻结", "贸易订单号:" + order.getTOrderNo() + " 下单冻结");
|
|
|
+
|
|
|
+ // 5. 验证贸易合同限量采购,若为限量采购则校验剩余可采购数量并扣减本次下单量
|
|
|
+ GoodsInfoDto contractGoodsInfo = tradeContractResDto.getGoodsInfoDto();
|
|
|
+ if (contractGoodsInfo.getAmount() != null && contractGoodsInfo.getAmount().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ BigDecimal performed = contractGoodsInfo.getPerformedAmount() != null ? contractGoodsInfo.getPerformedAmount() : BigDecimal.ZERO;
|
|
|
+ BigDecimal remaining = contractGoodsInfo.getAmount().subtract(performed);
|
|
|
+ if (tradeOrderParam.getAmount().compareTo(remaining) > 0) {
|
|
|
+ throw new BusinessException("当前贸易合同该商品为限量采购,剩余可采购数量为" + remaining.stripTrailingZeros().toPlainString() + ",本次下单数量超出限额");
|
|
|
+ }
|
|
|
+ remoteContractService.updateTradeContractGoodsPerformed(tradeOrderParam.getTradeContractId(), tradeOrderParam.getGoodsId(), tradeOrderParam.getAmount());
|
|
|
+ }
|
|
|
+ // ====== 下单前校验 end ======
|
|
|
+
|
|
|
WalletFreeze walletFreeze = new WalletFreeze();
|
|
|
walletFreeze.setOrderNo(order.getTOrderNo());
|
|
|
walletFreeze.setMoney(order.getPrice());
|
|
|
@@ -2086,6 +2182,59 @@ public class KwoTradeOrderService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 买家撤销贸易订单
|
|
|
+ */
|
|
|
+ public void cancelTradeOrder(CancelTradeOrderParam param) {
|
|
|
+ KwoTradeOrder order = kwoTradeOrderMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<KwoTradeOrder>()
|
|
|
+ .eq(KwoTradeOrder::getId, param.getId())
|
|
|
+ .eq(KwoTradeOrder::getDelFlag, 0)
|
|
|
+ );
|
|
|
+ if (Objects.isNull(order)) {
|
|
|
+ throw new BusinessException("贸易订单不存在");
|
|
|
+ }
|
|
|
+ if (!Objects.equals(order.getStatus(), TradeOrderStatusEnum.AUDIT.getCode())) {
|
|
|
+ throw new BusinessException("当前订单状态不允许撤销");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 线下钱包加回预付余额、减冻结金额
|
|
|
+ // TODO: 后续对接外部系统,执行线下钱包加回预付余额、减冻结金额
|
|
|
+ // refundOfflineWalletPrepaid(buyEntId, order.getPrice());
|
|
|
+
|
|
|
+ // 2. 记录钱包金额账数据,类型为解冻,备注为"贸易订单号:xxx 撤销订单"
|
|
|
+ // TODO: 后续对接外部系统,记录钱包流水
|
|
|
+ // recordWalletTransaction(buyEntId, order.getPrice(), "解冻", "贸易订单号:" + order.getTOrderNo() + " 撤销订单");
|
|
|
+
|
|
|
+ // 线上钱包解冻
|
|
|
+ WalletFreeze walletFreeze = new WalletFreeze();
|
|
|
+ walletFreeze.setTTradeOrderId(order.getId());
|
|
|
+ walletFreeze.setOrderNo(order.getTOrderNo());
|
|
|
+ BaseResult<Boolean> unfreezeResult = paymentDubboService.unfreezeMoney(walletFreeze);
|
|
|
+ if (unfreezeResult.getCode() != 60200) {
|
|
|
+ throw new BusinessException(unfreezeResult.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 贸易合同该商品有采购数量,则更新采购数量(加回本单总量)
|
|
|
+ KwoTradeOrderGoods orderGoods = kwoTradeOrderGoodsService.getByOrderId(order.getId());
|
|
|
+ KwoTradeOrderContract orderContract = kwoTradeOrderContractService.getByOrderId(order.getId());
|
|
|
+ if (Objects.nonNull(orderGoods) && Objects.nonNull(orderContract)) {
|
|
|
+ TradeContractResDto contractRes = remoteContractService.queryTradeContract(orderContract.getContractId(), orderGoods.getGoodsId());
|
|
|
+ if (Objects.nonNull(contractRes) && Objects.nonNull(contractRes.getGoodsInfoDto())) {
|
|
|
+ GoodsInfoDto goodsInfo = contractRes.getGoodsInfoDto();
|
|
|
+ if (goodsInfo.getAmount() != null && goodsInfo.getAmount().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ remoteContractService.updateTradeContractGoodsPerformed(
|
|
|
+ orderContract.getContractId(), orderGoods.getGoodsId(), order.getAmount().negate()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 更新贸易订单状态为"取消"
|
|
|
+ order.setStatus(TradeOrderStatusEnum.CANCEL.getCode());
|
|
|
+ kwoTradeOrderMapper.updateById(order);
|
|
|
+ }
|
|
|
+
|
|
|
private TradeContractResDto checkPara(TradeOrderParam tradeOrderParam, KwoTradeOrder order, KwpGoods goodsById) {
|
|
|
if (Objects.isNull(goodsById) || Objects.equals(goodsById.getDelFlag(), Global.YES)) {
|
|
|
throw new BusinessException("商品信息不存在");
|