skyouc
2 天以前 f84b3af27f34be7e22990c54612f3110559c7710
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package com.vincent.rsf.server.api.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.controller.erp.params.*;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.manager.controller.dto.LocStockDto;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.CompanysType;
import com.vincent.rsf.server.manager.enums.OrderType;
import com.vincent.rsf.server.manager.enums.OrderWorkType;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.manager.service.impl.MatnrServiceImpl;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.entity.Fields;
import com.vincent.rsf.server.system.service.FieldsItemService;
import com.vincent.rsf.server.system.service.FieldsService;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import com.vincent.rsf.server.api.service.ReceiveMsgService;
import lombok.extern.slf4j.Slf4j;
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.*;
import java.util.stream.Collectors;
 
/**
 * @author Ryan
 * @version 1.0
 * @title ErpApiServiceImpl
 * @description
 * @create 2025/3/4 16:27
 */
@Slf4j
@Service("erpApiService")
public class ReceiveMsgServiceImpl implements ReceiveMsgService {
 
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseItemService purchaseItemService;
    @Autowired
    private FieldsService fieldsService;
    @Autowired
    private FieldsItemService fieldsItemService;
    @Autowired
    private MatnrGroupService matnrGroupService;
    @Autowired
    private MatnrServiceImpl matnrService;
    @Autowired
    private AsnOrderService asnOrderService;
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private DeliveryItemService deliveryItemService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocItemService locItemService;
    @Autowired
    private WarehouseAreasService warehouseAreasService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private CompanysService companysService;
 
 
    /**
     * @author Ryan
     * @date 2025/8/15
     * @description: 保存PO/DO单据
     * @version 1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean syncPurchasee(List<OrderParams> orders) {
        if (orders.isEmpty()) {
            throw new CoolException("单据内容不能为空!!");
        }
        orders.forEach(ors -> {
            if (ors.getType().equals("po")) {
                Purchase purchase = new Purchase();
                BeanUtils.copyProperties(ors, purchase);
                String wkVal = SerialRuleUtils.generateRuleCode(SerialRuleCode.PURCHASE_CODE, purchase);
                purchase.setCode(wkVal)
                        .setType(OrderType.ORDER_IN.type);
                if (!purchaseService.save(purchase)) {
                    throw new CoolException("采购单据保存失败");
                }
 
                //查询扩展字段是否存在
                List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>().eq(Fields::getStatus, 1).eq(Fields::getFlagEnable, 1));
 
                //判断子列表不为空
                if (!ors.getChildren().isEmpty()) {
                    ArrayList<PurchaseItem> list = new ArrayList<>();
                    ors.getChildren().forEach(orderItem -> {
                        PurchaseItem item = new PurchaseItem();
                        BeanUtils.copyProperties(orderItem, item);
//                    if (!fields.isEmpty()) {
//                        List<String> fieldValue = fields.stream().map(Fields::getFields).collect(Collectors.toList());
//                        fieldValue.forEach(value -> {
//
//                        });
//                    }
                        item.setPurchaseId(purchase.getId());
                        list.add(item);
                    });
                    if (!purchaseItemService.saveBatch(list)) {
                        throw new CoolException("采购单明细保存失败!!");
                    }
                }
            } else {
                Delivery delivery = new Delivery();
                BeanUtils.copyProperties(ors, delivery);
                String wkVal = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_DELIVERY_RULE_CODE, delivery);
                delivery.setCode(wkVal)
                        .setType(OrderType.ORDER_OUT.type);
                if (!deliveryService.save(delivery)) {
                    throw new CoolException("采购单据保存失败");
                }
 
                //查询扩展字段是否存在
                List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>().eq(Fields::getStatus, 1).eq(Fields::getFlagEnable, 1));
 
                //判断子列表不为空
                if (!ors.getChildren().isEmpty()) {
                    ArrayList<DeliveryItem> list = new ArrayList<>();
                    ors.getChildren().forEach(orderItem -> {
                        DeliveryItem item = new DeliveryItem();
                        BeanUtils.copyProperties(orderItem, item);
//                    if (!fields.isEmpty()) {
//                        List<String> fieldValue = fields.stream().map(Fields::getFields).collect(Collectors.toList());
//                        fieldValue.forEach(value -> {
//
//                        });
//                    }
                        item.setDeliveryId(delivery.getId());
                        list.add(item);
                    });
                    if (!deliveryItemService.saveBatch(list)) {
                        throw new CoolException("采购单明细保存失败!!");
                    }
                }
            }
        });
 
        return true;
    }
 
    /**
     * 物料信息同步
     *
     * @param matnrs
     */
    @Override
    public void syncMatnrs(List<BaseMatParms> matnrs) {
        List<Matnr> syncMatnrs = new ArrayList<>();
        if (!matnrs.isEmpty()) {
            matnrs.forEach(matnr -> {
                Matnr mat = new Matnr();
                if (Objects.isNull(matnr.getMatnr())) {
                    throw new RuntimeException("物料编码不能为空!!");
                }
                BeanUtils.copyProperties(matnr, mat);
                mat.setCode(matnr.getMatnr()).setName(matnr.getMaktx());
                if (!Objects.isNull(matnr.getGroupName())) {
                    MatnrGroup matnrGroup = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getName, matnr.getGroupName()), false);
                    if (Objects.isNull(matnrGroup)) {
                        mat.setGroupCode(matnrGroup.getCode()).setGroupId(matnrGroup.getId());
                    }
                }
                syncMatnrs.add(mat);
            });
 
