123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- package com.shcd.service.impl;
- 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.shcd.conf.SnowflakeMakeConf;
- import com.shcd.entity.ShorePowerDeviceUsage;
- import com.shcd.entity.SpStatementInlandLow;
- import com.shcd.enums.AreaSortEnum;
- import com.shcd.enums.ShorePowerConnRecordInfoEnum;
- import com.shcd.service.SpStatementInlandLowService;
- import com.shcd.mapper.SpStatementInlandLowMapper;
- import com.shcd.utils.PrintLogUtils;
- import com.shcd.vo.ShorePowerConnRecordInfoVo;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.stream.Collectors;
- import javax.annotation.Resource;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- /**
- * @author 14390
- * @description 针对表【SP_STATEMENT_INLAND_LOW(内河低压分析表)】的数据库操作Service实现
- * @createDate 2024-01-22 14:57:44
- */
- @Service
- @Slf4j
- public class SpStatementInlandLowServiceImpl extends
- ServiceImpl<SpStatementInlandLowMapper, SpStatementInlandLow>
- implements SpStatementInlandLowService {
- @Resource
- SpStatementInlandLowMapper mapper;
- @Resource
- SnowflakeMakeConf snowflakeMakeConf;
- @Override
- public List<ShorePowerDeviceUsage> getOriginDataFromPage(String date) {
- Map<String, Object> params = new HashMap<>(2);
- params.put("reportDateStart", date);
- params.put("reportDateEnd", date);
- return mapper.getOriginDataFromPage(params);
- }
- @Override
- public List<ShorePowerDeviceUsage> getCurrentYearOriginDataFromPage() {
- return mapper.getCurrentYearOriginDataFromPage();
- }
- @Override
- public SpStatementInlandLow getLastMonthReportData(String date, Integer type) {
- return mapper.getLastMonthReportData(date, type);
- }
- @Override
- public List<ShorePowerConnRecordInfoVo> selectDataFromShorePower(Map<String, Object> params) {
- return mapper.selectDataFromShorePower(params);
- }
- @Override
- public List<ShorePowerConnRecordInfoVo> selectCurrentYearDataFromShorePower() {
- return mapper.selectCurrentYearDataFromShorePower();
- }
- @Override
- public void deleteAllCurrentMonthReport1(Date reportDate) {
- mapper.deleteAllCurrentMonthReport1(
- DateUtils.dateToString(reportDate, DateUtils.DATE_FORMAT_12));
- }
- @Override
- public void deleteAllCurrentMonthReport2(Date reportDate) {
- mapper.deleteAllCurrentMonthReport2(
- DateUtils.dateToString(reportDate, DateUtils.DATE_FORMAT_12));
- }
- @Override
- public List<SpStatementInlandLow> selectCurrentMonthReport2Count(Date reportDate) {
- return mapper.selectList(Wrappers.<SpStatementInlandLow>lambdaQuery()
- .eq(SpStatementInlandLow::getReportDate, reportDate)
- .eq(SpStatementInlandLow::getType, "2"));
- }
- @Override
- public List<SpStatementInlandLow> selectCurrentMonthReport1Count(Date reportDate) {
- return mapper.selectList(Wrappers.<SpStatementInlandLow>lambdaQuery()
- .eq(SpStatementInlandLow::getReportDate, reportDate)
- .eq(SpStatementInlandLow::getType, "1"));
- }
- @Override
- public void makeReport1(String currentMonthDate, String lastMonthDate) {
- try {
- // 查询当前年月的内河非标岸电设备使用情况
- List<ShorePowerDeviceUsage> originList = this.getOriginDataFromPage(
- currentMonthDate);
- log.info("统计当月原始数据为:{}", JSONObject.toJSONString(originList));
- // 查询上个月报表数据 用来计算环比
- List<SpStatementInlandLow> lastReportDataList = this.getAllLastMonthReportData(lastMonthDate,
- 1);
- log.info("统计上月报表数据为:{}", JSONObject.toJSONString(lastReportDataList));
- // 查询全年原始数据 用来计算当年累计
- // List<ShorePowerDeviceUsage> currentYearOriginList = this.getCurrentYearOriginDataFromPage();
- // log.info("当年累计原始数据为:{}", JSONObject.toJSONString(currentYearOriginList));
- List<SpStatementInlandLow> spStatementInlandLowList = new ArrayList<>(originList.size());
- // 计算各区
- executeEveryDistrict(originList, lastReportDataList, spStatementInlandLowList, currentMonthDate);
- // 计算合计
- executeTotal(spStatementInlandLowList, lastMonthDate);
- log.info("最终存储的内河低压分析表1为:{}", JSONObject.toJSONString(spStatementInlandLowList));
- // 查询本月报表数据
- List<SpStatementInlandLow> currentList = this.selectCurrentMonthReport1Count(
- spStatementInlandLowList.get(0).getReportDate());
- // 有数据情况下先删除后插入
- if (!CollectionUtils.isEmpty(currentList)) {
- this.deleteAllCurrentMonthReport1(
- spStatementInlandLowList.get(0).getReportDate());
- }
- super.saveBatch(spStatementInlandLowList);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("内河低压分析表 表1 中间表生成逻辑出错....原因:{}", e.getMessage());
- }
- }
- @Override
- public void makeReport2(String currentDate, String lastMonthDate, String last2MonthDate,
- String last3MonthDate) {
- try {
- String date = currentDate + "-24 23:59:59";
- String lastMonthDate11 = lastMonthDate + "-25 00:00:00";
- Map<String, Object> params = new HashMap<>(2);
- params.put("reportDateStart", lastMonthDate11);
- params.put("reportDateEnd", date);
- params.put("lastReportDateStart", last2MonthDate + "-25 00:00:00");
- params.put("lastReportDateEnd", lastMonthDate + "-24 23:59:59");
- // List<ShorePowerConnRecordInfoVo> currentMonthData = this.selectDataFromShorePower(
- // params);
- //低压标准化计算逻辑变更,这个是第二个版本,
- List<ShorePowerConnRecordInfoVo> currentMonthData = this.selectDataFromShorePowerV2(params);
- log.info("市级平台自动上传<当月>数据为:{}", JSONObject.toJSONString(currentMonthData));
- // 查询上个月报表数据 用来计算环比
- List<SpStatementInlandLow> lastReportDataList = this.getAllLastMonthReportData(lastMonthDate,
- 2);
- log.info("市级平台自动上传<上月>数据为:{}", JSONObject.toJSONString(lastReportDataList));
- // 查询全年原始数据 用来计算当年累计
- // String yearStartDate =
- // (Integer.parseInt(currentDate.split("-")[0]) - 1) + "-12-25 00:00:00";
- // String yearEndDate = currentDate + "-24 23:59:59";
- // Map<String, Object> yearParams = new HashMap<>(2);
- // yearParams.put("reportDateStart", yearStartDate);
- // yearParams.put("reportDateEnd", yearEndDate);
- // List<ShorePowerConnRecordInfoVo> currentYearDataList = this.selectDataFromShorePower(
- // yearParams);
- // log.info("市级平台自动上传<全年>数据为:{}", JSONObject.toJSONString(currentYearDataList));
- // 计算各区
- List<SpStatementInlandLow> spStatementInlandLowList = executeEveryDistrict2(currentMonthData,
- lastReportDataList, currentDate);
- // 计算合计
- executeTotal2(spStatementInlandLowList, lastMonthDate);
- // 查询当月是否有数据
- List<SpStatementInlandLow> currentList = this.selectCurrentMonthReport2Count(
- spStatementInlandLowList.get(0).getReportDate());
- // 有数据先删除,后插入
- if (!CollectionUtils.isEmpty(currentList)) {
- this.deleteAllCurrentMonthReport2(
- spStatementInlandLowList.get(0).getReportDate());
- }
- log.info("最终存储的内河低压分析表2为:{}", JSONObject.toJSONString(spStatementInlandLowList));
- super.saveBatch(spStatementInlandLowList);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("内河低压分析表 表2 中间表生成逻辑出错....原因:{}", e.getMessage());
- }
- }
- private List<ShorePowerConnRecordInfoVo> selectDataFromShorePowerV2(Map<String, Object> params) {
- return mapper.selectDataFromShorePowerV2(params);
- }
- @Override
- public List<SpStatementInlandLow> getAllLastMonthReportData(String lastMonthDate, int type) {
- return mapper.getAllLastMonthReportData(lastMonthDate, type);
- }
- private void executeEveryDistrict(List<ShorePowerDeviceUsage> originList,
- List<SpStatementInlandLow> lastReportDataList,
- List<SpStatementInlandLow> spStatementInlandLowList,String currentMonthDate) {
- try {
- // for (ShorePowerDeviceUsage thisMonthData : originList) {
- for (AreaSortEnum thisMonthData : AreaSortEnum.values()) {
- // 计算各区 当月数据
- SpStatementInlandLow spStatementInlandLow = new SpStatementInlandLow();
- String id = String.valueOf(snowflakeMakeConf.snowflakeId());
- spStatementInlandLow.setId(id);
- spStatementInlandLow.setSort(thisMonthData.getSort());
- spStatementInlandLow.setDistrict(thisMonthData.getArea());
- List<ShorePowerDeviceUsage> filterUsageList = originList.stream()
- .filter(i->i.getDistrict().equals(thisMonthData.getArea()))
- .collect(Collectors.toList());
- if (filterUsageList.isEmpty()) {
- spStatementInlandLow.setBoatUseNumberMonth(BigDecimal.ZERO);
- spStatementInlandLow.setPowerConnHoursMonth(BigDecimal.ZERO);
- spStatementInlandLow.setElectricityUsageMonth(BigDecimal.ZERO);
- spStatementInlandLow.setReportDate(
- DateUtils.stringToDate(currentMonthDate, DateUtils.DATE_FORMAT_12));
- } else {
- spStatementInlandLow.setBoatUseNumberMonth(filterUsageList.get(0).getShorePowerBoatUseNumber());
- spStatementInlandLow.setPowerConnHoursMonth(filterUsageList.get(0).getPowerConnHours());
- spStatementInlandLow.setElectricityUsageMonth(filterUsageList.get(0).getElectricityConsumption());
- spStatementInlandLow.setReportDate(
- DateUtils.stringToDate(currentMonthDate, DateUtils.DATE_FORMAT_12));
- }
- spStatementInlandLow.setType(1);
- // 计算当年累计 以及 环比
- Calendar thisCalendar = Calendar.getInstance();
- thisCalendar.setTime(DateUtils.stringToDate(currentMonthDate, DateUtils.DATE_FORMAT_12));
- for (SpStatementInlandLow lastReportData : lastReportDataList) {
- if (lastReportData.getDistrict().equals(spStatementInlandLow.getDistrict())) {
- if (thisCalendar.get(Calendar.MONTH) == Calendar.JANUARY) {
- spStatementInlandLow.setBoatUseNumberYearTotal(
- spStatementInlandLow.getBoatUseNumberMonth());
- spStatementInlandLow.setPowerConnHoursYearTotal(
- spStatementInlandLow.getPowerConnHoursMonth());
- spStatementInlandLow.setElectricityUsageYearTotal(
- spStatementInlandLow.getElectricityUsageMonth());
- }else{
- spStatementInlandLow.setBoatUseNumberYearTotal(
- spStatementInlandLow.getBoatUseNumberMonth()
- .add(lastReportData.getBoatUseNumberYearTotal())
- .setScale(2, RoundingMode.HALF_UP));
- spStatementInlandLow.setPowerConnHoursYearTotal(
- spStatementInlandLow.getPowerConnHoursMonth()
- .add(lastReportData.getPowerConnHoursYearTotal())
- .setScale(2, RoundingMode.HALF_UP));
- spStatementInlandLow.setElectricityUsageYearTotal(
- spStatementInlandLow.getElectricityUsageMonth()
- .add(lastReportData.getElectricityUsageYearTotal())
- .setScale(2, RoundingMode.HALF_UP));
- }
- // 计算环比
- spStatementInlandLow.setBoatUseNumberChain(
- getRate(spStatementInlandLow.getBoatUseNumberMonth(),
- lastReportData.getBoatUseNumberMonth()));
- }
- }
- spStatementInlandLowList.add(spStatementInlandLow);
- }
- }catch (Exception e){
- PrintLogUtils.printErrorLogInfo(e,"executeEveryDistrict异常");
- }
- }
- private static BigDecimal getRate(BigDecimal thisMonthData,
- BigDecimal lastMonthData) {
- if (Objects.isNull(lastMonthData) || lastMonthData.equals(BigDecimal.ZERO)) {
- return null;
- }
- BigDecimal result = (thisMonthData.subtract(lastMonthData)).multiply(BigDecimal.valueOf(100))
- .divide(lastMonthData, 2, RoundingMode.HALF_UP);
- return result.setScale(2, RoundingMode.HALF_UP);
- }
- private void executeTotal(List<SpStatementInlandLow> spStatementInlandLowList, String lastMonthDate) {
- // 计算合计
- SpStatementInlandLow spStatementInlandLow = new SpStatementInlandLow();
- spStatementInlandLow.setId(String.valueOf(snowflakeMakeConf.snowflakeStringId()));
- spStatementInlandLow.setSort(12);
- spStatementInlandLow.setDistrict("合计");
- spStatementInlandLow.setBoatUseNumberMonth(BigDecimal.ZERO);
- spStatementInlandLow.setBoatUseNumberChain(BigDecimal.ZERO);
- spStatementInlandLow.setBoatUseNumberYearTotal(BigDecimal.ZERO);
- spStatementInlandLow.setPowerConnHoursMonth(BigDecimal.ZERO);
- spStatementInlandLow.setPowerConnHoursYearTotal(BigDecimal.ZERO);
- spStatementInlandLow.setElectricityUsageMonth(BigDecimal.ZERO);
- spStatementInlandLow.setElectricityUsageYearTotal(BigDecimal.ZERO);
- spStatementInlandLowList.forEach(x -> {
- spStatementInlandLow.setBoatUseNumberMonth(
- x.getBoatUseNumberMonth().add(spStatementInlandLow.getBoatUseNumberMonth()));
- spStatementInlandLow.setBoatUseNumberYearTotal(
- x.getBoatUseNumberYearTotal().add(spStatementInlandLow.getBoatUseNumberYearTotal()));
- spStatementInlandLow.setPowerConnHoursMonth(
- x.getPowerConnHoursMonth().add(spStatementInlandLow.getPowerConnHoursMonth()));
- spStatementInlandLow.setPowerConnHoursYearTotal(
- x.getPowerConnHoursYearTotal().add(spStatementInlandLow.getPowerConnHoursYearTotal()));
- spStatementInlandLow.setElectricityUsageMonth(
- x.getElectricityUsageMonth().add(spStatementInlandLow.getElectricityUsageMonth()));
- spStatementInlandLow.setElectricityUsageYearTotal(x.getElectricityUsageYearTotal()
- .add(spStatementInlandLow.getElectricityUsageYearTotal()));
- spStatementInlandLow.setType(x.getType());
- spStatementInlandLow.setReportDate(x.getReportDate());
- });
- // 获取上个月的合计值用来计算环比
- SpStatementInlandLow lastMonthReportData = this.getLastMonthReportData(
- lastMonthDate, spStatementInlandLow.getType());
- if (Objects.isNull(lastMonthReportData) || Objects.isNull(
- lastMonthReportData.getBoatUseNumberMonth())
- || lastMonthReportData.getBoatUseNumberMonth().equals(BigDecimal.ZERO)) {
- spStatementInlandLow.setBoatUseNumberChain(null);
- } else {
- BigDecimal rate = (spStatementInlandLow.getBoatUseNumberMonth()
- .subtract(lastMonthReportData.getBoatUseNumberMonth())).multiply(
- BigDecimal.valueOf(100))
- .divide(lastMonthReportData.getBoatUseNumberMonth(), 2, RoundingMode.HALF_UP);
- spStatementInlandLow.setBoatUseNumberChain(rate);
- }
- spStatementInlandLowList.add(spStatementInlandLow);
- }
- private void executeTotal2(List<SpStatementInlandLow> spStatementInlandLowList,
- String lastMonthDate) {
- SpStatementInlandLow spStatementInlandLow = new SpStatementInlandLow();
- spStatementInlandLow.setId(String.valueOf(snowflakeMakeConf.snowflakeStringId()));
- spStatementInlandLow.setSort(12);
- spStatementInlandLow.setDistrict("合计");
- spStatementInlandLow.setBoatUseNumberMonth(BigDecimal.ZERO);
- spStatementInlandLow.setBoatUseNumberChain(BigDecimal.ZERO);
- spStatementInlandLow.setBoatUseNumberYearTotal(BigDecimal.ZERO);
- spStatementInlandLow.setPowerConnHoursMonth(BigDecimal.ZERO);
- spStatementInlandLow.setPowerConnHoursYearTotal(BigDecimal.ZERO);
- spStatementInlandLow.setElectricityUsageMonth(BigDecimal.ZERO);
- spStatementInlandLow.setElectricityUsageYearTotal(BigDecimal.ZERO);
- spStatementInlandLowList.forEach(x -> {
- spStatementInlandLow.setBoatUseNumberMonth(
- x.getBoatUseNumberMonth().add(spStatementInlandLow.getBoatUseNumberMonth()));
- spStatementInlandLow.setBoatUseNumberYearTotal(
- x.getBoatUseNumberYearTotal().add(spStatementInlandLow.getBoatUseNumberYearTotal()));
- spStatementInlandLow.setPowerConnHoursMonth(
- x.getPowerConnHoursMonth().add(spStatementInlandLow.getPowerConnHoursMonth()));
- spStatementInlandLow.setPowerConnHoursYearTotal(
- x.getPowerConnHoursYearTotal().add(spStatementInlandLow.getPowerConnHoursYearTotal()));
- spStatementInlandLow.setElectricityUsageMonth(
- x.getElectricityUsageMonth().add(spStatementInlandLow.getElectricityUsageMonth()));
- spStatementInlandLow.setElectricityUsageYearTotal(x.getElectricityUsageYearTotal()
- .add(spStatementInlandLow.getElectricityUsageYearTotal()));
- spStatementInlandLow.setType(x.getType());
- spStatementInlandLow.setReportDate(x.getReportDate());
- });
- // 拿上个月的中间表的"合计"数据 用来计算使用岸电船舶(艘次)的环比
- SpStatementInlandLow lastMonthReportData = this.getLastMonthReportData(
- lastMonthDate, spStatementInlandLow.getType());
- if (Objects.isNull(lastMonthReportData) || Objects.isNull(
- lastMonthReportData.getBoatUseNumberMonth())
- || lastMonthReportData.getBoatUseNumberMonth().equals(BigDecimal.ZERO)) {
- spStatementInlandLow.setBoatUseNumberChain(null);
- } else {
- BigDecimal rate = (spStatementInlandLow.getBoatUseNumberMonth()
- .subtract(lastMonthReportData.getBoatUseNumberMonth())).multiply(
- BigDecimal.valueOf(100))
- .divide(lastMonthReportData.getBoatUseNumberMonth(), 2, RoundingMode.HALF_UP);
- spStatementInlandLow.setBoatUseNumberChain(rate);
- }
- spStatementInlandLowList.add(spStatementInlandLow);
- }
- private List<SpStatementInlandLow> executeEveryDistrict2(
- List<ShorePowerConnRecordInfoVo> currentMonthData,
- List<SpStatementInlandLow> lastReportDataList, String currentDate)
- throws ParseException {
- List<SpStatementInlandLow> spStatementInlandLowList = new ArrayList<>(
- currentMonthData.size());
- for (ShorePowerConnRecordInfoEnum areaEnum : ShorePowerConnRecordInfoEnum.values()) {
- SpStatementInlandLow spStatementInlandLow = new SpStatementInlandLow();
- spStatementInlandLow.setId(String.valueOf(snowflakeMakeConf.snowflakeId()));
- spStatementInlandLow.setDistrict(areaEnum.getArea());
- spStatementInlandLow.setReportDate(
- DateUtils.stringToDate(currentDate, DateUtils.DATE_FORMAT_12));
- spStatementInlandLow.setSort(
- AreaSortEnum.getByArea(spStatementInlandLow.getDistrict()).getOwningArea());
- spStatementInlandLow.setType(2);
- List<ShorePowerConnRecordInfoVo> areaCollectData = currentMonthData.stream()
- .filter(
- i -> ShorePowerConnRecordInfoEnum.getByArea(Integer.parseInt(i.getArea())).getArea()
- .equals(areaEnum.getArea())).collect(
- Collectors.toList());
- ShorePowerConnRecordInfoVo currentMonth;
- if (!CollectionUtils.isEmpty(areaCollectData)) {
- currentMonth = areaCollectData.get(0);
- } else {
- currentMonth = new ShorePowerConnRecordInfoVo();
- }
- spStatementInlandLow.setBoatUseNumberMonth(currentMonth.getTotal());
- // 使用岸电小时在查询数据库时已经转换为小时计算
- spStatementInlandLow.setPowerConnHoursMonth(currentMonth.getDuration());
- spStatementInlandLow.setElectricityUsageMonth(currentMonth.getElect());
- // 计算当年累计 以及 环比
- Calendar thisCalendar = Calendar.getInstance();
- thisCalendar.setTime(DateUtils.stringToDate(currentDate, DateUtils.DATE_FORMAT_12));
- for (SpStatementInlandLow lastReportData : lastReportDataList) {
- if (lastReportData.getDistrict().equals(spStatementInlandLow.getDistrict())) {
- if (thisCalendar.get(Calendar.MONTH) == Calendar.JANUARY) {
- spStatementInlandLow.setBoatUseNumberYearTotal(
- spStatementInlandLow.getBoatUseNumberMonth());
- spStatementInlandLow.setPowerConnHoursYearTotal(
- spStatementInlandLow.getPowerConnHoursMonth());
- spStatementInlandLow.setElectricityUsageYearTotal(
- spStatementInlandLow.getElectricityUsageMonth());
- } else {
- spStatementInlandLow.setBoatUseNumberYearTotal(
- spStatementInlandLow.getBoatUseNumberMonth()
- .add(lastReportData.getBoatUseNumberYearTotal())
- .setScale(2, RoundingMode.HALF_UP));
- spStatementInlandLow.setPowerConnHoursYearTotal(
- spStatementInlandLow.getPowerConnHoursMonth()
- .add(lastReportData.getPowerConnHoursYearTotal())
- .setScale(2, RoundingMode.HALF_UP));
- spStatementInlandLow.setElectricityUsageYearTotal(
- spStatementInlandLow.getElectricityUsageMonth()
- .add(lastReportData.getElectricityUsageYearTotal())
- .setScale(2, RoundingMode.HALF_UP));
- }
- // 计算环比
- spStatementInlandLow.setBoatUseNumberChain(
- getRate(spStatementInlandLow.getBoatUseNumberMonth(),
- lastReportData.getBoatUseNumberMonth()));
- }
- }
- spStatementInlandLowList.add(spStatementInlandLow);
- }
- return spStatementInlandLowList;
- }
- }
|