skyouc
2025-03-28 193e9ca159a9420d67933c6fe390d453eedb86ac
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.entity.AsnOrderItem;
import com.vincent.rsf.server.manager.mapper.AsnOrderItemMapper;
import com.vincent.rsf.server.manager.mapper.AsnOrderMapper;
import com.vincent.rsf.server.manager.entity.AsnOrder;
import com.vincent.rsf.server.manager.mapper.PurchaseMapper;
import com.vincent.rsf.server.manager.service.AsnOrderItemService;
import com.vincent.rsf.server.manager.service.AsnOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.entity.SerialRule;
import com.vincent.rsf.server.system.mapper.SerialRuleMapper;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
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("asnOrderService")
public class AsnOrderServiceImpl extends ServiceImpl<AsnOrderMapper, AsnOrder> implements AsnOrderService {
 
    @Autowired
    private ReceiveMsgService receiveMsgService;
    @Autowired
    private ReportMsgService reportMsgService;
    @Resource
    private PurchaseMapper purchaseMapper;
 
    @Autowired
    private AsnOrderItemService asnOrderItemService;
 
    @Resource
    private SerialRuleMapper serialRuleMapper;
 
    @Override
    public boolean notifyInspect(List<AsnOrder> orders) {
        if (orders.isEmpty()) {
            throw new CoolException("上报参数不能为空!!");
        }
        Set<Long> asnIds = orders.stream().map(AsnOrder::getId).collect(Collectors.toSet());
        if (asnIds.isEmpty()) {
            throw new CoolException("ASN单据不能为空!!");
        }
        List<PoItemsDto> items = purchaseMapper.poList(asnIds);
        if (reportMsgService.reportInspectNotify(items)) {
            return true;
        } else {
            return false;
        }
    }
 
 
    @Override
    public List<AsnOrder> getListByMatnr(Map<String, String> params) {
        if (Objects.isNull(params)) {
            throw new CoolException("查询条件不能为空!!");
        }
        List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>()
                .like(!Objects.isNull(params.get("maktx")), AsnOrderItem::getMaktx, params.get("maktx"))
                .eq(!Objects.isNull(params.get("matnrCode")), AsnOrderItem::getMatnrCode, params.get("matnrCode")));
 
        if (orderItems.isEmpty()) {
            return new ArrayList<>();
        }
        List<Long> longList = orderItems.stream().map(AsnOrderItem::getAsnId).collect(Collectors.toList());
 
        return this.listByIds(longList);
    }
 
    @Override
    public R saveOrderAndItems(AsnOrderAndItemsParams params) {
        if (Objects.isNull(params.getOrders())) {
            throw new CoolException("主单信息不能为空");
        }
        AsnOrder orders = params.getOrders();
 
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_ASN_ORDER, orders);
        if (Objects.isNull(ruleCode)) {
            throw new CoolException("收货通知单编码生成失败!!");
        }
        orders.setCode(ruleCode);
        if (!this.save(orders)) {
            throw new CoolException("主单保存失败!!");
        }
        if (params.getItems().isEmpty()) {
            throw new CoolException("收货通知单明细不能为寒食节!!");
        }
        params.getItems().forEach(item -> {
            item.put("asnId", orders.getId());
            item.put("asnCode", orders.getCode());
            item.put("poCode", orders.getPoCode());
            if (!asnOrderItemService.fieldsSave(item)) {
                throw new CoolException("明细保存失败!!");
            }
 
        });
        return R.ok("保存成功!!");
    }
 
    /**
     * 表单明细修改
     * @param params
     * @return
     */
    @Override
    @Transactional
    public R updateOrderItem(AsnOrderAndItemsParams params) {
        if (Objects.isNull(params.getOrders())) {
            throw new CoolException("主单信息不能为空!!");
        }
        if (Objects.isNull(params.getOrders().getId())) {
            throw new CoolException("数据错误:单据ID不能为空!!");
        }
        if (this.updateById(params.getOrders())) {
            throw new CoolException("主单修改失败!!");
        }
        if (Objects.isNull(params.getItems()) || params.getItems().isEmpty()) {
            return R.ok("修改完成!!");
        }
        List<Map<String, Object>> items = params.getItems();
        List<AsnOrderItem> asnOrderItems = JSONArray.parseArray(JSONArray.toJSONString(items), AsnOrderItem.class);
        if (asnOrderItemService.saveOrUpdateBatch(asnOrderItems)) {
            throw new CoolException("明细修改失败!!");
        }
        return R.ok("修改完成!!");
    }
}