package com.shcd.service.impl;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.alibaba.fastjson.JSONObject;
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.entity.SpStatementSg;
import com.shcd.entity.SpStatementShipUsageMonth;
import com.shcd.entity.SpStatementYearAnalysis;
import com.shcd.enums.ShorePowerQuarterDataType;
import com.shcd.mapper.SpStatementContainerMapper;
import com.shcd.mapper.SpStatementShipUsageMonthMapper;
import com.shcd.service.SpStatementSgService;
import com.shcd.service.SpStatementShipUsageMonthService;
import com.shcd.service.SpStatementYearAnalysisService;
import com.shcd.utils.PrintLogUtils;
import com.shcd.vo.ShorePowerDataVo;
import com.shcd.vo.SpQuarterReportBlVo;
import com.shcd.vo.SpQuarterReportOriginVo;
import com.shcd.vo.SpStatementShipUsageDetail;
import com.shcd.vo.SpStatementShipUsageDetailV2;
import com.shcd.vo.SpStatementShipUsageMonthVo;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author csdl
 * @description 针对表【SP_STATEMENT_SHIP_USAGE_MONTH(上海市长江经济带船舶使用岸电情况报表)】的数据库操作Service实现
 * @createDate 2024-01-22 14:11:42
 */
@Service
@Slf4j
public class SpStatementShipUsageMonthServiceImpl extends
    ServiceImpl<SpStatementShipUsageMonthMapper, SpStatementShipUsageMonth>
    implements SpStatementShipUsageMonthService {

  @Resource
  private SpStatementShipUsageMonthMapper spStatementShipUsageMonthMapper;
  @Autowired
  private SpStatementYearAnalysisService spStatementYearAnalysisService;

  @Autowired
  private SpStatementSgService spStatementSgService;

  @Resource
  private SpStatementContainerMapper containerMapper;

  @Autowired
  private SnowflakeConfig snowflakeConfig;

  @Override
  public void makeShangHaiShorePowerUsageRecord(String currentDate) {
    int thisMonth;
    int thisYear;
    if (!StringUtils.isBlank(currentDate)) {
      thisMonth = Integer.parseInt(currentDate.split("-")[1]);
      thisYear = Integer.parseInt(currentDate.split("-")[0]);
    } else {
      Calendar instance = Calendar.getInstance();
      thisMonth = instance.get(Calendar.MONTH) + 1;
      thisYear = instance.get(Calendar.YEAR);
    }
    int lastMonth;
    int lastYear;
    int lastCycleMonth;
    int lastCycleYear;
    if (thisMonth == 1) {
      lastMonth = 12;
      lastYear = thisYear - 1;
      lastCycleYear = thisYear - 1;
      lastCycleMonth = 11;
    } else if (thisMonth == 2) {
      lastMonth = thisMonth - 1;
      lastYear = thisYear;
      lastCycleYear = thisYear - 1;
      lastCycleMonth = 12;
    } else {
      lastMonth = thisMonth - 1;
      lastYear = thisYear;
      lastCycleYear = thisYear;
      lastCycleMonth = thisMonth - 2;
    }
    try {
      SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
      Date thisDate = DateUtils.stringToDate(thisYear + "-" + thisMonth + "-25", "yyyy-MM-dd");
      Date lastDate = DateUtils.stringToDate(lastYear + "-" + lastMonth + "-25", "yyyy-MM-dd");
      Date toDate = DateUtils.stringToDate(thisYear + "-" + thisMonth + "-01", "yyyy-MM-dd");
      Date toDateBegin = DateUtils.stringToDate(thisYear + "-01-01", "yyyy-MM-dd");
      Date yearCountBegin = DateUtils.stringToDate(thisYear - 1 + "-12-25", "yyyy-MM-dd");
      Date lastCycleStart = DateUtils.stringToDate(lastCycleYear + "-" + lastCycleMonth + "-25",
          "yyyy-MM-dd");
      Date lastCycleEnd = DateUtils.stringToDate(lastYear + "-" + lastMonth + "-25", "yyyy-MM-dd");
      //删除
      String dateStr = thisYear + "-" + thisMonth + "-01";
      String dateStrLast;
      if (thisMonth == 1) {
        dateStrLast = dateStr;
      } else {
        dateStrLast = thisYear + "-" + (thisMonth - 1) + "-01";
      }
      spStatementShipUsageMonthMapper.deleteByDate(dateStr);
      //先算本次月度的
      CompletableFuture<List<SpStatementShipUsageDetail>> future1 = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.selectOriginDataMonth(thisDate, lastDate,
              lastCycleStart, lastCycleEnd));
      CompletableFuture<SpStatementShipUsageMonthVo> future2 = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.selectGhDataMonth(toDate));
      CompletableFuture<SpStatementShipUsageMonthVo> future5 = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.selectOriginSelfDataMonth(thisDate, lastDate));
      CompletableFuture.allOf(future1, future2, future5).join();
      List<SpStatementShipUsageDetail> spStatementShipUsageDetails = future1.get();
//      SpStatementShipUsageMonthVo originMonth = makeOriginMonthData(spStatementShipUsageDetails);

      SpStatementShipUsageMonthVo originMonth = new SpStatementShipUsageMonthVo();
      // 过滤低压标准化的数据
      List<SpStatementShipUsageDetail> nhdybzhList = spStatementShipUsageDetails.stream()
          .filter(x -> "低压(标准化)".equals(x.getConnectType())).collect(
              Collectors.toList());
      // 过滤非内河低压标准化数据
      List<SpStatementShipUsageDetail> otherList = spStatementShipUsageDetails.stream()
          .filter(x -> !"低压(标准化)".equals(x.getConnectType())).collect(
              Collectors.toList());
      SpStatementShipUsageMonthVo nhdybzhListMonth = makeOriginMonthDataV2d1(nhdybzhList);
      SpStatementShipUsageMonthVo otherListMonth = makeOriginMonthData(otherList);
      originMonth.setShipUsage(nhdybzhListMonth.getShipUsage().add(otherListMonth.getShipUsage()));
      originMonth.setShorePowerTime(
          nhdybzhListMonth.getShorePowerTime().add(otherListMonth.getShorePowerTime()));
      originMonth.setShorePowerCount(
          nhdybzhListMonth.getShorePowerCount().add(otherListMonth.getShorePowerCount()));

      SpStatementShipUsageMonthVo ghMonth = future2.get();
      SpStatementShipUsageMonthVo originSelfMonth = future5.get();
      //除本月之前合计的
      CompletableFuture<SpStatementShipUsageMonthVo> future3 = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.selectOriginDataYear(dateStrLast));
      CompletableFuture<SpStatementShipUsageMonthVo> future4 = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.selectGhDataYear(dateStrLast));
      //企业自维护的在中间表中没有额外计算即可
