tj
2025-04-01 16c60b7e3b834b05599882ab1ffe99ec78664762
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
package com.mzl.flower.service.impl.productOrders;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.component.SequenceNo;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.productOrders.ProductOrdersCreateDTO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.productOrders.ProductOrdersDO;
import com.mzl.flower.enums.PayStatusEnum;
import com.mzl.flower.enums.ProductTypeEnum;
import com.mzl.flower.enums.alipay.AlipayResultEnum;
import com.mzl.flower.mapper.productOrders.ProductOrdersMapper;
import com.mzl.flower.service.customer.CustomerService;
import com.mzl.flower.service.productOrders.ProductOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@TaoJie
 * @since 2025-03-31
 */
@Service
public class ProductOrdersServiceImpl extends ServiceImpl<ProductOrdersMapper, ProductOrdersDO> implements ProductOrdersService {
 
    @Autowired
    private ProductOrdersMapper productOrdersMapper;
 
    @Autowired
    private SequenceNo sequenceNo;
 
    @Autowired
    private CustomerService customerService;
 
 
    @Transactional
    @Override
    public ProductOrdersDO createProductOrders(ProductOrdersDO productOrdersDO) {
 
        String userId = SecurityUtils.getUserId();
 
//        ProductOrdersDO productOrdersDO = new ProductOrdersDO();
 
        productOrdersDO.setId(IdUtil.simpleUUID());
 
        productOrdersDO.setUserId(userId);
 
        // 待支付
        productOrdersDO.setPaymentStatus(PayStatusEnum.PAYING.getName());
 
 
        productOrdersDO.setOrderNo(getSalesNo());
 
 
        productOrdersDO.create(userId);
 
 
        int insert = productOrdersMapper.insert(productOrdersDO);
 
        return productOrdersMapper.selectById(productOrdersDO.getId());
    }
 
    @Transactional
    @Override
    public Boolean updateProductOrderStatus(Map<String, String> params) {
        String orderNo = params.get("out_trade_no"); // 本系统对应的订单好
        String tradeNo = params.get("trade_no");  //支付宝交易号。支付宝交易凭证号。
        String tradeStatus = params.get("trade_status"); //交易状态。咨询目前所处的状态。
        String gmtPayment = params.get("gmt_payment"); // 交易 付款时间。该笔交易的买家付款时间。格式为 yyyy-MM-dd HH:mm:ss。
        String gmtRefund = params.get("gmt_refund"); // 交易退款时间。该笔交易的退款时间。格式 为 yyyy-MM-dd HH:mm:ss.SS。
        String gmtClose = params.get("gmt_close"); // 交易退款时间。该笔交易的退款时间。格式 为 yyyy-MM-dd HH:mm:ss.SS。
 
 
        // 根据orderNo 查找订单
        QueryWrapper<ProductOrdersDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProductOrdersDO::getDeleted, false)
                .eq(ProductOrdersDO::getOrderNo, orderNo);
        List<ProductOrdersDO> productOrdersDOS = productOrdersMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(productOrdersDOS)){
 
            ProductOrdersDO productOrdersDO = productOrdersDOS.get(0);
 
            if(tradeStatus.equals(AlipayResultEnum.TRADE_CLOSED.getName())){
                // 未付款交易超时关闭,或支付完成后全额退款
                productOrdersDO.setPaymentStatus(PayStatusEnum.CANCEL.getName());
            }
            if(tradeStatus.equals(AlipayResultEnum.TRADE_SUCCESS.getName())){
                // 交易支付成功
                productOrdersDO.setPaymentStatus(PayStatusEnum.PAID.getName());
            }
            if(tradeStatus.equals(AlipayResultEnum.TRADE_FINISHED.getName())){
                // 交易完成(交易结束,不可退款)
                productOrdersDO.setPaymentStatus(PayStatusEnum.FINISHED.getName());
            }
            // 将yyyy-MM-dd HH:mm:ss转换成LocalDateTime
            if(gmtPayment != null){
                productOrdersDO.setPaymentTime(DateUtils.dateTimeStringToLocalDateTime(gmtPayment));
            }
 
 
            // 更新用户到期时间
            if(!StringUtils.isEmpty(productOrdersDO.getUserId())){
                Customer customer = customerService.getByUserId(productOrdersDO.getUserId());
                if(customer != null){
                    LocalDateTime overTime = null;
                    // 根据年卡,月卡、月卡持续计算到期时间
                    if(productOrdersDO.getOrderType().equals(ProductTypeEnum.MONTH_CONTINUED.getName())){
                        if(null!=customer.getMemberOvertime()){
                            // 在当前时间上增加一个月
                            overTime = customer.getMemberOvertime().plusMonths(1);
                        }else{
                            // 当前时间上增加一个月
                            overTime =LocalDateTime.now().plusMonths(1);
                        }
                    }
                    if(productOrdersDO.getOrderType().equals(ProductTypeEnum.MONTH.getName())){
                        if(null!=customer.getMemberOvertime()){
                            // 在当前时间上增加一个月
                            overTime = customer.getMemberOvertime().plusMonths(1);
                        }else{
                            // 当前时间上增加一个月
                            overTime =LocalDateTime.now().plusMonths(1);
                        }
                    }
                    if(productOrdersDO.getOrderType().equals(ProductTypeEnum.YEAR.getName())){
                        if(null!=customer.getMemberOvertime()){
                            // 在当前时间上增加一个月
                            overTime = customer.getMemberOvertime().plusYears(1);
                        }else{
                            // 当前时间上增加一个月
                            overTime = customer.getMemberOvertime().plusYears(1);
                        }
 
                    }
                    // 设置过期时间
                    customer.setMemberOvertime(overTime);
                    customer.setIsMember(true);  // 会员
                }
 
                // 更新会员信息
                customerService.updateMemberInfo(customer);
            }
 
 
            return   productOrdersMapper.updateById(productOrdersDO)>0;
 
        }
 
        return false;
    }
 
    private String getSalesNo(){
        String seq  = sequenceNo.getSeqNo(SequenceNo.ORDER_PRODUCT);
        return "PO" + DateUtils.format(LocalDateTime.now(), "yyyyMMddHHmmss") + seq;
    }
}