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
package com.mzl.flower.schedule;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.response.point.ExpiredPointDTO;
import com.mzl.flower.entity.flower.FlowerCategory;
import com.mzl.flower.entity.partner.Partner;
import com.mzl.flower.entity.point.CustomerPoint;
import com.mzl.flower.entity.point.CustomerPointDetail;
import com.mzl.flower.mapper.point.CustomerPointDetailMapper;
import com.mzl.flower.mapper.point.CustomerPointMapper;
import com.mzl.flower.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
 
@Component
@Slf4j
public class PointScheduleService {
 
 
    private final CustomerPointMapper customerPointMapper;
 
    private final CustomerPointDetailMapper customerPointDetailMapper;
 
    public PointScheduleService(CustomerPointMapper customerPointMapper, CustomerPointDetailMapper customerPointDetailMapper) {
        this.customerPointMapper = customerPointMapper;
        this.customerPointDetailMapper = customerPointDetailMapper;
    }
 
 
    /**
     * 定时计算用户过期积分
     */
    @Scheduled(cron = "0 10 1 * * ?")
    public void calculatingExpiredPoint() {
        log.info("过期积分计算开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        LocalDate now = LocalDate.now().minusDays(1);//前一天
        LocalDate lastYear = now.minus(1, ChronoUnit.YEARS); // 日期减去一年
        List<ExpiredPointDTO> pointDTOS = customerPointDetailMapper.tongjiExpiredPoint(lastYear,null);
        if(pointDTOS != null && pointDTOS.size() > 0){
            for (ExpiredPointDTO pointDTO : pointDTOS) {
                if(pointDTO.getAddPoint().intValue()> pointDTO.getReducePoint().intValue()){ //积分增加大于减少
                    Integer expiredPoint = pointDTO.getAddPoint().intValue() - pointDTO.getReducePoint().intValue();
                    CustomerPointDetail customerPointDetail = new CustomerPointDetail();
                    customerPointDetail.setUserId(pointDTO.getUserId());
                    customerPointDetail.setCustomerId(pointDTO.getCustomerId());
                    customerPointDetail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name());
                    customerPointDetail.setType(Constants.POINT_TYPE.expired.name());
                    customerPointDetail.setPoint(expiredPoint);
                    customerPointDetail.setRecordDate(lastYear);
                    customerPointDetail.create("sys");
                    customerPointDetail.setRemarks(DateUtils.toString(now,"yyyy-MM-dd")+"过期积分结算");
                    customerPointDetailMapper.insert(customerPointDetail);
 
                    //更新用户积分记录
                    CustomerPoint customerPoint = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
                            .eq(CustomerPoint::getCustomerId, pointDTO.getCustomerId())
                            .eq(CustomerPoint::getUserId, pointDTO.getUserId()));
                    if(customerPoint == null ){
                        log.error("用户积分记录不存在,userId={},customerId={}",pointDTO.getUserId(),pointDTO.getCustomerId());
                    }else {
                        customerPoint.setExpiredPoint(expiredPoint);
                        customerPointMapper.updateById(customerPoint);
                    }
 
                }
            }
        }
 
 
        log.info("过期积分计算结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
 
 
}