|  |  |  | 
|---|
|  |  |  | package cc.mrbird.febs.mall.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.OrderRefundStatusEnum; | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.OrderStatusEnum; | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.*; | 
|---|
|  |  |  | import cc.mrbird.febs.common.exception.FebsException; | 
|---|
|  |  |  | import cc.mrbird.febs.common.utils.AppContants; | 
|---|
|  |  |  | import cc.mrbird.febs.common.utils.LoginUserUtil; | 
|---|
|  |  |  | 
|---|
|  |  |  | import cc.mrbird.febs.mall.dto.*; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.*; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.*; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallMemberService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallMemberWalletService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallOrderInfoService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.MallMemberService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.vo.OrderDetailVo; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.vo.OrderListVo; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.vo.OrderRefundVo; | 
|---|
|  |  |  | import cc.mrbird.febs.pay.service.IPayService; | 
|---|
|  |  |  | import cc.mrbird.febs.rabbit.producter.AgentProducer; | 
|---|
|  |  |  | import cn.hutool.core.collection.CollUtil; | 
|---|
|  |  |  | import cn.hutool.core.date.DateUnit; | 
|---|
|  |  |  | import cn.hutool.core.date.DateUtil; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Random; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author wzy | 
|---|
|  |  |  | 
|---|
|  |  |  | private final MallOrderRefundMapper mallOrderRefundMapper; | 
|---|
|  |  |  | private final MallOrderRefundOperationMapper mallOrderRefundOperationMapper; | 
|---|
|  |  |  | private final MallShoppingCartMapper mallShoppingCartMapper; | 
|---|
|  |  |  | private final IApiMallMemberService memberService; | 
|---|
|  |  |  | private final RedisUtils redisUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final AgentProducer agentProducer; | 
|---|
|  |  |  | private final IPayService payService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (sku.getStock() < item.getCnt()) { | 
|---|
|  |  |  | throw new FebsException("库存不足"); | 
|---|
|  |  |  | throw new FebsException(sku.getSkuName() + "库存不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId()); | 
|---|
|  |  |  | if (MallGoods.ISSALE_STATUS_DISABLED.equals(mallGoods.getIsSale())) { | 
|---|
|  |  |  | throw new FebsException(mallGoods.getGoodsName() + "已下架"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MallOrderItem orderItem = new MallOrderItem(); | 
|---|
|  |  |  | 
|---|
|  |  |  | orderInfo.setAmount(total); | 
|---|
|  |  |  | this.baseMapper.updateById(orderInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String redisKey = StrUtil.format(AppContants.REDIS_ORDER_OVERTIME, member.getId(), orderInfo.getId()); | 
|---|
|  |  |  | redisUtils.set(redisKey, orderInfo.getId(), 900); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | agentProducer.sendOrderCancelDelayMsg(orderInfo.getId(), 15 * 60 * 1000L); | 
|---|
|  |  |  | return orderInfo.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public String payOrder(PayOrderDto payOrderDto) { | 
|---|
|  |  |  | public Map<String, Object> payOrder(PayOrderDto payOrderDto) { | 
|---|
|  |  |  | MallMember member = LoginUserUtil.getLoginUser(); | 
|---|
|  |  |  | MallOrderInfo orderInfo = this.baseMapper.selectOrderByMemberIdAndId(member.getId(), payOrderDto.getId()); | 
|---|
|  |  |  | if (orderInfo == null || AppContants.DEL_FLAG_Y == orderInfo.getDelFlag()) { | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new FebsException("订单状态不能支付"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String payResultStr = ""; | 
|---|
|  |  |  | switch (payOrderDto.getType()) { | 
|---|
|  |  |  | case "1": | 
|---|
|  |  |  | // TODO 微信支付 | 
|---|
|  |  |  | //                orderInfo.setPayOrderNo(payOrderDto.getPayOrderNo()); | 
|---|
|  |  |  | //                orderInfo.setPayImage(payOrderDto.getPayImage()); | 
|---|
|  |  |  | orderInfo.setPayMethod("微信支付"); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "2": | 
|---|
|  |  |  | // TODO 支付宝支付 | 
|---|
|  |  |  | if (StrUtil.isNotBlank(orderInfo.getPayOrderNo())) { | 
|---|
|  |  |  | payResultStr = orderInfo.getPayOrderNo(); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | payResultStr = payService.aliPay(orderInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | orderInfo.setPayOrderNo(payResultStr); | 
|---|
|  |  |  | orderInfo.setPayMethod("支付宝支付"); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case "3": | 
|---|
|  |  |  | return balancePay(orderInfo, payOrderDto.getTradePwd()); | 
|---|
|  |  |  | payResultStr = balancePay(orderInfo, payOrderDto.getTradePwd()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | orderInfo.setPayOrderNo(orderInfo.getOrderNo()); | 
|---|
|  |  |  | orderInfo.setPayMethod("余额支付"); | 
|---|
|  |  |  | orderInfo.setStatus(OrderStatusEnum.WAIT_SHIPPING.getValue()); | 
|---|
|  |  |  | orderInfo.setPayTime(new Date()); | 
|---|
|  |  |  | orderInfo.setPayResult("1"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                MallMember mallMember = memberMapper.selectById(member.getId()); | 
|---|
|  |  |  | //                if (AgentLevelEnum.ZERO_LEVEL.name().equals(mallMember.getLevel())) { | 
|---|
|  |  |  | //                    mallMember.setLevel(AgentLevelEnum.FIRST_LEVEL.name()); | 
|---|
|  |  |  | //                    memberMapper.updateById(mallMember); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 静态倍数 | 
|---|
|  |  |  | List<MallOrderItem> orderItems = this.baseMapper.getMallOrderItemByOrderId(orderInfo.getId()); | 
|---|
|  |  |  | if (CollUtil.isNotEmpty(orderItems)) { | 
|---|
|  |  |  | for (MallOrderItem orderItem : orderItems) { | 
|---|
|  |  |  | MallGoods mallGoods = mallGoodsMapper.selectById(orderItem.getGoodsId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MallGoodsSku sku = mallGoodsSkuMapper.selectById(orderItem.getSkuId()); | 
|---|
|  |  |  | BigDecimal score = sku.getOriginalPrice().multiply(mallGoods.getStaticMultiple()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletService.add(score, member.getId(), "score"); | 
|---|
|  |  |  | memberService.addMoneyFlow(member.getId(), score, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderInfo.getOrderNo(), null, null, null, null, FlowTypeEnum.SCORE.getValue()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberService.addMoneyFlow(member.getId(), orderInfo.getAmount().negate(), MoneyFlowTypeEnum.PAY.getValue(), orderInfo.getOrderNo(), null, null, null, null, FlowTypeEnum.BALANCE.getValue()); | 
|---|
|  |  |  | //                agentProducer.sendAutoLevelUpMsg(member.getId()); | 
|---|
|  |  |  | //                agentProducer.sendReturnMoneyMsg(orderInfo.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | default: | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ""; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.baseMapper.updateById(orderInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | map.put("orderInfo", payResultStr); | 
|---|
|  |  |  | map.put("orderNo", orderInfo.getOrderNo()); | 
|---|
|  |  |  | map.put("orderId", orderInfo.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private String balancePay(MallOrderInfo orderInfo, String tradePwd) { | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new FebsException("支付密码错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | memberWalletService.reduceBalance(orderInfo.getAmount(), mallMember.getId()); | 
|---|
|  |  |  | memberWalletService.reduce(orderInfo.getAmount(), mallMember.getId(), "balance"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | orderInfo.setStatus(OrderStatusEnum.WAIT_SHIPPING.getValue()); | 
|---|
|  |  |  | orderInfo.setPayTime(new Date()); | 
|---|
|  |  |  | orderInfo.setPayMethod("积分支付"); | 
|---|
|  |  |  | orderInfo.setPayOrderNo(orderInfo.getOrderNo()); | 
|---|
|  |  |  | orderInfo.setPayResult("1"); | 
|---|
|  |  |  | this.baseMapper.updateById(orderInfo); | 
|---|
|  |  |  | return orderInfo.getOrderNo(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | operation.setContent(StrUtil.format("用户:{}提交物流信息,快递公司:{},单号:{}", member.getName(), refundExpressDto.getExpressCom(), refundExpressDto.getExpressNo())); | 
|---|
|  |  |  | mallOrderRefundOperationMapper.insert(operation); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void autoCancelOrder(Long id) { | 
|---|
|  |  |  | MallOrderInfo orderInfo = this.baseMapper.selectById(id); | 
|---|
|  |  |  | if (orderInfo == null) { | 
|---|
|  |  |  | log.error("自动取消订单参数错误:{}", id); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (orderInfo.getStatus() == OrderStatusEnum.WAIT_PAY.getValue()) { | 
|---|
|  |  |  | log.info("自动取消订单:{},{}", orderInfo.getMemberId(), id); | 
|---|
|  |  |  | orderInfo.setStatus(OrderStatusEnum.CANCEL.getValue()); | 
|---|
|  |  |  | orderInfo.setCancelType(MallOrderInfo.CANCEL_OVERTIME_NO_PAY); | 
|---|
|  |  |  | this.baseMapper.updateById(orderInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|