// Path: tcc-transaction-tutorial-sample/tcc-transaction-sample/tcc-transaction-redpacket/src/main/java/org/pankai/tcctransaction/sample/redpacket/service/RedPacketTradeOrderService.java
// @Service
// public class RedPacketTradeOrderService {
// 
//     @Autowired
//     private RedPacketAccountRepository redPacketAccountRepository;
// 
//     @Autowired
//     private TradeOrderRepository tradeOrderRepository;
// 
//     //trying阶段,新建draft状态订单,扣除账户金额.
//     @Compensable(confirmMethod = "confirmRecord", cancelMethod = "cancelRecord")
//     @Transactional
//     public String record(TransactionContext transactionContext, RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         System.out.println("red packet try record called.");
// 
//         RedPacketAccount transferFromAccount = redPacketAccountRepository.findByUserId(redPacketTradeOrderDto.getSelfUserId());
//         if (transferFromAccount == null) {
//             throw new RuntimeException("指定用户没有红包账户.");
//         }
// 
//         TradeOrder tradeOrder = new TradeOrder(redPacketTradeOrderDto.getSelfUserId(), redPacketTradeOrderDto.getOppositeUserId(), redPacketTradeOrderDto.getMerchantOrderNo(), redPacketTradeOrderDto.getAmount());
// 
//         tradeOrderRepository.insert(tradeOrder);
// 
//         transferFromAccount.transferFrom(redPacketTradeOrderDto.getAmount());
// 
//         redPacketAccountRepository.save(transferFromAccount);
// 
//         return "success";
//     }
// 
//     @Transactional
//     public void confirmRecord(TransactionContext transactionContext, RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         System.out.println("red packet confirm record called.");
// 
//         TradeOrder tradeOrder = tradeOrderRepository.findByMerchantOrderNo(redPacketTradeOrderDto.getMerchantOrderNo());
//         if (tradeOrder == null) {
//             throw new RuntimeException("指定的订单不存在,订单号:" + redPacketTradeOrderDto.getMerchantOrderNo());
//         }
// 
//         tradeOrder.confirm();
//         tradeOrderRepository.update(tradeOrder);
// 
//         //出于测试目的,将红包余额加回去.
//         RedPacketAccount transferToAccount = redPacketAccountRepository.findByUserId(redPacketTradeOrderDto.getOppositeUserId());
//         transferToAccount.transferTo(redPacketTradeOrderDto.getAmount());
// 
//         redPacketAccountRepository.save(transferToAccount);
//     }
// 
//     @Transactional
//     public void cancelRecord(TransactionContext transactionContext, RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         System.out.println("red packet cancel record called");
// 
//         TradeOrder tradeOrder = tradeOrderRepository.findByMerchantOrderNo(redPacketTradeOrderDto.getMerchantOrderNo());
// 
//         if (null != tradeOrder && "DRAFT".equals(tradeOrder.getStatus())) {
//             tradeOrder.cancel();
//             tradeOrderRepository.update(tradeOrder);
// 
//             RedPacketAccount capitalAccount = redPacketAccountRepository.findByUserId(redPacketTradeOrderDto.getSelfUserId());
// 
//             capitalAccount.cancelTransfer(redPacketTradeOrderDto.getAmount());
// 
//             redPacketAccountRepository.save(capitalAccount);
//         }
// 
//     }
// }
// 
// Path: tcc-transaction-tutorial-sample/tcc-transaction-sample/tcc-transaction-redpacket/src/main/java/org/pankai/tcctransaction/sample/redpacket/web/controller/request/RedPacketTradeOrderRecordRequest.java
// public class RedPacketTradeOrderRecordRequest {
// 
//     private TransactionContext transactionContext;
//     private RedPacketTradeOrderDto redPacketTradeOrderDto;
// 
//     public TransactionContext getTransactionContext() {
//         return transactionContext;
//     }
// 
//     public void setTransactionContext(TransactionContext transactionContext) {
//         this.transactionContext = transactionContext;
//     }
// 
//     public RedPacketTradeOrderDto getRedPacketTradeOrderDto() {
//         return redPacketTradeOrderDto;
//     }
// 
//     public void setRedPacketTradeOrderDto(RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         this.redPacketTradeOrderDto = redPacketTradeOrderDto;
//     }
// }


import org.pankai.tcctransaction.sample.redpacket.service.RedPacketTradeOrderService;
import org.pankai.tcctransaction.sample.redpacket.web.controller.request.RedPacketTradeOrderRecordRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
package org.pankai.tcctransaction.sample.redpacket.web.controller;


/**
 * Created by pktczwd on 2016/12/26.
 */
@Controller
public class RedPacketController {

