admin
2019-11-03 788deca1b4a24f8a24e49c24f7d89975a1d74bbe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.yeshi.fanli.service.impl.shop;
 
import java.math.BigDecimal;
import java.util.Date;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.entity.wx.WXAPPInfo;
import org.yeshi.utils.exception.WXOrderException;
import org.yeshi.utils.wx.WXPayUtil;
 
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.redpack.RedPackBalanceException;
import com.yeshi.fanli.exception.redpack.RedPackDetailException;
import com.yeshi.fanli.exception.shop.BanLiShopOrderException;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
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.BanLiShopOrderPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
 
@Service
public class BanLiShopOrderPayServiceImpl implements BanLiShopOrderPayService {
    @Resource
    private BanLiShopGoodsSetService banLiShopGoodsSetService;
 
    @Resource
    private BanLiShopGoodsService banLiShopGoodsService;
 
    @Resource
    private BanLiShopGoodsClassService banLiShopGoodsClassService;
 
    @Resource
    private RedPackBalanceService redPackBalanceService;
 
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
 
    @Resource
    private RedPackDetailService redPackDetailService;
 
    @Transactional
    @Override
    public void payOrderByHongBao(Long orderId) throws BanLiShopOrderException, RedPackBalanceException {
        BanLiShopOrder order = banLiShopOrderService.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.setId(order.getId());
        update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_PAID);
        // 判断其他待支付项是否已经支付
        update.setUpdateTime(new Date());
        if (order.getState() == BanLiShopOrder.STATE_NO_PAY)
            if ((order.getBalancePaymentState() == null
                    || order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID)
                    && (order.getMoneyPaymentState() == null
                            || order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID))// 其他待支付项已经支付
                update.setState(BanLiShopOrder.STATE_PAID);
        banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
        if (update.getState() != null && update.getState() == BanLiShopOrder.STATE_PAID) {
            paySuccess(order);
        }
    }
 
    @Transactional
    @Override
    public void payOrderByMoney(Long orderId, BigDecimal money) throws BanLiShopOrderException {
        BanLiShopOrder order = banLiShopOrderService.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_NOPAY) {
            throw new BanLiShopOrderException(3, "重复支付");
        }
 
        if (order.getMoneyPayment().compareTo(money) > 0) {
            throw new BanLiShopOrderException(4, "支付金额不够");
        }
 
        // 支付成功
        BanLiShopOrder update = new BanLiShopOrder();
        update.setId(order.getId());
        update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_PAID);
        // 判断其他待支付项是否已经支付
        update.setUpdateTime(new Date());
        if (order.getState() == BanLiShopOrder.STATE_NO_PAY)
            if ((order.getBalancePaymentState() == null
                    || order.getBalancePaymentState() == BanLiShopOrder.PAY_STATE_PAID)
                    && (order.getHongBaoPaymentState() == null
                            || order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID))// 其他待支付项已经支付
                update.setState(BanLiShopOrder.STATE_PAID);
        banLiShopOrderService.udpateSelectiveByPrimaryKey(update);
        if (update.getState() != null && update.getState() == BanLiShopOrder.STATE_PAID) {
            paySuccess(order);
        }
    }
 
    /**
     * 支付成功
     */
    private void paySuccess(BanLiShopOrder order) {
        // 将红包明细外显
        RedPackDetail detail = null;
        try {
            detail = RedPackDetailFactory.createUseByShopOrder(order.getId(), order.getUid(), "", "",
                    order.getHongBaoPayment());
        } catch (RedPackDetailException e) {
            e.printStackTrace();
        }
 
        if (detail != null) {
            redPackDetailService.changeDisplayByIdentifyCode(detail.getIdentifyCode(), true);
        }
    }
 
    @Transactional
    @Override
    public void refund(Long orderId) throws BanLiShopOrderException {
        // 订单退款
        // 查询订单是否已经被拒绝
        BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKeyForUpdate(orderId);
        if (order == null)
            throw new BanLiShopOrderException(1, "订单不存在");
        if (order.getState() != BanLiShopOrder.STATE_REJECT)
            throw new BanLiShopOrderException(2, "订单未被拒绝/订单已退款");
 
        BanLiShopOrder update = new BanLiShopOrder();
        update.setId(order.getId());
        if (order.getHongBaoPaymentState() != null && order.getHongBaoPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
            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.createShopOrderDrawBack(orderId, order.getUid(), goodsClass.getName(),
                        set.getName(), order.getHongBaoPayment());
            } catch (RedPackDetailException e) {
                e.printStackTrace();
            }
 
            if (detail == null)
                throw new BanLiShopOrderException(4, "红包详情失败");
            try {
                redPackBalanceService.addRedPack(order.getUid(), order.getHongBaoPayment(), detail);
            } catch (RedPackBalanceException e) {
                throw new BanLiShopOrderException(5, "红包退款失败");
            }
            update.setHongBaoPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
        }
 
        // TODO 微信支付退款
        if (order.getMoneyPaymentState() != null && order.getMoneyPaymentState() == BanLiShopOrder.PAY_STATE_PAID) {
            try {
                boolean success = WXPayUtil.refund(order.getOrderNo(), order.getMoneyPayment(), order.getMoneyPayment(),
                        null, new WXAPPInfo(), null, null);
                if (success) {
                    update.setMoneyPaymentState(BanLiShopOrder.PAY_STATE_REFUND);
                } else
                    throw new BanLiShopOrderException(6, "微信支付退款失败");
 
            } catch (WXOrderException e) {
                throw new BanLiShopOrderException(6, "微信支付退款失败");
            }
        }
    }
 
}