zouyu
2023-09-27 75b2904fbaf4fab6a9fb3baf065d2c927cc7a7a2
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.mapper.ConsignmentMapper;
import com.yuanchu.mom.mapper.ReceiptMapper;
import com.yuanchu.mom.mapper.RepertoryMapper;
import com.yuanchu.mom.pojo.Consignment;
import com.yuanchu.mom.pojo.Receipt;
import com.yuanchu.mom.pojo.Repertory;
import com.yuanchu.mom.pojo.dto.ConsignmentDto;
import com.yuanchu.mom.pojo.dto.ConsignmentDto2;
import com.yuanchu.mom.service.ConsignmentService;
import com.yuanchu.mom.utils.MyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 成品发货表(Consignment)表服务实现类
 *
 * @author zss
 * @since 2023-08-10 15:08:02
 */
@Service
public class ConsignmentServiceImpl extends ServiceImpl<ConsignmentMapper, Consignment> implements ConsignmentService {
 
    @Resource
    ConsignmentMapper consignmentMapper;
 
    @Resource
    RepertoryMapper repertoryMapper;
 
    @Resource
    ReceiptMapper receiptMapper;
 
    //新增成品发货
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCon(String name, ConsignmentDto consignmentDto) {
        //生成一个押运单编号
        String escortCode = MyUtil.getTimeSixNumberCode("YY", "num");
        List<Consignment> consignments = consignmentMapper.selectAll();
        //如果发货表中没有数据也可以发货
        if (consignments.size() == 0) {
            /*新增成品发货*/
            return addConsign(name, consignmentDto, escortCode);
        }
        //获取发货表中的合同编号集合
        List<String> customerCodeList = consignments.stream().map(consignment -> {
            String customerCode = consignment.getCustomerCode();
            return customerCode;
        }).distinct().collect(Collectors.toList());
        if (customerCodeList.contains(consignmentDto.getCustomerCode())) {
            return "该合同订单已发过货,无法再次发货!";
        }
        /*新增成品发货*/
        return addConsign(name, consignmentDto, escortCode);
    }
 
    //查询发货记录列表
    @Override
    public IPage<Map<String, Object>> selectAllCon(Page<Object> page, String name, String specifications, String time) {
        return consignmentMapper.selectAllCon(page, name, specifications, time);
    }
 
    //新增发货一系列操作
    private String addConsign(String name, ConsignmentDto consignmentDto, String escortCode) {
        /*新增成品发货*/
        List<ConsignmentDto2> messages = consignmentDto.getMessages();
        for (ConsignmentDto2 message : messages) {
            //查询产品库存数量
            LambdaQueryWrapper<Repertory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Repertory::getName, message.getName())
                    .eq(Repertory::getSpecifications, message.getSpecifications())
                    .eq(Repertory::getUnit, message.getUnit())
                    .eq(Repertory::getType, 0)
                    .eq(Repertory::getOrderCode, consignmentDto.getOrderCode());
            Repertory repertory = repertoryMapper.selectOne(queryWrapper);
            //该成品库存不为空且数量大于等于发货的数量才能进行发货
            if (ObjectUtils.isNotEmpty(repertory) && repertory.getNumber() < message.getNumber()) {
                return "库存不足,无法发货!";
            }
            //构造成品发货实体类
            Consignment consig = Consignment.builder()
                    .customerCode(consignmentDto.getCustomerCode())
                    .escortCode(escortCode)
                    .orderCode(consignmentDto.getOrderCode())
                    .userName(name)
                    .checkName(consignmentDto.getCheckName())
                    .fitName(consignmentDto.getFitName())
                    .name(message.getName())
                    .specifications(message.getSpecifications())
                    .unit(message.getUnit())
                    .number(message.getNumber())
                    .qualityTraceability(repertory.getQualityTraceability())
                    .build();
            consignmentMapper.insert(consig);
            /*减少对应的库存*/
            repertory.setNumber(repertory.getNumber() - consig.getNumber());
            repertoryMapper.updateById(repertory);
        }
        /*发货成功之后需要新增一张回单*/
        Receipt receipt = new Receipt();
        BeanUtils.copyProperties(consignmentDto, receipt);
        receipt.setName(consignmentDto.getUsername());//收货联系人
        receipt.setUserName(name);//发货人
        receipt.setEscortCode(escortCode);//押运单编号
        receipt.setDeliverTime(new Date());//发货日期
        receiptMapper.insert(receipt);
        return "发货成功!";
    }
}