package com.shcd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanghaigeography.Util.DateUtils;
import com.shanghaigeography.Util.SnowflakeConfig;
import com.shcd.entity.SpStatementContainer;
import com.shcd.entity.SpStatementQuarterReport;
import com.shcd.enums.ShorePowerQuarterDataType;
import com.shcd.mapper.SpStatementContainerMapper;
import com.shcd.mapper.SpStatementShipUsageMonthMapper;
import com.shcd.service.SpStatementQuarterReportService;
import com.shcd.mapper.SpStatementQuarterReportMapper;
import com.shcd.utils.PrintLogUtils;
import com.shcd.vo.SpQuarterReportBlVo;
import com.shcd.vo.SpQuarterReportOriginVo;
import com.shcd.vo.SpShipUsageMonthQuarterVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.springframework.util.CollectionUtils;

/**
 * @author zoro
 * @description 针对表【SP_STATEMENT_QUARTER_REPORT(上海港船舶使用岸电情况分泊位统计表)】的数据库操作Service实现
 * @createDate 2024-02-01 10:59:29
 */
@Service
@Slf4j
public class SpStatementQuarterReportServiceImpl extends
    ServiceImpl<SpStatementQuarterReportMapper, SpStatementQuarterReport>
    implements SpStatementQuarterReportService {

  @Resource
  private SpStatementShipUsageMonthMapper spStatementShipUsageMonthMapper;

  @Resource
  private SpStatementContainerMapper containerMapper;

  @Autowired
  private SnowflakeConfig snowflakeConfig;
  private final String dateEnd = "-25 00:00:00";

  @Override
  public void generateReportSpQuarterReport(String currentDate) {
    try {
      log.info("开始计算季度报表  ===========");
      //      Calendar calendar = Calendar.getInstance();
//      int currentMonth = calendar.get(Calendar.MONTH) + 1;
//      int currentYear = calendar.get(Calendar.YEAR);
      int currentMonth;
      int currentYear;
      if (!StringUtils.isBlank(currentDate)) {
        currentMonth = Integer.parseInt(currentDate.split("-")[1]);
        currentYear = Integer.parseInt(currentDate.split("-")[0]);
      } else {
        Calendar instance = Calendar.getInstance();
        currentMonth = instance.get(Calendar.MONTH) + 1;
        currentYear = instance.get(Calendar.YEAR);
      }
      int currentQuarter = (currentMonth % 3 == 0) ? (currentMonth / 3) : (currentMonth / 3 + 1);
      Date startDate;
      Date endDate;
      Date newStartDate;
      Date newEndDate;
      List<String> quarterReportDateString = new ArrayList<>();
      if (currentQuarter == 1) {
        startDate = DateUtils.stringToDate(currentYear - 1 + "-" + "12" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        endDate = DateUtils.stringToDate(currentYear + "-" + "03" + dateEnd, "yyyy-MM-dd HH:mm:ss");
        newStartDate = DateUtils.stringToDate(currentYear - 1 + "-" + "11" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        newEndDate = DateUtils.stringToDate(currentYear + "-" + "02" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        quarterReportDateString.add(currentYear + "-01");
        quarterReportDateString.add(currentYear + "-02");
        quarterReportDateString.add(currentYear + "-03");
      } else if (currentQuarter == 2) {
        startDate = DateUtils.stringToDate(currentYear + "-" + "03" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        endDate = DateUtils.stringToDate(currentYear + "-" + "06" + dateEnd, "yyyy-MM-dd HH:mm:ss");
        newStartDate = DateUtils.stringToDate(currentYear + "-" + "02" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        newEndDate = DateUtils.stringToDate(currentYear + "-" + "05" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        quarterReportDateString.add(currentYear + "-04");
        quarterReportDateString.add(currentYear + "-05");
        quarterReportDateString.add(currentYear + "-06");
      } else if (currentQuarter == 3) {
        startDate = DateUtils.stringToDate(currentYear + "-" + "06" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        endDate = DateUtils.stringToDate(currentYear + "-" + "09" + dateEnd, "yyyy-MM-dd HH:mm:ss");
        newStartDate = DateUtils.stringToDate(currentYear + "-" + "05" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        newEndDate = DateUtils.stringToDate(currentYear + "-" + "08" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        quarterReportDateString.add(currentYear + "-07");
        quarterReportDateString.add(currentYear + "-08");
        quarterReportDateString.add(currentYear + "-09");
      } else {
        startDate = DateUtils.stringToDate(currentYear + "-" + "09" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        endDate = DateUtils.stringToDate(currentYear + "-" + "12" + dateEnd, "yyyy-MM-dd HH:mm:ss");
        newStartDate = DateUtils.stringToDate(currentYear + "-" + "08" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        newEndDate = DateUtils.stringToDate(currentYear + "-" + "11" + dateEnd,
            "yyyy-MM-dd HH:mm:ss");
        quarterReportDateString.add(currentYear + "-10");
        quarterReportDateString.add(currentYear + "-11");
        quarterReportDateString.add(currentYear + "-12");
      }
      //防止数据重复出现，先删后插
      this.remove(Wrappers.<SpStatementQuarterReport>lambdaQuery()
          .eq(SpStatementQuarterReport::getQuarter, currentQuarter)
          .eq(SpStatementQuarterReport::getYear, currentYear));
      //原始数据
      CompletableFuture<List<SpQuarterReportOriginVo>> future1 =
          CompletableFuture.supplyAsync(
              () -> spStatementShipUsageMonthMapper.selectQuarterOrigin(startDate, endDate,
                  newStartDate, newEndDate));
      //手动数据
      CompletableFuture<List<SpQuarterReportBlVo>> future2 = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.selectQuarterBlBerth(startDate, endDate));
      CompletableFuture.allOf(future1, future2).join();
      List<SpQuarterReportOriginVo> spQuarterOrigin = future1.get();
      List<SpQuarterReportBlVo> spQuarterBlBerth = future2.get();
      //原始数据处理
      HashMap<Integer, List<SpQuarterReportOriginVo>> resultMapOrigin = new HashMap<>();
      List<SpQuarterReportOriginVo> container = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(1)).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.CONTAINER_BERTH.getDataType(), container);
      List<SpQuarterReportOriginVo> roll = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(7)).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.ROLL_BERTH.getDataType(), roll);
      List<SpQuarterReportOriginVo> dryBulk2000 = spQuarterOrigin.stream().filter(
          i -> (i.getBerthType().equals(2) || i.getBerthType().equals(3))
              && i.getCapacity() >= 2000).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.DRY_BULK_CARGO_BERTH_2000.getDataType(),
          dryBulk2000);
      List<SpQuarterReportOriginVo> dryBulk = spQuarterOrigin.stream().filter(
              i -> (i.getBerthType().equals(2) || i.getBerthType().equals(3)) && i.getCapacity() < 2000)
          .collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.DRY_BULK_CARGO_BERTH_OTHER.getDataType(),
          dryBulk);
      List<SpQuarterReportOriginVo> multiUse2000 = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(11) && i.getCapacity() >= 2000)
          .collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.MULTI_USE_BERTH_2000.getDataType(),
          multiUse2000);
      List<SpQuarterReportOriginVo> multiUse = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(11) && i.getCapacity() < 2000)
          .collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.MULTI_USE_BERTH_OTHER.getDataType(), multiUse);
      List<SpQuarterReportOriginVo> common2000 = spQuarterOrigin.stream().filter(
          i -> (i.getBerthType().equals(9) || i.getBerthType().equals(10))
              && i.getCapacity() >= 2000).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.COMMON_BERTH_2000.getDataType(), common2000);
      List<SpQuarterReportOriginVo> common = spQuarterOrigin.stream().filter(
          i -> (i.getBerthType().equals(9) || i.getBerthType().equals(10))
              && i.getCapacity() < 2000).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.COMMON_BERTH_OTHER.getDataType(), common);
      List<SpQuarterReportOriginVo> cruise = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(6)).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.CRUISE_BERTH.getDataType(), cruise);
      List<SpQuarterReportOriginVo> passenger = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(4) || i.getBerthType().equals(5))
          .collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.PASSENGER_BERTH.getDataType(), passenger);
      List<SpQuarterReportOriginVo> other = spQuarterOrigin.stream()
          .filter(i -> i.getBerthType().equals(255)).collect(Collectors.toList());
      resultMapOrigin.put(ShorePowerQuarterDataType.OTHER_BERTH.getDataType(), other);
      //手动数据处理
      HashMap<Integer, List<SpQuarterReportBlVo>> resultMapBl = new HashMap<>();
      List<SpQuarterReportBlVo> containerBl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(1)).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.CONTAINER_BERTH.getDataType(), containerBl);
      List<SpQuarterReportBlVo> rollBl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(7)).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.ROLL_BERTH.getDataType(), rollBl);
      List<SpQuarterReportBlVo> dryBulk2000Bl = spQuarterBlBerth.stream().filter(
          i -> (i.getBerthType().equals(2) || i.getBerthType().equals(3))
              && i.getCapacity() >= 2000).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.DRY_BULK_CARGO_BERTH_2000.getDataType(),
          dryBulk2000Bl);
      List<SpQuarterReportBlVo> dryBulkBl = spQuarterBlBerth.stream().filter(
              i -> (i.getBerthType().equals(2) || i.getBerthType().equals(3)) && i.getCapacity() < 2000)
          .collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.DRY_BULK_CARGO_BERTH_OTHER.getDataType(),
          dryBulkBl);
      List<SpQuarterReportBlVo> multiUse2000Bl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(11) && i.getCapacity() >= 2000)
          .collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.MULTI_USE_BERTH_2000.getDataType(), multiUse2000Bl);
      List<SpQuarterReportBlVo> multiUseBl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(11) && i.getCapacity() < 2000)
          .collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.MULTI_USE_BERTH_OTHER.getDataType(), multiUseBl);
      List<SpQuarterReportBlVo> common2000Bl = spQuarterBlBerth.stream().filter(
          i -> (i.getBerthType().equals(9) || i.getBerthType().equals(10))
              && i.getCapacity() >= 2000).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.COMMON_BERTH_2000.getDataType(), common2000Bl);
      List<SpQuarterReportBlVo> commonBl = spQuarterBlBerth.stream().filter(
          i -> (i.getBerthType().equals(9) || i.getBerthType().equals(10))
              && i.getCapacity() < 2000).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.COMMON_BERTH_OTHER.getDataType(), commonBl);
      List<SpQuarterReportBlVo> cruiseBl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(6)).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.CRUISE_BERTH.getDataType(), cruiseBl);
      List<SpQuarterReportBlVo> passengerBl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(4) || i.getBerthType().equals(5))
          .collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.PASSENGER_BERTH.getDataType(), passengerBl);
      List<SpQuarterReportBlVo> otherBl = spQuarterBlBerth.stream()
          .filter(i -> i.getBerthType().equals(255)).collect(Collectors.toList());
      resultMapBl.put(ShorePowerQuarterDataType.OTHER_BERTH.getDataType(), otherBl);
      List<SpStatementQuarterReport> spStatementQuarterReports = new ArrayList<>();
      ShorePowerQuarterDataType[] dataTypes = ShorePowerQuarterDataType.values();
      //计算季度,即使某个类型没有数据也能存进表里 spStatementQuarterReports必定会有12条数据
      for (ShorePowerQuarterDataType dataType : dataTypes) {
        // 集装箱数据从指标报表中的集装箱数据中拿取
        if (ShorePowerQuarterDataType.CONTAINER_BERTH.equals(dataType)) {
          SpQuarterReportOriginVo spQuarterReportOriginVo = containerMapper.selectCurrentQuarterData(
              quarterReportDateString);
          SpStatementQuarterReport spStatementQuarterReport = new SpStatementQuarterReport();
          spStatementQuarterReport.setDataType(dataType.getDataType());
          spStatementQuarterReport.setId(snowflakeConfig.snowflakeStringId());
          spStatementQuarterReport.setYear(currentYear);
          spStatementQuarterReport.setQuarter(currentQuarter);
          spStatementQuarterReport.setShipUsageQuarter(spQuarterReportOriginVo.getShipUsage());
          spStatementQuarterReport.setChargeTimeQuarter(spQuarterReportOriginVo.getChargeTime());
          spStatementQuarterReport.setElectUseQuarter(spQuarterReportOriginVo.getElect());
          spStatementQuarterReports.add(spStatementQuarterReport);
        } else {
          SpStatementQuarterReport spStatementQuarterReport = new SpStatementQuarterReport();
          spStatementQuarterReport.setDataType(dataType.getDataType());
          spStatementQuarterReport.setId(snowflakeConfig.snowflakeStringId());
          spStatementQuarterReport.setYear(currentYear);
          spStatementQuarterReport.setQuarter(currentQuarter);
          calculateQuarterReportDetailOriginMonth(spStatementQuarterReport,
              resultMapOrigin.get(dataType.getDataType()));
          calculateQuarterReportDetailBlMonth(spStatementQuarterReport,
              resultMapBl.get(dataType.getDataType()));
          spStatementQuarterReports.add(spStatementQuarterReport);
        }
      }
