package com.mosty.wzzx.task;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mosty.base.utils.Constant;
import com.mosty.base.utils.DateUtils;
import com.mosty.wzzx.service.TbWzSbsswzService;
import com.mosty.wzzx.service.TbWzSswzService;
import com.mosty.wzzx.service.impl.TbWzSbsswzServiceImpl;
import com.mosty.wzzx.service.impl.TbWzSswzServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
@AllArgsConstructor
public class LocationClearTask {

    private final RedisTemplate redisTemplate;
    private final TbWzSbsswzService tbWzSbsswzService;
    private final TbWzSswzService tbWzSswzService;

    //每天凌晨执行（删除redis数据，删除实时位置的7天前的数据）
    @Scheduled(cron = "0 0 1 * * ? ")
    public void locationClear() {
        System.out.println("开始执行位置定时器位置清理");
        //清除人员位置
        String key = Constant.WZ_RYWZ + "*";
        Set<String> ryKeys = redisTemplate.keys(key);
        if (CollectionUtils.isNotEmpty(ryKeys)) {
            ryKeys.forEach(v -> {
                if (!v.contains(DateUtils.getSystemDateString())) {
                    redisTemplate.delete(v);
                }
            });
        }
        //清除设备位置
        String sbwzKey = Constant.WZ_SBWZ + "*";
        Set<String> sbKeys = redisTemplate.keys(sbwzKey);
        if (CollectionUtils.isNotEmpty(sbKeys)) {
            sbKeys.forEach(v -> {
                if (!v.contains(DateUtils.getSystemDateString())) {
                    redisTemplate.delete(v);
                }
            });
        }

        //删除实时位置7天前数据
        tbWzSswzService.delHistory();
        //删除设备实时位置7天前数据
        tbWzSbsswzService.delHistory();
    }

}