            if (!matnrService.saveOrUpdateBatch(syncMatnrs)) {
                throw new CoolException("物料信息保存成功!!");
            }
        }
    }
 
    /**
     * @author Ryan
     * @date 2025/8/15
     * @description: 订单查询
     * @version 1.0
     */
    @Override
    public R queryOrderStatus(QueryOrderParam queryParams) {
        WkOrder wkOrders = asnOrderService.getOne(new LambdaQueryWrapper<WkOrder>()
                .eq(WkOrder::getCode, queryParams.getOrderNo())
                .eq(WkOrder::getType, queryParams.getType()));
        if (Objects.isNull(wkOrders)) {
            throw new CoolException("单据不存在!!");
        }
        List<WkOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, wkOrders.getId()));
 
        WkOrderDto wkorderDto = new WkOrderDto();
        wkorderDto.setOrder(wkOrders).setOrderItems(orderItems);
 
        return R.ok().add(wkorderDto);
    }
 
    /**
     * @author Ryan
     * @date 2025/8/15
     * @description: 查询库位信息
     * @version 1.0
     */
    @Override
    public R syncLocsDetl(PageParam<Loc, BaseParam> pageParam, QueryWrapper<Loc> wrapper) {
        Page<Object> page = new Page<>();
        page.setCurrent(pageParam.getCurrent()).setSize(pageParam.getSize());
        IPage<LocStockDto> locStocks =  locService.getLocDetls(page);
        return R.ok().add(locStocks);
    }
 
    /**
     * @author Ryan
     * @date 2025/8/18
     * @description: 库位同步
     * @version 1.0
     */
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public R syncLocs(List<SyncLocsParams> locs) {
        List<Loc> syncLocs = new ArrayList<>();
        locs.forEach(loc -> {
            Loc loc1 = new Loc();
            BeanUtils.copyProperties(loc, loc1);
            loc1.setCode(loc.getLocCode()).setId(null);
            syncLocs.add(loc1);
        });
        if (!locService.saveBatch(syncLocs)) {
            throw new CoolException("库位同步失败!!");
        }
        return R.ok();
    }
 
    /**
     * @author Ryan
     * @date 2025/8/18
     * @description: 物料信息同步
     * @version 1.0
     */
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public R syncMatGroups(List<SyncMatGroupsParams> matGroupsParams) {
        List<MatnrGroup> syncMatGroups = new ArrayList<>();
        matGroupsParams.forEach(matGroupsParam -> {
            MatnrGroup matnrGroup = new MatnrGroup();
            BeanUtils.copyProperties(matGroupsParam, matnrGroup);
            if (Objects.isNull(matGroupsParam.getCode())) {
                throw new CoolException("物料分组编码不能为空!!");
            }
            if (Objects.isNull(matGroupsParam.getName())) {
                throw new CoolException("分组名称不能为空!!");
            }
            if (Objects.isNull(matGroupsParam.getParCode())) {
                throw new CoolException("上级物料分组编码不能为空!!");
            }
            syncMatGroups.add(matnrGroup);
        });
        if (!matnrGroupService.saveBatch(syncMatGroups)) {
            throw new CoolException("物料分组保存失败!!");
        }
        return R.ok();
    }
 
    /**
     * @author Ryan
     * @date 2025/8/18
     * @description: 库区信息同步
     * @version 1.0
     */
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public R syncWarehouseAreas(List<LocAreasParams> areasParams) {
        areasParams.forEach(param -> {
            WarehouseAreas locArea = new WarehouseAreas();
            BeanUtils.copyProperties(param, locArea);
            WarehouseAreas warehouseAreas = warehouseAreasService
                    .getOne(new LambdaQueryWrapper<WarehouseAreas>()
                    .eq(WarehouseAreas::getName, param.getName()));
            if (!Objects.isNull(warehouseAreas)) {
                locArea.setWarehouseId(warehouseAreas.getId());
            }
            locArea.setName(param.getName())
                    .setCode(param.getCode())
                    .setId(null);
            if (!warehouseAreasService.save(locArea)) {
                throw new CoolException("库区保存失败!!");
            }
        });
        return R.ok();
    }
 
    /**
     * @author Ryan
     * @date 2025/8/18
     * @description: 仓库同步
     * @version 1.0
     */
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public R syncWarehouse(List<WarehouseParams> warehouses) {
        warehouses.forEach(warehouse -> {
            Warehouse ware = new Warehouse();
            BeanUtils.copyProperties(warehouse, ware);
            ware.setId(null);
            if (!warehouseService.save(ware)) {
                throw new CoolException("仓库同步保存失败!!");
            }
        });
        return R.ok();
    }
 
    /**
     * @author Ryan
     * @date 2025/8/19
     * @description: 同步企业信息
     * @version 1.0
     */
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public R syncCompanies(List<CompaniesParam> companyParams) {
        companyParams.forEach(param -> {
            Companys companys = new Companys();
            BeanUtils.copyProperties(param, companys);
            if (Objects.isNull(companys.getCode())) {
                throw new CoolException("企业编码不能为空!!");
            }
            Companys one = companysService.getOne(new LambdaQueryWrapper<Companys>().eq(Companys::getName, param.getName()));
            if (Objects.isNull(one)) {
                String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_COMPANYS_CODE, null);
                companys.setCode(ruleCode);
            } else {
                throw new CoolException(one.getName() +  ",企业名重复!!");
            }
            companys.setType(CompanysType.getCustomVal(param.getType()))
                    .setId(null);
            if (!companysService.save(companys)) {
                throw new CoolException("企业保存失败!!");
            }
        });
        return R.ok();
    }
 
 
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public R syncCheckOrder(SyncOrderParams syncOrders, Long loginUserId) {
        List<SyncOrdersItem> ordersItems = syncOrders.getItems();
        Map<String, List<SyncOrdersItem>> listMap = ordersItems.stream().collect(Collectors.groupingBy(SyncOrdersItem::getOrderCode));
        listMap.keySet().forEach(orderCode -> {
            WkOrder wkOrder = new WkOrder();
            String ruleCode = null;
            if (syncOrders.getType().equals(OrderType.ORDER_CHECK.type)) {
                 ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_CHECK_RULE_CODE, null);
            } else if (syncOrders.getType().equals(OrderType.ORDER_IN.type)) {
                ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_ASN_ORDER, null);
            } else if (syncOrders.getType().equals(OrderType.ORDER_OUT.type)) {
                ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_OUT_STOCK_CODE, null);
            }  else if (syncOrders.getType().equals(OrderType.ORDER_REVISE.type)) {
                ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_LOC_REVISE_CODE, null);
            } else if (syncOrders.getType().equals(OrderType.ORDER_TRANSFER.type)) {
                ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TRANSFER_ORDER_CODE, null);
            }
 
            SyncOrdersItem ordersItem = listMap.get(orderCode).stream().findFirst().get();
            Double anfmes = listMap.get(orderCode).stream().mapToDouble(SyncOrdersItem::getAnfme).sum();
            wkOrder.setType(syncOrders.getType())
                    .setWkType(OrderWorkType.getWorkType(ordersItem.getWkType()))
                    .setAnfme(anfmes)
                    .setPoCode(orderCode)
                    .setPoId(ordersItem.getId())
                    .setCode(ruleCode)
                    .setId(null)
                    .setCreateTime(new Date())
                    .setUpdateTime(new Date())
                    .setCreateBy(loginUserId)
                    .setUpdateBy(loginUserId);
 
            if (!asnOrderService.save(wkOrder)) {
                throw new CoolException("单据保存失败!!");
            }
 
            listMap.get(orderCode).forEach(order -> {
                WkOrderItem wkOrderItem = new WkOrderItem();
                BeanUtils.copyProperties(order, wkOrderItem);
                wkOrderItem.setOrderCode(wkOrder.getCode())
                        .setOrderId(wkOrder.getId())
                        .setId(null);
 
                if (!asnOrderItemService.save(wkOrderItem)) {
                    throw new CoolException("单据明细保存失败!!");
                }
            });
        });
 
        return R.ok();
    }
 
}