package org.springblade.modules.quartz.task;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.activities.entity.SignIn;
import org.springblade.modules.activities.service.ISignInService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author pangqijun
 * Date 2023/6/13
 * Description 签到定时任务，用于计算用户是否连续签到
 */
@Slf4j
@Component("signTask")
public class SignTask {

    @Resource
    private ISignInService signInService;

    public void execute() {
        // 获取未连续签到的数据，将签到天数置为0
        List<SignIn> signIns = signInService.getNotContinuous();
        log.info("未连续签到的数据总条数[{}]", signIns.size());
        if (CollUtil.isNotEmpty(signIns)) {
            for (SignIn signIn : signIns) {
                signIn.setDays(0);
            }
        }
        signInService.updateBatchById(signIns);

        // 查询签到天数等于7天的数据，将7天的签到天数置为0
        List<SignIn> list = signInService.lambdaQuery().eq(SignIn::getDays, 7).list();
        if (CollUtil.isNotEmpty(list)) {
            for (SignIn signIn : list) {
                signIn.setDays(0);
            }
        }
        signInService.updateBatchById(list);
    }
}
