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.enums.DataSourcesEnum;
import com.cpp.web.mapper.datafactory.ParsingLogMapper;
import com.cpp.web.service.datafactory.ParsingLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    private final ParsingLogMapper parsingLogMapper;

    public ParsingLogServiceImpl(ParsingLogMapper parsingLogMapper) {
        this.parsingLogMapper = parsingLogMapper;
    }

    @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 (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.isNotNull("parsing_describe");
        }
        if (null != parsingFileStatus && !"".equals(parsingFileStatus) && "成功".equals(parsingFileStatus)){
            wrapper.eq("parsing_file_status",parsingFileStatus);
        }
        if (null != 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");
        List<ParsingLog> parsingLogs = parsingLogMapper.selectList(wrapper);
        return parsingLogs;
    }
}
