package cn.axzo.msg.center.message.xxl;

import cn.axzo.msg.center.dal.mapper.TodoLogMapper;
import cn.axzo.msg.center.domain.entity.TodoLog;
import cn.axzo.msg.center.inside.notices.config.PendingMessageBizConfig;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

import java.util.Date;

import static cn.axzo.msg.center.inside.notices.utils.Queries.query;

/**
 * @author yanglin
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ExpungeTodoLogJob extends IJobHandler {

    private final TodoLogMapper todoLogMapper;
    private final PendingMessageBizConfig cfg;

    @Override
    @XxlJob("expungeTodoLogJob")
    public ReturnT<String> execute(String jsonStr) throws Exception {
        Date atMost = DateTime.now()
                .minusDays(cfg.getExpungeTodoLogSinceDaysAgo())
                .toDate();
        int deletedCount = todoLogMapper.delete(query(TodoLog.class)
                .le(TodoLog::getCreateAt, atMost));
        log.info("deletedCount:{}", deletedCount);
        return ReturnT.SUCCESS;
    }

}