package com.jiayue.ipp.idp.service.an;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiayue.ipp.common.data.entity.an.ChannelStatus;
import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
import com.jiayue.ipp.common.data.entity.an.ReportingRateRecords;
import com.jiayue.ipp.idp.dto.ChannelStatusDto;
import com.jiayue.ipp.idp.dto.ParsingConstant;
import com.jiayue.ipp.idp.dto.ParsingStatusFileDto;
import com.jiayue.ipp.idp.dto.ReportingRateDto;
import com.jiayue.ipp.idp.util.FileUtil;
import com.jiayue.ipp.idp.util.JsonBeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 解析文件业务层
 *
 * @author tl
 * @date 2022-05-11 09:51:21
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class ParsingFileService {

    private final ReportingRateRecordsService reportingRateRecordsService;

    private final List<ParsingInterface> parsingInterfaces;

    private final ChannelStatusService channelStatusService;

    //文件解析
    /*
     * 解析配置格式规则：
     *   s=>>1=>>entity=
     *   s：单行标识
     *   1：取值在1行
     *   entity=：键标记标记
     *
     *   m=>>4-20=>>3
     *   m：多行标识
     *   4-20：取值在4-20行
     *   3：第三列
     * */
    public String parsingFile(File file, ParsingUrl parsingUrl) {

        log.info("开始尝试解析【{}】的 {}",parsingUrl.getStationCode(),file.getName());
        //因为并不知道当前文件属于哪种类型文件，所以遍历接口实现类来进行对应文件的解析
        for (ParsingInterface parsingInterface : parsingInterfaces) {
            String parsing = parsingInterface.parsing(file, parsingUrl);
            if (!parsing.equals(ParsingConstant.FAIL)) {
                return parsing;
            }
        }

        log.error("无配置符合{}文件解析失败！！！", file.getName());
        return ParsingConstant.FAIL + "无配置符合{}文件解析失败！！！";
    }



    /**
     * 对于状态文件单独解析
     *
     * @param file
     * @param parsingUrl
     * @return
     */
    public String parsingStatusFile(File file, ParsingUrl parsingUrl) {
        SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm");

        String parsingStatus = ParsingConstant.FAIL;
        try {
            String str = FileUtil.getStr(file);

            ParsingStatusFileDto parsingStatusFileDto = JsonBeanUtil.jsonToBean(str, ParsingStatusFileDto.class);

            Date date = simpleDateTimeFormat.parse(parsingStatusFileDto.getDate());

            List<ReportingRateRecords> reportingRateRecordsList = new ArrayList<>();
            for (ReportingRateDto reportingRateDto : parsingStatusFileDto.getReportingRates()) {
                ReportingRateRecords reportingRateRecords = new ReportingRateRecords();

                reportingRateRecords.setFileType(reportingRateDto.getRateType());
                reportingRateRecords.setReportingRate(reportingRateDto.getReportrate());
                reportingRateRecords.setStationCode(parsingUrl.getStationCode());
                reportingRateRecords.setUploadTime(date);

                reportingRateRecordsList.add(reportingRateRecords);
            }

            reportingRateRecordsService.saveBatch(reportingRateRecordsList);


            List<ChannelStatus> channelStatusList = new ArrayList<>();
            for (ChannelStatusDto channelStatusDto : parsingStatusFileDto.getChannelStatus()) {
                ChannelStatus channelStatus = new ChannelStatus();
                channelStatus.setChannelName(channelStatusDto.getChannelName());
                channelStatus.setStationCode(parsingUrl.getStationCode());
                channelStatus.setStatus(channelStatusDto.getStatus());
                channelStatus.setType(channelStatusDto.getType());
                channelStatus.setUploadTime(date);

                channelStatusList.add(channelStatus);
            }

            LambdaQueryWrapper<ChannelStatus> lambdaQueryWrapper = new LambdaQueryWrapper<ChannelStatus>();
            lambdaQueryWrapper.eq(ChannelStatus::getStationCode, parsingUrl.getStationCode());
            channelStatusService.remove(lambdaQueryWrapper);
            channelStatusService.saveBatch(channelStatusList);
            parsingStatus = "status";
        } catch (Exception e) {
            parsingStatus += "status 解析失败";
            log.error("解析状态数据失败！ /(ㄒoㄒ)/~~", e);
        }finally {
            return parsingStatus;
        }

    }


}