skyouc
5 天以前 40d9cd510741a098bd52cbe22a5f9e5528f45abc
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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.extension.service.impl.ServiceImpl;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.entity.dto.PoItemsDto;
import com.vincent.rsf.server.api.service.ReceiveMsgService;
import com.vincent.rsf.server.api.service.ReportMsgService;
import com.vincent.rsf.server.manager.controller.params.AsnOrderAndItemsParams;
import com.vincent.rsf.server.manager.controller.params.BatchUpdateParam;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.AsnExceStatus;
import com.vincent.rsf.server.manager.mapper.AsnOrderMapper;
import com.vincent.rsf.server.manager.mapper.PurchaseMapper;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.mapper.SerialRuleMapper;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author Ryan
 * @description
 * @throws
 * @return
 * @time 2025/3/7 08:02
 */
@Service("outStockServiceImpl")
public class OutStockServiceImpl extends ServiceImpl<AsnOrderMapper, AsnOrder> implements OutStockService {
 
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    @Autowired
    private AsnOrderLogService asnOrderLogService;
    @Autowired
    private AsnOrderItemLogService asnOrderItemLogService;
 
    @Autowired
    private DeliveryItemService deliveryItemService;
 
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private MatnrService matnrService;
 
 
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 更新或保存明细
     * @time 2025/4/7 13:28
     */
    @Transactional(rollbackFor = Exception.class)
    private void svaeOrUpdateOrderItem(AsnOrderAndItemsParams params, Long loginUserId) throws Exception {
        AsnOrder orders = params.getOrders();
 
        params.getItems().forEach(item -> {
            item.put("asnId", orders.getId());
            item.put("asnCode", orders.getCode());
            item.put("poCode", orders.getPoCode());
            item.put("createBy", loginUserId);
            item.put("updateBy", loginUserId);
            if (!asnOrderItemService.fieldsSave(item)) {
                throw new CoolException("明细保存失败!!");
            }
        });
        List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>()
                .eq(AsnOrderItem::getAsnId, params.getOrders().getId()));
        double sum = orderItems.stream().mapToDouble(AsnOrderItem::getAnfme).sum();
        orders.setAnfme(sum);
        if (!this.updateById(orders)) {
            throw new CoolException("计划收货数量修改失败!!");
        }
    }
 
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 删除原主单及明细,加入历史档
     * @time 2025/3/19 19:53
     */
    @Transactional(rollbackFor = Exception.class)
    private void operateOrderLogs(AsnOrder asrder) throws Exception {
        if (Objects.isNull(asrder) || Objects.isNull(asrder.getId())) {
            throw new CoolException("参数不能为空!!");
        }
        asrder.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_CLOSE.val);
 
        if (!this.updateById(asrder)) {
            throw new CoolException("单据关闭失败!!");
        }
        List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().eq(AsnOrderItem::getAsnId, asrder.getId()));
        if (orderItems.isEmpty()) {
            throw new CoolException("收货明细为空!!");
        }
