package com.ruoyi.order.service.impl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.chargingPile.api.dto.GetSiteListDTO;
|
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
|
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
|
import com.ruoyi.chargingPile.api.model.Site;
|
import com.ruoyi.chargingPile.api.model.TChargingGun;
|
import com.ruoyi.common.core.web.page.PageInfo;
|
import com.ruoyi.common.security.service.TokenService;
|
import com.ruoyi.integration.api.model.UploadRealTimeMonitoringData;
|
import com.ruoyi.order.api.model.TVinChargingOrder;
|
import com.ruoyi.order.api.query.VinChargingOrderQuery;
|
import com.ruoyi.order.api.vo.ChargingOrderVO;
|
import com.ruoyi.order.mapper.TVinChargingOrderMapper;
|
import com.ruoyi.order.service.TVinChargingOrderService;
|
import com.ruoyi.order.util.mongodb.service.UploadRealTimeMonitoringDataService;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.time.temporal.ChronoUnit;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.Optional;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class TVinChargingOrderServiceImpl extends ServiceImpl<TVinChargingOrderMapper, TVinChargingOrder>
|
implements TVinChargingOrderService {
|
|
@Resource
|
private TokenService tokenService;
|
@Resource
|
private SiteClient siteClient;
|
@Resource
|
private ChargingGunClient chargingGunClient;
|
@Resource
|
private UploadRealTimeMonitoringDataService monitoringDataService;
|
|
@Override
|
public PageInfo<ChargingOrderVO> pageList(VinChargingOrderQuery query) {
|
int pageCurr = query.getPageCurr() == null || query.getPageCurr() < 1 ? 1 : query.getPageCurr();
|
int pageSize = query.getPageSize() == null || query.getPageSize() < 1 ? 50 : query.getPageSize();
|
setAuthSiteIds(query);
|
PageInfo<ChargingOrderVO> page = new PageInfo<>(pageCurr, pageSize);
|
long total = baseMapper.count(query);
|
page.setTotal(total);
|
List<ChargingOrderVO> records = total == 0 ? Collections.emptyList()
|
: baseMapper.pageList(query, (long) (pageCurr - 1) * pageSize, pageSize);
|
fillExtra(records);
|
page.setRecords(records);
|
return page;
|
}
|
|
@Override
|
public List<ChargingOrderVO> exportList(VinChargingOrderQuery query) {
|
setAuthSiteIds(query);
|
long total = baseMapper.count(query);
|
List<ChargingOrderVO> result = new ArrayList<>();
|
for (long offset = 0; offset < total; offset += 5000) {
|
List<ChargingOrderVO> batch = baseMapper.pageList(query, offset, 5000);
|
if (batch == null || batch.isEmpty()) {
|
break;
|
}
|
result.addAll(batch);
|
}
|
fillExtra(result);
|
return result;
|
}
|
|
@Override
|
public Map<String, Object> summary(VinChargingOrderQuery query) {
|
setAuthSiteIds(query);
|
Map<String, Object> result = baseMapper.summary(query);
|
return result == null ? new HashMap<>() : result;
|
}
|
|
@Override
|
public Map<String, Object> detail(Long chargingOrderId) {
|
TVinChargingOrder order = getAuthorizedOrder(chargingOrderId);
|
if (order == null) {
|
return Collections.emptyMap();
|
}
|
Map<String, Object> result = new HashMap<>();
|
result.put("id", order.getId());
|
result.put("code", order.getCode());
|
result.put("vin", order.getVin());
|
result.put("actualVin", order.getActualVin());
|
result.put("expectedVin", order.getExpectedVin());
|
result.put("plateNum", order.getPlateNum());
|
result.put("phone", order.getPhone());
|
result.put("status", order.getStatus());
|
result.put("siteId", order.getSiteId());
|
result.put("chargingPileId", order.getChargingPileId());
|
result.put("chargingGunId", order.getChargingGunId());
|
result.put("startTime", order.getStartTime());
|
result.put("endTime", order.getEndTime());
|
result.put("chargingCapacity", order.getChargingCapacity());
|
result.put("paymentAmount", order.getPaymentAmount());
|
result.put("electrovalence", order.getElectrovalence());
|
result.put("serviceCharge", order.getServiceCharge());
|
List<UploadRealTimeMonitoringData> monitoring = monitoringDataService.getDataByOrderCodeAsc(order.getCode());
|
result.put("list", monitoring == null ? Collections.emptyList() : monitoring);
|
result.put("list1", baseMapper.accountingStrategyList(order.getId()));
|
return result;
|
}
|
|
@Override
|
public List<Map<String, Object>> accountingStrategyList(Long chargingOrderId) {
|
TVinChargingOrder order = getAuthorizedOrder(chargingOrderId);
|
return order == null ? Collections.emptyList() : baseMapper.accountingStrategyList(chargingOrderId);
|
}
|
|
private void setAuthSiteIds(VinChargingOrderQuery query) {
|
List<Integer> authorizedSiteIds = getAuthorizedSiteIds();
|
if (query.getSiteId() != null) {
|
authorizedSiteIds = authorizedSiteIds.contains(query.getSiteId())
|
? Collections.singletonList(query.getSiteId()) : Collections.singletonList(-1);
|
}
|
if (authorizedSiteIds.isEmpty()) {
|
authorizedSiteIds = Collections.singletonList(-1);
|
}
|
query.setSiteIds(authorizedSiteIds);
|
}
|
|
private TVinChargingOrder getAuthorizedOrder(Long chargingOrderId) {
|
if (chargingOrderId == null) {
|
return null;
|
}
|
TVinChargingOrder order = getById(chargingOrderId);
|
if (order == null || order.getSiteId() == null || !getAuthorizedSiteIds().contains(order.getSiteId())) {
|
return null;
|
}
|
return order;
|
}
|
|
private List<Integer> getAuthorizedSiteIds() {
|
Long userId = tokenService.getLoginUser().getUserid();
|
if (userId == null) {
|
return Collections.emptyList();
|
}
|
return Optional.ofNullable(siteClient.getSiteListByUserId(userId).getData())
|
.orElse(Collections.emptyList()).stream().map(GetSiteListDTO::getId).collect(Collectors.toList());
|
}
|
|
private void fillExtra(List<ChargingOrderVO> list) {
|
if (list == null || list.isEmpty()) {
|
return;
|
}
|
List<Long> orderIds = list.stream().map(ChargingOrderVO::getId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
Map<Long, Integer> periodCounts = new HashMap<>();
|
if (!orderIds.isEmpty()) {
|
for (Map<String, Object> item : baseMapper.countAccountingStrategies(orderIds)) {
|
Number chargingOrderId = (Number) item.get("chargingOrderId");
|
Number periodCount = (Number) item.get("periodCount");
|
if (chargingOrderId != null && periodCount != null) {
|
periodCounts.put(chargingOrderId.longValue(), periodCount.intValue());
|
}
|
}
|
}
|
List<Integer> siteIds = list.stream().map(ChargingOrderVO::getSiteId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
List<Integer> gunIds = list.stream().map(ChargingOrderVO::getChargingGunId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
Map<Integer, Site> sites = siteIds.isEmpty() ? Collections.emptyMap()
|
: Optional.ofNullable(siteClient.getSiteByIds(siteIds).getData()).orElse(Collections.emptyList()).stream()
|
.collect(Collectors.toMap(Site::getId, item -> item, (a, b) -> a));
|
Map<Integer, TChargingGun> guns = gunIds.isEmpty() ? Collections.emptyMap()
|
: Optional.ofNullable(chargingGunClient.getChargingGunByIds(gunIds).getData()).orElse(Collections.emptyList()).stream()
|
.collect(Collectors.toMap(TChargingGun::getId, item -> item, (a, b) -> a));
|
for (ChargingOrderVO item : list) {
|
Site site = sites.get(item.getSiteId());
|
item.setSiteName(site == null ? null : site.getName());
|
TChargingGun gun = guns.get(item.getChargingGunId());
|
item.setTerminalName(gun == null ? null : gun.getName());
|
if (item.getStartTime() != null && item.getEndTime() != null) {
|
item.setChargingSecond(ChronoUnit.SECONDS.between(item.getStartTime(), item.getEndTime()));
|
}
|
item.setCount(periodCounts.getOrDefault(item.getId(), 0));
|
item.setPlatformIncome(BigDecimal.ZERO);
|
item.setPartnerIncome(BigDecimal.ZERO);
|
}
|
}
|
}
|