skyouc
8 天以前 c731084698d89c12c23180f57598d521abb97a23
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.manager.controller.params.IsptItemsParams;
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.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.List;
import java.util.Objects;
import java.util.Set;
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;
    @Autowired
    private QlyIsptItemService qlyIsptItemService;
 
    /**
     * @param
     * @param loginUserId
     * @return
     * @author Ryan
     * @description 批量修改
     * @time 2025/4/1 09:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdate(IsptItemsParams params, Long loginUserId) {
        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 : isptItem) {
            List<QlyInspectResult> results = qlyInspectResultService.list(new LambdaQueryWrapper<QlyInspectResult>()
                    .eq(QlyInspectResult::getIsptId, item.getIspectId())
                    .eq(QlyInspectResult::getIsptItemId, item.getId()));
            if (!results.isEmpty()) {
                if (!qlyInspectResultService.remove(new LambdaUpdateWrapper<QlyInspectResult>()
                        .eq(QlyInspectResult::getIsptItemId, item.getId()))) {
                    throw new CoolException("历史质检结果移出失败!!");
                }
            }
 
            QlyInspectResult result = new QlyInspectResult();
            result.setIsptId(item.getIspectId())
                    .setIsptItemId(item.getId());
            if (!Objects.isNull(item.getDisQty()) && item.getDisQty().compareTo(0.00) > 0) {
                result.setAnfme(item.getDisQty())
                        .setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_DEFECT.val);
                qlyInspectResultService.saveOrUpdate(result);
            }
 
            result = new QlyInspectResult();
            result.setIsptId(item.getIspectId())
                    .setIsptItemId(item.getId());
            if (!Objects.isNull(item.getSafeQty()) && item.getSafeQty().compareTo(0.00) > 0) {
                result.setAnfme(item.getSafeQty())
                        .setIsptResult(QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val);
                qlyInspectResultService.saveOrUpdate(result);
            }
 
            List<QlyInspectResult> isptRelt = qlyInspectResultService.list(new LambdaQueryWrapper<QlyInspectResult>()
                    .eq(QlyInspectResult::getIsptId, item.getIspectId())
                    .eq(QlyInspectResult::getIsptItemId, item.getId()));
 
            Double isptQty = isptRelt.stream().mapToDouble(QlyInspectResult::getAnfme).sum();
 
            if (!qlyIsptItemService.update(new LambdaUpdateWrapper<QlyIsptItem>()
                    .set(QlyIsptItem::getUpdateBy, loginUserId)
                    .set(QlyIsptItem::getAnfme, isptQty)
                    .eq(QlyIsptItem::getId, item.getId()))) {
                throw new CoolException("单据明细数量修改失败!!");
            }
        }
 
        Set<Long> isptIds = isptItem.stream().map(QlyIsptItem::getIspectId).collect(Collectors.toSet());
        List<QlyInspectResult> items = qlyInspectResultService.list(new LambdaQueryWrapper<QlyInspectResult>().in(QlyInspectResult::getIsptId, isptIds));
        Double sum = items.stream().mapToDouble(QlyInspectResult::getAnfme).sum();
        if (sum.compareTo(0.00) > 0) {
            qlyInspectService.update(new LambdaUpdateWrapper<QlyInspect>()
                    .set(QlyInspect::getIsptQty, sum)
                    .set(QlyInspect::getUpdateBy, loginUserId)
                    .eq(QlyInspect::getId, isptItem.stream().findFirst().get().getIspectId()));
        }
 
        return true;
    }
 
    /**
     * @author Ryan
     * @date 2025/5/12
     * @description: 获取自定义Page
     * @version 1.0
     */
    @Override
    public IPage<QlyIsptItem> pageByWrapper(PageParam<QlyIsptItem, BaseParam> pageParam, QueryWrapper<QlyIsptItem> queryWrapper) {
        IPage<QlyIsptItem> page = this.page(pageParam, queryWrapper);
        if (!page.getRecords().isEmpty()) {
            List<QlyIsptItem> records = page.getRecords();
            for (int i = 0; i < records.size(); i++) {
                List<QlyInspectResult> results = qlyInspectResultService.list(new QueryWrapper<QlyInspectResult>()
                        .select("id, ispt_result, SUM(anfme) anfme, ispt_item_id, ispt_id")
                        .lambda()
                        .eq(QlyInspectResult::getIsptItemId, records.get(i).getId()).groupBy(QlyInspectResult::getIsptResult));
                if (!results.isEmpty()) {
                    for (QlyInspectResult result : results) {
                        if (result.getIsptResult() == QlyIsptResult.QLY_ISPT_RESULT_EXCELLENT.val) {
                            records.get(i).setSafeQty(result.getAnfme());
                        } else {
                            records.get(i).setDisQty(result.getAnfme());
                        }
                    }
                }
            }
            page.setRecords(records);
        }
        return page;
    }
}