package com.fzm.mall.server.admin.task.opration;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzm.mall.server.admin.opration.enums.status.IntegralReturnMerchantStatusEnum;
import com.fzm.mall.server.admin.opration.model.IntegralReturnMerchant;
import com.fzm.mall.server.admin.opration.service.IIntegralReturnMerchantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wangp
 * @date 2021/6/18 10:43
 * @description 积分返现
 * @since JDK 1.8
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IntegralReturnTask {

    private final IIntegralReturnMerchantService integralReturnMerchantService;

    @Scheduled(cron = "1 0 0 * * ?")
    public void IntegralReturnMerchantOverdue() {
        LambdaQueryWrapper<IntegralReturnMerchant> qw = new LambdaQueryWrapper<>();
        qw.ne(IntegralReturnMerchant::getStatus, IntegralReturnMerchantStatusEnum.DISABLE.getStatus());
        qw.ne(IntegralReturnMerchant::getStatus, IntegralReturnMerchantStatusEnum.DELETE.getStatus());
        qw.ne(IntegralReturnMerchant::getStatus, IntegralReturnMerchantStatusEnum.OVERDUE.getStatus());        List<IntegralReturnMerchant> list = integralReturnMerchantService.list(qw);
        list.forEach(v -> {
            if (v.getEffectiveStartTime() <= System.currentTimeMillis() && v.getEffectiveEndTime() >= System.currentTimeMillis()
                    && !v.getStatus().equals(IntegralReturnMerchantStatusEnum.DISABLE.getStatus())) {
                v.setStatus(IntegralReturnMerchantStatusEnum.ABLE.getStatus());
            }
            if (v.getEffectiveEndTime() < System.currentTimeMillis()) {
                v.setStatus(IntegralReturnMerchantStatusEnum.OVERDUE.getStatus());
            }
        });
        integralReturnMerchantService.updateBatchById(list);
    }

 }

