package com.tievd.jyz.service.impl;
|
|
import cn.hutool.core.date.DateUtil;
|
import com.tievd.jyz.dto.CarModelProportionDTO;
|
import com.tievd.jyz.dto.SalesStatDTO;
|
import com.tievd.jyz.dto.TrafficFlowStatDTO;
|
import com.tievd.jyz.entity.vo.*;
|
import com.tievd.jyz.mapper.OilRecordMapper;
|
import com.tievd.jyz.mapper.OilStaticMapper;
|
import com.tievd.jyz.mapper.TrafficFlowMapper;
|
import com.tievd.jyz.service.IOperationDataService;
|
import com.tievd.jyz.util.PercentUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.util.*;
|
import java.util.function.Function;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author yang'zhi'shui
|
*/
|
@Service
|
public class OperationDataServiceImpl implements IOperationDataService {
|
|
@Autowired
|
private TrafficFlowMapper trafficFlowMapper;
|
|
@Autowired
|
private OilRecordMapper oilRecordMapper;
|
|
@Autowired
|
private OilStaticMapper oilStaticMapper;
|
|
@Override
|
public OperationOverviewVO overview(String today, String orgCode) {
|
Long trafficFlow = trafficFlowMapper.getTrafficFlow(today, orgCode);
|
OperationOverviewVO overviewVO = oilRecordMapper.getOilRecordOverview(today, orgCode);
|
overviewVO.setTrafficFlow(trafficFlow);
|
if (trafficFlow == 0 || overviewVO.getInboundCount() == 0) {
|
overviewVO.setTurnInRate(0d);
|
overviewVO.setAddOilRate(0d);
|
} else {
|
overviewVO.setTurnInRate(BigDecimal.valueOf((float) overviewVO.getInboundCount() / trafficFlow).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
overviewVO.setAddOilRate(BigDecimal.valueOf((float) overviewVO.getAddOilCount() / overviewVO.getInboundCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
}
|
return overviewVO;
|
}
|
|
@Override
|
public Map<String, List> trafficFlowStat(Integer intervalDay, String endDay, String orgCode) {
|
List<TrafficFlowStatDTO> trafficFlowStatList = trafficFlowMapper.trafficFlowStat(intervalDay, endDay, orgCode);
|
List<TrafficFlowStatDTO> inboundCountStatList = oilRecordMapper.inboundCountStat(intervalDay, endDay, orgCode);
|
Map<String, Integer> inboundMap = inboundCountStatList.stream().collect(Collectors.toMap(TrafficFlowStatDTO::getDate, TrafficFlowStatDTO::getInboundCount));
|
List<String> dateList = new ArrayList();
|
List<Integer> trafficFlowList = new ArrayList();
|
List<Integer> inboundCountList = new ArrayList();
|
for (int i = 0; i < trafficFlowStatList.size(); i++) {
|
TrafficFlowStatDTO dto = trafficFlowStatList.get(i);
|
dateList.add(i, dto.getDate());
|
trafficFlowList.add(i, dto.getTrafficFlow());
|
inboundCountList.add(i, inboundMap.get(dto.getDate()));
|
}
|
Map<String, List> result = new HashMap<>();
|
result.put("dateArr", dateList);
|
result.put("trafficFlowArr", trafficFlowList);
|
result.put("inboundCountArr", inboundCountList);
|
return result;
|
}
|
|
@Override
|
public List<RegionTrafficFlowStatVO> regionTrafficFlowStat(String today, String orgCode, Integer limit) {
|
List<RegionTrafficFlowStatVO> trafficFlowStatList = trafficFlowMapper.regionTrafficFlowStat(today, orgCode, limit);
|
List<RegionTrafficFlowStatVO> inboundCountStatList = oilRecordMapper.regionTrafficFlowStat(today, orgCode, limit);
|
Map<String, Integer> inboundMap = inboundCountStatList.stream().collect(Collectors.toMap(RegionTrafficFlowStatVO::getDepartCode, RegionTrafficFlowStatVO::getInboundCount));
|
trafficFlowStatList.forEach(vo -> vo.setInboundCount(inboundMap.get(vo.getDepartCode())));
|
return trafficFlowStatList;
|
}
|
|
@Override
|
public CustomerStatVO customerStat(String orgCode) {
|
return oilStaticMapper.custommerStat(orgCode);
|
}
|
|
@Override
|
public Map<String, List> oilRecordTimeStat(String date, String orgCode) {
|
List<OilRecordTimeStatVO> list = oilRecordMapper.oilRecordTimeStat(date, orgCode);
|
Map<String, OilRecordTimeStatVO> oilRecordTimeStatVOMap = list.stream().collect(Collectors.toMap(OilRecordTimeStatVO::getTime, Function.identity()));
|
List<String> timeArr = new ArrayList<>();
|
List<Integer> stopCountArr = new ArrayList<>();
|
List<Integer> addOilCountArr = new ArrayList<>();
|
List<Double> addOilRateArr = new ArrayList<>();
|
Integer tempInboundCount = 0;
|
Integer tempStopCount = 0;
|
Integer tempAddOilCount = 0;
|
Integer index = 0;
|
for (int i = 0; i < 24; i++) {
|
OilRecordTimeStatVO vo = oilRecordTimeStatVOMap.get(String.valueOf(i));
|
if (vo == null) {
|
vo = new OilRecordTimeStatVO();
|
vo.setAddOilCount(0);
|
vo.setInboundCount(0);
|
vo.setStopCount(0);
|
}
|
tempInboundCount += vo.getInboundCount();
|
tempStopCount += vo.getInboundCount() - vo.getAddOilCount();
|
tempAddOilCount += vo.getAddOilCount();
|
if (i % 2 == 1) {
|
stopCountArr.add(index, tempStopCount);
|
addOilCountArr.add(index, tempAddOilCount);
|
if (tempInboundCount == 0) {
|
addOilRateArr.add(index, 0d);
|
} else {
|
addOilRateArr.add(index, BigDecimal.valueOf((float) tempAddOilCount / tempInboundCount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
}
|
timeArr.add(index, (i + 1) + "点");
|
index++;
|
tempInboundCount = 0;
|
tempStopCount = 0;
|
tempAddOilCount = 0;
|
}
|
}
|
Map<String, List> result = new HashMap<>();
|
result.put("timeArr", timeArr);
|
result.put("stopCountArr", stopCountArr);
|
result.put("addOilCountArr", addOilCountArr);
|
result.put("addOilRateArr", addOilRateArr);
|
return result;
|
}
|
|
@Override
|
public List<OilPositionHeatMapVO> oilPositionHeatMap(String today, String orgCode) {
|
List<OilPositionHeatMapVO> heatList = oilRecordMapper.getOilPositionHeatMap(today, orgCode);
|
OilPositionHeatMapVO maxVO = heatList.stream().max(Comparator.comparing(OilPositionHeatMapVO::getStopCount)).get();
|
if (maxVO.getStopCount() == 0) {
|
heatList.forEach(vo -> vo.setHeatIndex(0));
|
} else {
|
heatList.forEach(vo -> vo.setHeatIndex(vo.getStopCount() * 100 / maxVO.getStopCount()));
|
}
|
return heatList;
|
}
|
|
@Override
|
public Map<String, List> carModelStat(String today, String orgCode) {
|
List<CarModelProportionDTO> list = oilRecordMapper.carModelStat(today, orgCode);
|
long addOilSum = list.stream().mapToLong(CarModelProportionDTO::getAddOilCount).sum();
|
if (addOilSum == 0) {
|
list.forEach(vo -> vo.setProportion(0.00d));
|
} else {
|
list.forEach(vo -> vo.setProportion(BigDecimal.valueOf((float) vo.getAddOilCount() / addOilSum).doubleValue()));
|
}
|
List<String> carModelArr = new ArrayList<>();
|
List<Integer> addOilCountArr = new ArrayList<>();
|
List<Double> proportionArr = new ArrayList<>();
|
for (int i = 0; i < list.size(); i++) {
|
CarModelProportionDTO vo = list.get(i);
|
carModelArr.add(i, vo.getCarModel());
|
addOilCountArr.add(i, vo.getAddOilCount());
|
proportionArr.add(i, vo.getProportion());
|
}
|
Map<String, List> result = new HashMap<>();
|
result.put("carModelArr", carModelArr);
|
result.put("addOilCountArr", addOilCountArr);
|
if (addOilSum == 0) {
|
result.put("proportionArr", proportionArr);
|
} else {
|
// 校准百分比之和为1
|
result.put("proportionArr", PercentUtil.getPercentValue(proportionArr, 1, 0));
|
}
|
return result;
|
}
|
|
@Override
|
public Map<String, List> salesStat(Integer intervalDay, String today, String orgCode) {
|
List<SalesStatDTO> list = oilRecordMapper.salesStat(intervalDay, today, orgCode);
|
Integer pre = 0;
|
for (SalesStatDTO dto : list) {
|
Integer curr = dto.getSalesVolume();
|
if (pre == 0) {
|
dto.setGrowthRate(0.00d);
|
} else {
|
dto.setGrowthRate(BigDecimal.valueOf((float) (curr - pre) / pre).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
}
|
pre = curr;
|
}
|
List<String> dateArr = new ArrayList<>();
|
List<Integer> salesVolumeArr = new ArrayList<>();
|
List<Double> growthRateArr = new ArrayList<>();
|
for (int i = 0; i < list.size(); i++) {
|
SalesStatDTO dto = list.get(i);
|
dateArr.add(i, dto.getDate());
|
salesVolumeArr.add(i, dto.getSalesVolume());
|
growthRateArr.add(i, dto.getGrowthRate());
|
}
|
Map<String, List> result = new HashMap<>();
|
result.put("dateArr", dateArr);
|
result.put("salesVolumeArr", salesVolumeArr);
|
result.put("growthRateArr", growthRateArr);
|
return result;
|
}
|
|
@Override
|
public List<OilRecordStatVO> regionOilRecordStat(String orgCode, Integer freq, Integer limit) {
|
List<OilRecordStatVO> result;
|
if (freq == 0) { //当天
|
result = oilRecordMapper.getRegionOilRecordStatByDay(orgCode, DateUtil.today(), limit);
|
} else { //当月
|
result = oilRecordMapper.getRegionOilRecordStatByMonth(orgCode, DateUtil.format(new Date(), "yyyy-MM"), limit);
|
}
|
for (OilRecordStatVO vo : result) {
|
Integer inboundCount = vo.getInboundCount();
|
if (inboundCount == 0) {
|
vo.setAddOilRate(0.00d);
|
} else {
|
vo.setAddOilRate(BigDecimal.valueOf((float) vo.getAddOilCount() / inboundCount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
}
|
}
|
return result;
|
}
|
|
@Override
|
public List<SalesStatVO> salesRanking(String orgCode, Integer freq, Integer limit) {
|
List<SalesStatVO> result;
|
if (freq == 0) { //当天
|
result = oilRecordMapper.getSalesRankingByDay(orgCode, DateUtil.today(), limit);
|
} else { //当月
|
result = oilRecordMapper.getSalesRankingByMonth(orgCode, DateUtil.format(new Date(), "yyyy-MM"), limit);
|
}
|
return result;
|
}
|
|
}
|