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
package com.ruoyi.consumables.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.enums.StockInUnQualifiedRecordTypeEnum;
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumables.dto.ConsumablesInventoryDto;
import com.ruoyi.consumables.dto.ConsumablesOutRecordDto;
import com.ruoyi.consumables.dto.ConsumablesUnInventoryDto;
import com.ruoyi.consumables.execl.ConsumablesOutRecordExportData;
import com.ruoyi.consumables.mapper.ConsumablesInventoryMapper;
import com.ruoyi.consumables.mapper.ConsumablesOutRecordMapper;
import com.ruoyi.consumables.mapper.ConsumablesUnInventoryMapper;
import com.ruoyi.consumables.pojo.ConsumablesInRecord;
import com.ruoyi.consumables.pojo.ConsumablesInventory;
import com.ruoyi.consumables.pojo.ConsumablesOutRecord;
import com.ruoyi.consumables.pojo.ConsumablesUnInventory;
import com.ruoyi.consumables.service.ConsumablesOutRecordService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.util.List;
 
/**
 * <p>
 * 出库记录表 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-01-21 05:27:04
 */
@Service
@AllArgsConstructor
public class ConsumablesOutRecordServiceImpl extends ServiceImpl<ConsumablesOutRecordMapper, ConsumablesOutRecord> implements ConsumablesOutRecordService {
    private ConsumablesOutRecordMapper consumablesOutRecordMapper;
    private ConsumablesInventoryMapper consumablesInventoryMapper;
    private ConsumablesUnInventoryMapper consumablesUnInventoryMapper;
 
    @Override
    public IPage<ConsumablesOutRecordDto> listPage(Page page, ConsumablesOutRecordDto ConsumablesOutRecordDto) {
        return consumablesOutRecordMapper.listPage(page, ConsumablesOutRecordDto);
    }
 
    @Override
    public int add(ConsumablesOutRecordDto ConsumablesOutRecordDto) {
        String no = OrderUtils.countTodayByCreateTime(consumablesOutRecordMapper, "CK");
        ConsumablesOutRecordDto.setOutboundBatches(no);
        ConsumablesInRecord ConsumablesInRecord = new ConsumablesInRecord();
        BeanUtils.copyProperties(ConsumablesOutRecordDto, ConsumablesInRecord);
        return consumablesOutRecordMapper.insert(ConsumablesOutRecordDto);
    }
 
    @Override
    public int update(Long id, ConsumablesOutRecordDto ConsumablesOutRecordDto) {
        // 判断对象是否存在
        ConsumablesOutRecord ConsumablesOutRecord = consumablesOutRecordMapper.selectById(id);
        if (ConsumablesOutRecord == null){
            throw new BaseException("该出库记录不存在,无法更新!!!");
        }
 
        String[] ignoreProperties = {"id", "outbound_batches"};//排除id属性
        BeanUtils.copyProperties(ConsumablesOutRecordDto, ConsumablesOutRecord, ignoreProperties);
        return consumablesOutRecordMapper.updateById(ConsumablesOutRecord);
    }
 
    @Override
    public int batchDelete(List<Long> ids) {
        for (Long id : ids) {
            ConsumablesOutRecord consumablesOutRecord = consumablesOutRecordMapper.selectById(id);
            if (consumablesOutRecord.getType().equals("0")) {
                ConsumablesInventory consumablesInventory = consumablesInventoryMapper.selectOne(new LambdaQueryWrapper<ConsumablesInventory>().eq(ConsumablesInventory::getProductModelId, consumablesOutRecord.getProductModelId()));
                if (consumablesInventory == null) {
                    throw new BaseException("库存记录中没有对应的产品,无法删除!!!");
                }else {
                    ConsumablesInventoryDto consumablesInRecordDto = new ConsumablesInventoryDto();
                    consumablesInRecordDto.setProductModelId(consumablesInventory.getProductModelId());
                    consumablesInRecordDto.setQualitity(consumablesOutRecord.getConsumablesOutNum());
                    consumablesInventoryMapper.updateAddConsumablesInventory(consumablesInRecordDto);
                }
            }else if (consumablesOutRecord.getType().equals("1")) {
                ConsumablesUnInventory consumablesUnInventory = consumablesUnInventoryMapper.selectOne(new LambdaQueryWrapper<ConsumablesUnInventory>().eq(ConsumablesUnInventory::getProductModelId, consumablesOutRecord.getProductModelId()));
                if (consumablesUnInventory == null) {
                    throw new BaseException("库存记录中没有对应的产品,无法删除!!!");
                }else {
                    ConsumablesUnInventoryDto consumablesUnInventoryDto = new ConsumablesUnInventoryDto();
                    consumablesUnInventoryDto.setProductModelId(consumablesUnInventory.getProductModelId());
                    consumablesUnInventoryDto.setQualitity(consumablesOutRecord.getConsumablesOutNum());
                    consumablesUnInventoryMapper.updateAddConsumablesUnInventory(consumablesUnInventoryDto);
                }
            }
        }
        return consumablesOutRecordMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void exportConsumablesOutRecord(HttpServletResponse response, ConsumablesOutRecordDto ConsumablesOutRecordDto) {
        List<ConsumablesOutRecordExportData> list = consumablesOutRecordMapper.listConsumablesOutRecordExportData(ConsumablesOutRecordDto);
        for (ConsumablesOutRecordExportData consumablesInRecordExportData : list) {
            if (consumablesInRecordExportData.getType().equals("0")) {
                consumablesInRecordExportData.setRecordType(EnumUtil.fromCode(StockOutQualifiedRecordTypeEnum.class, Integer.parseInt(consumablesInRecordExportData.getRecordType())).getValue());
            }else {
                consumablesInRecordExportData.setRecordType(EnumUtil.fromCode(StockInUnQualifiedRecordTypeEnum.class, Integer.parseInt(consumablesInRecordExportData.getRecordType())).getValue());
            }
        }
        ExcelUtil<ConsumablesOutRecordExportData> util = new ExcelUtil<>(ConsumablesOutRecordExportData.class);
        util.exportExcel(response,list, "出库记录信息");
    }
}