Cui Zhi Feng
2024-09-03 e8ad1cfc313236b6962d4fedb73043074cb382ee
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
package com.mzl.flower.schedule;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.entity.flower.FlowerCategory;
import com.mzl.flower.entity.partner.Partner;
import com.mzl.flower.entity.payment.Order;
import com.mzl.flower.entity.payment.Transfer;
import com.mzl.flower.mapper.flower.FlowerCategoryMapper;
import com.mzl.flower.mapper.partner.PartnerMapper;
import com.mzl.flower.mapper.payment.OrderMapper;
import com.mzl.flower.service.coupon.CouponRecordService;
import com.mzl.flower.service.flower.FlowerCategoryService;
import com.mzl.flower.service.flower.FlowerService;
import com.mzl.flower.service.menber.impl.GrowthValueDealService;
import com.mzl.flower.service.payment.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
@Component
@Slf4j
public class ScheduleService {
 
    @Autowired
    private FlowerCategoryMapper categoryMapper;
 
    @Autowired
    private PartnerMapper partnerMapper;
 
    @Autowired
    private FlowerService flowerService;
 
    @Autowired
    private OrderMapper orderMapper;
 
    @Autowired
    private UserPaymentV3Service paymentV3Service;
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private OrderSettlementService settlementService;
 
    @Autowired
    private FlowerCategoryService categoryService;
 
    @Autowired
    private BillService billService;
 
    @Autowired
    private OrderItemSettlementService orderItemSettlementService;
 
    @Autowired
    private GrowthValueDealService growthValueDealService;
 
    @Autowired
    private CouponRecordService couponRecordService;
 
    @Scheduled(cron = "1 0/30 * * * ?")
    public void calculateAvePrice() {
        log.info("均价计算开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        List<FlowerCategory> cLs = categoryMapper.selectList(new QueryWrapper<FlowerCategory>()
                .isNotNull("parent_id"));
        if(cLs != null && cLs.size() > 0){
            List<Partner> ls = partnerMapper.selectList(new QueryWrapper<Partner>().eq("status", "P"));
            for(FlowerCategory c : cLs){
                flowerService.calculateCategoryDaily(c.getId(), null);
 
                if(ls != null && ls.size() > 0){
                    for(Partner p : ls){
                        flowerService.calculateCategoryDaily(c.getId(), p.getId());
                    }
                }
            }
        }
        log.info("均价计算结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "1 1/30 * * * ?")
    public void calculateCategoryPriceRange() {
        log.info("分类加价缓存开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        List<FlowerCategory> cLs = categoryMapper.selectList(new QueryWrapper<FlowerCategory>()
                .isNotNull("parent_id"));
        if(cLs != null && cLs.size() > 0){
            List<Partner> ls = partnerMapper.selectList(new QueryWrapper<Partner>().eq("status", "P"));
            for(FlowerCategory c : cLs){
                categoryService.calculateCategoryPriceRange(c.getId(), null);
 
                if(ls != null && ls.size() > 0){
                    for(Partner p : ls){
                        categoryService.calculateCategoryPriceRange(c.getId(), p.getId());
                    }
                }
            }
        }
        log.info("分类加价缓存结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "0 0/5 * * * ?")
    public void checkPrepayOrder() {//五分钟未付款自动取消
        log.info("未付款确认开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
 
        List<Order> ls = orderMapper.selectList(new QueryWrapper<Order>()
                .eq("status", Constants.ORDER_STATUS_BACKEND.PENDING.name())
                .eq("deleted", 0));
        if(ls != null && ls.size() > 0){
            for(Order o : ls){
                try {
                    LocalDateTime createdTime = o.getCreateTime().plusMinutes(5);
                    if (createdTime.isBefore(LocalDateTime.now())) {
                        boolean f = paymentV3Service.checkOrderStatus(o.getId());
                        if(!f){
                            paymentV3Service.cancelOrder(o.getId());
                        }
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
 
        log.info("未付款确认结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "0 10 0 1 * ?")
    public void calculateSupplierSaleNum(){
        log.info("计算上月供应商销售数量开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        orderService.calculateSupplierSaleNum();
        log.info("计算上月供应商销售数量结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "1 20 0/1 * * ?")
    public void autoReceive() {
        log.info("自动收货开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        List<Order> ls = orderService.autoReceive();
        if(ls != null && ls.size() > 0){
            for(Order o : ls){
                orderService.processAfterReceive(o);
            }
        }
        log.info("自动收货结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "1 0 0 * * ?")
    public void doSettlement() {
        log.info("结算开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        //settlementService.doSettlement();
 
        //新结算
        List<Order> ls = settlementService.getOrders4Settlement();
        if(ls != null && ls.size() > 0){
            List<String> orderIds = new ArrayList<>();
            for(Order o : ls){
                orderIds.add(o.getId());
            }
            orderItemSettlementService.saveItemSettlementInfo(orderIds);
            settlementService.doSettlementNew(ls);
        }
 
        log.info("结算结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "2 0/10 * * * ?")
    public void checkTransfer() {
        log.info("确认转账开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        List<Transfer> ls = paymentV3Service.getUnCompletedTransfer();
        if(ls != null && ls.size() > 0){
            for(Transfer t : ls){
                try {
                    paymentV3Service.checkTransferStatus(t);
                    settlementService.updateSettlementStatus(t.getId());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        log.info("确认转账结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "1 30 0 * * ?")
    public void generateBill() {
        log.info("账单开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        LocalDate date = LocalDate.now();
        billService.generateBill(date.plusDays(-1));
        log.info("账单结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    //成长值降级定时任务
    @Scheduled(cron = "0 0 5 * * ?")
    public void deductGrowthValue() {
        log.info("成长值扣除开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        //获取当前员工
        List<Order> orderList = orderMapper.getOrderInfoByReceiveTime();
        if(!CollectionUtils.isEmpty(orderList)){
            orderList.forEach(o->{
                try {
                    growthValueDealService.deductionGrowthValue(o);
                } catch (Exception e) {
                    // 记录错误信息,例如将错误信息写入日志
                    log.info("处理订单 " + o.getId() + " 时出错: " + e.getMessage());
                }
            });
        }
        log.info("成长值扣除结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
 
    @Scheduled(cron = "0 30 0 1 * ?")
    public void grantVipCouponRecordList() {
        log.info("会员优惠券开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        couponRecordService.grantVipCouponRecordList();
        log.info("会员优惠券结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
    @Scheduled(cron = "0 30 0 1 * ?")
    public void expiredCouponRecordLastMon() {
        log.info("会员优惠券开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        couponRecordService.expiredCouponRecordLastMon();
        log.info("会员优惠券结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
 
}