chen.lin
2 天以前 e74b573ca564027c78406354dd3797bb3e36de35
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.controller.params.ReviseLogParams;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.CommonExceStatus;
import com.vincent.rsf.server.manager.enums.OrderType;
import com.vincent.rsf.server.manager.enums.OrderWorkType;
import com.vincent.rsf.server.manager.mapper.ReviseLogMapper;
import com.vincent.rsf.server.manager.service.*;
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.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
@Service("reviseLogService")
public class ReviseLogServiceImpl extends ServiceImpl<ReviseLogMapper, ReviseLog> implements ReviseLogService {
 
    @Autowired
    private LocReviseService locReviseService;
 
    @Autowired
    private ReviseLogService reviseLogService;
 
    @Autowired
    private LocItemService locItemService;
 
    @Autowired
    private LocService locService;
 
    @Autowired
    private ReviseLogItemService reviseLogItemService;
 
    /**
     * 库存调整单明细添加
     *
     * @param revise
     * @param loginUserId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ReviseLog> reviseLoc(ReviseLogParams revise, Long loginUserId) {
        LocRevise locRevise = locReviseService.getById(revise.getReviseId());
        if (Objects.isNull(locRevise)) {
            throw new CoolException("调整单据不存在!!");
        }
        List<ReviseLog> items = revise.getItems();
        if (items.isEmpty()) {
            throw new CoolException("调整单明细参数为空!!");
        }
        items.forEach(item -> {
            ReviseLog reviseLog = new ReviseLog();
            BeanUtils.copyProperties(item, reviseLog);
            reviseLog.setAreaId(locRevise.getAreaId())
                    .setReviseId(locRevise.getId())
                    .setCreateBy(loginUserId)
                    .setUpdateBy(loginUserId)
                    .setReviseCode(locRevise.getCode());
            if (!reviseLogService.save(reviseLog)) {
                throw new CoolException("调整单明细保存失败!!");
            }
 
            List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>()
                    .eq(LocItem::getLocCode, item.getLocCode()));
            if (!locItems.isEmpty()) {
                locItems.forEach(ote -> {
                    ReviseLogItem logItem = new ReviseLogItem();
                    BeanUtils.copyProperties(ote, logItem);
                    logItem.setReviseLogId(reviseLog.getId());
                    if (!reviseLogItemService.save(logItem)) {
                        throw new CoolException("明细保存失败!!");
                    }
                });
            }
 
            Double sum = locItems.stream().mapToDouble(LocItem::getAnfme).sum();
 
            locRevise.setAnfme(Math.round((sum + locRevise.getAnfme()) * 1000000) / 1000000.0);
        });
 
        locRevise.setExceStatus(CommonExceStatus.COMMON_EXCE_STATUS_UN_EXCE.val);
 
        if (!locReviseService.updateById(locRevise)) {
            throw new CoolException("状态更新失败!!");
        }
        return items;
    }
 
    /**
     * @author Ryan
     * @date 2025/8/18
     * @description: 确认调整库存
     * @version 1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R complete(Long id, Long loginUserId) {
        LocRevise revise = locReviseService.getById(id);
        if (Objects.isNull(revise)) {
            throw new CoolException("调整单不存在!!");
        }
        if (!revise.getExceStatus().equals(CommonExceStatus.COMMON_EXCE_STATUS_EXCE_ING.val)) {
            throw new CoolException("单据状态未执行或已完成,无法执行完成操作!!");
        }
        List<ReviseLog> logs = reviseLogService.list(new LambdaQueryWrapper<ReviseLog>().eq(ReviseLog::getReviseId, revise.getId()));
        if (logs.isEmpty()) {
            throw new CoolException("库存日志不存在!!");
        }
        Set<Long> longs = logs.stream().map(ReviseLog::getId).collect(Collectors.toSet());
        List<ReviseLogItem> logItems = reviseLogItemService.list(new LambdaQueryWrapper<ReviseLogItem>().in(ReviseLogItem::getReviseLogId, longs));
        if (logItems.isEmpty()) {
            throw new CoolException("调整明细为空!!");
        }
        // 按库位ID分组,如果locId为null则按locCode分组
        Map<String, List<ReviseLogItem>> listMap = logItems.stream()
                .filter(item -> item.getLocCode() != null && !item.getLocCode().isEmpty())
                .collect(Collectors.groupingBy(item -> {
                    // 优先使用locId,如果为null则使用locCode作为key
                    return item.getLocId() != null ? String.valueOf(item.getLocId()) : item.getLocCode();
                }));
        
        listMap.keySet().forEach(key -> {
            List<ReviseLogItem> reviseItems = listMap.get(key);
            if (Objects.isNull(reviseItems) || reviseItems.isEmpty()) {
                throw new CoolException("调整明细为空!!");
            }
            
            // 获取第一个明细的库位信息
            ReviseLogItem firstItem = reviseItems.get(0);
            String locCode = firstItem.getLocCode();
            Long locId = firstItem.getLocId();
            
            // 查询库位:优先使用locId,如果为null则使用locCode
            Loc loc = null;
            if (locId != null) {
                loc = locService.getById(locId);
            }
            if (loc == null && locCode != null && !locCode.isEmpty()) {
                loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, locCode).eq(Loc::getDeleted, 0));
            }
            
            if (Objects.isNull(loc)) {
                throw new CoolException("库位不存在!库位编码:" + locCode + ", 库位ID:" + locId);
            }
            
            // 删除原库位的库存明细(如果存在)
            locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
 
            Loc finalLoc = loc;
            reviseItems.forEach(logItem -> {
                LocItem locDetl = new LocItem();
                BeanUtils.copyProperties(logItem, locDetl);
                locDetl.setLocId(finalLoc.getId())
                        .setType(OrderType.ORDER_REVISE.type)
                        .setWkType(Short.parseShort(OrderWorkType.ORDER_WORK_TYPE_STOCK_REVISE.type))
                        .setLocCode(finalLoc.getCode())
                        .setAnfme(logItem.getReviseQty())
                        .setUpdateBy(loginUserId)
                        .setId(null)
                        .setCreateBy(loginUserId);
                if (!locItemService.save(locDetl)) {
                    throw new CoolException("库存明细保存失败!!");
                }
            });
        });
        revise.setExceStatus(CommonExceStatus.COMMON_EXCE_STATUS_TASK_DONE.val);
        if (!locReviseService.updateById(revise)) {
            throw new CoolException("调整单修改失败!!");
        }
        return R.ok();
    }
}