rentaiming
2024-07-10 b29becf95484be48aa09e87fcc7cc19f94cf9c81
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.ruoyi.order.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.AuctionOrderTypeEnum;
import com.ruoyi.common.core.enums.BondStatusEnum;
import com.ruoyi.common.core.enums.PaymentMethodEnum;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.order.domain.Paylog;
import com.ruoyi.order.mapper.OrderAuctionBondMapper;
import com.ruoyi.order.service.IOrderAuctionBondService;
import com.ruoyi.order.service.IPaylogService;
import com.ruoyi.order.util.OrderUtil;
import com.ruoyi.order.util.SinataUtil;
import com.ruoyi.system.api.domain.AuctionGoods;
import com.ruoyi.system.api.domain.AuctionSalesroom;
import com.ruoyi.system.api.domain.OrderAuctionBond;
import com.ruoyi.system.api.domain.dto.MemberAuctionSalesroomBondDTO;
import com.ruoyi.system.api.domain.dto.OrderAuctionBondDTO;
import com.ruoyi.system.api.domain.dto.RefundDTO;
import com.ruoyi.system.api.domain.vo.PayInfoVO;
import com.ruoyi.system.api.feignClient.AuctionClient;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * <p>
 * 拍卖保证金表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-05-16
 */
@Slf4j
@Service
public class OrderAuctionBondServiceImpl extends ServiceImpl<OrderAuctionBondMapper, OrderAuctionBond> implements IOrderAuctionBondService {
 
    @Resource
    private IPaylogService iPaylogService;
 
    @Resource
    private PaylogServiceImpl   paylogServiceImpl;
 
 
    @Resource
    private AuctionClient auctionClient;
 
 
    @Override
    public R<T> getOrderAuctionBond(OrderAuctionBondDTO orderAuctionBondDTO) {
 
        LambdaQueryWrapper<OrderAuctionBond> wrapper = Wrappers.lambdaQuery();
        wrapper.notIn(OrderAuctionBond::getMemberId, orderAuctionBondDTO.getUserList());
        wrapper.eq(OrderAuctionBond::getDelFlag, 0);
        wrapper.eq(OrderAuctionBond::getAuctionSalesroomId, orderAuctionBondDTO.getAuctionSalesroomId());
        List<OrderAuctionBond> orderAuctionBondList = this.list(wrapper);
        for (OrderAuctionBond orderAuctionBond : orderAuctionBondList) {
            LambdaQueryWrapper<Paylog> wrapper1 = Wrappers.lambdaQuery();
            wrapper1.eq(Paylog::getOutTradeNo, orderAuctionBond.getOrderNo());
            wrapper1.last("limit 1");
            Paylog paylog = iPaylogService.getOne(wrapper1);
            if (paylog.getPayType() == 1) {//支付宝
                boolean bo = paylogServiceImpl.refundForAlipay(paylog.getOutTradeNo(), paylog.getTradeNo(), orderAuctionBond.getBond().doubleValue());
                if (!bo) {
                    return R.fail("支付宝退款失败!");
                }
            } else {//微信
                String refundMoney = SinataUtil.doubleRetainTwo(orderAuctionBond.getBond().doubleValue() * 100d);
                Integer refundFee = Integer.parseInt(refundMoney.substring(0, refundMoney.length() - 3));
                String money = SinataUtil.doubleRetainTwo(paylog.getPayMoney() * 100d);
                Integer totalFee = Integer.parseInt(money.substring(0, money.length() - 3));
                boolean bo = paylogServiceImpl.refundForWxpay(1, paylog.getTradeNo(), paylog.getOutTradeNo(), orderAuctionBond.getOrderNo(), totalFee, refundFee, "2");
                if (!bo) {
                    return R.fail("微信退款失败!");
                }
            }
 
        }
        return R.ok();
    }
 
