package com.ruoyi.production.service.impl; import cn.hutool.core.util.NumberUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; 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.SalesLedgerWorkDto; 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.apache.commons.lang3.ObjectUtils; 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.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.*; 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, "生产派工"); } @Override public Map reportAnalysis(String startDate, String endDate) { /* * production_order 新增订单表 * sales_ledger_scheduling 生产派工表 * sales_ledger_work 工序排产 * sales_ledger_production_accounting 生产报工 * */ Map map = new HashMap<>(); //汇总信息 Map totalMap = new HashMap<>(); LocalDateTime startTime = null; LocalDateTime endTime = null; if(ObjectUtils.anyNotNull(startDate,endDate)){ startTime = LocalDateTime.of(LocalDate.parse(startDate), LocalTime.MIN); endTime = LocalDateTime.of(LocalDate.parse(endDate), LocalTime.MAX); } LambdaQueryWrapper betweenWrapper = Wrappers.lambdaQuery().between(ObjectUtils.anyNotNull(startTime, endTime), ProductionOrder::getCreateTime, startTime, endTime); totalMap.put("totalOrderNum",productionOrderMapper.selectCount(betweenWrapper)); totalMap.put("tobeProduced", salesLedgerSchedulingMapper.selectTobeProduced(startTime,endTime)); totalMap.put("finishProduced",productionOrderMapper.selectFinishProduced(startTime,endTime)); totalMap.put("totalWorkhours",productionOrderMapper.selectTotalWorkhours(startTime,endTime)); map.put("totalData",totalMap); //生产报工统计 map.put("productData",productionOrderMapper.selectProductData(startTime,endTime)); //生产核算统计 map.put("workHours",productionOrderMapper.selectWorkhoursReport(startTime,endTime)); //生产报工详细 SalesLedgerWorkDto salesLedgerWorkDto = new SalesLedgerWorkDto(); salesLedgerWorkDto.setEntryDateStart(startDate); salesLedgerWorkDto.setEntryDateEnd(endDate); map.put("productDetails",salesLedgerWorkMapper.listPage(new Page<>(-1,-1), salesLedgerWorkDto).getRecords()); return map; } }