package com.ruoyi.production.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.production.dto.DaiDto; import com.ruoyi.production.dto.SalesLedgerSchedulingDto; import com.ruoyi.production.mapper.ProductionOrderMapper; import com.ruoyi.production.mapper.SalesLedgerSchedulingMapper; import com.ruoyi.production.mapper.SalesLedgerWorkMapper; import com.ruoyi.production.pojo.ProductionOrder; import com.ruoyi.production.pojo.SalesLedgerScheduling; import com.ruoyi.production.pojo.SalesLedgerWork; import com.ruoyi.production.service.ProductionOrderService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.stream.Collectors; /** * @author :yys * @date : 2025/11/26 14:20 */ @Service @Slf4j public class ProductionOrderServiceImpl extends ServiceImpl implements ProductionOrderService { @Autowired private ProductionOrderMapper productionOrderMapper; @Autowired private SalesLedgerWorkMapper salesLedgerWorkMapper; @Autowired private SalesLedgerSchedulingMapper salesLedgerSchedulingMapper; @Override public AjaxResult listPage(Page page, ProductionOrder productionOrder) { LambdaQueryWrapper productionOrderLambdaQueryWrapper = new LambdaQueryWrapper<>(); if(productionOrder != null){ if(StringUtils.isNotEmpty(productionOrder.getOrderNo())){ productionOrderLambdaQueryWrapper.like(ProductionOrder::getOrderNo, productionOrder.getOrderNo()); } if(StringUtils.isNotEmpty(productionOrder.getProductCategory())){ productionOrderLambdaQueryWrapper.like(ProductionOrder::getProductCategory, productionOrder.getProductCategory()); } if(StringUtils.isNotEmpty(productionOrder.getEntryDateStart()) && StringUtils.isNotEmpty(productionOrder.getEntryDateEnd())){ productionOrderLambdaQueryWrapper.ge(ProductionOrder::getRegisterDate, productionOrder.getEntryDateStart()) .le(ProductionOrder::getRegisterDate, productionOrder.getEntryDateEnd()); } } IPage list = productionOrderMapper.selectPage(page,productionOrderLambdaQueryWrapper); if(CollectionUtils.isEmpty(list.getRecords())){ return AjaxResult.success(list); } Set collect = list.getRecords().stream().map(ProductionOrder::getId).collect(Collectors.toSet()); // 获取排产数量 LambdaQueryWrapper salesLedgerSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<>(); salesLedgerSchedulingLambdaQueryWrapper.in(SalesLedgerScheduling::getSalesLedgerProductId, collect); List salesLedgerSchedulings = salesLedgerSchedulingMapper.selectList(salesLedgerSchedulingLambdaQueryWrapper); // 计算完工数量 LambdaQueryWrapper salesLedgerWorkLambdaQueryWrapper = new LambdaQueryWrapper<>(); salesLedgerWorkLambdaQueryWrapper.in(SalesLedgerWork::getSalesLedgerProductId, collect) .ne(SalesLedgerWork::getStatus, 1); List salesLedgerWorks = salesLedgerWorkMapper.selectList(salesLedgerWorkLambdaQueryWrapper); list.getRecords().forEach(i -> { // 获取排产数量 i.setSchedulingNum(salesLedgerSchedulings .stream() .filter(j -> j.getSalesLedgerProductId().equals(i.getId())) .map(SalesLedgerScheduling::getSchedulingNum) .reduce(BigDecimal.ZERO, BigDecimal::add)); // 获取完成数量 i.setSuccessNum(salesLedgerWorks .stream() .filter(j -> j.getSalesLedgerProductId().equals(i.getId())) .map(SalesLedgerWork::getFinishedNum) .reduce(BigDecimal.ZERO, BigDecimal::add)); // 状态 = 数量和完工数量比较 if(i.getSchedulingNum().compareTo(i.getSuccessNum()) == 0){ i.setStatus("已完成"); }else{ i.setStatus("未完成"); } }); return AjaxResult.success(list); } @Override public void export(HttpServletResponse response) { List list = this.list(); if(CollectionUtils.isEmpty(list)){ throw new RuntimeException("无导出数据"); } Set collect = list.stream().map(ProductionOrder::getId).collect(Collectors.toSet()); LambdaQueryWrapper salesLedgerWorkLambdaQueryWrapper = new LambdaQueryWrapper<>(); salesLedgerWorkLambdaQueryWrapper.in(SalesLedgerWork::getSalesLedgerProductId, collect) .ne(SalesLedgerWork::getStatus, 1); List salesLedgerWorks = salesLedgerWorkMapper.selectList(salesLedgerWorkLambdaQueryWrapper); list.forEach(i -> { // 获取完成数量 i.setSuccessNum(salesLedgerWorks .stream() .filter(j -> j.getSalesLedgerProductId().equals(i.getId())) .map(SalesLedgerWork::getFinishedNum) .reduce(BigDecimal.ZERO, BigDecimal::add)); // 状态 = 数量和完工数量比较 if(i.getQuantity().compareTo(i.getSuccessNum()) == 0){ i.setStatus("已完成"); }else{ i.setStatus("未完成"); } }); ExcelUtil util = new ExcelUtil<>(ProductionOrder.class); util.exportExcel(response, list, "生产订单"); } @Override public void exportOne(HttpServletResponse response) { List list = this.list(); if(CollectionUtils.isEmpty(list)){ throw new RuntimeException("无导出数据"); } List dais = new ArrayList<>(); list.forEach(i -> { DaiDto daiDto = new DaiDto(); BeanUtils.copyProperties(i, daiDto); // 获取待排产数量 daiDto.setDaiNum(daiDto.getQuantity().subtract(i.getSuccessNum())); dais.add(daiDto); }); ExcelUtil util = new ExcelUtil<>(DaiDto.class); util.exportExcel(response, dais, "生产派工"); } }