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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.dto.HistoryDto;
import com.yuanchu.mom.dto.WarehouseCellAndSampleDto;
import com.yuanchu.mom.dto.WarehouseDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.service.WarehouseCellService;
import com.yuanchu.mom.service.WarehouseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
* @author Administrator
* @description 针对表【ins_sample_user(样品负责人记录)】的数据库操作Service实现
* @createDate 2024-03-14 17:12:02
*/
@Service
@AllArgsConstructor
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse>
    implements WarehouseService {
 
    private WarehouseMapper warehouseMapper;
 
    private WarehouseShelfMapper warehouseShelfMapper;
 
    private WarehouseCellMapper warehouseCellMapper;
 
    private WarehouseHistoryMapper warehouseHistoryMapper;
 
    private WarehouseCellService warehouseCellService;
 
    private InsSampleMapper insSampleMapper;
 
    private InsOrderStateMapper insOrderStateMapper;
 
    private InsProductMapper insProductMapper;
 
    private InsOrderMapper insOrderMapper;
 
    @Override
    public int addWarehouse(String name) {
        Warehouse warehouse = new Warehouse();
        warehouse.setName(name);
        return warehouseMapper.insert(warehouse);
    }
 
    @Override
    public List<WarehouseDto> selectWarehouse() {
        return warehouseMapper.selectWarehouseList();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addShelf(WarehouseShelf warehouseShelf) {
        warehouseShelfMapper.insert(warehouseShelf);
        List<WarehouseCell> cells = new ArrayList<>();
        for (int i = 1; i < warehouseShelf.getRow() + 1; i++) {
            for (int a = 1; a < warehouseShelf.getCol() + 1; a++) {
                WarehouseCell cell = new WarehouseCell();
                cell.setRow(i);
                cell.setCol(a);
                cell.setState(1);
                cell.setShelfId(warehouseShelf.getId());
                cells.add(cell);
            }
        }
        warehouseCellService.saveBatch(cells);
        return 1;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delWarehouse(Integer id) {
        warehouseShelfMapper.delete(Wrappers.<WarehouseShelf>lambdaUpdate().eq(WarehouseShelf::getWarehouseId, id));
        return warehouseMapper.deleteById(id);
    }
 
    @Override
    public int upWarehouse(Warehouse warehouse) {
        return warehouseMapper.updateById(warehouse);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delShelf(Integer id) {
        warehouseCellMapper.update(null ,Wrappers.<WarehouseCell>lambdaUpdate().eq(WarehouseCell::getShelfId, id).set(WarehouseCell::getState, 0));
        return warehouseShelfMapper.deleteById(id);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upShelf(WarehouseShelf warehouseShelf) {
        WarehouseShelf shelf = warehouseShelfMapper.selectById(warehouseShelf.getId());
        if (shelf.getCol()>=warehouseShelf.getCol() && shelf.getRow() >= warehouseShelf.getRow()) {
            //如果维护的数据小于原有的行列数可以进行修改
            warehouseCellMapper.update(null, Wrappers.<WarehouseCell>lambdaUpdate()
                    .eq(WarehouseCell::getShelfId, warehouseShelf.getId())
                    .and(true, wrapper -> wrapper.gt(WarehouseCell::getRow, warehouseShelf.getRow())
                            .or()
                            .gt(WarehouseCell::getCol, warehouseShelf.getCol()))
                    .set(WarehouseCell::getState, 0));
        }else {
            //如果维护的数据大于原有的行列数需要进行额外添加
            List<WarehouseCell> cells = new ArrayList<>();
            for (int i = 1; i < warehouseShelf.getRow() + 1; i++) {
                for (int a = 1; a < warehouseShelf.getCol() + 1; a++) {
                    WarehouseCell cell = new WarehouseCell();
                    cell.setRow(i);
                    cell.setCol(a);
                    cell.setState(1);
                    cell.setShelfId(warehouseShelf.getId());
                    WarehouseCell warehouseCell = warehouseCellMapper.selectOne(Wrappers.<WarehouseCell>lambdaQuery()
                            .eq(WarehouseCell::getShelfId, warehouseShelf.getId())
                            .eq(WarehouseCell::getRow, i)
                            .eq(WarehouseCell::getCol, a));
                    if (ObjectUtils.isNotEmpty(warehouseCell)){
                        cell.setId(warehouseCell.getId());
                    }
                    cells.add(cell);
                }
            }
            warehouseCellService.saveOrUpdateBatch(cells);
        }
        warehouseShelfMapper.updateById(warehouseShelf);
        return 0;
    }
 
    @Override
    public List<WarehouseCellAndSampleDto> getWarehouse(Integer shelfId,String sampleCode) {
        return warehouseCellMapper.getWarehouse(shelfId,sampleCode);
    }
 
    @Override
    public int inWarehouse(String trees, String sampleCode,String entrustCode) {
        String[] tree = trees.split("-");
        if(tree.length < 4){
            throw new ErrorException("库位选择错误");
        }
        List<InsOrder> insOrders = insOrderMapper.selectList(Wrappers.<InsOrder>lambdaQuery()
                .ne(InsOrder::getState,3)//排除撤销的订单
                .eq(InsOrder::getEntrustCode, entrustCode));
        if(insOrders.size()!=1){
            throw new ErrorException("订单编号输入有误,或者该订单编号已经撤销");
        }
        List<InsSample> samples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery()
                .eq(InsSample::getSampleCode, sampleCode)
                .eq(InsSample::getInsOrderId,insOrders.get(0).getId())
                .select(InsSample::getId,InsSample::getCellId));
        if(samples.size()!=1){
            throw new ErrorException("样品编号输入有误");
        }
        if(samples.get(0).getCellId()!=null){
            throw new ErrorException("样品 " + sampleCode + " 未出库");
        }
        WarehouseCell cell = warehouseCellMapper.selectOne(Wrappers.<WarehouseCell>lambdaQuery().eq(WarehouseCell::getShelfId, tree[1]).eq(WarehouseCell::getRow, tree[2]).eq(WarehouseCell::getCol, tree[3]).eq(WarehouseCell::getState, 1).select(WarehouseCell::getId,WarehouseCell::getRow,WarehouseCell::getCol));
        WarehouseShelf shelf = warehouseShelfMapper.selectById(tree[1]);
        Warehouse warehouse = warehouseMapper.selectById(tree[0]);
        WarehouseHistory history = new WarehouseHistory();
        history.setCellId(cell.getId());
        history.setState(1);
        history.setInsSampleId(samples.get(0).getId());
        history.setWarehouseCode(warehouse.getName()+"-"+shelf.getName()+"-"+cell.getRow()+"-"+cell.getCol());
        samples.get(0).setCellId(cell.getId());
        insSampleMapper.updateById(samples.get(0));
        return warehouseHistoryMapper.insert(history);
    }
 
    @Override
    public int outWarehouse(String sampleCode, boolean a,String entrustCode) {
        List<InsOrder> insOrders = insOrderMapper.selectList(Wrappers.<InsOrder>lambdaQuery()
                .ne(InsOrder::getState,3)//排除撤销的订单
                .eq(InsOrder::getEntrustCode, entrustCode));
        if(insOrders.size()!=1){
            throw new ErrorException("订单编号输入有误,或者该订单编号已经撤销");
        }
        List<InsSample> samples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery()
                .eq(InsSample::getSampleCode, sampleCode)
                .eq(InsSample::getInsOrderId,insOrders.get(0).getId()));
        if(samples.size()!=1){
            throw new ErrorException("样品编号输入有误");
        }
        if(samples.get(0).getCellId()==null){
            throw new ErrorException("样品 " + sampleCode + " 未入库");
        }
        WarehouseHistory history = new WarehouseHistory();
        history.setState(2);
        history.setInsSampleId(samples.get(0).getId());
        history.setCellId(samples.get(0).getCellId());
        history.setWarehouseCode(warehouseHistoryMapper.selectOne(Wrappers.<WarehouseHistory>lambdaQuery().eq(WarehouseHistory::getInsSampleId, samples.get(0).getId()).select(WarehouseHistory::getWarehouseCode).orderByDesc(WarehouseHistory::getId).last("limit 1")).getWarehouseCode());
        insSampleMapper.update(null, Wrappers.<InsSample>lambdaUpdate().eq(InsSample::getId, samples.get(0).getId()).set(InsSample::getCellId, null));
        //该订单闭环
        InsOrder insOrder = insOrders.get(0);
        insOrder.setState(4);
        insOrderMapper.updateById(insOrder);
        return warehouseHistoryMapper.insert(history);
    }
 
    @Override
    public Map<String, Object> getSampleRecord(Integer id) {
        InsSample insSample = insSampleMapper.selectById(id);
        List<HistoryDto> histories = warehouseHistoryMapper.getHistoryListBySampleId(id);
        Map<String, Object> map = new HashMap<>();
        Map<String, String> sampleHistory = new HashMap<>();
        WarehouseHistory history = histories.get(histories.size() - 1);
        if(history.getState() == 1){
            sampleHistory.put("date", history.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            sampleHistory.put("user", warehouseHistoryMapper.getUserNameById(history.getCreateUser()));
            sampleHistory.put("code", history.getWarehouseCode());
        }
        map.put("sampleHistory", sampleHistory);
        map.put("insSample", insSample);
        map.put("histories", histories);
        map.put("products", insProductMapper.getProductAndResult(id));
        return map;
    }
 
}