package com.jiayue.passback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.passback.entity.UploadFileLog;
import com.jiayue.passback.entity.eunm.FileStatusEnum;
import com.jiayue.passback.mapper.UploadFileLogMapper;
import com.jiayue.passback.service.UploadFileLogService;
import com.jiayue.passback.util.DateMomentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class UploadFileLogServiceImpl extends ServiceImpl<UploadFileLogMapper, UploadFileLog> implements UploadFileLogService {
    @Value("${tunnelNo}")
    private String tunnelNo;

    @Value("${dqType}")
    private String dqType;

    @Value("${cdqType}")
    private String cdqType;

    @Value("${cdqUpMin}")
    private int cdqUpMin;

    @Resource
    private UploadFileLogMapper uploadFileLogMapper;

    @Override
    public Map<String, Object> GenerateData(Date date) {

        Date cdqStartTime = new Date(date.getTime() - cdqUpMin * 60 * 1000);

        Date cdqEndTime = new Date(date.getTime() + 15 * 60 * 1000);

//        Date dqStartTime = new Date(date.getTime() - 15 * 60 * 1000);
        Date dqStartTime = new Date(DateMomentUtil.getDayStartTime(new Date().getTime()));

//        Date dqEndTime = new Date(date.getTime() + 15 * 60 * 1000);
        Date dqEndTime = new Date(DateMomentUtil.getDayLastTime(new Date().getTime()));
        Map<String, Object> map = new HashMap<>();


        if (list(lambdaQuery()
                .between(UploadFileLog::getCreatTime, cdqStartTime, cdqEndTime)
                .eq(UploadFileLog::getFileTypeEnum, cdqType)
                .eq(UploadFileLog::getFileStatusEnum, FileStatusEnum.E2.name())
                .eq(UploadFileLog::getUploadObjectNo, tunnelNo)
                .getWrapper()).size() > 0) {
            map.put("cdqStatus", 1);
        } else {
            map.put("cdqStatus", 0);
        }

        if (list(lambdaQuery()
                .between(UploadFileLog::getCreatTime, dqStartTime, dqEndTime)
                .eq(UploadFileLog::getFileTypeEnum, dqType)
                .eq(UploadFileLog::getUploadObjectNo, tunnelNo)
                .eq(UploadFileLog::getFileStatusEnum, "E2")
                .getWrapper()).size() > 0) {
            map.put("dqStatus", 1);
        } else {
            map.put("dqStatus", 0);
        }

        log.info("获取短期上报状态：{}，获取超短期上报状态：{}", map.get("dqStatus"), map.get("cdqStatus"));

        return map;
    }

    /**
     * 根据文件类型获取最新的上报文件信息
     *
     * @param type
     * @return
     */
    @Override
    public UploadFileLog getNewDateByType(String[] type) {
        UploadFileLog uploadFileLog = new UploadFileLog();
        LambdaQueryWrapper<UploadFileLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UploadFileLog::getFileTypeEnum, type);
        lambdaQueryWrapper.orderByDesc(UploadFileLog::getCreatTime);
        List<UploadFileLog> uploadFileLogList = uploadFileLogMapper.selectList(lambdaQueryWrapper);
        if (uploadFileLogList != null && uploadFileLogList.size() > 0) {
            uploadFileLog = uploadFileLogList.get(0);
        }
        return uploadFileLog;
    }
}
