From 5129d2c63fbef70c6ee45ba5ec12654937e05231 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期一, 12 四月 2021 19:05:57 +0800 Subject: [PATCH] seata集成 --- src/main/java/com/yeshi/buwan/service/imp/order/OrderServiceImpl.java | 141 +++++++++++++++++++++++++++-------------------- 1 files changed, 81 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/yeshi/buwan/service/imp/order/OrderServiceImpl.java b/src/main/java/com/yeshi/buwan/service/imp/order/OrderServiceImpl.java index 35aa27d..290e192 100644 --- a/src/main/java/com/yeshi/buwan/service/imp/order/OrderServiceImpl.java +++ b/src/main/java/com/yeshi/buwan/service/imp/order/OrderServiceImpl.java @@ -2,10 +2,10 @@ import com.alipay.api.AlipayApiException; import com.alipay.api.response.AlipayTradeQueryResponse; -import com.yeshi.buwan.dao.vip.VIPOrderRecordDao; +import com.yeshi.buwan.dao.vip.OrderRecordDao; import com.yeshi.buwan.domain.vip.OrderType; import com.yeshi.buwan.domain.vip.UserVIPInfo; -import com.yeshi.buwan.domain.vip.VIPOrderRecord; +import com.yeshi.buwan.domain.vip.OrderRecord; import com.yeshi.buwan.domain.vip.VIPPriceType; import com.yeshi.buwan.dto.order.PayWayInfoDTO; import com.yeshi.buwan.exception.PPTVException; @@ -15,11 +15,13 @@ import com.yeshi.buwan.exception.vip.VIPException; import com.yeshi.buwan.pptv.PPTVVipManager; import com.yeshi.buwan.service.inter.order.OrderService; +import com.yeshi.buwan.service.manager.GoldCornManager; import com.yeshi.buwan.util.Constant; import com.yeshi.buwan.util.RedisManager; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.util.user.VipUtil; import com.yeshi.buwan.util.vip.VIPOrderUtil; +import io.seata.spring.annotation.GlobalTransactional; import org.hibernate.Query; import org.hibernate.Session; import org.slf4j.Logger; @@ -42,13 +44,12 @@ @Service public class OrderServiceImpl implements OrderService { - private static Logger logger = LoggerFactory.getLogger("OrderService"); private static Logger orderLogger = LoggerFactory.getLogger("order"); @Resource - private VIPOrderRecordDao vipOrderRecordDao; + private OrderRecordDao vipOrderRecordDao; @Resource private PPTVVipManager pptvVipManager; @@ -56,15 +57,18 @@ @Resource private RedisManager redisManager; + @Resource + private GoldCornManager goldCornManager; + @Transactional(rollbackFor = Exception.class) @Override - public VIPOrderRecord createOrder(VIPOrderRecord record) throws OrderException { + public OrderRecord createOrder(OrderRecord record) throws OrderException { if (record.getMoney() == null || record.getType() == null || record.getOrderType() == null || record.getUid() == null) { throw new OrderException(1, "鍙傛暟涓嶅畬鏁�"); } if (record.getState() == null) { - record.setState(VIPOrderRecord.STATE_NOT_PAY); + record.setState(OrderRecord.STATE_NOT_PAY); } if (record.getCreateTime() == null) { @@ -80,16 +84,23 @@ } @Transactional(rollbackFor = Exception.class) + @GlobalTransactional(timeoutMills = 30000, name = "buwan-order", rollbackFor = Exception.class) @Override - public PayWayInfoDTO payOrder(VIPOrderRecord record) throws OrderException, GoldCornException, PayException { + public PayWayInfoDTO payOrder(OrderRecord record) throws OrderException, GoldCornException, PayException { //TODO 褰辫璞嗘敮浠� String orderNo = VIPOrderUtil.getOutOrderNo(record.getOrderType(), record.getId()); switch (record.getPayWay()) { - case VIPOrderRecord + case OrderRecord .PAY_WAY_ALIPAY_GOLDCORN: - //TODO 鎵e奖瑙嗚眴 + + try { + goldCornManager.consumeGoldCorn(record, record.getGoldCorn()); + } catch (Exception e) { + throw new GoldCornException(1, "褰辫璞嗘墸闄ゅ嚭閿�"); + } + orderLogger.info("璁㈠崟褰辫璞嗘敮浠樻垚鍔燂細id-{}", record.getId()); - case VIPOrderRecord + case OrderRecord .PAY_WAY_ALIPAY: { //鐢熸垚鏀粯瀹濇敮浠樿鍗� String form = VipUtil.getVipChargeAlipayForm(record.getId(), orderNo, record.getMoney()); @@ -98,14 +109,18 @@ redisManager.cacheCommonString(id, form, 120); String payUrl = Constant.HOST + "/BuWan/alipay/printPayForm?formId=" + id; orderLogger.info("璁㈠崟鍒涘缓鏀粯瀹濇敮浠橀摼鎺ユ垚鍔燂細id-{} 閾炬帴-{}", record.getId(), payUrl); - return new PayWayInfoDTO(VIPOrderRecord + return new PayWayInfoDTO(OrderRecord .PAY_WAY_ALIPAY, payUrl); } - case VIPOrderRecord + case OrderRecord .PAY_WAY_WX_GOLDCORN: - //TODO 鎵e奖瑙嗚眴 + try { + goldCornManager.consumeGoldCorn(record, record.getGoldCorn()); + } catch (Exception e) { + throw new GoldCornException(1, "褰辫璞嗘墸闄ゅ嚭閿�"); + } orderLogger.info("璁㈠崟褰辫璞嗘敮浠樻垚鍔燂細id-{} 绫诲瀷-{}", record.getId(), record.getOrderType().name()); - case VIPOrderRecord + case OrderRecord .PAY_WAY_WX: { //鐢熸垚寰俊鏀粯璁㈠崟 try { @@ -117,15 +132,19 @@ } String payUrl = VipUtil.createWXOrder(record.getId(), record.getIpInfo(), orderNo, record.getMoney(), title); orderLogger.info("璁㈠崟鍒涘缓寰俊鏀粯閾炬帴鎴愬姛锛歩d-{} 閾炬帴-{}", record.getId(), payUrl); - return new PayWayInfoDTO(VIPOrderRecord.PAY_WAY_WX, payUrl); + return new PayWayInfoDTO(OrderRecord.PAY_WAY_WX, payUrl); } catch (Exception e) { logger.error("鐢熸垚寰俊鏀粯璁㈠崟鍑洪敊", e); throw new PayException(2, e.getMessage()); } } - case VIPOrderRecord.PAY_WAY_GOLDCORN: { - //TODO 鎵e奖瑙嗚眴 + case OrderRecord.PAY_WAY_GOLDCORN: { + try { + goldCornManager.consumeGoldCorn(record, record.getGoldCorn()); + } catch (Exception e) { + throw new GoldCornException(1, "褰辫璞嗘墸闄ゅ嚭閿�"); + } orderLogger.info("璁㈠崟褰辫璞嗘敮浠樻垚鍔燂細id-{}", record.getId()); } break; @@ -133,49 +152,49 @@ throw new OrderException(1, "鏀粯鏂瑰紡涓嶅瓨鍦�"); } - private int getPayState(VIPOrderRecord record) { + private int getPayState(OrderRecord record) { switch (record.getPayWay()) { - case VIPOrderRecord.PAY_WAY_ALIPAY: - case VIPOrderRecord.PAY_WAY_WX: + case OrderRecord.PAY_WAY_ALIPAY: + case OrderRecord.PAY_WAY_WX: if (record.getMoneyPay() != null && record.getMoneyPay()) - return VIPOrderRecord.STATE_PAY; + return OrderRecord.STATE_PAY; else - return VIPOrderRecord.STATE_NOT_PAY; - case VIPOrderRecord.PAY_WAY_ALIPAY_GOLDCORN: - case VIPOrderRecord.PAY_WAY_WX_GOLDCORN: + return OrderRecord.STATE_NOT_PAY; + case OrderRecord.PAY_WAY_ALIPAY_GOLDCORN: + case OrderRecord.PAY_WAY_WX_GOLDCORN: if (record.getMoneyPay() != null && record.getMoneyPay() && record.getGoldCornPay() != null && record.getGoldCornPay()) - return VIPOrderRecord.STATE_PAY; + return OrderRecord.STATE_PAY; else - return VIPOrderRecord.STATE_NOT_PAY; - case VIPOrderRecord.PAY_WAY_GOLDCORN: + return OrderRecord.STATE_NOT_PAY; + case OrderRecord.PAY_WAY_GOLDCORN: if (record.getGoldCornPay() != null && record.getGoldCornPay()) - return VIPOrderRecord.STATE_PAY; + return OrderRecord.STATE_PAY; else - return VIPOrderRecord.STATE_NOT_PAY; + return OrderRecord.STATE_NOT_PAY; } - return VIPOrderRecord.STATE_NOT_PAY; + return OrderRecord.STATE_NOT_PAY; } @Transactional(rollbackFor = Exception.class) @Override - public VIPOrderRecord paySuccess(String id, int payWay, BigDecimal payMoney, Date payTime) throws + public OrderRecord paySuccess(String id, int payWay, BigDecimal payMoney, Date payTime) throws VIPException, PPTVException { orderLogger.info("璁㈠崟鏀粯鎴愬姛鐩殑鎵ц锛歩d-{}锛屾敮浠樻柟寮�-{},鏀粯閲戦-{}锛屾敮浠樻椂闂�-{}", id, payWay, payMoney, payTime.getTime()); Session session = vipOrderRecordDao.getSession(); //淇敼璁板綍 - Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(VIPOrderRecord.class).setCacheable(false).setParameter(0, id); - List<VIPOrderRecord> list = query.list(); + Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(OrderRecord.class).setCacheable(false).setParameter(0, id); + List<OrderRecord> list = query.list(); if (list != null && list.size() > 0) { - VIPOrderRecord record = list.get(0); - if (record.getState() != VIPOrderRecord.STATE_NOT_PAY) + OrderRecord record = list.get(0); + if (record.getState() != OrderRecord.STATE_NOT_PAY) throw new VIPException(1, "璁㈠崟鏈浜庡緟鏀粯鐘舵��"); - if (record.getPayWay() != VIPOrderRecord.PAY_WAY_GOLDCORN) { + if (record.getPayWay() != OrderRecord.PAY_WAY_GOLDCORN) { record.setPayMoney(payMoney); record.setMoneyPay(true); } else { @@ -187,7 +206,7 @@ record.setState(getPayState(record)); //宸茬粡鏀粯鎴愬姛 - if (record.getState() == VIPOrderRecord.STATE_PAY) { + if (record.getState() == OrderRecord.STATE_PAY) { session.createSQLQuery("insert into wk_vip_order_pay_success(id,create_time) value(?,now())").setParameter(0, id).executeUpdate(); } @@ -202,7 +221,7 @@ session.update(record); - if (record.getState() == VIPOrderRecord.STATE_PAY) { + if (record.getState() == OrderRecord.STATE_PAY) { orderLogger.info("璁㈠崟鍏ㄩ儴鏀粯鎴愬姛锛歩d-{}", id); //璐拱VIP pptvVipManager.buyVIP(record); @@ -217,7 +236,7 @@ record.setVipEndTime(new Date(payTime.getTime() + 1000 * 60 * 60L * 24 * 7)); session.update(record); //鍗曠墖璐拱鎴愬姛锛�7澶╂湁鏁堟湡锛� - if (record.getState() == VIPOrderRecord.STATE_PAY) { + if (record.getState() == OrderRecord.STATE_PAY) { orderLogger.info("璁㈠崟鍏ㄩ儴鏀粯鎴愬姛锛歩d-{}", id); //璐拱鍗曠墖 pptvVipManager.buyVideo(record); @@ -236,32 +255,34 @@ @Transactional(rollbackFor = Exception.class) @Override - public void cancelOrder(String id, String reason) throws OrderException { + public void cancelOrder(String id, String reason) throws OrderException, GoldCornException { Session session = vipOrderRecordDao.getSession(); - Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(VIPOrderRecord.class).setCacheable(false).setParameter(0, id); - List<VIPOrderRecord> list = query.list(); + Query query = session.createSQLQuery("select * from wk_vip_order_record r where r.id=? for update").addEntity(OrderRecord.class).setCacheable(false).setParameter(0, id); + List<OrderRecord> list = query.list(); //璁㈠崟涓嶅瓨鍦� if (list == null || list.size() == 0) throw new OrderException(1, "璁㈠崟涓嶅瓨鍦�"); - VIPOrderRecord record = list.get(0); - if (record.getState() == VIPOrderRecord.STATE_PAY) { + OrderRecord record = list.get(0); + if (record.getState() == OrderRecord.STATE_PAY) { throw new OrderException(1, "宸叉敮浠樻垚鍔熺殑璁㈠崟涓嶈兘鍙栨秷"); } - if (record.getPayWay() != VIPOrderRecord.PAY_WAY_GOLDCORN && record.getMoneyPay() != null && record.getMoneyPay()) { + if (record.getPayWay() != OrderRecord.PAY_WAY_GOLDCORN && record.getMoneyPay() != null && record.getMoneyPay()) { throw new OrderException(2, "璧勯噾鏀粯鎴愬姛鐨勮鍗曚笉鑳藉彇娑�"); } //杩斿洖璞� if (record.getGoldCornPay() != null && record.getGoldCornPay() && record.getGoldCorn() != null && record.getGoldCorn() > 0) { - //TODO 閫�鍥炲奖瑙嗚眴 - - + try { + goldCornManager.drawbackGoldCorn(record); + } catch (Exception e) { + throw new GoldCornException(1, "褰辫璞嗛��娆炬垚鍔�"); + } } //璁剧疆璁㈠崟鐘舵�� - record.setState(VIPOrderRecord.STATE_CANCEL); + record.setState(OrderRecord.STATE_CANCEL); record.setUpdateTime(new Date()); record.setRemarks(reason); vipOrderRecordDao.update(record); @@ -271,13 +292,13 @@ @Transactional(rollbackFor = Exception.class) @Override - public VIPOrderRecord checkOrderPayState(String id) { - VIPOrderRecord record = vipOrderRecordDao.find(VIPOrderRecord.class, id); - if (record.getState() == VIPOrderRecord.STATE_PAY) + public OrderRecord checkOrderPayState(String id) { + OrderRecord record = vipOrderRecordDao.find(OrderRecord.class, id); + if (record.getState() == OrderRecord.STATE_PAY) return record; switch (record.getPayWay()) { - case VIPOrderRecord.PAY_WAY_ALIPAY: { + case OrderRecord.PAY_WAY_ALIPAY: { //鏀粯瀹� AlipayTradeQueryResponse res = null; try { @@ -285,7 +306,7 @@ //鏀粯鎴愬姛 if (res.isSuccess() && "TRADE_SUCCESS".equalsIgnoreCase(res.getTradeStatus())) { try { - return paySuccess(id, VIPOrderRecord.PAY_WAY_ALIPAY, new BigDecimal(res.getTotalAmount()), new Date()); + return paySuccess(id, OrderRecord.PAY_WAY_ALIPAY, new BigDecimal(res.getTotalAmount()), new Date()); } catch (Exception e) { e.printStackTrace(); } @@ -296,7 +317,7 @@ } break; - case VIPOrderRecord.PAY_WAY_WX: { + case OrderRecord.PAY_WAY_WX: { //寰俊 try { WXPayOrderInfoV3 info = WXPayV3Util.getPayOrderInfo(VIPOrderUtil.getOutOrderNo(record.getOrderType(), id), VipUtil.getWXAPP()); @@ -316,9 +337,9 @@ @Override - public List<VIPOrderRecord> listOrderRecord(String uid, Integer state, int page, int pageSize) { + public List<OrderRecord> listOrderRecord(String uid, Integer state, int page, int pageSize) { - VIPOrderRecordDao.DaoQuery query = new VIPOrderRecordDao.DaoQuery(); + OrderRecordDao.DaoQuery query = new OrderRecordDao.DaoQuery(); query.start = (page - 1) * pageSize; query.count = pageSize; query.uid = uid; @@ -329,15 +350,15 @@ @Override public long countOrderRecord(String uid, Integer state) { - VIPOrderRecordDao.DaoQuery query = new VIPOrderRecordDao.DaoQuery(); + OrderRecordDao.DaoQuery query = new OrderRecordDao.DaoQuery(); query.uid = uid; query.state = state; return vipOrderRecordDao.count(query); } @Override - public VIPOrderRecord getOrderRecord(String id) { - return vipOrderRecordDao.find(VIPOrderRecord.class, id); + public OrderRecord getOrderRecord(String id) { + return vipOrderRecordDao.find(OrderRecord.class, id); } -- Gitblit v1.8.0