//      CompletableFuture<SpStatementShipUsageMonthVo> future6 = CompletableFuture.supplyAsync(
//          () -> spStatementShipUsageMonthMapper.selectOriginSelfDataYear(yearCountBegin, thisDate));
      CompletableFuture.allOf(future3, future4).join();
      SpStatementShipUsageMonthVo originYear = future3.get();
      SpStatementShipUsageMonthVo ghYear = future4.get();
      //SpStatementShipUsageMonthVo originSelfYear = future6.get();
      if (ghMonth == null) {
        ghMonth = new SpStatementShipUsageMonthVo();
      }
      if (originYear == null) {
        originYear = new SpStatementShipUsageMonthVo();
      }
      if (ghYear == null) {
        ghYear = new SpStatementShipUsageMonthVo();
      }
      if (originSelfMonth == null) {
        originSelfMonth = new SpStatementShipUsageMonthVo();
      }
//      if (originSelfYear == null) {
//        originSelfYear = new SpStatementShipUsageMonthVo();
//      }
      ArrayList<SpStatementShipUsageMonth> spStatementShipUsageMonths = new ArrayList<>();
      SpStatementShipUsageMonth origin = new SpStatementShipUsageMonth();
      origin.setId(snowflakeGenerator.next().toString());
      origin.setShipUsageMonth(originMonth.getShipUsage().add(originSelfMonth.getShipUsage())
          .setScale(2, RoundingMode.HALF_UP));
      origin.setShipUsageYear(originYear.getShipUsage().add(originMonth.getShipUsage())
          .add(originSelfMonth.getShipUsage())
          .setScale(2, RoundingMode.HALF_UP));
      origin.setShorePowerTimeMonth(
          originMonth.getShorePowerTime().add(originSelfMonth.getShorePowerTime())
              .setScale(2, RoundingMode.HALF_UP));
      origin.setShorePowerTimeYear(
          originYear.getShorePowerTime().add(originMonth.getShorePowerTime())
              .add(originSelfMonth.getShorePowerTime())
              .setScale(2, RoundingMode.HALF_UP));
      origin.setShorePowerCountMonth(
          originMonth.getShorePowerCount().add(originSelfMonth.getShorePowerCount())
              .setScale(2, RoundingMode.HALF_UP));
      origin.setShorePowerCountYear(
          originYear.getShorePowerCount().add(originMonth.getShorePowerCount())
              .add(originSelfMonth.getShorePowerCount())
              .setScale(2, RoundingMode.HALF_UP));
      origin.setReportDate(DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      origin.setDataSource(0);
      spStatementShipUsageMonths.add(origin);
      SpStatementShipUsageMonth gh = new SpStatementShipUsageMonth();
      Thread.sleep(1);
      gh.setId(snowflakeGenerator.next().toString());
      gh.setShipUsageMonth(ghMonth.getShipUsage().setScale(2, RoundingMode.HALF_UP));
      gh.setShipUsageYear(
          ghYear.getShipUsage().add(ghMonth.getShipUsage()).setScale(2, RoundingMode.HALF_UP));
      gh.setShorePowerTimeMonth(ghMonth.getShorePowerTime().setScale(2, RoundingMode.HALF_UP));
      gh.setShorePowerTimeYear(ghYear.getShorePowerTime().add(ghMonth.getShorePowerTime())
          .setScale(2, RoundingMode.HALF_UP));
      gh.setShorePowerCountMonth(ghMonth.getShorePowerCount().setScale(2, RoundingMode.HALF_UP));
      gh.setShorePowerCountYear(ghYear.getShorePowerCount().add(ghMonth.getShorePowerCount())
          .setScale(2, RoundingMode.HALF_UP));
      gh.setReportDate(DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      gh.setDataSource(10);
      spStatementShipUsageMonths.add(gh);
      //新增线下数据
      //月度的
      SpStatementShipUsageMonthVo underLineMonth = spStatementShipUsageMonthMapper.getUnderLineMonthData(
          toDate);
      //之前线下年度
      SpStatementShipUsageMonthVo underLineYear = spStatementShipUsageMonthMapper.getUnderLineYearData(
          toDateBegin, toDate);
      SpStatementShipUsageMonth underLine = new SpStatementShipUsageMonth();
      if (underLineMonth != null || underLineYear != null) {
        if (underLineMonth == null) {
          underLineMonth = new SpStatementShipUsageMonthVo();
        }
        if (underLineYear == null) {
          underLineYear = new SpStatementShipUsageMonthVo();
        }
        underLine.setId(snowflakeGenerator.next().toString());
        underLine.setReportDate(toDate);
        underLine.setDataSource(20);
        underLine.setShipUsageYear(underLineYear.getShipUsage().setScale(2, RoundingMode.HALF_UP));
        underLine.setShipUsageMonth(
            underLineMonth.getShipUsage().setScale(2, RoundingMode.HALF_UP));
        underLine.setShorePowerCountMonth(
            underLineMonth.getShorePowerCount().setScale(2, RoundingMode.HALF_UP));
        underLine.setShorePowerCountYear(
            underLineYear.getShorePowerCount().setScale(2, RoundingMode.HALF_UP));
        underLine.setShorePowerTimeMonth(
            underLineMonth.getShorePowerTime().setScale(2, RoundingMode.HALF_UP));
        underLine.setShorePowerTimeYear(
            underLineYear.getShorePowerTime().setScale(2, RoundingMode.HALF_UP));
        spStatementShipUsageMonths.add(underLine);
      } else {
        underLine.setShipUsageYear(BigDecimal.ZERO);
        underLine.setShipUsageMonth(BigDecimal.ZERO);
        underLine.setShorePowerCountMonth(BigDecimal.ZERO);
        underLine.setShorePowerCountYear(BigDecimal.ZERO);
        underLine.setShorePowerTimeMonth(BigDecimal.ZERO);
        underLine.setShorePowerTimeYear(BigDecimal.ZERO);
      }
      SpStatementShipUsageMonth sum = new SpStatementShipUsageMonth();
      Thread.sleep(1);
      sum.setId(snowflakeGenerator.next().toString());
      sum.setReportDate(DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      sum.setDataSource(99);
      sum.setShipUsageMonth(
          origin.getShipUsageMonth().add(gh.getShipUsageMonth()).add(underLine.getShipUsageMonth())
              .setScale(2, RoundingMode.HALF_UP));
      sum.setShipUsageYear(
          origin.getShipUsageYear().add(gh.getShipUsageYear()).add(underLine.getShipUsageYear())
              .setScale(2, RoundingMode.HALF_UP));
      sum.setShorePowerCountMonth(origin.getShorePowerCountMonth().add(gh.getShorePowerCountMonth())
          .add(underLine.getShorePowerCountMonth()).setScale(2, RoundingMode.HALF_UP));
      sum.setShorePowerCountYear(origin.getShorePowerCountYear().add(gh.getShorePowerCountYear())
          .add(underLine.getShorePowerCountYear()).setScale(2, RoundingMode.HALF_UP));
      sum.setShorePowerTimeMonth(origin.getShorePowerTimeMonth().add(gh.getShorePowerTimeMonth())
          .add(underLine.getShorePowerTimeMonth()).setScale(2, RoundingMode.HALF_UP));
      sum.setShorePowerTimeYear(origin.getShorePowerTimeYear().add(gh.getShorePowerTimeYear())
          .add(underLine.getShorePowerTimeYear()).setScale(2, RoundingMode.HALF_UP));
      spStatementShipUsageMonths.add(sum);
      //zoro备注：要多算一部分数据，外电1，2，3
      List<SpStatementShipUsageDetail> sbWgq = spStatementShipUsageMonthMapper.selectWgqMonth(
          thisDate, lastDate, lastCycleStart, lastCycleEnd);
      SpStatementShipUsageMonthVo wgqMonth = makeOriginMonthData(sbWgq);
      SpStatementShipUsageMonthVo wgqYear = spStatementShipUsageMonthMapper.selectWgqYear(
          dateStrLast);
      if (wgqYear == null) {
        wgqYear = new SpStatementShipUsageMonthVo();
      }
      SpStatementShipUsageMonth wgq = new SpStatementShipUsageMonth();
      wgq.setId(snowflakeGenerator.next().toString());
      wgq.setDataSource(100);
      wgq.setReportDate(DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      wgq.setShorePowerCountMonth(wgqMonth.getShorePowerCount());
      wgq.setShipUsageMonth(wgqMonth.getShipUsage());
      wgq.setShorePowerTimeMonth(wgqMonth.getShorePowerTime());
      wgq.setShipUsageYear(
          wgqYear.getShipUsage().add(wgqMonth.getShipUsage()).setScale(2, RoundingMode.HALF_UP));
      wgq.setShorePowerTimeYear(wgqYear.getShorePowerTime().add(wgqMonth.getShorePowerTime())
          .setScale(2, RoundingMode.HALF_UP));
      wgq.setShorePowerCountYear(wgqYear.getShorePowerCount().add(wgqMonth.getShorePowerCount())
          .setScale(2, RoundingMode.HALF_UP));
      spStatementShipUsageMonths.add(wgq);
      //有一个合计的用于上海市岸电使用指标完成情况统计表，对应的dataSource为101
      //获取对应月的数据
//      List<SpStatementShipUsageDetail> nwSum = spStatementShipUsageMonthMapper.selectNhAndWgMonth(thisDate,lastDate,lastCycleStart,lastCycleEnd);
//      SpStatementShipUsageMonthVo realSum = makeOriginMonthData(nwSum);.
      //设计低压标准化,方法修改第二版本,上面是第一版本
      List<SpStatementShipUsageDetailV2> nwSum = spStatementShipUsageMonthMapper.selectNhAndWgMonthV2(
          thisDate, lastDate);
      SpStatementShipUsageMonthVo realSum = makeOriginMonthDataV2(nwSum);
      //获取该月前一个月的合计数据
      SpStatementShipUsageMonthVo nwSumLastSum = spStatementShipUsageMonthMapper.selectNhAndWgLastMonthSum(
          DateUtils.stringToDate(dateStrLast, "yyyy-MM"));
      SpStatementShipUsageMonth nwSumSave = new SpStatementShipUsageMonth();
      if (nwSumLastSum == null) {
        nwSumLastSum = new SpStatementShipUsageMonthVo();
      }
      nwSumSave.setId(snowflakeGenerator.next().toString());
      nwSumSave.setReportDate(DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      nwSumSave.setDataSource(101);
      nwSumSave.setShipUsageMonth(realSum.getShipUsage());
      nwSumSave.setShorePowerCountMonth(realSum.getShorePowerCount());
      nwSumSave.setShorePowerTimeMonth(realSum.getShorePowerTime());
      nwSumSave.setShipUsageYear(nwSumLastSum.getShipUsage().add(realSum.getShipUsage())
          .setScale(2, RoundingMode.HALF_UP));
      nwSumSave.setShorePowerTimeYear(
          nwSumLastSum.getShorePowerTime().add(realSum.getShorePowerTime())
              .setScale(2, RoundingMode.HALF_UP));
      nwSumSave.setShorePowerCountYear(
          nwSumLastSum.getShorePowerCount().add(realSum.getShorePowerCount())
              .setScale(2, RoundingMode.HALF_UP));
      spStatementShipUsageMonths.add(nwSumSave);
      //获取对应年的合计
      //计算内河平台数据
      //先获取本月的数据
//      CompletableFuture<List<SpStatementShipUsageDetail>> futureInRiver = CompletableFuture.supplyAsync(
//          () -> spStatementShipUsageMonthMapper.getInRiverDataThisMonth(thisDate, lastDate,lastCycleStart,lastCycleEnd));
      //低压标准化修改第二版本,上面为第一版本
      CompletableFuture<List<SpStatementShipUsageDetailV2>> futureInRiver = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.getInRiverDataThisMonthV2(thisDate, lastDate));
      CompletableFuture<SpStatementShipUsageMonthVo> futureInRiverSelf = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.getInRiverDataSelfThisMonth(thisDate, lastDate));
      //获取本月之前合计数据
      CompletableFuture<SpStatementShipUsageMonthVo> futureInRiverLast = CompletableFuture.supplyAsync(
          () -> spStatementShipUsageMonthMapper.getInRiverDataBeforeThisMonth(dateStrLast));
      CompletableFuture.allOf(futureInRiver, futureInRiverSelf, futureInRiverLast).join();
      List<SpStatementShipUsageDetailV2> inRiverDetail = futureInRiver.get();
      SpStatementShipUsageMonthVo inRiverSelfMonth = futureInRiverSelf.get();
      SpStatementShipUsageMonthVo inRiverLast = futureInRiverLast.get();
//      SpStatementShipUsageMonthVo inRiver = makeOriginMonthData(inRiverDetail);
      SpStatementShipUsageMonthVo inRiver = makeOriginMonthDataV2(inRiverDetail);
      if (inRiverLast == null) {
        inRiverLast = new SpStatementShipUsageMonthVo();
      }
      if (inRiverSelfMonth == null) {
        inRiverSelfMonth = new SpStatementShipUsageMonthVo();
      }
      SpStatementShipUsageMonth inRiverShipUsage = new SpStatementShipUsageMonth();
      inRiverShipUsage.setId(snowflakeGenerator.next().toString());
      inRiverShipUsage.setReportDate(DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      inRiverShipUsage.setDataSource(1);
      inRiverShipUsage.setShipUsageMonth(inRiver.getShipUsage().add(inRiverSelfMonth.getShipUsage())
          .setScale(2, RoundingMode.HALF_UP));
      inRiverShipUsage.setShorePowerTimeMonth(
          inRiver.getShorePowerTime().add(inRiverSelfMonth.getShorePowerTime())
              .setScale(2, RoundingMode.HALF_UP));
      inRiverShipUsage.setShorePowerCountMonth(
          inRiver.getShorePowerCount().add(inRiverSelfMonth.getShorePowerCount())
              .setScale(2, RoundingMode.HALF_UP));
      inRiverShipUsage.setShipUsageYear(
          inRiverShipUsage.getShipUsageMonth().add(inRiverLast.getShipUsage())
              .setScale(2, RoundingMode.HALF_UP));
      inRiverShipUsage.setShorePowerCountYear(
          inRiverShipUsage.getShorePowerCountMonth().add(inRiverLast.getShorePowerCount())
              .setScale(2, RoundingMode.HALF_UP));
      inRiverShipUsage.setShorePowerTimeYear(
          inRiverShipUsage.getShorePowerTimeMonth().add(inRiverLast.getShorePowerTime())
              .setScale(2, RoundingMode.HALF_UP));
      spStatementShipUsageMonths.add(inRiverShipUsage);
      //计算外港平台数据
      SpStatementShipUsageMonth outRiverShipUsage = new SpStatementShipUsageMonth();
      outRiverShipUsage.setId(snowflakeGenerator.next().toString());
      outRiverShipUsage.setReportDate(
          DateUtils.stringToDate(thisYear + "-" + thisMonth, "yyyy-MM"));
      outRiverShipUsage.setDataSource(2);
      outRiverShipUsage.setShipUsageMonth(
          origin.getShipUsageMonth().subtract(inRiverShipUsage.getShipUsageMonth()));
      outRiverShipUsage.setShorePowerCountMonth(
          origin.getShorePowerCountMonth().subtract(inRiverShipUsage.getShorePowerCountMonth()));
      outRiverShipUsage.setShorePowerTimeMonth(
          origin.getShorePowerTimeMonth().subtract(inRiverShipUsage.getShorePowerTimeMonth()));
      outRiverShipUsage.setShipUsageYear(
          origin.getShipUsageYear().subtract(inRiverShipUsage.getShipUsageYear()));
      outRiverShipUsage.setShorePowerCountYear(
          origin.getShorePowerCountYear().subtract(inRiverShipUsage.getShorePowerCountYear()));
      outRiverShipUsage.setShorePowerTimeYear(
          origin.getShorePowerTimeYear().subtract(inRiverShipUsage.getShorePowerTimeYear()));
      spStatementShipUsageMonths.add(outRiverShipUsage);
      // 计算portId = 1146的每个月数据存储到长江经济带中间表 dataSource102
      ShorePowerDataVo portIdEq1146;
      SpStatementShipUsageMonth portIdEq1146Data = new SpStatementShipUsageMonth();
      String portIdEq1146ReportDate = currentDate + "-01";
      String portIdEq1146Year = portIdEq1146ReportDate.split("-")[0];
      String portIdEq1146LastYear = String.valueOf(Integer.parseInt(portIdEq1146Year) - 1);
      String portIdEq1146CurrentStart;
      String portIdEq1146CurrentEnd = "";
      String portIdEq1146LastStart = "";
      String portIdEq1146LastEnd = "";
      if (portIdEq1146ReportDate.split("-")[1].equals("01")) {
        portIdEq1146CurrentStart = portIdEq1146LastYear + "-12-25 00:00:00";
        portIdEq1146CurrentEnd = portIdEq1146Year + "-01-24 23:59:59";
        portIdEq1146LastStart = portIdEq1146LastYear + "-11-25 00:00:00";
        portIdEq1146LastEnd = portIdEq1146LastYear + "-12-24 23:59:59";
      } else if (portIdEq1146ReportDate.split("-")[1].equals("02")) {
        portIdEq1146CurrentStart = portIdEq1146Year + "-01-25 00:00:00";
        portIdEq1146CurrentEnd = portIdEq1146Year + "-02-24 23:59:59";
        portIdEq1146LastStart = portIdEq1146LastYear + "-12-25 00:00:00";
        portIdEq1146LastEnd = portIdEq1146Year + "-01-24 23:59:59";
      } else {
        Integer portIdEq1146lastMonth = Integer.parseInt(portIdEq1146ReportDate.split("-")[1]) - 1;
        Integer last2Month = Integer.parseInt(portIdEq1146ReportDate.split("-")[1]) - 2;
        portIdEq1146CurrentStart = portIdEq1146Year + "-" + portIdEq1146lastMonth + "-25 00:00:00";
        portIdEq1146CurrentEnd =
            portIdEq1146Year + "-" + Integer.parseInt(portIdEq1146ReportDate.split("-")[1])
                + "-24 23:59:59";
        portIdEq1146LastStart = portIdEq1146Year + "-" + last2Month + "-25 00:00:00";
        portIdEq1146LastEnd = portIdEq1146Year + "-" + portIdEq1146lastMonth + "-24 23:59:59";
      }
      log.info("========>当前月份{} 查询参数为 :{},{},{},{}", portIdEq1146ReportDate,
          portIdEq1146CurrentStart, portIdEq1146CurrentEnd, portIdEq1146LastStart,
          portIdEq1146LastEnd);
      List<ShorePowerDataVo> dataVos = spStatementShipUsageMonthMapper.selectPortIdEq1146(
          portIdEq1146CurrentStart, portIdEq1146CurrentEnd, portIdEq1146LastStart,
          portIdEq1146LastEnd);
      if (CollectionUtils.isEmpty(dataVos)) {
        portIdEq1146 = new ShorePowerDataVo();
      } else {
        portIdEq1146 = dataVos.get(0);
      }
      log.info("当月{},添加的portId等于1146的数据为:{}", portIdEq1146ReportDate,
          JSONObject.toJSONString(portIdEq1146));
      portIdEq1146Data.setId(snowflakeGenerator.next().toString());
      portIdEq1146Data.setReportDate(
          DateUtils.stringToDate(portIdEq1146ReportDate, DateUtils.DATE_FORMAT_10));
      portIdEq1146Data.setDataSource(102);
      portIdEq1146Data.setShipUsageMonth(portIdEq1146.getBoatUse());
      portIdEq1146Data.setShorePowerTimeMonth(portIdEq1146.getElectTime());
      portIdEq1146Data.setShorePowerCountMonth(portIdEq1146.getElectCount());
      log.info("当月{},长江经济带存储的portId等于1146的数据为:{}", portIdEq1146ReportDate,
          JSONObject.toJSONString(portIdEq1146Data));
      spStatementShipUsageMonths.add(portIdEq1146Data);
      this.saveBatch(spStatementShipUsageMonths);
      //岸电年比
      ArrayList<SpStatementYearAnalysis> save = new ArrayList<>();
      //删除合计
      spStatementYearAnalysisService.remove(Wrappers.<SpStatementYearAnalysis>lambdaQuery()
          .eq(SpStatementYearAnalysis::getYear, thisYear)
          .eq(SpStatementYearAnalysis::getMonth, 13));
      spStatementYearAnalysisService.remove(Wrappers.<SpStatementYearAnalysis>lambdaQuery()
          .eq(SpStatementYearAnalysis::getYear, thisYear)
          .eq(SpStatementYearAnalysis::getMonth, thisMonth));
      //如果多次生成保证数据唯一性，不能使用过去的数据了
      List<SpStatementYearAnalysis> totalList = spStatementYearAnalysisService.list(
          Wrappers.<SpStatementYearAnalysis>lambdaQuery()
              .eq(SpStatementYearAnalysis::getYear, thisYear)
              .lt(SpStatementYearAnalysis::getMonth, thisMonth));
      List<SpStatementYearAnalysis> thisYearShipUsage = totalList.stream()
          .filter(i -> i.getDataType() == 0).collect(Collectors.toList());
      List<SpStatementYearAnalysis> thisYearShipUsageTime = totalList.stream()
          .filter(i -> i.getDataType() == 1).collect(Collectors.toList());
      List<SpStatementYearAnalysis> thisYearShipUsageElect = totalList.stream()
          .filter(i -> i.getDataType() == 2).collect(Collectors.toList());
      //获取去年同期数据
      List<SpStatementYearAnalysis> beforeList = spStatementYearAnalysisService.list(
          Wrappers.<SpStatementYearAnalysis>lambdaQuery()
              .eq(SpStatementYearAnalysis::getYear, thisYear - 1)
              .eq(SpStatementYearAnalysis::getMonth, thisMonth));
      //获取往期合计值总计
      List<SpStatementYearAnalysis> list = spStatementYearAnalysisService.list(
          Wrappers.<SpStatementYearAnalysis>lambdaQuery()
              .eq(SpStatementYearAnalysis::getYear, thisYear - 1)
              .eq(SpStatementYearAnalysis::getMonth, 13));

      //用电次数
      SpStatementYearAnalysis spShipUsage = new SpStatementYearAnalysis();
      spShipUsage.setYear(Long.valueOf(thisYear));
      spShipUsage.setMonth(Long.valueOf(thisMonth));
      spShipUsage.setData(sum.getShipUsageMonth());
      spShipUsage.setDataType(0);
      spShipUsage.setId(snowflakeGenerator.next().toString());
      SpStatementYearAnalysis shipUsage = new SpStatementYearAnalysis();
      shipUsage.setData(BigDecimal.ZERO);
      //同期
      if (beforeList.stream().anyMatch(e -> e.getDataType() == 0)) {
        shipUsage = beforeList.stream().filter(e -> e.getDataType() == 0)
            .collect(Collectors.toList()).get(0);
      }
      spShipUsage.setCircleDiff(
          sum.getShipUsageMonth().subtract(shipUsage.getData()).setScale(2, RoundingMode.HALF_UP));
      spShipUsage.setCircleDiffRatio(shipUsage.getData().equals(BigDecimal.ZERO) ? null
          : spShipUsage.getCircleDiff().multiply(BigDecimal.valueOf(100))
              .divide(shipUsage.getData(), 2, RoundingMode.HALF_UP));
      save.add(spShipUsage);
      //用电次数合计
      if (thisMonth == 12) {
        SpStatementYearAnalysis spShipUsageTotal = new SpStatementYearAnalysis();
        spShipUsageTotal.setYear(Long.valueOf(thisYear));
        spShipUsageTotal.setMonth(13l);
        spShipUsageTotal.setId(snowflakeGenerator.next().toString());
        //同年合计
        SpStatementYearAnalysis shipUsageBefore = new SpStatementYearAnalysis();
        shipUsageBefore.setData(BigDecimal.ZERO);
        if (!thisYearShipUsage.isEmpty()) {
          thisYearShipUsage.forEach(
              i -> shipUsageBefore.setData(shipUsageBefore.getData().add(i.getData())));
        }
        spShipUsageTotal.setData(shipUsageBefore.getData().add(sum.getShipUsageMonth()));
        spShipUsageTotal.setDataType(0);
        //合计值互减
        spShipUsageTotal.setCircleDiff(spShipUsageTotal.getData().subtract(
            list.stream().filter(i -> i.getDataType() == 0).collect(Collectors.toList()).get(0)
                .getData()).setScale(2, RoundingMode.HALF_UP));
        spShipUsageTotal.setCircleDiffRatio(
            list.stream().filter(i -> i.getDataType() == 0).collect(Collectors.toList()).get(0)
                .getData().equals(BigDecimal.ZERO) ? null
                : spShipUsageTotal.getCircleDiff().multiply(BigDecimal.valueOf(100)).divide(
                    list.stream().filter(i -> i.getDataType() == 0).collect(Collectors.toList())
                        .get(0).getData(), 2, RoundingMode.HALF_UP));
        save.add(spShipUsageTotal);
      }
      //用电时间
      SpStatementYearAnalysis spShipUsageTime = new SpStatementYearAnalysis();
      spShipUsageTime.setYear(Long.valueOf(thisYear));
      spShipUsageTime.setMonth(Long.valueOf(thisMonth));
      spShipUsageTime.setId(snowflakeGenerator.next().toString());
      spShipUsageTime.setData(sum.getShorePowerTimeMonth());
      spShipUsageTime.setDataType(1);
      //同期值
      SpStatementYearAnalysis usageTime = new SpStatementYearAnalysis();
      usageTime.setData(BigDecimal.ZERO);
      if (beforeList.stream().anyMatch(i -> i.getDataType() == 1)) {
        usageTime = beforeList.stream().filter(i -> i.getDataType() == 1)
            .collect(Collectors.toList()).get(0);
      }
      spShipUsageTime.setCircleDiff(sum.getShorePowerTimeMonth().subtract(usageTime.getData())
          .setScale(2, RoundingMode.HALF_UP));
      spShipUsageTime.setCircleDiffRatio(usageTime.getData().equals(BigDecimal.ZERO) ? null
          : spShipUsageTime.getCircleDiff().multiply(BigDecimal.valueOf(100))
              .divide(usageTime.getData(), 2, RoundingMode.HALF_UP));
      save.add(spShipUsageTime);
      //用电时间合计
      //非12月不计算
      if (thisMonth == 12) {
        SpStatementYearAnalysis spShipUsageTimeTotal = new SpStatementYearAnalysis();
        spShipUsageTimeTotal.setYear(Long.valueOf(thisYear));
        spShipUsageTimeTotal.setMonth(13l);
        spShipUsageTimeTotal.setId(snowflakeGenerator.next().toString());
        SpStatementYearAnalysis spShipUsageTimeBefore = new SpStatementYearAnalysis();
        spShipUsageTimeBefore.setData(BigDecimal.ZERO);
        //同年合计
        if (!thisYearShipUsageTime.isEmpty()) {
          thisYearShipUsageTime.forEach(
              i -> spShipUsageTimeBefore.setData(spShipUsageTimeBefore.getData().add(i.getData())));
        }
        spShipUsageTimeTotal.setData(
            spShipUsageTimeBefore.getData().add(sum.getShorePowerTimeMonth()));
        spShipUsageTimeTotal.setDataType(1);
        spShipUsageTimeTotal.setCircleDiff(
            spShipUsageTimeTotal.getData().subtract(spShipUsageTimeBefore.getData())
                .setScale(2, RoundingMode.HALF_UP));
        spShipUsageTimeTotal.setCircleDiffRatio(
            list.stream().filter(i -> i.getDataType() == 1).collect(Collectors.toList()).get(0)
                .getData().equals(BigDecimal.ZERO) ? null
                : spShipUsageTimeTotal.getCircleDiff().multiply(BigDecimal.valueOf(100)).divide(
                    list.stream().filter(i -> i.getDataType() == 1).collect(Collectors.toList())
                        .get(0).getData(), 2, RoundingMode.HALF_UP));
        save.add(spShipUsageTimeTotal);
      }
      //用电量
      SpStatementYearAnalysis spShipUsageElec = new SpStatementYearAnalysis();
      spShipUsageElec.setYear(Long.valueOf(thisYear));
      spShipUsageElec.setMonth(Long.valueOf(thisMonth));
      spShipUsageElec.setId(snowflakeGenerator.next().toString());
      SpStatementYearAnalysis spShipUsageElecBefore = new SpStatementYearAnalysis();
      spShipUsageElecBefore.setData(BigDecimal.ZERO);
      //获取同期值
      if (beforeList.stream().anyMatch(e -> e.getDataType() == 2)) {
        spShipUsageElecBefore = beforeList.stream().filter(e -> e.getDataType() == 2)
            .collect(Collectors.toList()).get(0);
      }
      spShipUsageElec.setData(sum.getShorePowerCountMonth());
      spShipUsageElec.setDataType(2);
      spShipUsageElec.setCircleDiff(
          spShipUsageElec.getData().subtract(spShipUsageElecBefore.getData())
              .setScale(2, RoundingMode.HALF_UP));
      spShipUsageElec.setCircleDiffRatio(
          spShipUsageElecBefore.getData().equals(BigDecimal.ZERO) ? null
              : spShipUsageElec.getCircleDiff().multiply(BigDecimal.valueOf(100))
                  .divide(spShipUsageElecBefore.getData(), 2, RoundingMode.HALF_UP));
      save.add(spShipUsageElec);
      //用电时长合计
      //非12月不计算
      if (thisMonth == 12) {
        SpStatementYearAnalysis spShipUsageElecTimeTotal = new SpStatementYearAnalysis();
        spShipUsageElecTimeTotal.setYear(Long.valueOf(thisYear));
        spShipUsageElecTimeTotal.setMonth(13l);
        spShipUsageElecTimeTotal.setId(snowflakeGenerator.next().toString());
        //同年合计
        SpStatementYearAnalysis spShipUsageElecTimeTotalBefore = new SpStatementYearAnalysis();
        spShipUsageElecTimeTotalBefore.setData(BigDecimal.ZERO);
        if (!thisYearShipUsageElect.isEmpty()) {
          thisYearShipUsageElect.forEach(i -> spShipUsageElecTimeTotalBefore.setData(
              spShipUsageElecTimeTotalBefore.getData().add(i.getData())));
        }
        spShipUsageElecTimeTotal.setData(
            spShipUsageElecTimeTotalBefore.getData().add(sum.getShorePowerCountMonth()));
        spShipUsageElecTimeTotal.setDataType(2);
        spShipUsageElecTimeTotal.setCircleDiff(
            spShipUsageElecTimeTotal.getData().subtract(spShipUsageElecTimeTotalBefore.getData())
                .setScale(2, RoundingMode.HALF_UP));
        spShipUsageElecTimeTotal.setCircleDiffRatio(
            list.stream().filter(i -> i.getDataType() == 2).collect(Collectors.toList()).get(0)
                .getData().equals(BigDecimal.ZERO) ? null
                : spShipUsageElecTimeTotal.getCircleDiff().multiply(BigDecimal.valueOf(100)).divide(
                    list.stream().filter(i -> i.getDataType() == 2).collect(Collectors.toList())
                        .get(0).getData(), 2, RoundingMode.HALF_UP));
        save.add(spShipUsageElecTimeTotal);
      }
      spStatementYearAnalysisService.saveBatch(save);
      //岸电使用情况表中第二部分数据,每个月都要计算一个合计以及合计的同比
      //先删后插
      spStatementSgService.remove(Wrappers.<SpStatementSg>lambdaQuery()
          .eq(SpStatementSg::getReportDate,
              thisYear + "-" + (thisMonth < 10 ? "0" + thisMonth : thisMonth)));
      log.info("开始计算靠泊上港集团码头的集装箱船舶部分报表 ===========");
      try {
        executeSg(toDate, thisYear, thisMonth, snowflakeGenerator, lastMonth,lastDate, thisDate);
      } catch (Exception e) {
        PrintLogUtils.printErrorLogInfo(e);
        log.info("计算靠泊上港集团码头的集装箱船舶部分报表出错  ==========  原因:{}",
            e.getMessage());
      }
      // 计算靠泊集装箱码头船舶数据
      log.info("开始计算靠泊集装箱码头船舶数据部分报表 ===========");
      try {
        executeContainer(currentDate, lastDate, thisDate, lastCycleStart, lastCycleEnd, thisYear,
            thisMonth);
      } catch (Exception e) {
        PrintLogUtils.printErrorLogInfo(e);
        log.info("计算靠泊集装箱码头船舶数据部分报表出错  ==========  原因:{}",
            e.getMessage());
      }
    } catch (Exception e) {
      e.printStackTrace();
      log.error(e.getMessage());
    }
  }

  private void executeContainer(String currentDate, Date lastDate, Date thisDate,
      Date lastCycleStart,
      Date lastCycleEnd, int thisYear, int thisMonth) {
    List<SpQuarterReportOriginVo> spQuarterReportOriginVos = spStatementShipUsageMonthMapper.selectQuarterOrigin(
        lastDate, thisDate, lastCycleStart, lastCycleEnd);
    List<SpQuarterReportBlVo> spQuarterReportBlVos = spStatementShipUsageMonthMapper.selectQuarterBlBerth(
        lastDate, thisDate);
    List<SpQuarterReportOriginVo> container = spQuarterReportOriginVos.stream()
        .filter(i -> i.getBerthType().equals(1)).collect(Collectors.toList());
    List<SpQuarterReportBlVo> containerBl = spQuarterReportBlVos.stream()
        .filter(i -> i.getBerthType().equals(1)).collect(Collectors.toList());
    SpStatementQuarterReport spStatementQuarterReport = new SpStatementQuarterReport();
    spStatementQuarterReport.setDataType(ShorePowerQuarterDataType.CONTAINER_BERTH.getDataType());
    spStatementQuarterReport.setId(snowflakeConfig.snowflakeStringId());
    spStatementQuarterReport.setYear(thisYear);
    spStatementQuarterReport.setQuarter(
        (thisMonth % 3 == 0) ? (thisMonth / 3) : (thisMonth / 3 + 1));
    calculateQuarterReportDetailOriginMonth(spStatementQuarterReport, container);
    calculateQuarterReportDetailBlMonth(spStatementQuarterReport, containerBl);
    saveCurrentMonthContainerDataToDB(spStatementQuarterReport, currentDate);
  }

  private void executeSg(Date toDate, int thisYear, int thisMonth,
      SnowflakeGenerator snowflakeGenerator, int lastMonth,Date lastDate,Date thisDate) {
//    SpStatementShipUsageMonthVo underLineSg = spStatementShipUsageMonthMapper.getUnderLineMonthDataSg(
//        toDate);
    SpStatementShipUsageMonthVo underLineSg = spStatementShipUsageMonthMapper.getUnderLineMonthDataSgNew(lastDate,thisDate);
    if (underLineSg == null) {
      underLineSg = new SpStatementShipUsageMonthVo();
    }
    //查询去年同期数据
    SpStatementSg sameTimeLastYear = spStatementSgService.getOne(
        Wrappers.<SpStatementSg>lambdaQuery().eq(SpStatementSg::getReportDate,
            thisYear - 1 + "-" + (thisMonth < 10 ? "0" + thisMonth : thisMonth)));
    //获取去年最后一个月
    SpStatementSg last = spStatementSgService.getOne(
        Wrappers.<SpStatementSg>lambdaQuery()
            .eq(SpStatementSg::getReportDate, thisYear - 1 + "-12"));
    SpStatementSg spStatementSg = new SpStatementSg();
    spStatementSg.setId(snowflakeGenerator.next().toString());
    spStatementSg.setReportDate(DateUtils.dateToString(toDate, "yyyy-MM"));
    spStatementSg.setShipUsage(underLineSg.getShipUsage().setScale(2, RoundingMode.HALF_UP));
    spStatementSg.setChargeTime(
        underLineSg.getShorePowerTime().setScale(2, RoundingMode.HALF_UP));
    spStatementSg.setElectUsage(
        underLineSg.getShorePowerCount().setScale(2, RoundingMode.HALF_UP));
    spStatementSg.setShipUsageTon(
        underLineSg.getShipUsage().subtract(sameTimeLastYear.getShipUsage())
            .multiply(BigDecimal.valueOf(100))
            .divide(sameTimeLastYear.getShipUsage(), 2, RoundingMode.HALF_UP));
    spStatementSg.setElectUsageTon(
        underLineSg.getShorePowerCount().subtract(sameTimeLastYear.getElectUsage())
            .multiply(BigDecimal.valueOf(100))
            .divide(sameTimeLastYear.getElectUsage(), 2, RoundingMode.HALF_UP));
    //如果当前是第一月
    if (toDate.getMonth() + 1 == 1) {
      //不用查往期合计
//      spStatementSg.setSumChargeTime(
//          underLineSg.getShorePowerTime().setScale(2, RoundingMode.HALF_UP));
//      spStatementSg.setSumShipUsageNum(
//          underLineSg.getShipUsage().setScale(2, RoundingMode.HALF_UP));
//      spStatementSg.setSumElectUse(
//          underLineSg.getShorePowerCount().setScale(2, RoundingMode.HALF_UP));
//      spStatementSg.setSumShipUsageTon(
//          spStatementSg.getSumShipUsageNum().subtract(last.getSumShipUsageNum())
//              .multiply(BigDecimal.valueOf(100))
//              .divide(last.getSumShipUsageNum(), 2, RoundingMode.HALF_UP));
//      spStatementSg.setSumElectUseTon(
//          spStatementSg.getSumElectUse().subtract(last.getSumElectUse())
//              .multiply(BigDecimal.valueOf(100))
//              .divide(last.getSumElectUse(), 2, RoundingMode.HALF_UP));
    } else {
      //查询往期合计，计算本月合计
      SpStatementSg lastMonthSg = spStatementSgService.getOne(
          Wrappers.<SpStatementSg>lambdaQuery()
              .eq(SpStatementSg::getReportDate,
                  thisYear + "-" + (lastMonth < 10 ? "0" + lastMonth : lastMonth)));
//      spStatementSg.setSumChargeTime(
//          underLineSg.getShorePowerTime().add(lastMonthSg.getSumChargeTime())
//              .setScale(2, RoundingMode.HALF_UP));
//      spStatementSg.setSumShipUsageNum(
//          underLineSg.getShipUsage().add(lastMonthSg.getSumShipUsageNum())
//              .setScale(2, RoundingMode.HALF_UP));
//      spStatementSg.setSumElectUse(
//          underLineSg.getShorePowerCount().add(lastMonthSg.getSumElectUse())
//              .setScale(2, RoundingMode.HALF_UP));
//      spStatementSg.setSumShipUsageTon(
//          spStatementSg.getSumShipUsageNum().subtract(last.getSumShipUsageNum())
//              .multiply(BigDecimal.valueOf(100))
//              .divide(last.getSumShipUsageNum(), 2, RoundingMode.HALF_UP));
//      spStatementSg.setSumElectUseTon(
//          spStatementSg.getSumElectUse().subtract(last.getSumElectUse())
//              .multiply(BigDecimal.valueOf(100))
//              .divide(last.getSumElectUse(), 2, RoundingMode.HALF_UP));
    }
    spStatementSgService.save(spStatementSg);
  }


  private void saveCurrentMonthContainerDataToDB(SpStatementQuarterReport spStatementQuarterReport,
      String currentDate) {
    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(spStatementQuarterReport.getShipUsageQuarter());
    containerSingleData.setChargeTime(spStatementQuarterReport.getChargeTimeQuarter());
    containerSingleData.setElectUsage(spStatementQuarterReport.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));
    }
  }

  private SpStatementShipUsageMonthVo makeOriginMonthDataV2(
      List<SpStatementShipUsageDetailV2> spStatementShipUsageDetails) {
    SpStatementShipUsageMonthVo spStatementShipUsageMonthVo = new SpStatementShipUsageMonthVo();
    spStatementShipUsageMonthVo.setShipUsage(
        BigDecimal.valueOf(spStatementShipUsageDetails.size()));
    for (SpStatementShipUsageDetailV2 spStatementShipUsageDetail : spStatementShipUsageDetails) {
      spStatementShipUsageMonthVo.setShorePowerCount(
          spStatementShipUsageMonthVo.getShorePowerCount()
              .add(spStatementShipUsageDetail.getShorePowerCount()));
      spStatementShipUsageMonthVo.setShorePowerTime(spStatementShipUsageDetail.getDuration()
          .divide(BigDecimal.valueOf(60), RoundingMode.HALF_UP)
          .add(spStatementShipUsageMonthVo.getShorePowerTime()));
    }
    BigDecimal decimal = spStatementShipUsageMonthVo.getShorePowerTime()
        .setScale(2, RoundingMode.HALF_UP);
    spStatementShipUsageMonthVo.setShorePowerTime(decimal);
    return spStatementShipUsageMonthVo;
  }

  private SpStatementShipUsageMonthVo makeOriginMonthDataV2d1(
      List<SpStatementShipUsageDetail> spStatementShipUsageDetails) {
    SpStatementShipUsageMonthVo spStatementShipUsageMonthVo = new SpStatementShipUsageMonthVo();
    spStatementShipUsageMonthVo.setShipUsage(
        BigDecimal.valueOf(spStatementShipUsageDetails.size()));
    for (SpStatementShipUsageDetail spStatementShipUsageDetail : spStatementShipUsageDetails) {
      spStatementShipUsageMonthVo.setShorePowerCount(
          spStatementShipUsageMonthVo.getShorePowerCount()
              .add(spStatementShipUsageDetail.getShorePowerCount()));
      spStatementShipUsageMonthVo.setShorePowerTime(spStatementShipUsageDetail.getDuration()
          .divide(BigDecimal.valueOf(60), RoundingMode.HALF_UP)
          .add(spStatementShipUsageMonthVo.getShorePowerTime()));
    }
    BigDecimal decimal = spStatementShipUsageMonthVo.getShorePowerTime()
        .setScale(2, RoundingMode.HALF_UP);
    spStatementShipUsageMonthVo.setShorePowerTime(decimal);
    return spStatementShipUsageMonthVo;
  }

  private SpStatementShipUsageMonthVo makeOriginMonthData(
      List<SpStatementShipUsageDetail> spStatementShipUsageDetails) {
    SpStatementShipUsageMonthVo spStatementShipUsageMonthVo = new SpStatementShipUsageMonthVo();
    spStatementShipUsageMonthVo.setShipUsage(
        BigDecimal.valueOf(spStatementShipUsageDetails.size()));
    for (SpStatementShipUsageDetail spStatementShipUsageDetail : spStatementShipUsageDetails) {
      spStatementShipUsageMonthVo.setShorePowerTime(spStatementShipUsageMonthVo.getShorePowerTime()
          .add(BigDecimal.valueOf(spStatementShipUsageDetail.getChargeEndDate().getTime()
                  - spStatementShipUsageDetail.getChargeStartDate().getTime())
              .divide(BigDecimal.valueOf(1000 * 60 * 60), 2, RoundingMode.HALF_UP)));
      spStatementShipUsageMonthVo.setShorePowerCount(
          spStatementShipUsageMonthVo.getShorePowerCount()
              .add(spStatementShipUsageDetail.getShorePowerCount()));
    }
    return spStatementShipUsageMonthVo;
  }
}




