skyouc
1 天以前 f02540a23e86cd1a59d316efebfa271e147a8e61
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
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.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.controller.params.ReviseLogItemParams;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.mapper.ReviseLogItemMapper;
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.Objects;
import java.util.stream.Collectors;
 
@Service("reviseLogItemService")
public class ReviseLogItemServiceImpl extends ServiceImpl<ReviseLogItemMapper, ReviseLogItem> implements ReviseLogItemService {
 
    @Autowired
    private ReviseLogService reviseLogService;
    @Autowired
    private LocItemService locItemService;
    @Autowired
    private LocService locService;
    @Autowired
    private ReviseLogItemService reviseLogItemService;
    @Autowired
    private LocReviseService locReviseService;
 
 
    /**
     * 保存库存调整历史单明细
     *
     * @param reviseLogItem
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ReviseLogItem> itemSave(ReviseLogItemParams reviseLogItem, Long userId) {
        ReviseLog reviseLog = reviseLogService.getById(reviseLogItem.getReviseLogId());
        if (Objects.isNull(reviseLog)) {
            throw new RuntimeException("库存历史单据不存在!!");
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
                .eq(Loc::getCode, reviseLog.getLocCode()));
        if (Objects.isNull(loc)) {
            throw new RuntimeException("库位库存不存在!!");
        }
        List<ReviseLogItem> items = reviseLogItem.getItems();
 
        this.remove(new LambdaQueryWrapper<ReviseLogItem>()
                .eq(ReviseLogItem::getLocId, loc.getId()));
 
        items.forEach(item -> {
            ReviseLogItem logItem = new ReviseLogItem();
            BeanUtils.copyProperties(item, logItem);
            logItem.setReviseLogId(reviseLogItem.getReviseLogId())
                    .setUpdateBy(userId)
                    .setCreateBy(userId)
                    .setLocId(loc.getId())
                    .setLocCode(loc.getCode());
            if (Objects.isNull(item.getAnfme())) {
                logItem.setAnfme(0.0);
            }
            if (!this.saveOrUpdate(logItem)) {
                throw new RuntimeException("库存明细调整失败");
            }
 
            LocItem one = locItemService.getOne(new LambdaQueryWrapper<LocItem>()
                    .eq(LocItem::getMatnrId, logItem.getMatnrId())
                    .eq(StringUtils.isNotBlank(logItem.getBatch()), LocItem::getBatch, logItem.getBatch())
                    .eq(StringUtils.isNotBlank(logItem.getFieldsIndex()), LocItem::getFieldsIndex, logItem.getFieldsIndex())
                    .eq(LocItem::getLocCode, logItem.getLocCode()));
            if (Objects.isNull(one)) {
                LocItem locDetl = new LocItem();
                BeanUtils.copyProperties(logItem, locDetl);
                locDetl.setLocId(loc.getId())
                        .setLocCode(loc.getCode())
                        .setAnfme(logItem.getReviseQty())
                        .setUpdateBy(userId)
                        .setId(null)
                        .setCreateBy(userId);
                if (!locItemService.save(locDetl)) {
                    throw new CoolException("库存明细保存失败!!");
                }
            } else {
                one.setAnfme(logItem.getReviseQty());
                if (!locItemService.updateById(one)) {
                    throw new RuntimeException("库存明细修改失败!!");
                }
            }
        });
 
        List<ReviseLog> list = reviseLogService.list(new LambdaQueryWrapper<ReviseLog>().eq(ReviseLog::getReviseId, reviseLog.getReviseId()));
        if (Objects.isNull(list)) {
            throw new RuntimeException("数据错误:库存调整单不存在!!");
        }
        List<Long> reviseIds = list.stream().map(ReviseLog::getId).collect(Collectors.toList());
        List<ReviseLogItem> logItems = reviseLogItemService.list(new LambdaQueryWrapper<ReviseLogItem>()
                .in(ReviseLogItem::getReviseLogId, reviseIds));
 
        Double anfems = logItems.stream().mapToDouble(ReviseLogItem::getAnfme).sum();
        Double reviseQty = logItems.stream().mapToDouble(ReviseLogItem::getReviseQty).sum();
 
        if (!locReviseService.update(new LambdaUpdateWrapper<LocRevise>()
                .eq(LocRevise::getId, reviseLog.getReviseId())
                        .set(LocRevise::getAnfme, anfems)
                        .set(LocRevise::getReviseQty, reviseQty))) {
            throw new RuntimeException("库存调整单修改失败!!");
        }
        return items;
    }
}