skyouc
2025-04-01 aee6b945f2249c37fd52a8697526ae8bd4915ef8
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.controller.params.IsptItemsParams;
import com.vincent.rsf.server.manager.controller.params.QlyInspectAndItem;
import com.vincent.rsf.server.manager.entity.QlyInspect;
import com.vincent.rsf.server.manager.mapper.QlyIsptItemMapper;
import com.vincent.rsf.server.manager.entity.QlyIsptItem;
import com.vincent.rsf.server.manager.service.QlyInspectService;
import com.vincent.rsf.server.manager.service.QlyIsptItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service("qlyIsptItemService")
public class QlyIsptItemServiceImpl extends ServiceImpl<QlyIsptItemMapper, QlyIsptItem> implements QlyIsptItemService {
 
    @Autowired
    private QlyInspectService qlyInspectService;
 
    /**
     * @author Ryan
     * @description 批量修改
     * @param
     * @return
     * @time 2025/4/1 09:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdate(IsptItemsParams params) {
        if (Objects.isNull(params.getIsptItem()) || params.getIsptItem().isEmpty()) {
            throw new CoolException("ID不能为空!!");
        }
        List<QlyIsptItem> isptItem =  params.getIsptItem();
        List<Long> list = isptItem.stream().map(QlyIsptItem::getId).collect(Collectors.toList());
        if (params.getType().equals("2") || params.getType().equals("1")) {
            isptItem.forEach(item -> {
                if (params.getType().equals("1")) {
                    //TODO 先捡后收为送货数量,先收后捡为收货数量
                    item.setSafeQty(item.getDlyQty());
                    item.setDisQty(0.0);
                } else {
                    item.setSafeQty(0.0);
                    item.setDisQty(item.getDlyQty());
                }
                if (!this.update(new LambdaUpdateWrapper<QlyIsptItem>()
                        .set(QlyIsptItem::getSafeQty, item.getSafeQty())
                        .set(QlyIsptItem::getIsptResult, params.getType())
                        .set(QlyIsptItem::getDisQty, item.getDisQty())
                        .in(QlyIsptItem::getId, item.getId()))) {
                    throw new CoolException("修改失败!!");
                }
            });
        } else {
            if (!this.updateBatchById(isptItem)) {
                throw new CoolException("明细修改失败");
            }
        }
 
        List<QlyIsptItem> isptItems = this.list(new LambdaQueryWrapper<QlyIsptItem>().in(QlyIsptItem::getId, list));
        if (isptItems.isEmpty()) {
            throw new CoolException("数据错误:明细不存在!!");
        }
        Map<Long, List<QlyIsptItem>> listMap = isptItems.stream().collect(Collectors.groupingBy(QlyIsptItem::getIspectId));
        for (Long key : listMap.keySet()) {
            QlyInspect inspect = qlyInspectService.getById(key);
            List<QlyIsptItem> items = listMap.get(key);
            Double safeQty = items.stream().mapToDouble(QlyIsptItem::getSafeQty).sum();
            Double dlyQty = items.stream().mapToDouble(QlyIsptItem::getDlyQty).sum();
            Double disQty = items.stream().mapToDouble(QlyIsptItem::getDisQty).sum();
            Double qlyQty = safeQty + disQty;
            //安全数量,质检数量
            inspect.setSafeQty(safeQty)
                    .setIsptQty(qlyQty);
            if (Double.compare(disQty, 0) > 0) {
                inspect.setIsptResult(Short.parseShort("4"));
            }
            if (Double.compare(safeQty, dlyQty) == 0) {
                inspect.setIsptStatus("1");
                inspect.setIsptResult(Short.parseShort("1"));
            }
            if (!qlyInspectService.updateById(inspect)) {
                throw new CoolException("质检数量修改失败!!");
            }
        }
        return true;
    }
}