    @Autowired
// Path: tcc-transaction-tutorial-sample/tcc-transaction-sample/tcc-transaction-redpacket/src/main/java/org/pankai/tcctransaction/sample/redpacket/service/RedPacketTradeOrderService.java
// @Service
// public class RedPacketTradeOrderService {
// 
//     @Autowired
//     private RedPacketAccountRepository redPacketAccountRepository;
// 
//     @Autowired
//     private TradeOrderRepository tradeOrderRepository;
// 
//     //trying阶段,新建draft状态订单,扣除账户金额.
//     @Compensable(confirmMethod = "confirmRecord", cancelMethod = "cancelRecord")
//     @Transactional
//     public String record(TransactionContext transactionContext, RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         System.out.println("red packet try record called.");
// 
//         RedPacketAccount transferFromAccount = redPacketAccountRepository.findByUserId(redPacketTradeOrderDto.getSelfUserId());
//         if (transferFromAccount == null) {
//             throw new RuntimeException("指定用户没有红包账户.");
//         }
// 
//         TradeOrder tradeOrder = new TradeOrder(redPacketTradeOrderDto.getSelfUserId(), redPacketTradeOrderDto.getOppositeUserId(), redPacketTradeOrderDto.getMerchantOrderNo(), redPacketTradeOrderDto.getAmount());
// 
//         tradeOrderRepository.insert(tradeOrder);
// 
//         transferFromAccount.transferFrom(redPacketTradeOrderDto.getAmount());
// 
//         redPacketAccountRepository.save(transferFromAccount);
// 
//         return "success";
//     }
// 
//     @Transactional
//     public void confirmRecord(TransactionContext transactionContext, RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         System.out.println("red packet confirm record called.");
// 
//         TradeOrder tradeOrder = tradeOrderRepository.findByMerchantOrderNo(redPacketTradeOrderDto.getMerchantOrderNo());
//         if (tradeOrder == null) {
//             throw new RuntimeException("指定的订单不存在,订单号:" + redPacketTradeOrderDto.getMerchantOrderNo());
//         }
// 
//         tradeOrder.confirm();
//         tradeOrderRepository.update(tradeOrder);
// 
//         //出于测试目的,将红包余额加回去.
//         RedPacketAccount transferToAccount = redPacketAccountRepository.findByUserId(redPacketTradeOrderDto.getOppositeUserId());
//         transferToAccount.transferTo(redPacketTradeOrderDto.getAmount());
// 
//         redPacketAccountRepository.save(transferToAccount);
//     }
// 
//     @Transactional
//     public void cancelRecord(TransactionContext transactionContext, RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         System.out.println("red packet cancel record called");
// 
//         TradeOrder tradeOrder = tradeOrderRepository.findByMerchantOrderNo(redPacketTradeOrderDto.getMerchantOrderNo());
// 
//         if (null != tradeOrder && "DRAFT".equals(tradeOrder.getStatus())) {
//             tradeOrder.cancel();
//             tradeOrderRepository.update(tradeOrder);
// 
//             RedPacketAccount capitalAccount = redPacketAccountRepository.findByUserId(redPacketTradeOrderDto.getSelfUserId());
// 
//             capitalAccount.cancelTransfer(redPacketTradeOrderDto.getAmount());
// 
//             redPacketAccountRepository.save(capitalAccount);
//         }
// 
//     }
// }
// 
// Path: tcc-transaction-tutorial-sample/tcc-transaction-sample/tcc-transaction-redpacket/src/main/java/org/pankai/tcctransaction/sample/redpacket/web/controller/request/RedPacketTradeOrderRecordRequest.java
// public class RedPacketTradeOrderRecordRequest {
// 
//     private TransactionContext transactionContext;
//     private RedPacketTradeOrderDto redPacketTradeOrderDto;
// 
//     public TransactionContext getTransactionContext() {
//         return transactionContext;
//     }
// 
//     public void setTransactionContext(TransactionContext transactionContext) {
//         this.transactionContext = transactionContext;
//     }
// 
//     public RedPacketTradeOrderDto getRedPacketTradeOrderDto() {
//         return redPacketTradeOrderDto;
//     }
// 
//     public void setRedPacketTradeOrderDto(RedPacketTradeOrderDto redPacketTradeOrderDto) {
//         this.redPacketTradeOrderDto = redPacketTradeOrderDto;
//     }
// }


// Path: tcc-transaction-tutorial-sample/tcc-transaction-sample/tcc-transaction-redpacket/src/main/java/org/pankai/tcctransaction/sample/redpacket/web/controller/RedPacketController.java
import org.pankai.tcctransaction.sample.redpacket.service.RedPacketTradeOrderService;
import org.pankai.tcctransaction.sample.redpacket.web.controller.request.RedPacketTradeOrderRecordRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

package org.pankai.tcctransaction.sample.redpacket.web.controller;


/**
 * Created by pktczwd on 2016/12/26.
 */
@Controller
public class RedPacketController {

    @Autowired
    private RedPacketTradeOrderService redPacketTradeOrderService;