package com.sptg.third.biz.qinlin.quartz;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.device.api.constant.DeviceConstants;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.third.api.entity.qinlin.QLinDevice;
import com.sptg.third.api.entity.qinlin.QLinOpenRecord;
import com.sptg.third.api.entity.qinlin.QLinQuery;
import com.sptg.third.api.entity.qinlin.QLinUserDevice;
import com.sptg.third.api.feigns.qinlin.QLinFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * title: TimeStrategyQuartzTask
 * package: com.sptg.device.biz.quartz
 * description: 亲邻查询用户通行记录定时任务
 *
 *
 *
 */
@Slf4j
@Component
@RequiredArgsConstructor
@Profile({"prod"})
public class QLinOpenRecordQuartzTask {

    private final RedisTemplate<String,String> redisTemplate;
    private final DeviceFeign deviceFeign;
    private final QLinFeign qLinFeign;

    // @Scheduled(cron = "0 0 3 * * ?")     每天3点执行
    // @Scheduled(cron = "*/5 * * * * ?")   每隔5秒执行

    private final static String REDIS_KEY = "qinlin.openRecord.query.quartz";
    private String s = "2";

    /**
     * 功能描述: 亲邻设备信息查询定时任务
     * 每隔10分钟查询一次
     *
     */
    @Scheduled(cron = "0 */10 * * * ?")
    public void qLinOpenRecordQueryTask(){
        ValueOperations<String, String> operations = this.redisTemplate.opsForValue();
        String value = operations.get(REDIS_KEY);
        //  值存在,则结束定时器
//        if (StringUtils.isNotBlank(value)){
//            return;
//        }else {
//            operations.set(REDIS_KEY,"qinlin openRecord query quartz running");
//        }
        PageBean<Device, DeviceDto> pageBean = new PageBean<>();
        int pageNo = 0;
        DeviceDto dto = new DeviceDto();
        dto.setDeviceFactory(DeviceConstants.DEVICE_FACTORY_QINLIN);
        pageBean.setDto(dto);
        pageBean.setSize(50);
        Date endDate = new Date();
        Calendar cd = Calendar.getInstance();
        cd.setTime(endDate);
        cd.add(Calendar.MINUTE, -10);
        Date startDate = cd.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = sdf.format(startDate);
        String endTime = sdf.format(endDate);
        while (true) {
            try {
                pageNo = pageNo + 1;
                pageBean.setCurrent(pageNo);
                //  分页获取任务
                ResultBean<Page<Device>> resultBean = this.deviceFeign.getPageByCondition(pageBean);
                Page<Device> page = resultBean.getData();
                if (page ==null || page.getRecords().size() == 0){
                    //  结束循环
                    break;
                }
                List<Device> records = page.getRecords();
                for(Device device : records) {
                    if (device.getDeviceFactory().equals("qinlin")) {
                        //  获取用户通行记录--通过设备ID
                        QLinQuery qLinQuery = new QLinQuery();
                        qLinQuery.setDeviceId(device.getSerialNumber());
                        qLinQuery.setStartTime(startTime);
                        qLinQuery.setEndTime(endTime);
                        try{
                            this.qLinFeign.getLogs(qLinQuery);
                        }catch (Exception e){
                            e.printStackTrace();
                            continue;
                        }

                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                //  定时器运行完后删除Key
//                this.redisTemplate.delete(REDIS_KEY);
                continue;
            }
        }

        //  定时器运行完后删除Key
//        this.redisTemplate.delete(REDIS_KEY);
    }
}
