zjj
7 天以前 b140f11c4744cd1e63b4a2409288f700d0f65175
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
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.AsnOrderItem;
import com.vincent.rsf.server.manager.entity.QlyInspect;
import com.vincent.rsf.server.manager.entity.QlyInspectResult;
import com.vincent.rsf.server.manager.enums.QlyIsptResult;
import com.vincent.rsf.server.manager.enums.QlyIsptStatus;
import com.vincent.rsf.server.manager.mapper.QlyIsptItemMapper;
import com.vincent.rsf.server.manager.entity.QlyIsptItem;
import com.vincent.rsf.server.manager.service.AsnOrderItemService;
import com.vincent.rsf.server.manager.service.QlyInspectResultService;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Arrays;
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;
    @Autowired
    private AsnOrderItemService asnOrderItemService;
 
    @Autowired
    private QlyInspectResultService qlyInspectResultService;
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 批量修改
     * @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());
 
        List<QlyIsptItem> isptItems = this.list(new LambdaQueryWrapper<QlyIsptItem>().in(QlyIsptItem::getId, list));
        if (isptItems.isEmpty()) {
            throw new CoolException("数据错误:明细不存在!!");
        }
 
        for (QlyIsptItem item : isptItems) {
            List<QlyInspectResult> results = qlyInspectResultService.list(new LambdaQueryWrapper<QlyInspectResult>()
                    .eq(QlyInspectResult::getIsptId, item.getIspectId())
                    .eq(QlyInspectResult::getIsptItemId, item.getId()));
            if (!results.isEmpty()) {
                //不为空做更新操作
 
 
            } else {
                //为空做新增操作
                QlyInspectResult result = new QlyInspectResult();
                result.setIsptId(item.getIspectId())
                        .setAnfme(item.getSafeQty())
                        .setIsptItemId(item.getId());
                if (item.getDisQty().compareTo(0.00) < 1) {
                    continue;
                }
                result.setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_DEFECT.val);
                result.setAnfme(item.getDisQty());
 
                if (!qlyInspectResultService.save(result)) {
                    throw new CoolException("质检结论保存失败!!");
                }
 
                if (item.getSafeQty().compareTo(0.00) < 1) {
                    continue;
                }
                result.setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val)
                        .setAnfme(item.getSafeQty());
 
                if (!qlyInspectResultService.save(result)) {
                    throw new CoolException("质检结论保存失败!!");
                }
            }
 
        }
 
 
        if (Short.parseShort(params.getType()) == QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val || Short.parseShort(params.getType()) == QlyIsptResult.QLY_ISPT_RESULT_DEFECT.val) {
            //1:合格, 2:不合格, 0: 其它(默认)
            isptItem.forEach(item -> {
                if (Short.parseShort(params.getType()) == QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val) {
                    //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, Short.parseShort(params.getType()))
//                        .set(QlyIsptItem::getDisQty, item.getDisQty())
                        .in(QlyIsptItem::getId, item.getId()))) {
                    throw new CoolException("修改失败!!");
                }
            });
        } else {
            if (!this.updateBatchById(isptItem)) {
                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);
            items.forEach(qlyIsptItem -> {
                AsnOrderItem orderItem = asnOrderItemService.getById(qlyIsptItem.getAsnItemId());
//                if (qlyIsptItem.getSafeQty().compareTo(orderItem.getAnfme()) >= 0.00) {
//                    orderItem.setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val)
//                            .setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_DONE.val);
//                } else if (qlyIsptItem.getSafeQty().compareTo(0.00) > 0.00 && qlyIsptItem.getSafeQty().compareTo(orderItem.getAnfme()) < 0.00) {
//                    orderItem.setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_SECTION.val);
//                    if (Double.compare(qlyIsptItem.getSafeQty() + qlyIsptItem.getDisQty(), orderItem.getAnfme()) >= 0.00 ) {
//                        orderItem.setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_DONE.val);
//                    } else {
//                        orderItem.setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_ING.val);
//                    }
//                } else {
//                    orderItem.setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_DEFECT.val)
//                            .setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_DONE.val);
//                }
 
                if (!asnOrderItemService.updateById(orderItem)) {
                    throw new CoolException("收货单明细修改失败!!");
                }
            });
//            Double safeQty = items.stream().mapToDouble(QlyIsptItem::getSafeQty).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(QlyIsptResult.QLY_ISPT_RESULT_SECTION.val);
//            }
//            if (Double.compare(safeQty, inspect.getDlyQty()) == 0) {
////                inspect.setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_DONE.val);
//                inspect.setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val);
//            }
            if (!qlyInspectService.updateById(inspect)) {
                throw new CoolException("质检数量修改失败!!");
            }
        }
        return true;
    }
}