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 implements SpStatementInlandLowService { @Resource SpStatementInlandLowMapper mapper; @Resource SnowflakeMakeConf snowflakeMakeConf; @Override public List getOriginDataFromPage(String date) { Map params = new HashMap<>(2); params.put("reportDateStart", date); params.put("reportDateEnd", date); return mapper.getOriginDataFromPage(params); } @Override public List getCurrentYearOriginDataFromPage() { return mapper.getCurrentYearOriginDataFromPage(); } @Override public SpStatementInlandLow getLastMonthReportData(String date, Integer type) { return mapper.getLastMonthReportData(date, type); } @Override public List selectDataFromShorePower(Map params) { return mapper.selectDataFromShorePower(params); } @Override public List 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 selectCurrentMonthReport2Count(Date reportDate) { return mapper.selectList(Wrappers.lambdaQuery() .eq(SpStatementInlandLow::getReportDate, reportDate) .eq(SpStatementInlandLow::getType, "2")); } @Override public List selectCurrentMonthReport1Count(Date reportDate) { return mapper.selectList(Wrappers.lambdaQuery() .eq(SpStatementInlandLow::getReportDate, reportDate) .eq(SpStatementInlandLow::getType, "1")); } @Override public void makeReport1(String currentMonthDate, String lastMonthDate) { try { // 查询当前年月的内河非标岸电设备使用情况 List originList = this.getOriginDataFromPage( currentMonthDate); log.info("统计当月原始数据为:{}", JSONObject.toJSONString(originList)); // 查询上个月报表数据 用来计算环比 List lastReportDataList = this.getAllLastMonthReportData(lastMonthDate, 1); log.info("统计上月报表数据为:{}", JSONObject.toJSONString(lastReportDataList)); // 查询全年原始数据 用来计算当年累计 // List currentYearOriginList = this.getCurrentYearOriginDataFromPage(); // log.info("当年累计原始数据为:{}", JSONObject.toJSONString(currentYearOriginList)); List spStatementInlandLowList = new ArrayList<>(originList.size()); // 计算各区 executeEveryDistrict(originList, lastReportDataList, spStatementInlandLowList, currentMonthDate); // 计算合计 executeTotal(spStatementInlandLowList, lastMonthDate); log.info("最终存储的内河低压分析表1为:{}", JSONObject.toJSONString(spStatementInlandLowList)); // 查询本月报表数据 List 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 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 currentMonthData = this.selectDataFromShorePower( // params); //低压标准化计算逻辑变更,这个是第二个版本, List currentMonthData = this.selectDataFromShorePowerV2(params); log.info("市级平台自动上传<当月>数据为:{}", JSONObject.toJSONString(currentMonthData)); // 查询上个月报表数据 用来计算环比 List 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 yearParams = new HashMap<>(2); // yearParams.put("reportDateStart", yearStartDate); // yearParams.put("reportDateEnd", yearEndDate); // List currentYearDataList = this.selectDataFromShorePower( // yearParams); // log.info("市级平台自动上传<全年>数据为:{}", JSONObject.toJSONString(currentYearDataList)); // 计算各区 List spStatementInlandLowList = executeEveryDistrict2(currentMonthData, lastReportDataList, currentDate); // 计算合计 executeTotal2(spStatementInlandLowList, lastMonthDate); // 查询当月是否有数据 List 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 selectDataFromShorePowerV2(Map params) { return mapper.selectDataFromShorePowerV2(params); } @Override public List getAllLastMonthReportData(String lastMonthDate, int type) { return mapper.getAllLastMonthReportData(lastMonthDate, type); } private void executeEveryDistrict(List originList, List lastReportDataList, List 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 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 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 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 executeEveryDistrict2( List currentMonthData, List lastReportDataList, String currentDate) throws ParseException { List 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 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; } }