|
|
|
@ -1,25 +1,43 @@ |
|
|
|
|
package com.psdc.service.impl; |
|
|
|
|
|
|
|
|
|
import com.psdc.entity.PsdcDevice; |
|
|
|
|
import com.psdc.entity.PsdcStatisticsYear; |
|
|
|
|
import com.psdc.mapper.PsdcDeviceMapper; |
|
|
|
|
import com.psdc.mapper.PsdcStatisticsDayMapper; |
|
|
|
|
import com.psdc.mapper.PsdcStatisticsMonthMapper; |
|
|
|
|
import com.psdc.mapper.PsdcStatisticsYearMapper; |
|
|
|
|
import com.psdc.service.IStatisticsAnalysisService; |
|
|
|
|
import com.psdc.utils.SecurityUtils; |
|
|
|
|
import com.psdc.utils.bean.BeanUtils; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.poi.ss.formula.functions.T; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.text.ParseException; |
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 统计分析页面服务层 |
|
|
|
|
*/ |
|
|
|
|
@Service |
|
|
|
|
@Slf4j |
|
|
|
|
public class StatisticsAnalysisService implements IStatisticsAnalysisService { |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private PsdcStatisticsYearMapper psdcStatisticsYearMapper; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private PsdcDeviceMapper psdcDeviceMapper; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private PsdcStatisticsMonthMapper psdcStatisticsMonthMapper; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private PsdcStatisticsDayMapper psdcStatisticsDayMapper; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 计算单个设备的同比数据 |
|
|
|
|
* @param deviceId 设备id |
|
|
|
@ -45,39 +63,146 @@ public class StatisticsAnalysisService implements IStatisticsAnalysisService { |
|
|
|
|
Double addUp = 0.0; |
|
|
|
|
for (int i = 1; i < 13; i++) { |
|
|
|
|
HashMap<String, Object> stringObjectHashMap = new HashMap<>(); |
|
|
|
|
if(i < 10 ){ |
|
|
|
|
stringObjectHashMap.put("month","0" + i + "月"); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("month",i + "月"); |
|
|
|
|
} |
|
|
|
|
if ( yearList.isEmpty() ){ |
|
|
|
|
stringObjectHashMap.put("year",null); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("year",yearList.get(i)); |
|
|
|
|
} |
|
|
|
|
if ( toYearList.isEmpty()){ |
|
|
|
|
stringObjectHashMap.put("toYear",null); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("toYear",toYearList.get(i)); |
|
|
|
|
} |
|
|
|
|
stringObjectHashMap.put("month", i < 10 ? "0" + i + "月" : i + "月"); |
|
|
|
|
stringObjectHashMap.put("year",yearList.isEmpty() ? null : yearList.get(i)); |
|
|
|
|
stringObjectHashMap.put("toYear",toYearList.isEmpty() ? null : toYearList.get(i)); |
|
|
|
|
Double compute = null; |
|
|
|
|
if ( !yearList.isEmpty() && yearList.get(i) != null && ( !toYearList.isEmpty() && toYearList.get(i) != null)){ |
|
|
|
|
if ( !yearList.isEmpty() && yearList.get(i) != null && !yearList.get(i).equals("") && ( !toYearList.isEmpty() && toYearList.get(i) != null && !toYearList.get(i).equals(""))){ |
|
|
|
|
double yearDouble = Double.parseDouble(yearList.get(i)); |
|
|
|
|
double toYearDouble = Double.parseDouble(toYearList.get(i)); |
|
|
|
|
if (toYearDouble == 0){ |
|
|
|
|
toYearDouble = 1; |
|
|
|
|
} |
|
|
|
|
compute = ( yearDouble - toYearDouble ) / toYearDouble / 100; |
|
|
|
|
|
|
|
|
|
compute = ( yearDouble - toYearDouble ) / toYearDouble * 100; |
|
|
|
|
} |
|
|
|
|
if (compute != null ){ |
|
|
|
|
addUp = addUp + compute; |
|
|
|
|
stringObjectHashMap.put("compute",String.format("%.2f%%",compute)); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("compute",null); |
|
|
|
|
} |
|
|
|
|
stringObjectHashMap.put("compute",compute); |
|
|
|
|
stringObjectHashMap.put("addUp",addUp); |
|
|
|
|
stringObjectHashMap.put("addUp",String.format("%.2f%%",addUp)); |
|
|
|
|
mapList.add(stringObjectHashMap); |
|
|
|
|
} |
|
|
|
|
map.put("yearOnYear",mapList); |
|
|
|
|
|
|
|
|
|
return map; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 计算环比数据 |
|
|
|
|
* @param timeType 时间类型 1 日 2 月 3 年 |
|
|
|
|
* @param datetime 时间 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public List<HashMap<String,Object>> computeLinkRelativeRatio(Integer timeType, String datetime) throws ParseException { |
|
|
|
|
//查找设备列表
|
|
|
|
|
List<PsdcDevice> psdcDevices = psdcDeviceMapper.controlQueryByUserId(SecurityUtils.getUserId()); |
|
|
|
|
List<HashMap<String,Object>> mapList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
if ( timeType == 1 ){ |
|
|
|
|
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
|
Calendar instance = Calendar.getInstance(); |
|
|
|
|
instance.setTime(dayFormat.parse(datetime)); |
|
|
|
|
instance.add(Calendar.DATE,-1); |
|
|
|
|
String today = dayFormat.format(dayFormat.parse(datetime)); |
|
|
|
|
String yesterday = dayFormat.format(instance.getTime()); |
|
|
|
|
log.info("日环比,本期:{},同期:{}",today,yesterday); |
|
|
|
|
mapList = psdcDevices.stream().map(psdcDevice -> { |
|
|
|
|
HashMap<String, Object> stringObjectHashMap = new HashMap<>(); |
|
|
|
|
Double compute = null; |
|
|
|
|
Double addUp = null; |
|
|
|
|
Integer deviceId = psdcDevice.getDeviceId(); |
|
|
|
|
Double todayTotal = psdcStatisticsDayMapper.queryOneLineSum(deviceId, today); |
|
|
|
|
Double yesterdayTotal = psdcStatisticsDayMapper.queryOneLineSum(deviceId, yesterday); |
|
|
|
|
if( todayTotal == null ){ |
|
|
|
|
todayTotal = 0.0; |
|
|
|
|
} |
|
|
|
|
if ( yesterdayTotal != null && yesterdayTotal != 0.0) { |
|
|
|
|
addUp = todayTotal - yesterdayTotal; |
|
|
|
|
compute = (todayTotal - yesterdayTotal) / yesterdayTotal * 100; |
|
|
|
|
stringObjectHashMap.put("lastMonthTotal", String.format("%.2f", yesterdayTotal)); |
|
|
|
|
stringObjectHashMap.put("addUp", String.format("%.2f", addUp)); |
|
|
|
|
stringObjectHashMap.put("compute", String.format("%.2f%%", compute)); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("addUp", null); |
|
|
|
|
stringObjectHashMap.put("compute", null); |
|
|
|
|
stringObjectHashMap.put("lastMonthTotal", null); |
|
|
|
|
} |
|
|
|
|
stringObjectHashMap.put("deviceId", psdcDevice.getDeviceId()); |
|
|
|
|
stringObjectHashMap.put("deviceName", psdcDevice.getDeviceName()); |
|
|
|
|
stringObjectHashMap.put("thisMonthTotal", String.format("%.2f", todayTotal)); |
|
|
|
|
return stringObjectHashMap; |
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
} else if ( timeType == 2 ){ |
|
|
|
|
SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM"); |
|
|
|
|
Calendar instance = Calendar.getInstance(); |
|
|
|
|
instance.setTime(monthFormat.parse(datetime)); |
|
|
|
|
instance.add(Calendar.MONTH,-1); |
|
|
|
|
String thisMonth = monthFormat.format(monthFormat.parse(datetime)); |
|
|
|
|
String lastMonth = monthFormat.format(instance.getTime()); |
|
|
|
|
log.info("月环比,本期:{},同期:{}",thisMonth,lastMonth); |
|
|
|
|
mapList = psdcDevices.stream().map(psdcDevice -> { |
|
|
|
|
HashMap<String, Object> stringObjectHashMap = new HashMap<>(); |
|
|
|
|
Double compute = null; |
|
|
|
|
Double addUp = null; |
|
|
|
|
Integer deviceId = psdcDevice.getDeviceId(); |
|
|
|
|
Double thisMonthTotal = psdcStatisticsMonthMapper.queryOneLineSum(deviceId, thisMonth); |
|
|
|
|
Double lastMonthTotal = psdcStatisticsMonthMapper.queryOneLineSum(deviceId, lastMonth); |
|
|
|
|
if( thisMonthTotal == null ){ |
|
|
|
|
thisMonthTotal = 0.0; |
|
|
|
|
} |
|
|
|
|
if ( lastMonthTotal != null && lastMonthTotal != 0.0) { |
|
|
|
|
addUp = thisMonthTotal - lastMonthTotal; |
|
|
|
|
compute = (thisMonthTotal - lastMonthTotal) / lastMonthTotal * 100; |
|
|
|
|
stringObjectHashMap.put("lastMonthTotal", String.format("%.2f", lastMonthTotal)); |
|
|
|
|
stringObjectHashMap.put("addUp", String.format("%.2f", addUp)); |
|
|
|
|
stringObjectHashMap.put("compute", String.format("%.2f%%", compute)); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("addUp", null); |
|
|
|
|
stringObjectHashMap.put("compute", null); |
|
|
|
|
stringObjectHashMap.put("lastMonthTotal", null); |
|
|
|
|
} |
|
|
|
|
stringObjectHashMap.put("deviceId", psdcDevice.getDeviceId()); |
|
|
|
|
stringObjectHashMap.put("deviceName", psdcDevice.getDeviceName()); |
|
|
|
|
stringObjectHashMap.put("thisMonthTotal", String.format("%.2f", thisMonthTotal)); |
|
|
|
|
return stringObjectHashMap; |
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
} else if ( timeType == 3){ |
|
|
|
|
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); |
|
|
|
|
Calendar instance = Calendar.getInstance(); |
|
|
|
|
instance.setTime(yearFormat.parse(datetime)); |
|
|
|
|
instance.add(Calendar.YEAR,-1); |
|
|
|
|
String thisYear = yearFormat.format(yearFormat.parse(datetime)); |
|
|
|
|
String lastYear = yearFormat.format(instance.getTime()); |
|
|
|
|
log.info("年环比,本期:{},同期:{}",thisYear,lastYear); |
|
|
|
|
mapList = psdcDevices.stream().map(psdcDevice -> { |
|
|
|
|
HashMap<String, Object> stringObjectHashMap = new HashMap<>(); |
|
|
|
|
Double compute = null; |
|
|
|
|
Double addUp = null; |
|
|
|
|
Integer deviceId = psdcDevice.getDeviceId(); |
|
|
|
|
Double thisYearTotal = psdcStatisticsYearMapper.queryOneLineSum(deviceId, thisYear); |
|
|
|
|
Double lastYearTotal = psdcStatisticsYearMapper.queryOneLineSum(deviceId, lastYear); |
|
|
|
|
if( thisYearTotal == null ){ |
|
|
|
|
thisYearTotal = 0.0; |
|
|
|
|
} |
|
|
|
|
if ( lastYearTotal != null && lastYearTotal != 0.0) { |
|
|
|
|
addUp = thisYearTotal - lastYearTotal; |
|
|
|
|
compute = (thisYearTotal - lastYearTotal) / lastYearTotal * 100; |
|
|
|
|
stringObjectHashMap.put("lastMonthTotal", String.format("%.2f", lastYearTotal)); |
|
|
|
|
stringObjectHashMap.put("addUp", String.format("%.2f", addUp)); |
|
|
|
|
stringObjectHashMap.put("compute", String.format("%.2f%%", compute)); |
|
|
|
|
} else { |
|
|
|
|
stringObjectHashMap.put("addUp", null); |
|
|
|
|
stringObjectHashMap.put("compute", null); |
|
|
|
|
stringObjectHashMap.put("lastMonthTotal", null); |
|
|
|
|
} |
|
|
|
|
stringObjectHashMap.put("deviceId", psdcDevice.getDeviceId()); |
|
|
|
|
stringObjectHashMap.put("deviceName", psdcDevice.getDeviceName()); |
|
|
|
|
stringObjectHashMap.put("thisMonthTotal", String.format("%.2f", thisYearTotal)); |
|
|
|
|
return stringObjectHashMap; |
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
} |
|
|
|
|
return mapList; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|