package beilv.quartz.task;

import beilv.voucher.domain.BeilvVoucherMember;
import beilv.voucher.mapper.BeilvVoucherMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时改变优惠券过期状态任务
 */
@Component("couponTask")
public class CouponTask {

    @Autowired
    private BeilvVoucherMemberMapper beilvVoucherMemberMapper;

    public void couponIsValidity() {
        List<BeilvVoucherMember> voucherMemberList = beilvVoucherMemberMapper.selectAllVoucherMemberList(new BeilvVoucherMember("1"));
        // 遍历未过期的代金券用户关系，检查是否过期
        for (BeilvVoucherMember voucherMember : voucherMemberList) {
            if (voucherMember.getValidityTime() != null) {
                long currentTime = System.currentTimeMillis();
                long validityTime = voucherMember.getValidityTime().getTime();
                // 如果当前时间超过了有效期时间，更新状态为已过期
                if (currentTime > validityTime) {
                    voucherMember.setIsValidity("3"); // 设置为已过期
                    beilvVoucherMemberMapper.updateBeilvVoucherMember(voucherMember);  // 更新数据库
                }
            }
        }
    }
}
