package com.xy.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xy.entity.DeviceTempRecords;
import com.xy.entity.SysDictRedis;
import com.xy.service.DeviceTempRecordsServiceImpl;
import com.xy.sys.EnumDataClearSize;
import com.xy.utils.DataTime;
import com.xy.utils.RecursionUtils;
import com.xy.utils.SysDictUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 设备温度日志job
 * </p>
 *
 * @author lijin
 * @since 2023-01-11
 */
@Slf4j
@Component
@AllArgsConstructor
public class DeviceTempRecordsJob {

    private DeviceTempRecordsServiceImpl deviceTempRecordsService;

    /**
     * 数据清除
     *
     * @return
     */
    @XxlJob("delDeviceTempRecords")
    public ReturnT<String> delDeviceTempRecords() {
        //数据保留的天数
        SysDictRedis sysDictRedis = SysDictUtils.get(EnumDataClearSize.Code.CODE.getCode(), EnumDataClearSize.DEVICE_TEMP_RECORDS_DAY.getCode());
        Integer saveDay = Integer.valueOf(sysDictRedis.getValue());
        String beginTime = DataTime.getStringAround(0, 0, (~(saveDay - 1)), 0, 0, 0);
        LambdaQueryWrapper<DeviceTempRecords> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceTempRecords>()
                .le(DeviceTempRecords::getCreateTime, beginTime)
                .last("limit 10000");
        RecursionUtils.recursion(current -> {
            boolean remove = deviceTempRecordsService.remove(lambdaQueryWrapper);
            if (!remove) {
                return false;
            }
            return true;
        });
        return ReturnT.SUCCESS;
    }
}
