zouyu
2026-01-16 f605b84620bf35bb02a2ee5ef2086c164b520e67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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<ProductionOrderMapper, ProductionOrder> implements ProductionOrderService {
 
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
 
    @Autowired
    private SalesLedgerWorkMapper salesLedgerWorkMapper;
 
    @Autowired
    private SalesLedgerSchedulingMapper salesLedgerSchedulingMapper;
 
    @Override
    public AjaxResult listPage(Page page, ProductionOrder productionOrder) {
        LambdaQueryWrapper<ProductionOrder> 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<ProductionOrder> list = productionOrderMapper.selectPage(page,productionOrderLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(list.getRecords())){
            return AjaxResult.success(list);
        }
        Set<Long> collect = list.getRecords().stream().map(ProductionOrder::getId).collect(Collectors.toSet());
        // 获取排产数量
        LambdaQueryWrapper<SalesLedgerScheduling> salesLedgerSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerSchedulingLambdaQueryWrapper.in(SalesLedgerScheduling::getSalesLedgerProductId, collect);
        List<SalesLedgerScheduling> salesLedgerSchedulings = salesLedgerSchedulingMapper.selectList(salesLedgerSchedulingLambdaQueryWrapper);
        // 计算完工数量
        LambdaQueryWrapper<SalesLedgerWork> salesLedgerWorkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerWorkLambdaQueryWrapper.in(SalesLedgerWork::getSalesLedgerProductId, collect)
                .ne(SalesLedgerWork::getStatus, 1);
        List<SalesLedgerWork> 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<ProductionOrder> list = this.list();
        if(CollectionUtils.isEmpty(list)){
            throw new RuntimeException("无导出数据");
        }
        Set<Long> collect = list.stream().map(ProductionOrder::getId).collect(Collectors.toSet());
        LambdaQueryWrapper<SalesLedgerWork> salesLedgerWorkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerWorkLambdaQueryWrapper.in(SalesLedgerWork::getSalesLedgerProductId, collect)
                .ne(SalesLedgerWork::getStatus, 1);
        List<SalesLedgerWork> 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<ProductionOrder> util = new ExcelUtil<>(ProductionOrder.class);
        util.exportExcel(response, list, "生产订单");
    }
 
    @Override
    public void exportOne(HttpServletResponse response) {
        List<ProductionOrder> list = this.list();
        if(CollectionUtils.isEmpty(list)){
            throw new RuntimeException("无导出数据");
        }
        List<DaiDto> 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<DaiDto> util = new ExcelUtil<>(DaiDto.class);
        util.exportExcel(response, dais, "生产派工");
    }
 
    @Override
    public Map<String, Object> reportAnalysis(String startDate, String endDate) {
        /*
        * production_order                    新增订单表
        * sales_ledger_scheduling             生产派工表
        * sales_ledger_work                   工序排产
        * sales_ledger_production_accounting  生产报工
        * */
        Map<String, Object> map = new HashMap<>();
        //汇总信息
        Map<String, Object> 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<ProductionOrder> betweenWrapper = Wrappers.<ProductionOrder>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;
 
    }
 
}