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.DeviceNetRecord;
import com.xy.entity.SysDictRedis;
import com.xy.service.DeviceNetRecordServiceImpl;
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>
 * 机器-联网记录 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2023-01-11
 */
@Slf4j
@Component
@AllArgsConstructor
public class DeviceNetRecordJob {

    private DeviceNetRecordServiceImpl deviceNetRecordService;

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