maven
4 天以前 3ca0af49b87942c86cdd973d5a0aea5757b87e71
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
package com.ruoyi.procurementrecord.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.procurementrecord.dto.*;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
import com.ruoyi.procurementrecord.pojo.ProcurementRecord;
import com.ruoyi.procurementrecord.service.ProcurementRecordService;
import com.ruoyi.quality.pojo.QualityInspect;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/7/7 14:38
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ProcurementRecordServiceImpl extends ServiceImpl<ProcurementRecordMapper, ProcurementRecord> implements ProcurementRecordService {
 
    private final ProcurementRecordMapper procurementRecordMapper;
 
    private final SalesLedgerProductMapper salesLedgerProductMapper;
 
    @Override
    public List<ProcurementDto> listProcurementBySalesLedgerId(ProcurementDto procurementDto) {
        List<ProcurementDto> procurementDtos = procurementRecordMapper.listProcurementBySalesLedgerId(procurementDto);
        // 计算待入库数量
        // 查询采购记录已入库数量
        List<Integer> collect = procurementDtos.stream().map(ProcurementDto::getId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty( collect)){
            return procurementDtos;
        }
        LambdaQueryWrapper<ProcurementRecord> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        procurementRecordLambdaQueryWrapper.in(ProcurementRecord::getSalesLedgerProductId, collect);
        List<ProcurementRecord> procurementRecords = procurementRecordMapper.selectList(procurementRecordLambdaQueryWrapper);
        if(CollectionUtils.isEmpty( procurementRecords)){
            return procurementDtos;
        }
        for (ProcurementDto dto : procurementDtos) {
            // 根据采购台账ID筛选对应的入库记录
            List<ProcurementRecord> collect1 = procurementRecords.stream()
                    .filter(procurementRecord -> procurementRecord.getSalesLedgerProductId().equals(dto.getId()))
                    .collect(Collectors.toList());
            
            // 如果没有相关的入库记录,跳过该条数据
            if(CollectionUtils.isEmpty(collect1)){
                dto.setQuantity0(dto.getQuantity());
                continue;
            }
            
            // 计算已入库数量总和,并设置待入库数量
            BigDecimal totalInboundNum = collect1.stream()
                    .map(ProcurementRecord::getInboundNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            // 待入库数量 = 总数量 - 已入库数量
            dto.setQuantity0(dto.getQuantity().subtract(totalInboundNum));
        }
        return procurementDtos;
    }
 
    public ProcurementRecord getProcurementRecordById(Integer id){
        ProcurementRecord procurementRecord = procurementRecordMapper.selectById(id);
        if(procurementRecord == null) {
            throw new RuntimeException("未找到该采购入库记录");
        }
        return procurementRecord;
    }
 
    public List<ProcurementRecord> getProcurementRecordByIds(List<Integer> id){
        List<ProcurementRecord> procurementRecord = procurementRecordMapper.selectBatchIds(id);
        if(procurementRecord == null) {
            throw new RuntimeException("未找到该采购入库记录");
        }
        return procurementRecord;
    }
 
    @Override
    public int updatePro(ProcurementUpdateDto procurementDto) {
        ProcurementRecord procurementRecordById = getProcurementRecordById(procurementDto.getId());
        procurementRecordById.setInboundNum(procurementDto.getQuantityStock());
        return procurementRecordMapper.updateById(procurementRecordById);
    }
 
    @Override
    public int deletePro(ProcurementUpdateDto procurementDto) {
        List<ProcurementRecord> procurementRecordById = getProcurementRecordByIds(procurementDto.getIds());
        procurementRecordMapper.deleteBatchIds(procurementRecordById.stream().map(ProcurementRecord::getId).collect(Collectors.toList()));
        return 0;
    }
 
    @Override
    public void export(HttpServletResponse response) {
        List<ProcurementPageDto> list =procurementRecordMapper.list();
        ExcelUtil<ProcurementPageDto> util = new ExcelUtil<ProcurementPageDto>(ProcurementPageDto.class);
        util.exportExcel(response, list, "入库台账");
    }
 
    @Override
    public int add(ProcurementAddDto procurementDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 批量新增
        for (Details detail : procurementDto.getDetails()) {
            // 查询采购入库数量
            LambdaQueryWrapper<ProcurementRecord> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            procurementRecordLambdaQueryWrapper.eq(ProcurementRecord::getSalesLedgerProductId, detail.getId());
            Long aLong = procurementRecordMapper.selectCount(procurementRecordLambdaQueryWrapper);
 
            ProcurementRecord.ProcurementRecordBuilder procurementRecordBuilder = ProcurementRecord.builder()
                    .salesLedgerProductId(detail.getId())
                    .inboundBatches(aLong.equals(0L) ? "第1批次" : "第"+ (aLong + 1) + "批次")
                    .inboundNum(detail.getInboundQuantity())
                    .createDate(LocalDateTime.now())
                    .tenantId(loginUser.getTenantId())
                    .createBy(procurementDto.getNickName());
            this.save(procurementRecordBuilder.build());
            // 入库成功减掉采购数量
//            LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            salesLedgerProductLambdaQueryWrapper.eq(SalesLedgerProduct::getId, detail.getId());
//            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectOne(salesLedgerProductLambdaQueryWrapper);
//            if(salesLedgerProduct == null){
//               throw new RuntimeException("未找到该商品");
//            }
//            salesLedgerProduct.setQuantity(salesLedgerProduct.getQuantity().subtract(detail.getInboundQuantity()));
//            salesLedgerProductMapper.updateById(salesLedgerProduct);
        }
        return 1;
    }
 
    @Override
    public IPage<ProcurementPageDto> listPage(Page page, ProcurementPageDto procurementDto) {
        return procurementRecordMapper.listPage(page,procurementDto);
    }
 
}