//      saveCurrentMonthContainerDataToDB(spStatementQuarterReports, currentDate);
      //过滤出额外的总和,合并明细的时候会增加一个总和的数据，但是都是0
      SpStatementQuarterReport reportSum = spStatementQuarterReports.stream()
          .filter(i -> i.getDataType().equals(ShorePowerQuarterDataType.SUM.getDataType()))
          .collect(Collectors.toList()).get(0);
      //季度明细
      spStatementQuarterReports = spStatementQuarterReports.stream()
          .filter(i -> !i.getDataType().equals(ShorePowerQuarterDataType.SUM.getDataType()))
          .collect(Collectors.toList());
      //计算季度总和  需要与上海市长江经济带船舶报送交通委的差值   根据上海市长江经济带船舶报送交通委中间表进行对比  总和必须一致 差值补充到其他类型
      Date start = null;
      Date end = null;
      if (currentQuarter == 1) {
        start = DateUtils.stringToDate(currentYear + "-" + "01", "yyyy-MM");
        end = DateUtils.stringToDate(currentYear + "-" + "03", "yyyy-MM");
      } else if (currentQuarter == 2) {
        start = DateUtils.stringToDate(currentYear + "-" + "04" + dateEnd, "yyyy-MM");
        end = DateUtils.stringToDate(currentYear + "-" + "06" + dateEnd, "yyyy-MM");
      } else if (currentQuarter == 3) {
        start = DateUtils.stringToDate(currentYear + "-" + "07" + dateEnd, "yyyy-MM");
        end = DateUtils.stringToDate(currentYear + "-" + "09" + dateEnd, "yyyy-MM");
      } else {
        start = DateUtils.stringToDate(currentYear + "-" + "10" + dateEnd, "yyyy-MM");
        end = DateUtils.stringToDate(currentYear + "-" + "12" + dateEnd, "yyyy-MM");
      }
      SpStatementQuarterReport otherSumEntity = spStatementQuarterReports.stream()
          .filter(i -> i.getDataType().equals(ShorePowerQuarterDataType.OTHER_BERTH.getDataType()))
          .collect(Collectors.toList()).get(0);
      SpShipUsageMonthQuarterVo spShipUsageMonthQuarterVo = spStatementShipUsageMonthMapper.selectThreeMonthsDataTotal(
          start, end);
      BigDecimal originChargeTimeSum = BigDecimal.ZERO;
      BigDecimal originShipUsageSum = BigDecimal.ZERO;
      BigDecimal originElectUseSum = BigDecimal.ZERO;
      for (SpStatementQuarterReport report : spStatementQuarterReports.stream()
          .filter(i -> !i.getDataType().equals(ShorePowerQuarterDataType.OTHER_BERTH.getDataType()))
          .collect(Collectors.toList())) {
        // 计算接电时间的合计值和其他泊位值
        originChargeTimeSum = reportSum.getChargeTimeQuarter()
            .add(report.getChargeTimeQuarter()).setScale(2, RoundingMode.HALF_UP);
        reportSum.setChargeTimeQuarter(originChargeTimeSum);
        // 计算使用岸电次数的合计值和其他泊位值
        originShipUsageSum = reportSum.getShipUsageQuarter()
            .add(report.getShipUsageQuarter()).setScale(2, RoundingMode.HALF_UP);
        reportSum.setShipUsageQuarter(originShipUsageSum);
        // 计算用电量的合计值和其他泊位值
        originElectUseSum = reportSum.getElectUseQuarter()
            .add(report.getElectUseQuarter()).setScale(2, RoundingMode.HALF_UP);
        reportSum.setElectUseQuarter(originElectUseSum);
      }
      BigDecimal otherChargeTimeSum = spShipUsageMonthQuarterVo.getShorePowerTimeMonth()
          .subtract(originChargeTimeSum);
      otherSumEntity.setChargeTimeQuarter(otherChargeTimeSum.setScale(2, RoundingMode.HALF_UP));
      reportSum.setChargeTimeQuarter(
          spShipUsageMonthQuarterVo.getShorePowerTimeMonth().setScale(2, RoundingMode.HALF_UP));
      BigDecimal otherShipUsageSum = spShipUsageMonthQuarterVo.getShipUsageMonth()
          .subtract(originShipUsageSum);
      otherSumEntity.setShipUsageQuarter(otherShipUsageSum.setScale(2, RoundingMode.HALF_UP));
      reportSum.setShipUsageQuarter(
          spShipUsageMonthQuarterVo.getShipUsageMonth().setScale(2, RoundingMode.HALF_UP));
      BigDecimal otherElectUseSum = spShipUsageMonthQuarterVo.getShorePowerCountMonth()
          .subtract(originElectUseSum);
      otherSumEntity.setElectUseQuarter(otherElectUseSum.setScale(2, RoundingMode.HALF_UP));
      reportSum.setElectUseQuarter(
          spShipUsageMonthQuarterVo.getShorePowerCountMonth().setScale(2, RoundingMode.HALF_UP));
      spStatementQuarterReports.add(reportSum);
      //获取往期数据
      List<SpStatementQuarterReport> pastData = this.list(
          Wrappers.<SpStatementQuarterReport>lambdaQuery()
              .eq(SpStatementQuarterReport::getYear, currentYear)
              .eq(SpStatementQuarterReport::getQuarter, currentQuarter - 1));
      if (CollectionUtils.isEmpty(pastData)) {
        for (SpStatementQuarterReport spStatementQuarterReport : spStatementQuarterReports) {
          spStatementQuarterReport.setShipUsageYear(spStatementQuarterReport.getShipUsageQuarter());
          spStatementQuarterReport.setChargeTimeYear(
              spStatementQuarterReport.getChargeTimeQuarter());
          spStatementQuarterReport.setElectUseYear(spStatementQuarterReport.getElectUseQuarter());
        }
      } else {
        //分类
        Map<Integer, List<SpStatementQuarterReport>> pastList = pastData.stream()
            .collect(Collectors.groupingBy(SpStatementQuarterReport::getDataType));
        for (SpStatementQuarterReport spStatementQuarterReport : spStatementQuarterReports) {
          spStatementQuarterReport.setElectUseYear(spStatementQuarterReport.getElectUseQuarter()
              .add(pastList.get(spStatementQuarterReport.getDataType()).get(0).getElectUseYear()));
          spStatementQuarterReport.setShipUsageYear(spStatementQuarterReport.getShipUsageQuarter()
              .add(pastList.get(spStatementQuarterReport.getDataType()).get(0).getShipUsageYear()));
          spStatementQuarterReport.setChargeTimeYear(spStatementQuarterReport.getChargeTimeQuarter()
              .add(
                  pastList.get(spStatementQuarterReport.getDataType()).get(0).getChargeTimeYear()));
        }
      }
      BigDecimal shipUsageYear = reportSum.getShipUsageYear();
      BigDecimal chargeTimeYear = reportSum.getChargeTimeYear();
      BigDecimal electUseYear = reportSum.getElectUseYear();
      reportSum.setShipUsageYear(spShipUsageMonthQuarterVo.getShipUsageYear());
      reportSum.setChargeTimeYear(spShipUsageMonthQuarterVo.getShorePowerTimeYear());
      reportSum.setElectUseYear(spShipUsageMonthQuarterVo.getShorePowerCountYear());
      otherSumEntity.setShipUsageYear(reportSum.getShipUsageYear().subtract(shipUsageYear)
          .add(otherSumEntity.getShipUsageYear()));
      otherSumEntity.setChargeTimeYear(reportSum.getChargeTimeYear().subtract(chargeTimeYear)
          .add(otherSumEntity.getChargeTimeYear()));
      otherSumEntity.setElectUseYear(
          reportSum.getElectUseYear().subtract(electUseYear).add(otherSumEntity.getElectUseYear()));
