| | |
| | | package com.ruoyi.order.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient; |
| | | import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient; |
| | | import com.ruoyi.chargingPile.api.feignClient.SiteClient; |
| | | import com.ruoyi.chargingPile.api.dto.GetSiteListDTO; |
| | | 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.order.api.model.TCardChargingOrder; |
| | | import com.ruoyi.order.api.query.CardChargingOrderQuery; |
| | | import com.ruoyi.order.api.vo.ChargingOrderVO; |
| | | import com.ruoyi.order.mapper.TCardChargingOrderMapper; |
| | | import com.ruoyi.order.service.TCardChargingOrderService; |
| | | import com.ruoyi.chargingPile.api.model.Site; |
| | | 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 TCardChargingOrderServiceImpl extends ServiceImpl<TCardChargingOrderMapper, TCardChargingOrder> |
| | | implements TCardChargingOrderService { |
| | | |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private SiteClient siteClient; |
| | | @Resource |
| | | private ChargingPileClient chargingPileClient; |
| | | @Resource |
| | | private ChargingGunClient chargingGunClient; |
| | | |
| | | @Override |
| | | public IPage<Map<String, Object>> pageList(CardChargingOrderQuery query) { |
| | | Page<Map<String, Object>> page = new Page<>(query.getPageCurr(), query.getPageSize()); |
| | | return baseMapper.pageList(page, query); |
| | | public PageInfo<ChargingOrderVO> pageList(CardChargingOrderQuery query) { |
| | | String[] timeRange = parseTimeRange(query); |
| | | int pageCurr = query.getPageCurr() == null || query.getPageCurr() < 1 ? 1 : query.getPageCurr(); |
| | | int pageSize = query.getPageSize() == null || query.getPageSize() < 1 ? 50 : query.getPageSize(); |
| | | PageInfo<ChargingOrderVO> pageInfo = new PageInfo<>(pageCurr, pageSize); |
| | | setAuthSiteIds(query); |
| | | long offset = (long) (pageCurr - 1) * pageSize; |
| | | long total = baseMapper.count(query, timeRange[0], timeRange[1], timeRange[2], timeRange[3]); |
| | | pageInfo.setTotal(total); |
| | | List<ChargingOrderVO> list = total == 0 ? Collections.emptyList() |
| | | : baseMapper.pageList(query, timeRange[0], timeRange[1], timeRange[2], timeRange[3], offset, pageSize); |
| | | fillPageExtra(list); |
| | | pageInfo.setRecords(list); |
| | | return pageInfo; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> summary(CardChargingOrderQuery query) { |
| | | return baseMapper.summary(query); |
| | | String[] timeRange = parseTimeRange(query); |
| | | setAuthSiteIds(query); |
| | | Map<String, Object> result = baseMapper.summary(query, timeRange[0], timeRange[1], timeRange[2], timeRange[3]); |
| | | if (result == null) { |
| | | result = new HashMap<>(); |
| | | } |
| | | result.putIfAbsent("platformIncome", BigDecimal.ZERO); |
| | | result.putIfAbsent("partnerIncome", BigDecimal.ZERO); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | |
| | | public List<Map<String, Object>> accountingStrategyList(Long chargingOrderId) { |
| | | return baseMapper.accountingStrategyList(chargingOrderId); |
| | | } |
| | | |
| | | private void setAuthSiteIds(CardChargingOrderQuery query) { |
| | | List<Integer> siteIds = new ArrayList<>(); |
| | | if (query.getSiteId() != null) { |
| | | siteIds.add(query.getSiteId()); |
| | | } else { |
| | | Long userId = tokenService.getLoginUser().getUserid(); |
| | | if (userId != null) { |
| | | siteIds = Optional.ofNullable(siteClient.getSiteListByUserId(userId).getData()).orElse(Collections.emptyList()) |
| | | .stream().map(GetSiteListDTO::getId).collect(Collectors.toList()); |
| | | } |
| | | } |
| | | if (siteIds.isEmpty()) { |
| | | siteIds.add(-1); |
| | | } |
| | | query.setSiteIds(siteIds); |
| | | } |
| | | |
| | | private void fillPageExtra(List<ChargingOrderVO> list) { |
| | | if (list == null || list.isEmpty()) { |
| | | return; |
| | | } |
| | | 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 = 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.setPlatformIncome(BigDecimal.ZERO); |
| | | item.setPartnerIncome(BigDecimal.ZERO); |
| | | } |
| | | } |
| | | |
| | | private String[] parseTimeRange(CardChargingOrderQuery query) { |
| | | return new String[]{query.getStartTime1(), query.getStartTime2(), query.getEndTime1(), query.getEndTime2()}; |
| | | } |
| | | } |