    /**
     * 查询保证金订单列表
     *
     * @param id                   拍卖商品id
     * @param auctionOrderTypeEnum 拍卖类型: 普通拍品 拍卖会拍品
     * @param bondStatusEnum       保证金状态 待支付 已支付 已退款 为null则查询所有
     * @return List<OrderAuctionBond>
     */
    @Override
    public List<OrderAuctionBond> getOrderAuctionBondList(Long id,
            AuctionOrderTypeEnum auctionOrderTypeEnum, BondStatusEnum bondStatusEnum) {
        return this.lambdaQuery().eq(OrderAuctionBond::getAuctionGoodsId, id)
                .eq(OrderAuctionBond::getBondType, auctionOrderTypeEnum).eq(
                        StringUtils.isNotNull(bondStatusEnum), OrderAuctionBond::getBoundStatus,
                        bondStatusEnum).list();
    }
 
    /**
     * 批量更新保证金订单状态
     *
     * @param ids            保证金订单id列表
     * @param bondStatusEnum 保证金状态
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateOrderAuctionBondStatus(List<Long> ids, BondStatusEnum bondStatusEnum) {
        for (Long id:ids){
            OrderAuctionBond byId = this.getById(id);
            byId.setBoundStatus(bondStatusEnum);
            this.updateById(byId);
        }
    }
 
    @Override
    public PayInfoVO SaveOrderAuctionBond(MemberAuctionSalesroomBondDTO memberAuctionSalesroomBondDTO) {
        PayInfoVO payInfoVO = new PayInfoVO();
        if (memberAuctionSalesroomBondDTO.getBondType() == 1) {
            AuctionGoods data = auctionClient.getauctionGoodsOne(memberAuctionSalesroomBondDTO, SecurityConstants.INNER).getData();
 
            if (data.getEndTime().isAfter(LocalDateTime.now())){
                throw new ServiceException("商品拍卖已结束");
            }
            if (data.getListingStatus().getCode()==1){
                throw new ServiceException("商品拍卖下线");
            }
 
            if (data.getBond()==null||data.getBond().intValue() == 0) {
                OrderAuctionBond orderAuctionBond = new OrderAuctionBond();
                orderAuctionBond.setOrderNo(OrderUtil.getOrderNoForPrefix("BO"));
                orderAuctionBond.setAuctionGoodsId(memberAuctionSalesroomBondDTO.getAuctionGoodsId());
                orderAuctionBond.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                orderAuctionBond.setBond(data.getBond());
                orderAuctionBond.setBoundStatus(BondStatusEnum.PAID);
                orderAuctionBond.setBondType(AuctionOrderTypeEnum.AUCTION_ITEMS);
                this.save(orderAuctionBond);
                payInfoVO.setOrderNO(orderAuctionBond.getOrderNo());
                payInfoVO.setType(0);
                payInfoVO.setSubject("保证金");
                payInfoVO.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                payInfoVO.setBody("保证金");
            } else {
                OrderAuctionBond orderAuctionBond = new OrderAuctionBond();
                orderAuctionBond.setOrderNo(OrderUtil.getOrderNoForPrefix("BO"));
                orderAuctionBond.setAuctionGoodsId(memberAuctionSalesroomBondDTO.getAuctionGoodsId());
                orderAuctionBond.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                orderAuctionBond.setBond(data.getBond());
                orderAuctionBond.setBoundStatus(BondStatusEnum.TO_PLAY);
                orderAuctionBond.setBondType(AuctionOrderTypeEnum.AUCTION_ITEMS);
                if (memberAuctionSalesroomBondDTO.getPaymentMethod() == 1) {
                    orderAuctionBond.setPaymentMethod(PaymentMethodEnum.WECHAT);
                } else {
                    orderAuctionBond.setPaymentMethod(PaymentMethodEnum.ALIPAY);
                }
                this.save(orderAuctionBond);
                payInfoVO.setOrderNO(orderAuctionBond.getOrderNo());
                if (memberAuctionSalesroomBondDTO.getPaymentMethod() == 1) {
                    payInfoVO.setType(2);
                } else {
                    payInfoVO.setType(1);
                }
                payInfoVO.setOrderNO(orderAuctionBond.getOrderNo());
                payInfoVO.setSubject("保证金");
                payInfoVO.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                payInfoVO.setBody("保证金");
            }
        } else {
            AuctionSalesroom data = auctionClient.getauctionSalesroomOne(memberAuctionSalesroomBondDTO, SecurityConstants.INNER).getData();
 
            if (data.getBond().intValue() == 0) {
                OrderAuctionBond orderAuctionBond = new OrderAuctionBond();
                orderAuctionBond.setOrderNo(OrderUtil.getOrderNoForPrefix("BO"));
                orderAuctionBond.setAuctionSalesroomId(memberAuctionSalesroomBondDTO.getAuctionSalesroomId());
                orderAuctionBond.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                orderAuctionBond.setBond(data.getBond());
                orderAuctionBond.setBoundStatus(BondStatusEnum.PAID);
                orderAuctionBond.setBondType(AuctionOrderTypeEnum.AUCTION_ITEMS);
                this.save(orderAuctionBond);
                payInfoVO.setOrderNO(orderAuctionBond.getOrderNo());
                payInfoVO.setType(0);
                payInfoVO.setSubject("保证金");
                payInfoVO.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                payInfoVO.setBody("保证金");
            } else {
                OrderAuctionBond orderAuctionBond = new OrderAuctionBond();
                orderAuctionBond.setOrderNo(OrderUtil.getOrderNoForPrefix("BO"));
                orderAuctionBond.setAuctionSalesroomId(memberAuctionSalesroomBondDTO.getAuctionSalesroomId());
                orderAuctionBond.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                orderAuctionBond.setBond(data.getBond());
                orderAuctionBond.setBoundStatus(BondStatusEnum.TO_PLAY);
                orderAuctionBond.setBondType(AuctionOrderTypeEnum.AUCTION_ITEMS);
                if (memberAuctionSalesroomBondDTO.getPaymentMethod() == 1) {
                    orderAuctionBond.setPaymentMethod(PaymentMethodEnum.WECHAT);
                } else {
                    orderAuctionBond.setPaymentMethod(PaymentMethodEnum.ALIPAY);
                }
                this.save(orderAuctionBond);
                payInfoVO.setOrderNO(orderAuctionBond.getOrderNo());
                if (memberAuctionSalesroomBondDTO.getPaymentMethod() == 1) {
                    payInfoVO.setType(2);
                } else {
                    payInfoVO.setType(1);
                }
                payInfoVO.setOrderNO(orderAuctionBond.getOrderNo());
                payInfoVO.setSubject("保证金");
                payInfoVO.setMemberId(memberAuctionSalesroomBondDTO.getMemberId());
                payInfoVO.setBody("保证金");
            }
        }
        return payInfoVO;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void returnBondByAuctionGoodsId(Long id) {
        List<OrderAuctionBond> list = this.lambdaQuery().eq(OrderAuctionBond::getAuctionGoodsId, id)
                .eq(OrderAuctionBond::getBoundStatus, BondStatusEnum.PAID).list();
        Set<String> orderNoSet = list.stream().map(OrderAuctionBond::getOrderNo)
                .collect(Collectors.toSet());
        List<RefundDTO> refundDTOList = list.stream().map(item -> {
            RefundDTO refundDTO = new RefundDTO();
            refundDTO.setAmount(item.getBond());
            refundDTO.setOrderNo(item.getOrderNo());
            return refundDTO;
        }).collect(Collectors.toList());
        Map<String, Object> data = iPaylogService.refund(refundDTOList);
        // 处理退款返回结果
        List<String> successfulOrders = new ArrayList<>();
        List<String> failedOrders = new ArrayList<>();
 
        data.forEach((key, value) -> {
            if ((boolean) value) {
                successfulOrders.add(key);
            } else {
                failedOrders.add(key);
            }
        });
        log.info("订单号:{},退款失败", failedOrders);
        // 退款成功的订单修改状态
        List<OrderAuctionBond> collect = list.stream().map(bond -> {
            if (successfulOrders.contains(bond.getOrderNo())) {
                bond.setBoundStatus(BondStatusEnum.REFUNDED);
                return bond;
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        this.updateBatchById(collect);
    }
 
}