//        if (Objects.isNull(asrder.getAnfme()) || asrder.getAnfme().compareTo(0.00) == 0) {
//            throw new CoolException("收货数量不能为零!!");
//        }
        AsnOrder order = this.getById(asrder.getId());
        AsnOrderLog orderLog = new AsnOrderLog();
        order.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_DONE.val);
        BeanUtils.copyProperties(order, orderLog);
        orderLog.setId(null);
        orderLog.setAsnId(order.getId());
 
        if (!this.saveOrUpdate(order)) {
            throw new CoolException("状态修改失败!!");
        }
        orderLog.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_CLOSE.val);
        if (!asnOrderLogService.save(orderLog)) {
            throw new CoolException("主单历史档添加失败!!");
        }
        List<AsnOrderItemLog> logs = new ArrayList<>();
        List<AsnOrderItem> items = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().eq(AsnOrderItem::getAsnId, order.getId()));
        items.forEach(item -> {
            AsnOrderItemLog itemLog = new AsnOrderItemLog();
            BeanUtils.copyProperties(item, itemLog);
            itemLog.setAsnItemId(itemLog.getId())
                    .setLogId(orderLog.getId())
                    .setAsnId(item.getAsnId());
            logs.add(itemLog);
        });
 
        if (!asnOrderItemLogService.saveBatch(logs)) {
            throw new CoolException("通知单明细历史档保存失败!!");
        }
        if (!asnOrderItemService.remove(new LambdaQueryWrapper<AsnOrderItem>().eq(AsnOrderItem::getAsnId, order.getId()))) {
            throw new CoolException("原单据明细删除失败!!");
        }
        if (!this.removeById(asrder.getId())) {
            throw new CoolException("原单据删除失败!!");
        }
    }
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 取消出库单据
     * @time 2025/4/22 10:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R cancelOutOrder(String id) {
        if (Cools.isEmpty(id)) {
            throw new CoolException("参数不能为空!!");
        }
        AsnOrder order = this.getById(id);
        if (Objects.isNull(order)) {
            throw new CoolException("单据不存在!!");
        }
        if (!order.getExceStatus().equals(AsnExceStatus.ASN_EXCE_STATUS_UN_EXCE.val)) {
            throw new CoolException("当前单据状态为" + AsnExceStatus.getExceStatus(order.getExceStatus()) + ", 不可执行取消操作!!");
        }
        order.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_TASK_CANCEL.val).setStatus(0);
 
        if (!this.saveOrUpdate(order)) {
            throw new CoolException("单据取消失败!!");
        }
        return R.ok("操作成功");
    }
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 通过DO单生成出库单
     * @time 2025/4/23 16:24
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R genOutStock(List<Long> ids) {
        if (Objects.isNull(ids) || ids.isEmpty()) {
            throw new CoolException("参数不能为空!!");
        }
        List<DeliveryItem> items = deliveryItemService.list(new LambdaQueryWrapper<DeliveryItem>().in(DeliveryItem::getId, ids));
        if (items.isEmpty()) {
            throw new CoolException("单据不存在!!");
        }
        Map<Long, List<DeliveryItem>> listMap = items.stream().collect(Collectors.groupingBy(DeliveryItem::getDeliveryId));
        listMap.keySet().forEach(key -> {
            //TODO 判断单据是否已经存在,如存在则累加修改子表,不存在才新建
            Delivery delivery = deliveryService.getById(key);
            if (Objects.isNull(delivery)) {
                throw new CoolException("单据不存在!!");
            }
            AsnOrder order = new AsnOrder();
            BeanUtils.copyProperties(delivery, order);
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_OUT_STOCK_CODE, order);
            if (Objects.isNull(ruleCode) || StringUtils.isBlank(ruleCode)) {
                throw new CoolException("编码规则错误:请检查 「SYS_OUT_STOCK_CODE」编码是否设置成功");
            }
            order.setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_UN_EXCE.val)
                    .setCode(ruleCode)
                    .setPoId(delivery.getId())
                    .setId(null)
                    .setPoCode(delivery.getCode());
            if (!this.save(order)) {
                throw new CoolException("主单保存失败!!");
            }
            List<AsnOrderItem> orderItems = new ArrayList<>();
            listMap.get(key).forEach(item -> {
                AsnOrderItem orderItem = new AsnOrderItem();
                BeanUtils.copyProperties(item, orderItem);
                orderItem.setId(null)
                        .setPoCode(order.getPoCode())
                        .setAsnId(order.getId())
                        .setAsnCode(order.getCode())
                        .setPlatItemId(item.getPlatItemId())
                        .setPoDetlId(item.getId());
                orderItems.add(orderItem);
            });
 
            double sum = orderItems.stream().mapToDouble(AsnOrderItem::getAnfme).sum();
            //修改计划数量
            order.setAnfme(sum);
            if (!this.saveOrUpdate(order)) {
                throw new CoolException("主单数量修改失败!!");
            }
            if (!asnOrderItemService.saveBatch(orderItems)) {
                throw new CoolException("明细保存失败!!");
            }
        });
        return R.ok();
    }
}