package com.ruoyi.tms.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.annotation.Excel; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.page.PageDomain; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.service.BaseService; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.file.DownloadExportUtil; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.tms.domain.*; import com.ruoyi.tms.mapper.*; import com.ruoyi.tms.service.ITmsDispatchFeeSummaryViService; import com.ruoyi.tms.service.ITmsDispatchOrderService; import com.ruoyi.tms.service.ITmsReceivableFeeService; import com.ruoyi.tms.service.ITmsTripService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) public class TmsDispatchFeeSummaryViServiceImpl extends BaseService implements ITmsDispatchFeeSummaryViService { @Autowired private RedisCache redisCache; @Resource private TmsReceivableFeeItemMapper tmsReceivableFeeItemMapper; @Resource private TmsPayableFeeItemMapper tmsPayableFeeItemMapper; @Resource private TmsDispatchOrderMapper tmsDispatchOrderMapper; @Resource private TmsDispatchFeeSummaryViLogMapper logMapper; @Override public Page list(PageDomain pageDomain,TmsDispatchFeeSummaryVi bo) { Page pageParam = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (StringUtils.isNotEmpty(bo.getOrderTimeBegin()) && StringUtils.isNotEmpty(bo.getOrderTimeEnd())){ wrapper.between(TmsDispatchFeeSummaryVi::getOrderTime, bo.getOrderTimeBegin(), bo.getOrderTimeEnd()); } wrapper.like(StringUtils.isNotEmpty(bo.getDispatchNo()),TmsDispatchFeeSummaryVi::getDispatchNo, bo.getDispatchNo()); wrapper.like(StringUtils.isNotEmpty(bo.getCustomerName()),TmsDispatchFeeSummaryVi::getCustomerName, bo.getCustomerName()); wrapper.like(StringUtils.isNotEmpty(bo.getProjectName()),TmsDispatchFeeSummaryVi::getProjectName, bo.getProjectName()); wrapper.like(StringUtils.isNotEmpty(bo.getAddress()),TmsDispatchFeeSummaryVi::getShipperRegionLabel, bo.getAddress()); wrapper.like(StringUtils.isNotEmpty(bo.getAddress()),TmsDispatchFeeSummaryVi::getShipperAddress, bo.getAddress()); wrapper.like(StringUtils.isNotEmpty(bo.getAddress()),TmsDispatchFeeSummaryVi::getReceiverAddress, bo.getAddress()); wrapper.like(StringUtils.isNotEmpty(bo.getAddress()),TmsDispatchFeeSummaryVi::getReceiverRegionLabel, bo.getAddress()); wrapper.like(StringUtils.isNotEmpty(bo.getProviderName()),TmsDispatchFeeSummaryVi::getProviderName, bo.getProviderName()); wrapper.like(StringUtils.isNotEmpty(bo.getCustomsServiceProviderName()),TmsDispatchFeeSummaryVi::getCustomsServiceProviderName, bo.getCustomsServiceProviderName()); wrapper.like(StringUtils.isNotEmpty(bo.getLoadingServiceProviderName()),TmsDispatchFeeSummaryVi::getLoadingServiceProviderName, bo.getLoadingServiceProviderName()); wrapper.eq(StringUtils.isNotEmpty(bo.getRequiredVehicleTypes()),TmsDispatchFeeSummaryVi::getRequiredVehicleTypes, bo.getRequiredVehicleTypes()); wrapper.eq(bo.getStatus() != null,TmsDispatchFeeSummaryVi::getStatus, bo.getStatus()); wrapper.eq(StringUtils.isNotEmpty(bo.getActualVehicleType()),TmsDispatchFeeSummaryVi::getActualVehicleType, bo.getActualVehicleType()); wrapper.notIn(TmsDispatchFeeSummaryVi::getStatus, 0,5); wrapper.orderByDesc(TmsDispatchFeeSummaryVi::getDispatchId); pageParam = baseMapper.selectPage(pageParam,wrapper); List records = pageParam.getRecords(); records.forEach(item -> { BigDecimal rTotalRmb = item.getRShippingFeeRmb() .add(item.getRServerFeeRmb()) .add(item.getRActualFeeRmb()); BigDecimal rTotalHkd = item.getRShippingFeeHkd() .add(item.getRServerFeeHkd()) .add(item.getRActualFeeHkd()); item.setRTotalRmb(rTotalRmb); item.setRTotalHkd(rTotalHkd); BigDecimal pTotalRmb = item.getPShippingFeeRmb() .add(item.getPServerFeeRmb()) .add(item.getPActualFeeRmb()); BigDecimal pTotalHkd = item.getPShippingFeeHkd() .add(item.getPServerFeeHkd()) .add(item.getPActualFeeHkd()); item.setPTotalRmb(pTotalRmb); item.setPTotalHkd(pTotalHkd); item.setGrossProfitRmb(item.getRTotalRmb().subtract(item.getPTotalRmb())); item.setGrossProfitHkd(item.getRTotalHkd().subtract(item.getPTotalHkd())); if (item.getRTotalRmb() != null && item.getRTotalRmb().compareTo(BigDecimal.ZERO) != 0) { item.setGrossProfitRadioRmb(item.getGrossProfitRmb() .divide(item.getRTotalRmb(), 4, RoundingMode.HALF_UP)); } else { item.setGrossProfitRadioRmb(BigDecimal.ZERO); // 设置默认值 } if (item.getRTotalHkd() != null && item.getRTotalHkd().compareTo(BigDecimal.ZERO) != 0) { item.setGrossProfitRadioHkd(item.getGrossProfitHkd() .divide(item.getRTotalHkd(), 4, RoundingMode.HALF_UP)); } else { item.setGrossProfitRadioHkd(BigDecimal.ZERO); // 设置默认值 } }); pageParam.setRecords( records); return pageParam; } @Async @Override public void export(TmsDispatchFeeSummaryVi vi, String exportKey) { String fileName = ExcelUtil.encodeFileName("派车台账"+exportKey); // 设置当前任务为“下载中”状态 DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, "0"); // 只创建一次 ExcelUtil ExcelUtil excelUtil = new ExcelUtil<>(TmsDispatchFeeSummaryVi.class); excelUtil.initialize("派车台账", null, Excel.Type.EXPORT); Set dispatchNoSet = new HashSet<>(); int pageNum = 1; while (true) { List records = list(new PageDomain(pageNum, Constants.EXPORT_PATE_SIZE), vi).getRecords(); if (records == null || records.isEmpty()) { break; } // 导出当前页的数据 excelUtil.exportExcel(records); dispatchNoSet.addAll( records.stream() .map(TmsDispatchFeeSummaryVi::getDispatchNo) .collect(Collectors.toSet()) ); pageNum++; } //切换 Sheet:应收明细 exportReceivableFeeSheet(excelUtil, dispatchNoSet); //切换 Sheet:应付明细 exportPayableFeeSheet(excelUtil, dispatchNoSet); excelUtil.finishExport(fileName); DownloadExportUtil.setDownloadFile(redisCache, exportKey, fileName); } private void exportReceivableFeeSheet( ExcelUtil excelUtil, Set dispatchNos ) { // 切换到第二个 Sheet ExcelUtil receivableExcel = excelUtil.switchSheet(1, "应收明细" , TmsReceivableFeeItem.class); List list = tmsReceivableFeeItemMapper.selectTmsReceivableFeeItemByDispatchNo(dispatchNos); if (list != null && !list.isEmpty()) { receivableExcel.exportExcel(list); } } private void exportPayableFeeSheet( ExcelUtil excelUtil, Set dispatchNos ) { // 切换到第二个 Sheet ExcelUtil receivableExcel = excelUtil.switchSheet(2, "应付明细" , TmsPayableFeeItem.class); List list = tmsPayableFeeItemMapper.selectTmsPayableFeeItemByDispatchNo(dispatchNos); if (list != null && !list.isEmpty()) { receivableExcel.exportExcel(list); } } @Override public int note(TmsDispatchFeeSummaryVi bo) { tmsDispatchOrderMapper.updateTmsDispatchOrder(new TmsDispatchOrder(){{setId(bo.getDispatchId().intValue()); setNotes(bo.getRemark());}}); TmsDispatchFeeSummaryViLog tmsDispatchFeeSummaryViLog = new TmsDispatchFeeSummaryViLog(); tmsDispatchFeeSummaryViLog.setNotes(bo.getRemark()); tmsDispatchFeeSummaryViLog.setHeadId(bo.getDispatchId().intValue()); tmsDispatchFeeSummaryViLog.setCreateBy(SecurityUtils.getUsername()); tmsDispatchFeeSummaryViLog.setCreateTime(new Date()); return logMapper.insertTmsDispatchFeeSummaryViLog(tmsDispatchFeeSummaryViLog); } }