package com.energy.manage.service.service.analysis.impl;

import com.energy.manage.common.enums.ResultReportEnums;
import com.energy.manage.common.po.analysis.AnalysisResultReportPo;
import com.energy.manage.service.domain.vo.analysis.AnalysisResultReportTypeVo;
import com.energy.manage.service.domain.vo.analysis.AnalysisResultReportVo;
import com.energy.manage.service.mappers.analysis.AnalysisResultReportMapper;
import com.energy.manage.service.service.analysis.AnalysisResultReportService;
import com.energy.manage.service.util.NetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chy
 * @date 2024/9/20 17:33
 * @desc
 */
@Service
public class AnalysisResultReportServiceImpl implements AnalysisResultReportService {

    @Autowired
    private AnalysisResultReportMapper analysisResultReportMapper;
    /**
     * 批量添加保存报告
     *
     * @param resultReportVos
     * @return
     */
    @Override
    public int saveReportBatch(List<AnalysisResultReportVo> resultReportVos) {
        AtomicInteger integer = new AtomicInteger(0);
        resultReportVos.forEach(item -> {
            item.setReportAddr(item.getReportAddr().replaceFirst(NetUtils.IP_PORT_REGEX, ""));
            AnalysisResultReportPo resultReportPo = analysisResultReportMapper.selectByBatchCodeAndReportType(item.getBatchCode(),item.getReportType());
            if(resultReportPo != null){
                analysisResultReportMapper.updateReport(item);
            }else{
                analysisResultReportMapper.insertReport(item);
            }
            integer.getAndIncrement();
        });
        return integer.get();
    }

    /**
     * 查询报告类型
     *
     * @return
     */
    @Override
    public List<AnalysisResultReportTypeVo> getRportType(Boolean isIntranet) {
        List<AnalysisResultReportTypeVo> resultReportTypeVos = new ArrayList<>();
        Arrays.stream(ResultReportEnums.values()).forEach(item -> {
            AnalysisResultReportTypeVo resultReportTypeVo = new AnalysisResultReportTypeVo();
            resultReportTypeVo.setReportType(item.getReportType());
            resultReportTypeVo.setReportTypeName(item.getReportName());
            resultReportTypeVos.add(resultReportTypeVo);
        });
        return resultReportTypeVos;
    }



}
