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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.production.dto.*;
import com.ruoyi.production.mapper.SalesLedgerSchedulingMapper;
import com.ruoyi.production.mapper.SalesLedgerWorkMapper;
import com.ruoyi.production.mapper.SpeculativeTradingInfoMapper;
import com.ruoyi.production.pojo.SalesLedgerScheduling;
import com.ruoyi.production.pojo.SalesLedgerWork;
import com.ruoyi.production.pojo.SpeculativeTradingInfo;
import com.ruoyi.production.service.SalesLedgerSchedulingService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.sales.mapper.LossMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.Loss;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/7/21 14:41
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SalesLedgerSchedulingServiceImpl extends ServiceImpl<SalesLedgerSchedulingMapper, SalesLedgerScheduling> implements SalesLedgerSchedulingService {
 
    private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper;
 
    private final SalesLedgerWorkMapper salesLedgerWorkMapper;
 
    @Override
    public IPage<SalesLedgerSchedulingDto> listPage(Page page, SalesLedgerSchedulingDto salesLedgerSchedulingDto) {
        IPage<SalesLedgerSchedulingDto> list = salesLedgerSchedulingMapper.listPage(page, salesLedgerSchedulingDto);
        if(list.getTotal() == 0){
            return list;
        }
        Set<Long> collect = list.getRecords().stream().map(SalesLedgerSchedulingDto::getSalesLedgerProductId).collect(Collectors.toSet());
        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.setSuccessNum(salesLedgerWorks
                    .stream()
                    .filter(j -> j.getSalesLedgerProductId().equals(i.getSalesLedgerProductId()))
                    .map(SalesLedgerWork::getFinishedNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            // 状态 = 数量和完工数量比较
            if(i.getSchedulingNum().compareTo(new BigDecimal(0)) == 0){
                i.setStatus("未完成");
            } else if(i.getSchedulingNum().compareTo(i.getSuccessNum()) == 0){
                i.setStatus("已完成");
            }else{
                i.setStatus("生产中");
            }
            // 计算生产总量 = 规格 * 数量 / 1000
            String[] split = i.getSpecificationModel().split("\\*");
            if(split.length == 2){
                BigDecimal multiply = new BigDecimal(split[0])
                        .multiply(new BigDecimal(split[1])
                                .multiply(i.getQuantity()).divide(new BigDecimal(1000),2, RoundingMode.CEILING));
                i.setTotalProduction(multiply);
            }
 
        });
        return list;
    }
 
    @Override
    public void export(HttpServletResponse response) {
        List<SalesLedgerSchedulingDto> list = salesLedgerSchedulingMapper.list();
        if(CollectionUtils.isEmpty(list)){
            throw new RuntimeException("无导出数据");
        }
        Set<Long> collect = list.stream().map(SalesLedgerSchedulingDto::getSalesLedgerProductId).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.getSalesLedgerProductId()))
                    .map(SalesLedgerWork::getFinishedNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
        });
        ExcelUtil<SalesLedgerSchedulingDto> util = new ExcelUtil<>(SalesLedgerSchedulingDto.class);
        util.exportExcel(response, list, "生产订单");
    }
 
    private final SysUserMapper sysUserMapper;
 
    private final SpeculativeTradingInfoMapper speculativeTradingInfoMapper;
 
    @Override
    public String productionDispatch(List<ProductionDispatchAddDto> productionDispatchAddDtoList) {
        int i = 0;
        int successNum = 0;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (ProductionDispatchAddDto productionDispatchAddDto : productionDispatchAddDtoList) {
            SysUser sysUser = sysUserMapper.selectUserById(productionDispatchAddDto.getSchedulingUserId() == null ? loginUser.getUser().getUserId() : productionDispatchAddDto.getSchedulingUserId());
            if(sysUser == null){
                i++;
                continue;
            }
            // 获取空余炒机
            String[] split = productionDispatchAddDto.getSpeculativeTradingName().split(",");
            if(split != null && split.length == 0){
                i++;
                continue;
            }
            List<SpeculativeTradingInfo> speculativeTradingInfos = speculativeTradingInfoMapper.selectList(new LambdaQueryWrapper<SpeculativeTradingInfo>()
                    .in(SpeculativeTradingInfo::getName, Arrays.asList(split))
                    .orderByAsc(SpeculativeTradingInfo::getSort));
            if(CollectionUtils.isEmpty(speculativeTradingInfos)){
                i++;
                continue;
            }
            AtomicReference<String> name = new AtomicReference<>("");  //需要绑定的炒机
            //通过规格型号和排产数量计算本次生产产量
            String[] split1 = productionDispatchAddDto.getSpecificationModel().split("\\*");
            if(split1.length != 2){
                i++;
                continue;
            }
            // 本次生产产量
            BigDecimal productionNum = new BigDecimal(split1[0])
                    .multiply(new BigDecimal(split1[1]).multiply(productionDispatchAddDto.getSchedulingNum()));
            // 多个炒机情况
            if(speculativeTradingInfos.size() > 1){
                for (SpeculativeTradingInfo speculativeTradingInfo : speculativeTradingInfos) {
                    // 获取该炒机正在排产量
                    BigDecimal schedulingNumBySpeculativeTradingName = getSchedulingNumBySpeculativeTradingName(speculativeTradingInfo.getName());
                    // 如果该炒机总量(单位kg需要乘1000) - 正在排产量 >=本次生产产量就分配此炒机
                    if(speculativeTradingInfo.getWorkLoad().multiply(new BigDecimal(1000)).subtract(schedulingNumBySpeculativeTradingName).compareTo(productionNum) >= 0){
                        name.set(speculativeTradingInfo.getName());
                        break;
                    }
                }
            }else{
                // 单个炒机情况
                name.set(speculativeTradingInfos.get(0).getName());
            }
            if(name.get().isEmpty()){
                i++;
                continue;
            }
            SalesLedgerScheduling salesLedgerScheduling = SalesLedgerScheduling.builder()
                    .salesLedgerId(productionDispatchAddDto.getSalesLedgerId())
                    .salesLedgerProductId(productionDispatchAddDto.getSalesLedgerProductId())
                    .speculativeTradingName(name.get())
                    .schedulingUserId(sysUser.getUserId())
                    .schedulingUserName(sysUser.getNickName())
                    .schedulingNum(productionDispatchAddDto.getSchedulingNum())
                    .schedulingDate(productionDispatchAddDto.getSchedulingDate() == null ? LocalDate.now() : LocalDate.parse(productionDispatchAddDto.getSchedulingDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                    .status(1)
                    .build();
            salesLedgerSchedulingMapper.insert(salesLedgerScheduling);
            successNum++;
        }
 
        return "派工成功数量" + successNum + ",失败数量" + i;
    }
 
    private final SalesLedgerProductMapper salesLedgerProductMapper;
 
    /**
     *通过炒机名称获取当天正在排产量
     * @return
     */
    public BigDecimal getSchedulingNumBySpeculativeTradingName(String speculativeTradingName){
        LambdaQueryWrapper<SalesLedgerScheduling> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SalesLedgerScheduling::getSpeculativeTradingName, speculativeTradingName)
                .eq(SalesLedgerScheduling::getSchedulingDate, LocalDate.now());
        List<SalesLedgerScheduling> salesLedgerSchedulings = salesLedgerSchedulingMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(salesLedgerSchedulings)){
            return BigDecimal.ZERO;
        }
        List<Long> collect = salesLedgerSchedulings.stream().map(SalesLedgerScheduling::getSalesLedgerProductId).collect(Collectors.toList());
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .in(SalesLedgerProduct::getId, collect));
        if(CollectionUtils.isEmpty(salesLedgerProducts)) return BigDecimal.ZERO;
        AtomicInteger totalNum = new AtomicInteger(0); //总数
        salesLedgerSchedulings.forEach(item ->{
            List<SalesLedgerProduct> collect1 = salesLedgerProducts.stream()
                    .filter(j -> j.getId().equals(item.getSalesLedgerProductId()))
                    .collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect1)){
                SalesLedgerProduct salesLedgerProduct = collect1.get(0);
                // 根据产品规格 * 排产数量 获取本次生产产量并累计
                String[] split = salesLedgerProduct.getSpecificationModel().split("\\*");
                BigDecimal productionNum = new BigDecimal(split[0])
                        .multiply(new BigDecimal(split[1]).multiply(item.getSchedulingNum()));
                totalNum.addAndGet(productionNum.intValue());
            }
        });
        // 需要 / 损耗率
        Loss loss = lossMapper.selectOne(new LambdaQueryWrapper<Loss>().last("limit 1"));
        BigDecimal lossNum = loss == null ? new BigDecimal(6) : loss.getRate(); //没有损耗率则默认为6
 
        return new BigDecimal(totalNum.get()).multiply(new BigDecimal(100)).divide(lossNum, 2,RoundingMode.HALF_UP);
    }
 
    private LossMapper lossMapper;
 
 
    /**
     *通过批量炒机名称获取当天正在排产量
     * @return
     */
    public BigDecimal getSchedulingNumBySpeculativeTradingNameList(List<String> speculativeTradingName){
        LambdaQueryWrapper<SalesLedgerScheduling> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SalesLedgerScheduling::getSpeculativeTradingName, speculativeTradingName)
                .eq(SalesLedgerScheduling::getSchedulingDate, LocalDate.now());
        List<SalesLedgerScheduling> salesLedgerSchedulings = salesLedgerSchedulingMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(salesLedgerSchedulings)){
            return BigDecimal.ZERO;
        }
        List<Long> collect = salesLedgerSchedulings.stream().map(SalesLedgerScheduling::getSalesLedgerProductId).collect(Collectors.toList());
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .in(SalesLedgerProduct::getId, collect));
        if(CollectionUtils.isEmpty(salesLedgerProducts)) return BigDecimal.ZERO;
        AtomicInteger totalNum = new AtomicInteger(0); //总数
        salesLedgerSchedulings.forEach(item ->{
            List<SalesLedgerProduct> collect1 = salesLedgerProducts.stream()
                    .filter(j -> j.getId().equals(item.getSalesLedgerProductId()))
                    .collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect1)){
                SalesLedgerProduct salesLedgerProduct = collect1.get(0);
                // 根据产品规格 * 排产数量 获取本次生产产量并累计
                String[] split = salesLedgerProduct.getSpecificationModel().split("\\*");
                BigDecimal productionNum = new BigDecimal(split[0])
                        .multiply(new BigDecimal(split[1]).multiply(item.getSchedulingNum()));
                totalNum.addAndGet(productionNum.intValue());
            }
        });
        return new BigDecimal(totalNum.get());
    }
 
    @Override
    public IPage<SalesLedgerSchedulingProcessDto> listPageProcess(Page page, SalesLedgerSchedulingProcessDto salesLedgerSchedulingDto) {
        IPage<SalesLedgerSchedulingProcessDto> list = salesLedgerSchedulingMapper.listPageProcess(page, salesLedgerSchedulingDto);
//        Set<Long> collect = list.getRecords().stream().map(SalesLedgerSchedulingProcessDto::getId).collect(Collectors.toSet());
//        if(CollectionUtils.isEmpty(collect)) return list;
//        LambdaQueryWrapper<SalesLedgerWork> salesLedgerWorkLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        salesLedgerWorkLambdaQueryWrapper.in(SalesLedgerWork::getSalesLedgerSchedulingId, collect)
//                .ne(SalesLedgerWork::getStatus, 1);
//        List<SalesLedgerWork> salesLedgerWorks = salesLedgerWorkMapper.selectList(salesLedgerWorkLambdaQueryWrapper);
        list.getRecords().forEach(i -> {
            // 计算生产总量 = 规格 * 数量 / 1000
            String[] split = i.getSpecificationModel().split("\\*");
            if(split.length == 2){
                BigDecimal multiply = new BigDecimal(split[0])
                        .multiply(new BigDecimal(split[1])
                                .multiply(i.getSuccessNum()).divide(new BigDecimal(1000),2, RoundingMode.CEILING));
                i.setTotalProduction(multiply);
            }
        });
        return list;
    }
 
    @Override
    public int productionDispatchDelete(List<Long> ids) {
        LambdaQueryWrapper<SalesLedgerScheduling> salesLedgerSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<SalesLedgerScheduling>();
        salesLedgerSchedulingLambdaQueryWrapper.in(SalesLedgerScheduling::getId, ids);
        List<SalesLedgerScheduling> salesLedgerSchedulings = salesLedgerSchedulingMapper.selectList(salesLedgerSchedulingLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(salesLedgerSchedulings)) throw new RuntimeException("排产不存在");
        List<SalesLedgerScheduling> collect = salesLedgerSchedulings.stream().filter(i -> !i.getStatus().equals(1)).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(collect)) throw new RuntimeException("排产已开始,请勿删除");
        salesLedgerSchedulingMapper.deleteBatchIds(ids);
        return 0;
    }
 
    @Override
    public int processScheduling(List<ProcessSchedulingDto> processSchedulingDtos) {
        for (ProcessSchedulingDto processSchedulingDto : processSchedulingDtos) {
            SalesLedgerScheduling salesLedgerScheduling = salesLedgerSchedulingMapper.selectById(processSchedulingDto.getId());
            if(salesLedgerScheduling == null) throw new RuntimeException("排产不存在");
            if(salesLedgerScheduling.getStatus().equals(3)) throw new RuntimeException("排产已完成,请勿重复排产");
            SysUser sysUser = sysUserMapper.selectUserById(processSchedulingDto.getSchedulingUserId());
            if(sysUser == null) throw new RuntimeException("排产人不存在");
            salesLedgerScheduling.setFinishedNum(salesLedgerScheduling.getFinishedNum().add(processSchedulingDto.getSchedulingNum()));
//            LambdaQueryWrapper<SalesLedgerWork> salesLedgerWorkLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            salesLedgerWorkLambdaQueryWrapper.eq(SalesLedgerWork::getSalesLedgerSchedulingId, salesLedgerScheduling.getId())
//                    .ne(SalesLedgerWork::getStatus, 1);
//            List<SalesLedgerWork> salesLedgerWorks = salesLedgerWorkMapper.selectList(salesLedgerWorkLambdaQueryWrapper);
            if(salesLedgerScheduling.getSchedulingNum().compareTo(salesLedgerScheduling.getFinishedNum()) < 0){
                throw new RuntimeException("当前排产数量大于待排产数量,请仔细核对!");
            }
            if(salesLedgerScheduling.getSchedulingNum().compareTo(salesLedgerScheduling.getFinishedNum()) == 0){
                salesLedgerScheduling.setStatus(3);
            }else{
                salesLedgerScheduling.setStatus(2);
            }
            salesLedgerSchedulingMapper.updateById(salesLedgerScheduling);
            SalesLedgerWork.SalesLedgerWorkBuilder salesLedgerWorkBuilder = SalesLedgerWork.builder()
                    .salesLedgerSchedulingId(salesLedgerScheduling.getId())
                    .salesLedgerId(salesLedgerScheduling.getSalesLedgerId())
                    .remark(processSchedulingDto.getRemark())
                    .type(processSchedulingDto.getType())
                    .loss(processSchedulingDto.getLoss())
                    .receive(processSchedulingDto.getReceive())
                    .salesLedgerProductId(salesLedgerScheduling.getSalesLedgerProductId())
                    .schedulingUserId(salesLedgerScheduling.getSchedulingUserId())
                    .schedulingUserName(sysUser.getNickName())
                    .schedulingNum(processSchedulingDto.getSchedulingNum())
                    .workHours(processSchedulingDto.getWorkHours())
                    .process(processSchedulingDto.getProcess())
                    .status(1)
                    .schedulingDate(LocalDate.parse(processSchedulingDto.getSchedulingDate(), DateTimeFormatter.ISO_LOCAL_DATE));
            salesLedgerWorkMapper.insert(salesLedgerWorkBuilder.build());
        }
        return 0;
    }
 
    @Override
    public void exportOne(HttpServletResponse response) {
        List<SalesLedgerSchedulingDto> list = salesLedgerSchedulingMapper.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.getSchedulingNum()));
            dais.add(daiDto);
        });
        ExcelUtil<DaiDto> util = new ExcelUtil<>(DaiDto.class);
        util.exportExcel(response, dais, "生产派工");
    }
}