package com.yeshi.fanli.service.impl.shop;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.aliyun.openservices.ons.api.Message;
|
import com.aliyun.openservices.ons.api.Producer;
|
import com.aliyun.openservices.ons.api.SendResult;
|
import com.google.gson.Gson;
|
import com.yeshi.fanli.dao.mybatis.shop.BanLiShopOrderMapper;
|
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
|
import com.yeshi.fanli.dto.mq.order.body.BanLiShopOrderMQMsg;
|
import com.yeshi.fanli.entity.mq.MQUnSendInfo;
|
import com.yeshi.fanli.entity.redpack.RedPackDetail;
|
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
|
import com.yeshi.fanli.entity.shop.BanLiShopGoodsClass;
|
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets;
|
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
|
import com.yeshi.fanli.exception.mq.MQUnSendInfoException;
|
import com.yeshi.fanli.exception.redpack.RedPackBalanceException;
|
import com.yeshi.fanli.exception.redpack.RedPackDetailException;
|
import com.yeshi.fanli.exception.shop.BanLiShopOrderException;
|
import com.yeshi.fanli.service.inter.mq.MQUnSendInfoService;
|
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsClassService;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
|
import com.yeshi.fanli.util.rocketmq.MQTopicName;
|
import com.yeshi.fanli.util.shop.BanLiShopOrderUtil;
|
|
@Service
|
public class BanLiShopOrderServiceImpl implements BanLiShopOrderService {
|
|
@Resource
|
private BanLiShopOrderMapper banLiShopOrderMapper;
|
|
@Resource
|
private BanLiShopGoodsSetService banLiShopGoodsSetService;
|
|
@Resource
|
private BanLiShopGoodsService banLiShopGoodsService;
|
|
@Resource
|
private BanLiShopGoodsClassService banLiShopGoodsClassService;
|
|
@Resource
|
private RedPackBalanceService redPackBalanceService;
|
|
@Resource(name = "producer")
|
private Producer producer;
|
|
@Resource
|
private MQUnSendInfoService mqUnSendInfoService;
|
|
@Transactional
|
@Override
|
public void addOrder(BanLiShopOrder order) throws BanLiShopOrderException {
|
// 查询必要的参数是否添加
|
if (order.getGoodsSet() == null || order.getGoodsSet().getId() == null || order.getGoods() == null
|
|| order.getGoods().getId() == null)
|
throw new BanLiShopOrderException(1, "未选择商品");
|
|
if (order.getUid() == null)
|
throw new BanLiShopOrderException(1, "缺少用户ID");
|
|
if (order.getHongBaoPayment() == null || order.getBalancePayment() == null || order.getMoneyPayment() == null) {
|
throw new BanLiShopOrderException(1, "未选择支付方式");
|
}
|
|
if (order.getHongBaoPayment() != null) {
|
order.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
|
}
|
|
if (order.getBalancePayment() != null) {
|
order.setBalancePaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
|
}
|
|
if (order.getMoneyPayment() != null) {
|
order.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_NOPAY);
|
}
|
|
order.setState(BanLiShopOrder.STATE_NO_PAY);
|
// 判断商品套餐是否存在
|
BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
|
if (set == null) {
|
throw new BanLiShopOrderException(1, "商品套餐不存在");
|
}
|
|
BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
|
if (goods == null) {
|
throw new BanLiShopOrderException(1, "商品已下线");
|
}
|
|
if (goods.getCreateTime() == null)
|
goods.setCreateTime(new Date());
|
|
banLiShopOrderMapper.insertSelective(order);
|
|
// 添加订单号
|
String orderNo = BanLiShopOrderUtil.createOrderNo(order.getId());
|
// 更新订单号
|
BanLiShopOrder update = new BanLiShopOrder();
|
update.setId(order.getId());
|
update.setOrderNo(orderNo);
|
banLiShopOrderMapper.updateByPrimaryKeySelective(update);
|
|
// 订单添加成功 ,延时通知后续
|
sendPlaceOrderMsg(order.getId(), order.getUid());
|
}
|
|
private void sendPlaceOrderMsg(Long orderId, Long uid) {
|
|
Message msg = new Message(MQTopicName.TOPIC_ORDER.name(), OrderTopicTagEnum.banLiShopOrderDelay.name(),
|
new Gson().toJson(new BanLiShopOrderMQMsg(orderId, uid)).getBytes());
|
msg.setKey(orderId + "");
|
long delayTime = System.currentTimeMillis() + (Constant.IS_TEST ? 1000 * 30L : 1000 * 60 * 10);// 10分钟后通知
|
msg.setStartDeliverTime(delayTime);
|
SendResult sendResult = producer.send(msg);
|
if (sendResult == null) {
|
MQUnSendInfo info = new MQUnSendInfo();
|
info.setBody(new String(msg.getBody()));
|
if (msg.getStartDeliverTime() > 0)
|
info.setDeliverTime(new Date(msg.getStartDeliverTime()));
|
info.setKey(msg.getKey());
|
info.setTag(msg.getTag());
|
info.setTopic(msg.getTopic());
|
try {
|
mqUnSendInfoService.addMQUnSendInfo(info);
|
} catch (MQUnSendInfoException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
@Override
|
public List<BanLiShopOrder> listByUid(Long uid, int page, int pageSize) {
|
return null;
|
}
|
|
@Override
|
public long countByUid(Long uid) {
|
return 0;
|
}
|
|
@Override
|
public BanLiShopOrder selectByPrimaryKey(Long id) {
|
return null;
|
}
|
|
@Override
|
public void udpateSelectiveByPrimaryKey(BanLiShopOrder order) {
|
|
}
|
|
@Transactional
|
@Override
|
public void payOrderByHongBao(Long orderId) throws BanLiShopOrderException, RedPackBalanceException {
|
BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(orderId);
|
if (order == null) {
|
throw new BanLiShopOrderException(1, "订单不存在");
|
}
|
if (order.getHongBaoPayment() == null)
|
throw new BanLiShopOrderException(2, "不需要采用红包支付");
|
|
if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
|
throw new BanLiShopOrderException(3, "重复支付");
|
}
|
|
BanLiShopGoods goods = banLiShopGoodsService.selectByPrimaryKey(order.getGoods().getId());
|
BanLiShopGoodsClass goodsClass = banLiShopGoodsClassService.selectByPrimaryKey(goods.getGoodsClass().getId());
|
BanLiShopGoodsSets set = banLiShopGoodsSetService.selectByPrimaryKey(order.getGoodsSet().getId());
|
RedPackDetail detail = null;
|
try {
|
detail = RedPackDetailFactory.createUseByShopOrder(orderId, order.getUid(), goodsClass.getName(),
|
set.getName(), order.getHongBaoPayment());
|
} catch (RedPackDetailException e) {
|
e.printStackTrace();
|
}
|
|
if (detail == null)
|
throw new RedPackBalanceException(4, "红包详情失败");
|
redPackBalanceService.subRedPack(order.getUid(), order.getHongBaoPayment(), detail);
|
|
BanLiShopOrder update = new BanLiShopOrder();
|
update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_PAID);
|
// 判断其他待支付项是否已经支付
|
update.setUpdateTime(new Date());
|
order.setHongBaoPaymentState(update.getHongBaoPaymentState());
|
update.setState(getOrderPayState(order));
|
banLiShopOrderMapper.updateByPrimaryKeySelective(update);
|
}
|
|
private int getOrderPayState(BanLiShopOrder order) {
|
int needPayCount = 0;
|
int paidCount = 0;
|
// 获取订单付款状态
|
if (order.getHongBaoPayment() != null) {
|
needPayCount++;
|
if (order.getHongBaoPaymentState() != null
|
&& order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID)
|
paidCount++;
|
}
|
|
if (order.getBalancePayment() != null) {
|
needPayCount++;
|
if (order.getBalancePaymentState() != null
|
&& order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID)
|
paidCount++;
|
}
|
|
if (order.getMoneyPayment() != null) {
|
needPayCount++;
|
if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID)
|
paidCount++;
|
}
|
|
if (needPayCount == paidCount)
|
return BanLiShopOrder.STATE_PAID;
|
else {
|
if (paidCount == 0)
|
return BanLiShopOrder.STATE_NO_PAY;
|
else
|
return BanLiShopOrder.STATE_PART_PAY;
|
}
|
}
|
|
@Override
|
public void payOrderByMoney(Long orderId, BigDecimal money) throws BanLiShopOrderException {
|
BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(orderId);
|
if (order == null) {
|
throw new BanLiShopOrderException(1, "订单不存在");
|
}
|
if (order.getMoneyPayment() == null)
|
throw new BanLiShopOrderException(2, "不需要采用现金支付");
|
|
if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
|
throw new BanLiShopOrderException(3, "重复支付");
|
}
|
|
if (order.getMoneyPayment().compareTo(money) > 0) {
|
throw new BanLiShopOrderException(4, "支付金额不够");
|
}
|
|
// 支付成功
|
BanLiShopOrder update = new BanLiShopOrder();
|
update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_PAID);
|
// 判断其他待支付项是否已经支付
|
update.setUpdateTime(new Date());
|
order.setMoneyPaymentState(update.getMoneyPaymentState());
|
update.setState(getOrderPayState(order));
|
banLiShopOrderMapper.updateByPrimaryKeySelective(update);
|
}
|
|
@Override
|
public BanLiShopOrder selectByPrimaryKeyForUpdate(Long id) {
|
return banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
|
}
|
|
@Override
|
public void invalidOrderByOrderId(Long id, String desc) {
|
// 判断订单是否存在
|
BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
|
if (order == null)
|
return;
|
|
// 判断是否处于未付款
|
if (order.getState() == BanLiShopOrder.STATE_NO_PAY) {
|
BanLiShopOrder update = new BanLiShopOrder();
|
update.setId(id);
|
update.setStateDesc(desc);
|
update.setState(BanLiShopOrder.STATE_INVALID);
|
update.setUpdateTime(new Date());
|
banLiShopOrderMapper.updateByPrimaryKeySelective(update);
|
}
|
}
|
|
@Transactional
|
@Override
|
public void rejectOrder(Long id, String msg) throws BanLiShopOrderException {
|
// 判断订单是否处于付款状态
|
BanLiShopOrder order = banLiShopOrderMapper.selectByPrimaryKeyForUpdate(id);
|
if (order == null)
|
throw new BanLiShopOrderException(1, "订单不存在");
|
|
if (order.getState() != BanLiShopOrder.STATE_PAID)
|
throw new BanLiShopOrderException(1, "订单未处于待审核状态");
|
|
// TODO 订单退款
|
BanLiShopOrder update = new BanLiShopOrder();
|
update.setState(BanLiShopOrder.STATE_REJECT);
|
update.setStateDesc(msg);
|
update.setUpdateTime(new Date());
|
banLiShopOrderMapper.updateByPrimaryKeySelective(update);
|
}
|
|
}
|