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
package com.xiaopeng.account.schedule;
 
import cn.hutool.core.collection.CollUtil;
import com.xiaopeng.account.model.vo.XpAppUserCouponVo;
import com.xiaopeng.account.service.IXpAppUserCouponService;
import com.xiaopeng.api.feignClient.pushMessage.SubscribeMessageClient;
import com.xiaopeng.api.model.account.XpAppUserCoupon;
import com.xiaopeng.api.vo.pushMessage.SendSubscribeMessageVo;
import com.xiaopeng.common.core.enums.MessageTemplateEnum;
import com.xiaopeng.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
@Slf4j
@Component
public class AppUserCouponTask {
 
    @Resource
    private  IXpAppUserCouponService xpAppUserCouponService;
    @Resource
    private SubscribeMessageClient subscribeMessageClient;
 
    @Resource
    private RedissonClient redissonClient;
 
    /**
     * 优惠卷过期 前 24 小时 发送微信通知
     * 每5分钟执行一次
     */
    @Scheduled(fixedRate = 60*1000)
    public void appUserCoupon_expired() throws InterruptedException {
        List<XpAppUserCoupon> list = xpAppUserCouponService.findListExpirationTime(null);
 
 
 
        if(CollUtil.isNotEmpty( list)){
 
            Map<Long, List<XpAppUserCoupon>> groupedByUserId = list.stream()
                    .collect(java.util.stream.Collectors.groupingBy(XpAppUserCoupon::getAppUserId));
 
            for (Map.Entry<Long, List<XpAppUserCoupon>> entry : groupedByUserId.entrySet()) {
                Long appUserId = entry.getKey();
                RLock lock = redissonClient.getLock("app_user_coupon_expired_" + appUserId );
                try {
                    if(lock.tryLock(0, 10, TimeUnit.SECONDS)){
                        List<Long> ids = new ArrayList<>();
                        BigDecimal money = BigDecimal.ZERO;
                        String couponName = "";
                        for (XpAppUserCoupon xpAppUserCoupon : entry.getValue()) {
                            ids.add(xpAppUserCoupon.getId());
                            if(money.compareTo(xpAppUserCoupon.getMoney())<0){
                                money = xpAppUserCoupon.getMoney();
                                couponName = xpAppUserCoupon.getCouponName();
                            }
                        }
                        //发送微信通知
                        SendSubscribeMessageVo msgVo = new SendSubscribeMessageVo();
                        msgVo.setUserId(appUserId);
                        msgVo.setTemplateId(MessageTemplateEnum.C.getTemplateId());
                        String json = MessageTemplateEnum.C.getRemarks();
                        json = json.replace("优惠券名称", couponName);
                        json = json.replace("XXX", money.toPlainString() );
                        msgVo.setJson(json);
                        subscribeMessageClient.sendSubscribeMessage(msgVo);
 
                        xpAppUserCouponService.updateExpirationTime(ids);
 
                    }
                } catch (Exception e) {
                    log.error("优惠券过期发送微信通知失败", e);
                } finally {
                    lock.unlock();
                }
            }
        }
    }
 
 
}