package com.ycwl.basic.task;


import com.ycwl.basic.device.DeviceFactory;
import com.ycwl.basic.device.operator.IDeviceStorageOperator;
import com.ycwl.basic.mapper.DeviceMapper;
import com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity;
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
import com.ycwl.basic.model.pc.device.req.DeviceReqQuery;
import com.ycwl.basic.model.pc.device.resp.DeviceRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
@Slf4j
@Profile("prod")
public class VideoPieceCleaner {
    @Autowired
    private DeviceMapper deviceMapper;

    @Scheduled(cron = "0 0 0 * * ?")
    public void clean() {
        log.info("开始删除视频文件");
        List<DeviceEntity> deviceList = deviceMapper.listAll();
        for (DeviceEntity device : deviceList) {
            DeviceConfigEntity config = deviceMapper.getConfigByDeviceId(device.getId());
            if (config == null) {
                continue;
            }
            if (config.getStoreExpireDay() == null) {
                continue;
            }
            if (config.getStoreExpireDay() <= 0) {
                continue;
            }
            IDeviceStorageOperator storageOperator = DeviceFactory.getDeviceStorageOperator(device, config);
            if (storageOperator == null) {
                continue;
            }
            storageOperator.removeFilesBeforeDate(new Date(System.currentTimeMillis() - config.getStoreExpireDay() * 24 * 60 * 60 * 1000));
            log.info("删除视频文件完成");
        }
    }
}
