package com.cpp.web.service.datafactory.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpp.web.domain.datafactory.ParsingLog;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.enums.DataSourcesEnum;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.mapper.datafactory.ParsingLogMapper;
import com.cpp.web.service.datafactory.ParsingLogService;
import com.cpp.web.service.station.ElectricFieldService;
import com.cpp.web.utils.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 解析记录业务层实现类
 *
 * @author tl
 * @date 2022-05-11 18:07:03
 */
@Service
@RequiredArgsConstructor
public class ParsingLogServiceImpl extends ServiceImpl<ParsingLogMapper, ParsingLog> implements ParsingLogService {

    private final ParsingLogMapper parsingLogMapper;

    private final ElectricFieldService electricFieldService;

    @Override
    public QueryWrapper<ParsingLog> getByStationCodeAndCreateTimeAndFileStatusAndFileType(String stationCode, Date startTime, Date endTime, String fileStatus, String fileType) {
        QueryWrapper<ParsingLog> wrapper = new QueryWrapper<>();
        if (stationCode != null && !stationCode.equals("")) {
            wrapper.eq("station_code", stationCode);
        }
        if (startTime != null && !startTime.equals("") && endTime != null && !endTime.equals("")) {
            wrapper.between("cpp_parsing_time", startTime, endTime);
        }
        if (fileStatus != null && !fileStatus.equals("")) {
            wrapper.eq("cpp_parsing_file_status", fileStatus);
        }
        if (fileType != null && !fileType.equals("")) {
            wrapper.eq("file_type", fileType);
        }
        return wrapper;
    }

    @Override
    public QueryWrapper<ParsingLog> getByStationCodeAndCreateTimeAndFileType(String stationCode, Date startTime, Date endTime, String fileType, String dataSources, String parsingDescribe, String parsingFileStatus) {
        QueryWrapper<ParsingLog> wrapper = new QueryWrapper<>();

        if ("未知".equals(stationCode)) {
            wrapper.isNull("station_code");
        } else if (stationCode != null && !"".equals(stationCode)) {
            wrapper.eq("station_code", stationCode);
        }
        if (null != startTime && !"".equals(startTime) && null != endTime && !"".equals(endTime)) {
            wrapper.between("parsing_time", startTime, endTime);
        }
        if (null != fileType && !"".equals(fileType)) {
            wrapper.eq("file_type", fileType);
        }
        if (null != dataSources && !"".equals(dataSources)) {
            wrapper.eq("data_sources", DataSourcesEnum.valueOf(dataSources));
        }
        if ("null".equals(parsingDescribe)) {
            wrapper.eq("parsing_describe", "");
        }
        if ("notNull".equals(parsingDescribe)) {
            wrapper.ne("parsing_describe", "");
        }
        if (null != parsingFileStatus && !"".equals(parsingFileStatus) && ("成功".equals(parsingFileStatus) || "失败".equals(parsingFileStatus))) {
            wrapper.eq("parsing_file_status", parsingFileStatus);
        }
        return wrapper;
    }

    @Override
    public List<ParsingLog> findByTimeBetweenAndFileTypeAndStationCode(Date startTime, Date endTime, String fileType, String stationCode) {
        QueryWrapper<ParsingLog> wrapper = new QueryWrapper<>();
        if (stationCode != null && !"".equals(stationCode)) {
            wrapper.eq("station_code", stationCode);
        }
        if (null != startTime && !"".equals(startTime) && null != endTime && !"".equals(endTime)) {
            wrapper.between("parsing_time", startTime, endTime);
        }
        if (null != fileType && !"".equals(fileType)) {
            wrapper.eq("file_type", fileType);
        }
        wrapper.eq("data_sources", "E1");
        return list(wrapper);
    }

    @Override
    public List<ElectricField> notTodayLogStation(FileTypeEnum fileTypeEnum, DataSourcesEnum dataSourcesEnum) {
        Date date = DateTimeUtil.getDayStartTime(System.currentTimeMillis());

        QueryWrapper<ParsingLog> wrapper = new QueryWrapper<>();
        wrapper.ge("parsing_time", date);
        wrapper.eq("data_sources", dataSourcesEnum.name());
        wrapper.eq("file_type", fileTypeEnum.name());
        List<ParsingLog> parsingLogs = parsingLogMapper.selectList(wrapper);


        List<ElectricField> electricFields = electricFieldService.list();

        List<ElectricField> electricFieldList = new ArrayList<>();

        for (ElectricField electricField : electricFields) {
            if (!parsingLogs.stream().anyMatch(p->p.getStationCode().equals(electricField.getStationCode()))){
                electricFieldList.add(electricField);
            }
        }

        return electricFieldList;
    }
}
