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
package com.ruoyi.consumables.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumables.dto.ConsumablesInRecordDto;
import com.ruoyi.consumables.dto.ConsumablesInventoryDto;
import com.ruoyi.consumables.dto.ConsumablesOutRecordDto;
import com.ruoyi.consumables.dto.ConsumablesUnInventoryDto;
import com.ruoyi.consumables.execl.ConsumablesUnInventoryExportData;
import com.ruoyi.consumables.mapper.ConsumablesUnInventoryMapper;
import com.ruoyi.consumables.pojo.ConsumablesUnInventory;
import com.ruoyi.consumables.service.ConsumablesInRecordService;
import com.ruoyi.consumables.service.ConsumablesOutRecordService;
import com.ruoyi.consumables.service.ConsumablesUnInventoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.util.List;
 
/**
 * <p>
 * 不合格库存表 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-01-22 10:17:45
 */
@Service
@AllArgsConstructor
public class ConsumablesUnInventoryServiceImpl extends ServiceImpl<ConsumablesUnInventoryMapper, ConsumablesUnInventory> implements ConsumablesUnInventoryService {
 
    private ConsumablesUnInventoryMapper ConsumablesUnInventoryMapper;
    private ConsumablesOutRecordService ConsumablesOutRecordService;
    private ConsumablesInRecordService ConsumablesInRecordService;
 
    @Override
    public IPage<ConsumablesUnInventoryDto> pageConsumablesUnInventory(Page page, ConsumablesUnInventoryDto ConsumablesUnInventoryDto) {
        return ConsumablesUnInventoryMapper.pageConsumablesUnInventory(page, ConsumablesUnInventoryDto);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addConsumablesUnInventory(ConsumablesUnInventoryDto ConsumablesUnInventoryDto) {
        //新增入库记录再添加库存
        ConsumablesInRecordDto ConsumablesInRecordDto = new ConsumablesInRecordDto();
        ConsumablesInRecordDto.setRecordId(ConsumablesUnInventoryDto.getRecordId());
        ConsumablesInRecordDto.setRecordType(ConsumablesUnInventoryDto.getRecordType());
        ConsumablesInRecordDto.setConsumablesInNum(ConsumablesUnInventoryDto.getQualitity());
        ConsumablesInRecordDto.setProductModelId(ConsumablesUnInventoryDto.getProductModelId());
        ConsumablesInRecordDto.setType("1");
        ConsumablesInRecordService.add(ConsumablesInRecordDto);
        //再进行新增库存数量库存
        //先查询库存表中的产品是否存在,不存在新增,存在更新
        ConsumablesUnInventory oldConsumablesUnInventory = ConsumablesUnInventoryMapper.selectOne(new QueryWrapper<ConsumablesUnInventory>().lambda().eq(ConsumablesUnInventory::getProductModelId, ConsumablesUnInventoryDto.getProductModelId()));
        if (ObjectUtils.isEmpty(oldConsumablesUnInventory)) {
            ConsumablesUnInventory newConsumablesUnInventory = new ConsumablesUnInventory();
            newConsumablesUnInventory.setProductModelId(ConsumablesUnInventoryDto.getProductModelId());
            newConsumablesUnInventory.setQualitity(ConsumablesUnInventoryDto.getQualitity());
            newConsumablesUnInventory.setVersion(1);
            newConsumablesUnInventory.setRemark(ConsumablesUnInventoryDto.getRemark());
            ConsumablesUnInventoryMapper.insert(newConsumablesUnInventory);
        }else {
            ConsumablesUnInventoryMapper.updateAddConsumablesUnInventory(ConsumablesUnInventoryDto);
        }
        return 1;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer subtractConsumablesUnInventory(ConsumablesUnInventoryDto ConsumablesUnInventoryDto) {
 
        //  新增出库记录
        ConsumablesOutRecordDto ConsumablesOutRecordDto = new ConsumablesOutRecordDto();
        ConsumablesOutRecordDto.setRecordId(ConsumablesUnInventoryDto.getRecordId());
        ConsumablesOutRecordDto.setRecordType(ConsumablesUnInventoryDto.getRecordType());
        ConsumablesOutRecordDto.setConsumablesOutNum(ConsumablesUnInventoryDto.getQualitity());
        ConsumablesOutRecordDto.setProductModelId(ConsumablesUnInventoryDto.getProductModelId());
        ConsumablesOutRecordDto.setType("1");
        ConsumablesOutRecordService.add(ConsumablesOutRecordDto);
        ConsumablesUnInventory oldConsumablesInventory = ConsumablesUnInventoryMapper.selectOne(new QueryWrapper<ConsumablesUnInventory>().lambda().eq(ConsumablesUnInventory::getProductModelId, ConsumablesUnInventoryDto.getProductModelId()));
        if (ObjectUtils.isEmpty(oldConsumablesInventory)) {
            throw new RuntimeException("产品库存不存在");
        }else {
            ConsumablesUnInventoryMapper.updateSubtractConsumablesUnInventory(ConsumablesUnInventoryDto);
        }
        return 1;
    }
 
    @Override
    public void exportConsumablesUnInventory(HttpServletResponse response, ConsumablesUnInventoryDto ConsumablesUnInventoryDto) {
        List<ConsumablesUnInventoryExportData> list = ConsumablesUnInventoryMapper.listConsumablesInventoryExportData(ConsumablesUnInventoryDto);
        ExcelUtil<ConsumablesUnInventoryExportData> util = new ExcelUtil<>(ConsumablesUnInventoryExportData.class);
        util.exportExcel(response,list, "不合格库存信息");
    }
 
    @Override
    public Boolean frozenConsumables(ConsumablesInventoryDto ConsumablesInventoryDto) {
        ConsumablesUnInventory ConsumablesUnInventory = ConsumablesUnInventoryMapper.selectById(ConsumablesInventoryDto.getId());
        if (ConsumablesUnInventory.getQualitity().compareTo(ConsumablesInventoryDto.getLockedQuantity())<0) {
            throw new RuntimeException("冻结数量不能超过库存数量");
        }
        if (ObjectUtils.isEmpty(ConsumablesUnInventory.getLockedQuantity())) {
            ConsumablesUnInventory.setLockedQuantity(ConsumablesInventoryDto.getLockedQuantity());
        }else {
            ConsumablesUnInventory.setLockedQuantity(ConsumablesUnInventory.getLockedQuantity().add(ConsumablesInventoryDto.getLockedQuantity()));
        }
        return this.updateById(ConsumablesUnInventory);
    }
 
    @Override
    public Boolean thawConsumables(ConsumablesInventoryDto ConsumablesInventoryDto) {
        ConsumablesUnInventory ConsumablesUnInventory = ConsumablesUnInventoryMapper.selectById(ConsumablesInventoryDto.getId());
        if (ConsumablesUnInventory.getLockedQuantity().compareTo(ConsumablesInventoryDto.getLockedQuantity())<0) {
            throw new RuntimeException("解冻数量不能超过冻结数量");
        }
        ConsumablesUnInventory.setLockedQuantity(ConsumablesUnInventory.getLockedQuantity().subtract(ConsumablesInventoryDto.getLockedQuantity()));
        return this.updateById(ConsumablesUnInventory);
    }
}