李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.warehouse.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.warehouse.dto.AdjustDetailDTO;
import com.chinaztt.mes.warehouse.dto.StockAddDTO;
import com.chinaztt.mes.warehouse.entity.AdjustDetail;
import com.chinaztt.mes.warehouse.entity.Stock;
import com.chinaztt.mes.warehouse.mapper.AdjustDetailMapper;
import com.chinaztt.mes.warehouse.mapper.StockMapper;
import com.chinaztt.mes.warehouse.service.AdjustDetailService;
import com.chinaztt.mes.warehouse.util.StockUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * 库存调整表
 *
 * @author sunxl
 * @date 2020-11-03 14:33:19
 */
@Service
@AllArgsConstructor
public class AdjustDetailServiceImpl extends ServiceImpl<AdjustDetailMapper, AdjustDetail> implements AdjustDetailService {
    private NumberGenerator<AdjustDetail> numberGenerator;
    private StockMapper stockMapper;
    private StockUtils stockUtils;
 
    @Override
    public boolean fullSave(AdjustDetail adjustDetail) {
        numberMonitoring(adjustDetail);
        adjustDetail.setAdjNo(numberGenerator.generateNumberWithPrefix(AdjustDetail.DIGIT, AdjustDetail.PREFIX, AdjustDetail::getAdjNo));
        adjustDetail.setAdjTime(LocalDateTime.now());
        baseMapper.insert(adjustDetail);
        return true;
    }
 
    @Override
    public IPage<List<AdjustDetailDTO>> getAdjust(Page page, QueryWrapper<AdjustDetailDTO> gen) {
        return baseMapper.getAdjust(page, gen);
    }
 
    @Override
    public AdjustDetailDTO getAdjustById(Long id) {
        return baseMapper.getAdjustById(id);
    }
 
    @Override
    public boolean updateByAdjust(AdjustDetail adjustDetail) {
        numberMonitoring(adjustDetail);
        baseMapper.updateById(adjustDetail);
        return true;
    }
 
    @Override
    public boolean updateAdjust(List<Long> ids) {
        for (Long id : ids) {
            AdjustDetail adjustDetail = baseMapper.selectById(id);
            adjustDetail.setStatus("已下发");
            baseMapper.updateById(adjustDetail);
            //原零件的实时库存
            Stock stock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId, adjustDetail.getFromPartId())
                    .eq(Stock::getLocationId, adjustDetail.getFromLocId()).eq(Stock::getPartBatchNo, adjustDetail.getFromPartBatchNo()).eq(Stock::getSystemNo, adjustDetail.getSystemNo()));
            stockUtils.updateById(stock.getId(), adjustDetail.getAdjNum().negate(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, adjustDetail.getAdjNo(), "ADJUST");
            //新零件的实时库存
            Stock newStock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId, adjustDetail.getToPartId())
                    .eq(Stock::getLocationId, adjustDetail.getToLocId()).eq(Stock::getPartBatchNo, adjustDetail.getToPartBatchNo()).eq(Stock::getSystemNo, adjustDetail.getSystemNo()));
            //如果存在,加上调整数量
            if (newStock != null) {
                stockUtils.updateById(newStock.getId(), adjustDetail.getAdjNum(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, adjustDetail.getAdjNo(), "ADJUST");
            }
            if (newStock == null) {
                StockAddDTO stockAdd = new StockAddDTO();
                stockAdd.setPartsId(adjustDetail.getToPartId());
                stockAdd.setNewLocationId(adjustDetail.getToLocId());
                stockAdd.setNewPartBatchNo(adjustDetail.getToPartBatchNo());
                stockAdd.setNewSystemNo(adjustDetail.getSystemNo());
 
                Stock stock1 = stockUtils.query(stockAdd);
                stockUtils.updateById(stock1.getId(), adjustDetail.getAdjNum(), BigDecimal.ZERO,  BigDecimal.ZERO, BigDecimal.ZERO,adjustDetail.getAdjNo(), "ADJUST");
            }
        }
        return false;
    }
 
    public void numberMonitoring(AdjustDetail adjustDetail) {
        Stock stock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId, adjustDetail.getFromPartId())
                .eq(Stock::getLocationId, adjustDetail.getFromLocId()).eq(Stock::getPartBatchNo, adjustDetail.getFromPartBatchNo()));
        if (stock.getAvailableStockQuantity().compareTo(adjustDetail.getAdjNum()) == -1) {
            throw new RuntimeException("调整数量超过了可用库存数量,请重新填写");
        }
    }
}