skyouc
2025-03-14 fcb49d1a0ac87f894d0249c41043aff6e8c73375
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
package com.vincent.rsf.server.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vincent.rsf.common.domain.CommonReponse;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.config.RemotesInfoProperties;
import com.vincent.rsf.server.api.entity.dto.PoItemsDto;
import com.vincent.rsf.server.api.entity.params.ErpInspectItem;
import com.vincent.rsf.server.api.entity.params.ErpInspectParams;
import com.vincent.rsf.server.api.service.ReportMsgService;
import com.vincent.rsf.server.manager.entity.AsnOrder;
import com.vincent.rsf.server.manager.entity.AsnOrderItem;
import com.vincent.rsf.server.manager.entity.Purchase;
import com.vincent.rsf.server.manager.service.AsnOrderItemService;
import com.vincent.rsf.server.manager.service.AsnOrderService;
import com.vincent.rsf.server.manager.service.PurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author Ryan
 * @version 1.0
 * @title ReportMsgServiceImpl
 * @description
 * @create 2025/3/12 17:08
 */
@Slf4j
@Service
public class ReportMsgServiceImpl implements ReportMsgService {
 
    @Autowired
    private PurchaseService purchaseService;
 
    @Autowired
    private RemotesInfoProperties.ApiInfo apiInfo;
 
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RemotesInfoProperties remotesInfoProperties;
 
    @Autowired
    private AsnOrderService asnOrderService;
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    /**
     * @author Ryan
     * @description 上报质检列表
     * @params [items 列表]
     * @return boolean
     * @time 2025/3/7 09:12
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean reportInspectNotify(List<PoItemsDto> items) {
        if (items.isEmpty()) {
            throw new CoolException("上报内容为空!!");
        }
        List<ErpInspectParams> inspectParams =  new ArrayList<>();
        Map<String, List<PoItemsDto>> listMap = items.stream().collect(Collectors.groupingBy(PoItemsDto::getPurchaseId));
        listMap.keySet().forEach(key -> {
            Purchase purchase = purchaseService.getOne(new LambdaQueryWrapper<Purchase>().eq(Purchase::getId, Long.valueOf(key)));
            if (Objects.isNull(purchase)) {
                throw new CoolException("采购单信息有误!!");
            }
            ErpInspectParams params = new ErpInspectParams();
            List<ErpInspectItem> inspectItems = new ArrayList<>();
            //赋值erp主单编码,编号
            params.setPoId(purchase.getPlatId())
                    .setPoCode(purchase.getPlatCode());
            List<PoItemsDto> dtos = listMap.get(key);
            if (dtos.isEmpty()) {
                throw new CoolException("单据信息错误!!");
            }
            dtos.forEach(dto -> {
                ErpInspectItem inspect= new ErpInspectItem();
                BeanUtils.copyProperties(dto, inspect);
                //赋值erp明细编码
                inspect.setPoItemId(dto.getPlatItemId());
                inspectItems.add(inspect);
            });
            params.setChildren(inspectItems);
            inspectParams.add(params);
 
        });
 
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        //获取上报质检接口
        String url = remotesInfoProperties.getBaseUrl() + apiInfo.getNotifyInspect();
        // 设置请求参数
        params.add("params", JSONObject.toJSONString(inspectParams));
        log.info("请求地址:{},请求参数:{}", url, JSONObject.toJSONString(inspectParams));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(params, headers);
        // 请求
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        log.info("下发任务 返回结果:{}", exchange);
        if (Objects.isNull(exchange.getBody()) || exchange.getBody() == null) {
            throw new CoolException("下发任务失败!!");
        } else {
            CommonReponse reponse = (CommonReponse) JSON.parse(exchange.getBody());
            if (reponse.getCode() == 0) {
                //修改asn上报状态
                Set<String> itemSets = items.stream().map(PoItemsDto::getId).collect(Collectors.toSet());
                Set<Long> longSet = new HashSet<>();
                itemSets.forEach(set -> {
                    longSet.add(Long.parseLong(set));
                });
 
                List<AsnOrderItem> asnOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().in(AsnOrderItem::getPoDetlId, longSet));
                if (asnOrderItems.isEmpty()) {
                    throw new CoolException("ASN单据不存在!!");
                }
                List<Long> list = asnOrderItems.stream().map(AsnOrderItem::getId).collect(Collectors.toList());
 
                /**************************     修改ASN明细单状态         ******************************/
 
 
                if (!asnOrderItemService.update(new LambdaUpdateWrapper<AsnOrderItem>().in(AsnOrderItem::getId, list).set(AsnOrderItem::getStatus, 1))) {
                    throw new CoolException("ASN明细单据状态修改失败!!");
                }
 
                /**************************     修改ASN主单数据         ******************************/
                //获取ASN明细订单标识
                List<Long> ids = asnOrderItems.stream().map(AsnOrderItem::getAsnId).collect(Collectors.toList());
                //ASN明细单据分组
                Map<Long, List<AsnOrderItem>> asnIds = asnOrderItems.stream().collect(Collectors.groupingBy(AsnOrderItem::getAsnId));
                ids.forEach(id -> {
                    int count = asnOrderService.count(new LambdaQueryWrapper<AsnOrder>().in(AsnOrder::getId, id));
                    if (count == asnIds.get(id).size()) {
                        if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>().eq(AsnOrder::getId, id).set(AsnOrder::getStatus, 1))) {
                            throw new CoolException("ASN主单状态修改失败!!");
                        }
                    } else {
                        if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>().eq(AsnOrder::getId, id).set(AsnOrder::getStatus, 2))) {
                            throw new CoolException("ASN主单状态修改失败!!");
                        }
                    }
                });
                return true;
            } else {
                throw new CoolException(reponse.getMsg());
            }
        }
    }
}