package com.gyee.power.fitting.controller.gf;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.power.fitting.common.result.JsonResult;
import com.gyee.power.fitting.common.result.ResultCode;
import com.gyee.power.fitting.dispersionanalysis.InverterPowerAnalysis2;
import com.gyee.power.fitting.dispersionanalysis.ReliabilityCalculator;
import com.gyee.power.fitting.model.*;
import com.gyee.power.fitting.service.IPhotovoltaicAnalysisService;
import com.gyee.power.fitting.service.IStateCauseService;
import com.gyee.power.fitting.service.ProBasicEquipmentService;
import com.gyee.power.fitting.service.ProBasicPowerstationService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
* 光伏分析 前端控制器
*
*
* @author gfhd
* @since 2023-10-13
*/
@RestController
@RequestMapping("/photovoltaic")
public class PhotovoltaicAnalysisController {
@Resource
private IPhotovoltaicAnalysisService photovoltaicAnalysisService;
@Resource
private ProBasicEquipmentService proBasicEquipmentService;
@Resource
private IStateCauseService stateCauseService;
@Resource
private InverterPowerAnalysis2 inverterPowerAnalysis;
@Resource
private ProBasicPowerstationService powerstationService;
@Resource
private ReliabilityCalculator reliabilityCalculator;
//状态转换
@GetMapping("/state_transition")
public JSONObject stateTransition(
@RequestParam("stationId") String stationId,
@RequestParam("startTime") Long startTime,
@RequestParam("endTime") Long endTime,
@RequestParam("type") String type,
@RequestParam("pageNum") int pageNum,
@RequestParam("pageSize") int pageSize) {
List equipments = proBasicEquipmentService.getStationMap(type).get(stationId);
if (equipments == null) return JsonResult.successData(ResultCode.SUCCESS, new HashMap<>());
Map stringMap = equipments.stream().collect(Collectors.toMap(ProBasicEquipment::getId, ProBasicEquipment::getNemCode));
List collect = equipments.stream().map(ProBasicEquipment::getId).collect(Collectors.toList());
List pageItems = collect.subList(pageSize * (pageNum - 1), Math.min(collect.size(), pageSize * pageNum));
QueryWrapper scWrapper = new QueryWrapper<>();
// Page page = new Page<>(pageNum, pageSize);
scWrapper.eq("state_type", "8种状态").in("equipment_id", pageItems)
.ge("start_time", startTime).lt("end_time", endTime)
.orderByAsc("equipment_id,start_time");
List list = stateCauseService.list(scWrapper);
Map> listMap = list.stream().peek(sc -> sc.setEquipmentId(stringMap.get(sc.getEquipmentId()))).collect(Collectors.groupingBy(StateCause::getEquipmentId, LinkedHashMap::new, Collectors.toList()));
// PageMap stateCauses = (PageMap) stateCauseService.page(page, scWrapper);
// stateCauses.setRecordMap(stateCauses.getRecords().stream().collect(Collectors.groupingBy(StateCause::getEquipmentId)));
// stateCauses.setRecords(Collections.emptyList());
Map map = new HashMap<>();
map.put("total", collect.size());
map.put("size", listMap.size());
map.put("current", pageNum);
map.put("data", listMap);
return JsonResult.successData(ResultCode.SUCCESS, map);
}
//状态时间
@GetMapping("/state_time")
public JSONObject stateTime(
@RequestParam("stationId") String stationId,
@RequestParam("startTime") Long startTime,
@RequestParam("endTime") Long endTime,
@RequestParam("type") String type,
@RequestParam("pageNum") int pageNum,
@RequestParam("pageSize") int pageSize) {
Map> stationMap = proBasicEquipmentService.getStationMap(type);
List equipments = stationMap.get(stationId);
if (equipments == null) return JsonResult.successData(ResultCode.SUCCESS, new HashMap<>());
Map stringMap = equipments.stream().collect(Collectors.toMap(ProBasicEquipment::getId, ProBasicEquipment::getNemCode));
List collect = equipments.stream().map(ProBasicEquipment::getId).collect(Collectors.toList());
List pageItems = collect.subList(pageSize * (pageNum - 1), Math.min(collect.size(), pageSize * pageNum));
QueryWrapper scWrapper = new QueryWrapper<>();
// Page page = new Page<>(pageNum, pageSize);
scWrapper.select("equipment_id,event,sum(time)/3600 hour");
scWrapper.eq("state_type", "8种状态").in("equipment_id", pageItems)
.ge("start_time", startTime).lt("end_time", endTime);
scWrapper.groupBy("equipment_id,event").orderByAsc("equipment_id");
List causePage = stateCauseService.list(scWrapper);
Map> listMap = causePage.stream().peek(sc -> sc.setEquipmentId(stringMap.get(sc.getEquipmentId()))).collect(Collectors.groupingBy(StateCause::getEquipmentId, LinkedHashMap::new, Collectors.toList()));
List deviceList = statecauses2Devices(listMap);
/*PageMap pageMap = new PageMap<>();
pageMap.
PageMap stateCauses = (PageMap) causePage;
Map> collect = stateCauses.getRecords().stream().collect(Collectors.groupingBy(StateCause::getEquipmentId,
Collectors.groupingBy(StateCause::getEvent, Collectors.summingDouble(t -> t.getTime() / (60 * 24.0)))));
stateCauses.setRecordMap(collect);
stateCauses.setRecords(Collections.emptyList());*/
// protected long total;
// protected long size;
// protected long current;
Map map = new HashMap<>();
map.put("total", collect.size());
map.put("size", deviceList.size());
map.put("current", pageNum);
map.put("data", deviceList);
return JsonResult.successData(ResultCode.SUCCESS, map);
}
//MTBF&MTTR
@GetMapping("/mtbf_mttr")
public JSONObject getMtbfAndMttr(
@RequestParam(value = "companyId", required = false) String companyId,
@RequestParam(value = "stationId", required = false) String stationId,
@RequestParam("time") String time,
@RequestParam("type") String type,
@RequestParam("timeType") String timeType,
@RequestParam(value = "pageNum", required = false) Integer pageNum,
@RequestParam(value = "pageSize", required = false) Integer pageSize) {
Date date, startDate, endDate;
DateRange range = null;
if ("year".equals(timeType)) {
date = DateUtil.parse(time, "yyyy");
startDate = DateUtil.beginOfYear(date);
endDate = DateUtil.endOfYear(date);
range = DateUtil.range(startDate, endDate, DateField.MONTH);
} else if ("month".equals(timeType)) {
date = DateUtil.parse(time, "yyyy-MM");
startDate = DateUtil.beginOfMonth(date);
endDate = DateUtil.endOfMonth(date);
} else {
date = DateUtil.parse(time);
startDate = DateUtil.beginOfDay(date);
endDate = DateUtil.endOfDay(date);
}
Map stationMap = powerstationService.getCacheList(companyId, type)
.stream().collect(Collectors.toMap(ProBasicPowerstation::getId, Function.identity()));
List mttrs = new ArrayList<>();
List mtbfs = new ArrayList<>();
for (String stId : stationMap.keySet()) {
MtbfAndMttr mt = new MtbfAndMttr();
mt.setStationId(stId);
mt.setStationName(stationMap.get(stId).getName());
mttrs.add(mt);
MtbfAndMttr mt2 = new MtbfAndMttr();
mt2.setStationId(stId);
mt2.setStationName(stationMap.get(stId).getName());
mtbfs.add(mt2);
}
Map mttrMap = mttrs.stream().collect(Collectors.toMap(MtbfAndMttr::getStationId, Function.identity()));
Map mtbfMap = mtbfs.stream().collect(Collectors.toMap(MtbfAndMttr::getStationId, Function.identity()));
for (DateTime dateTime : range) {
DateTime endOfMonth = DateUtil.endOfMonth(dateTime);
//故障
QueryWrapper gzWrapper = new QueryWrapper<>();
gzWrapper.select("station_id,count(*) time");
gzWrapper.eq("state_type", "8种状态").in("station_id", stationMap.keySet())
.eq("event", 2)
.ge("start_time", dateTime).lt("end_time", endOfMonth);
gzWrapper.groupBy("station_id");
// List gzlist = ;
Map gzMap = stateCauseService.list(gzWrapper)
.stream().collect(Collectors.toMap(StateCause::getStationId, Function.identity()));
//维修
QueryWrapper wxWrapper = new QueryWrapper<>();
wxWrapper.select("station_id,sum(time)/3600 hour,count(*) time");
wxWrapper.eq("state_type", "8种状态").in("station_id", stationMap.keySet())
.eq("event", 3)
.ge("start_time", dateTime).lt("end_time", endOfMonth);
wxWrapper.groupBy("station_id");
// List wxlist = ;
Map wxMap = stateCauseService.list(wxWrapper)
.stream().collect(Collectors.toMap(StateCause::getStationId, Function.identity()));
for (String stId : stationMap.keySet()) {
StateCause wx = wxMap.get(stId);
double wxh = 0, wxt = 0;
if (wx != null) {
wxh = wx.getHour();
wxt = wx.getTime();
}
double mttr = reliabilityCalculator.calculateMTTR(wxh, (int) wxt);
double l = (endOfMonth.getTime() - dateTime.getTime()) / 3600000.0;
double mtbf = reliabilityCalculator.calculateMTBF(l, gzMap.get(stId)==null?0: gzMap.get(stId).getTime().intValue());
if (DateUtil.month(dateTime) == 0) {
mttrMap.get(stId).setCurrentMonth01(mttr);
mtbfMap.get(stId).setCurrentMonth01(mtbf);
} else if (DateUtil.month(dateTime) == 1) {
mttrMap.get(stId).setCurrentMonth02(mttr);
mtbfMap.get(stId).setCurrentMonth02(mtbf);
} else if (DateUtil.month(dateTime) == 2) {
mttrMap.get(stId).setCurrentMonth03(mttr);
mtbfMap.get(stId).setCurrentMonth03(mtbf);
} else if (DateUtil.month(dateTime) == 3) {
mttrMap.get(stId).setCurrentMonth04(mttr);
mtbfMap.get(stId).setCurrentMonth04(mtbf);
} else if (DateUtil.month(dateTime) == 4) {
mttrMap.get(stId).setCurrentMonth05(mttr);
mtbfMap.get(stId).setCurrentMonth05(mtbf);
} else if (DateUtil.month(dateTime) == 5) {
mttrMap.get(stId).setCurrentMonth06(mttr);
mtbfMap.get(stId).setCurrentMonth06(mtbf);
} else if (DateUtil.month(dateTime) == 6) {
mttrMap.get(stId).setCurrentMonth07(mttr);
mtbfMap.get(stId).setCurrentMonth07(mtbf);
} else if (DateUtil.month(dateTime) == 7) {
mttrMap.get(stId).setCurrentMonth08(mttr);
mtbfMap.get(stId).setCurrentMonth08(mtbf);
} else if (DateUtil.month(dateTime) == 8) {
mttrMap.get(stId).setCurrentMonth09(mttr);
mtbfMap.get(stId).setCurrentMonth09(mtbf);
} else if (DateUtil.month(dateTime) == 9) {
mttrMap.get(stId).setCurrentMonth10(mttr);
mtbfMap.get(stId).setCurrentMonth10(mtbf);
} else if (DateUtil.month(dateTime) == 10) {
mttrMap.get(stId).setCurrentMonth11(mttr);
mtbfMap.get(stId).setCurrentMonth11(mtbf);
} else if (DateUtil.month(dateTime) == 11) {
mttrMap.get(stId).setCurrentMonth12(mttr);
mtbfMap.get(stId).setCurrentMonth12(mtbf);
}
}
}
Map map = new HashMap<>();
map.put("MTBF", mtbfs);
map.put("MTTR", mttrs);
return JsonResult.successData(ResultCode.SUCCESS, map);
}
private List statecauses2Devices(Map> listMap) {
List deviceList = new ArrayList<>();
listMap.forEach((wt, scs) -> {
Device device = new Device();
device.setStationId(scs.get(0).getStationId());
device.setDeviceId(wt);
AtomicReference sum = new AtomicReference<>((double) 0);
scs.forEach(sc -> {
sum.updateAndGet(v -> v + sc.getHour());
switch (sc.getEvent()) {
case 0:
device.setStandbyTime(sc.getHour());
break;
case 1:
device.setGridConnectionTime(sc.getHour());
break;
case 2:
device.setFaultTime(sc.getHour());
break;
case 3:
device.setMaintenanceTime(sc.getHour());
break;
case 4:
device.setPowerLimitTime(sc.getHour());
break;
case 5:
device.setStressedTime(sc.getHour());
break;
case 6:
device.setOfflineTime(sc.getHour());
break;
}
});
device.setTotal(NumberUtil.round(sum.get(), 2).doubleValue());
deviceList.add(device);
});
return deviceList;
}
//性能等级评估
@GetMapping("/analysis")
public JSONObject photovoltaicAnalysis(
@RequestParam("stationId") String stationId,
@RequestParam("time") String time,
@RequestParam("timeType") String timeType,
@RequestParam("pageNum") int pageNum,
@RequestParam("pageSize") int pageSize) {
Date date, startDate, endDate;
if ("year".equals(timeType)) {
date = DateUtil.parse(time, "yyyy");
startDate = DateUtil.beginOfYear(date);
endDate = DateUtil.endOfYear(date);
} else if ("month".equals(timeType)) {
date = DateUtil.parse(time, "yyyy-MM");
startDate = DateUtil.beginOfMonth(date);
endDate = DateUtil.endOfMonth(date);
} else {
date = DateUtil.parse(time);
startDate = DateUtil.beginOfDay(date);
endDate = DateUtil.endOfDay(date);
}
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.select("station_id,equipment_id,avg(scatter) scatter,avg(conversion_efficiency)" +
" conversion_efficiency,sum(equivalent_generating_time) equivalent_generating_time," +
"avg(device_availability) device_availability").between("data_date", startDate, endDate)
.eq("station_id", stationId)
.groupBy("station_id,equipment_id").orderByAsc("equipment_id");
Page page = new Page<>(pageNum, pageSize);
Page list = photovoltaicAnalysisService.page(page, wrapper);
Map collect = proBasicEquipmentService.getWtNcMap("IN");
list.setRecords(list.getRecords().stream().peek(pa -> pa.setEquipmentId(collect.get(pa.getEquipmentId()))).collect(Collectors.toList()));
return JsonResult.successData(ResultCode.SUCCESS, list);
}
//逆变器分析
@GetMapping("/analysis/inverter")
public JSONObject photovoltaicInverterAnalysis(
@RequestParam("stationId") String stationId,
@RequestParam("time") String time,
@RequestParam("timeType") String timeType,
@RequestParam("pageNum") int pageNum,
@RequestParam("pageSize") int pageSize) {
Date date, startDate, endDate;
if ("year".equals(timeType)) {
date = DateUtil.parse(time, "yyyy");
startDate = DateUtil.beginOfYear(date);
endDate = DateUtil.endOfYear(date);
} else if ("month".equals(timeType)) {
date = DateUtil.parse(time, "yyyy-MM");
startDate = DateUtil.beginOfMonth(date);
endDate = DateUtil.endOfMonth(date);
} else {
date = DateUtil.parse(time);
startDate = DateUtil.beginOfDay(date);
endDate = DateUtil.endOfDay(date);
}
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.select("station_id,equipment_id,avg(average_light_intensity) average_light_intensity,avg(average_power)" +
" average_power,avg(scatter) scatter,avg(equivalent_generating_time_stan) equivalent_generating_time_stan")
.between("data_date", startDate, endDate)
.eq("station_id", stationId)
.groupBy("station_id,equipment_id").orderByAsc("equipment_id");
Page page = new Page<>(pageNum, pageSize);
Page list = photovoltaicAnalysisService.page(page, wrapper);
Map collect = proBasicEquipmentService.getWtNcMap("IN");
list.setRecords(list.getRecords().stream().peek(pa -> {
pa.setEquipmentId(collect.get(pa.getEquipmentId()));
pa.setScatterStatus(inverterPowerAnalysis.analyzeInverterStatus(pa.getScatter(), pa.getAveragePower()));
}).collect(Collectors.toList()));
return JsonResult.successData(ResultCode.SUCCESS, list);
}
}