//      //分类
//      Map<Integer, List<SpStatementQuarterReport>> pastList = pastData.stream()
//          .collect(Collectors.groupingBy(SpStatementQuarterReport::getDataType));
//      //年度合计
//      BigDecimal originShipUseYear = BigDecimal.ZERO;
//      BigDecimal originChargeTimeYearSum = BigDecimal.ZERO;
//      BigDecimal originElectUseYearSum = BigDecimal.ZERO;
//      for (SpStatementQuarterReport spStatementQuarterReport : spStatementQuarterReports.stream()
//          .filter(i -> !i.getDataType().equals(ShorePowerQuarterDataType.OTHER_BERTH.getDataType()))
//          .collect(Collectors.toList())) {
//        SpStatementQuarterReport reportPast;
//        if (pastList.get(spStatementQuarterReport.getDataType()) != null) {
//          reportPast = pastList.get(spStatementQuarterReport.getDataType()).get(0);
//        } else {
//          reportPast = new SpStatementQuarterReport();
//        }
//        // 计算使用岸电船舶艘次的其他泊位和合计值
//        originShipUseYear = reportPast.getShipUsageYear()
//            .add(spStatementQuarterReport.getShipUsageQuarter())
//            .setScale(2, RoundingMode.HALF_UP);
//        // 计算使用岸电时间其他泊位和合计值
//        originChargeTimeYearSum = reportPast.getChargeTimeYear()
//            .add(spStatementQuarterReport.getChargeTimeQuarter())
//            .setScale(2, RoundingMode.HALF_UP);
//        // 计算使用岸电电量其他泊位和合计值
//        originElectUseYearSum = reportPast.getElectUseYear()
//            .add(spStatementQuarterReport.getElectUseQuarter())
//            .setScale(2, RoundingMode.HALF_UP);
//      }
//      BigDecimal otherShipUseYearSum = spShipUsageMonthQuarterVo.getShipUsageYear()
//          .subtract(originShipUseYear);
//      otherSumEntity.setShipUsageYear(otherShipUseYearSum.setScale(2, RoundingMode.HALF_UP));
//      reportSum.setShipUsageYear(
//          spShipUsageMonthQuarterVo.getShipUsageYear().setScale(2, RoundingMode.HALF_UP));
//      BigDecimal otherOriginChargeTimeYearSum = spShipUsageMonthQuarterVo.getShorePowerTimeYear()
//          .subtract(originChargeTimeYearSum);
//      otherSumEntity.setChargeTimeYear(
//          otherOriginChargeTimeYearSum.setScale(2, RoundingMode.HALF_UP));
//      reportSum.setChargeTimeYear(
//          spShipUsageMonthQuarterVo.getShorePowerTimeYear().setScale(2, RoundingMode.HALF_UP));
//      BigDecimal otherElectUseYearSum = spShipUsageMonthQuarterVo.getShorePowerCountYear()
//          .subtract(originElectUseYearSum);
//      otherSumEntity.setElectUseYear(otherElectUseYearSum.setScale(2, RoundingMode.HALF_UP));
//      reportSum.setElectUseYear(
//          spShipUsageMonthQuarterVo.getShorePowerCountYear());
      this.saveBatch(spStatementQuarterReports);
      log.info("季度报表生成结束  当前季度为 :{}",currentQuarter);
    } catch (Exception e) {
      PrintLogUtils.printErrorLogInfo(e);
      log.info("季度报表生成异常  异常内容{}" + e.getMessage());
    }
  }

  private void saveCurrentMonthContainerDataToDB(
      List<SpStatementQuarterReport> spStatementQuarterReports,
      String currentDate) {
    SpStatementQuarterReport currentMonthContainerData = spStatementQuarterReports.stream()
        .filter(
            x -> ShorePowerQuarterDataType.CONTAINER_BERTH.getDataType().equals(x.getDataType()))
        .collect(
            Collectors.toList()).get(0);
    int thisMonth = Integer.parseInt(currentDate.split("-")[1]);
    int thisYear = Integer.parseInt(currentDate.split("-")[0]);
    // 先删后插 避免数据重复
    containerMapper.delete(Wrappers.<SpStatementContainer>lambdaQuery()
        .eq(SpStatementContainer::getReportDate,
            thisYear + "_" + (thisMonth < 10 ? "0" + thisMonth : thisMonth)));
    SpStatementContainer sameTimeLastYear = containerMapper.selectOne(
        Wrappers.<SpStatementContainer>lambdaQuery()
            .eq(SpStatementContainer::getReportDate,
                thisYear - 1 + "-" + (thisMonth < 10 ? "0" + thisMonth : thisMonth)));
    SpStatementContainer containerSingleData = new SpStatementContainer();
    containerSingleData.setId(snowflakeConfig.snowflakeStringId());
    containerSingleData.setReportDate(currentDate);
    containerSingleData.setShipUsage(currentMonthContainerData.getShipUsageQuarter());
    containerSingleData.setChargeTime(currentMonthContainerData.getChargeTimeQuarter());
    containerSingleData.setElectUsage(currentMonthContainerData.getElectUseQuarter());
    if (!Objects.isNull(sameTimeLastYear)) {
      containerSingleData.setShipUsageTon(
          containerSingleData.getShipUsage().subtract(sameTimeLastYear.getShipUsage())
              .multiply(BigDecimal.valueOf(100))
              .divide(sameTimeLastYear.getShipUsage(), 2, RoundingMode.HALF_UP));
      containerSingleData.setElectUsageTon(
          containerSingleData.getElectUsage().subtract(sameTimeLastYear.getElectUsage())
              .multiply(BigDecimal.valueOf(100))
              .divide(sameTimeLastYear.getElectUsage(), 2, RoundingMode.HALF_UP));
    }
    containerMapper.insert(containerSingleData);
  }

  private void calculateQuarterReportDetailBlMonth(
      SpStatementQuarterReport spStatementQuarterReport,
      List<SpQuarterReportBlVo> spQuarterReportBlVos) {
    if (spQuarterReportBlVos == null || spQuarterReportBlVos.isEmpty()) {
      return;
    }
    for (SpQuarterReportBlVo blVo : spQuarterReportBlVos) {
      spStatementQuarterReport.setShipUsageQuarter(
          spStatementQuarterReport.getShipUsageQuarter().add(blVo.getUseCount())
              .setScale(2, RoundingMode.HALF_UP));
      spStatementQuarterReport.setChargeTimeQuarter(
          spStatementQuarterReport.getChargeTimeQuarter().add(blVo.getElect())
              .setScale(2, RoundingMode.HALF_UP));
      spStatementQuarterReport.setElectUseQuarter(
          spStatementQuarterReport.getElectUseQuarter().add(blVo.getUseElectricity())
              .setScale(2, RoundingMode.HALF_UP));
    }
  }

  private void calculateQuarterReportDetailOriginMonth(
      SpStatementQuarterReport spStatementQuarterReport, List<SpQuarterReportOriginVo> value) {
    if (value == null || value.isEmpty()) {
      return;
    }
    spStatementQuarterReport.setShipUsageQuarter(
        BigDecimal.valueOf(value.size()).setScale(2, RoundingMode.HALF_UP));
    for (SpQuarterReportOriginVo SpQuarterReportOriginVo : value) {
      spStatementQuarterReport.setChargeTimeQuarter(spStatementQuarterReport.getChargeTimeQuarter()
          .add(BigDecimal.valueOf(SpQuarterReportOriginVo.getEndTime().getTime()
                  - SpQuarterReportOriginVo.getStartTime().getTime())
              .divide(BigDecimal.valueOf(60 * 60 * 1000), 2, RoundingMode.HALF_UP))
          .setScale(2, RoundingMode.UP));
      spStatementQuarterReport.setElectUseQuarter(
          spStatementQuarterReport.getElectUseQuarter().add(SpQuarterReportOriginVo.getElect())
              .setScale(2, RoundingMode.HALF_UP));
    }
  }
}




