kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package com.nanometer.smartlab.service;
 
import com.nanometer.smartlab.dao.BaseMetaDao;
import com.nanometer.smartlab.dao.OpeReagentStatusDao;
import com.nanometer.smartlab.dao.SysWarehouseDao;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.dto.InWarehouseInfoDto;
import com.nanometer.smartlab.entity.dto.ReagentReceivingDto;
import com.nanometer.smartlab.entity.dto.SysWarehouseDto;
import com.nanometer.smartlab.entity.enumtype.*;
import com.nanometer.smartlab.exception.AlarmCode;
import com.nanometer.smartlab.exception.AlarmException;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.util.IDUtils;
import com.nanometer.smartlab.util.MessageUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * Created by cmower on 17/11/20.
 */
@Service("sysWarehouseService")
public class SysWarehouseServiceImpl implements SysWarehouseService {
 
    private static Logger logger = Logger.getLogger(SysWarehouseService.class);
 
    @Resource(name = "sysWarehouseDao")
    SysWarehouseDao sysWarehouseDao;
    @Resource
    DangerousEncodeService dangerousEncodeService;
    @Resource
    BaseMetaService baseMetaService;
    @Resource
    BaseMetaDao baseMetaDao;
    @Resource
    SysReagentService sysReagentService;
    @Resource
    OpeReagentStatusService opeReagentStatusService;
    @Resource
    OpeUseFlowService opeUseFlowService;
    @Resource
    OpeWarehouseReserveService opeWarehouseReserveService;
    @Resource
    private SysLaboratoryService sysLaboratoryService;
    @Resource
    private OpeReagentStatusDao opeReagentStatusDao;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysWarehouse> getSysWarehouseList(String type, String name, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(type)) {
                params.put("type", type);
            }
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.sysWarehouseDao.getSysWarehouseList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public int getSysWarehouseTotalCount(String type, String name) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(type)) {
                params.put("type", type);
            }
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            return this.sysWarehouseDao.getSysWarehouseTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysWarehouse getSysWarehouse(Long id) {
        try {
            return this.sysWarehouseDao.getSysWarehouse(id);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public SysWarehouse insertSysWarehouse(SysWarehouse sysWarehouse) {
        try {
//            if (sysWarehouse.getId() == null) {
//                sysWarehouse.setId(IDUtils.uuid());
//            }
            this.sysWarehouseDao.insertSysWarehouse(sysWarehouse);
            return sysWarehouse;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateSysWarehouse(SysWarehouse sysWarehouse) {
        try {
            int row = this.sysWarehouseDao.updateSysWarehouse(sysWarehouse);
 
            if (row == 0) {
                return false;
            }
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteSysWarehouse(List<SysWarehouse> sysWarehouseList) {
        try {
            if (sysWarehouseList == null || sysWarehouseList.size() == 0) {
                return false;
            }
 
            List<Long> ids = new ArrayList<Long>();
            for (SysWarehouse sysWarehouse : sysWarehouseList) {
                ids.add(sysWarehouse.getId());
            }
 
            int row = this.sysWarehouseDao.deleteSysWarehouses(ids);
            if (row == 0) {
                return false;
            }
 
            return true;
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
    public List<SysWarehouse> getAllSysWarehouseList() {
        return this.sysWarehouseDao.getAllSysWarehouseList();
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean isSysWarehouseExist(String barCode, Long editId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("barCode", barCode);
            params.put("editId", editId);
 
            int count = this.sysWarehouseDao.getSysWarehouseTotalCount(params);
            return count > 0;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Override
    public List<SysWarehouseDto> getWarehouseList() {
        return sysWarehouseDao.selectWarehouse();
    }
 
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void putInWarehouse(InWarehouseInfoDto inWarehouseInfo) throws Exception {
        List<String> codes = inWarehouseInfo.getReagentCode();
        if (codes !=null && codes.size()>0){
            //
            codes.forEach(code->{
             //获取试剂名称码 5到8位
                String reagentCode = code.substring(4, 8);
                //获取厂商编码 第3位
                String factoryCode = code.substring(2, 3);
                //获取规格  第4位
                String specificationsCode = code.substring(3, 4);
                //获取包装  第19位
                String packingCode = code.substring(18, 19);
 
                //厂商
                BaseMeta factory = baseMetaService
                        .getBaseMeta("encode_replace_"+ReplaceDictType.factory.getKey(), factoryCode,null);
                //规格
                BaseMeta specifications = baseMetaService
                        .getBaseMeta("encode_replace_"+ReplaceDictType.specifications.getKey(), specificationsCode,null);
                //厂商信息
                BaseMeta packing= baseMetaService
                        .getBaseMeta("encode_replace_"+ReplaceDictType.packing.getKey(), packingCode,null);
 
                //Todo 校验
                DangerousEncode dangerousEncode = new DangerousEncode();
                //数据库存储的前缀00空着
                dangerousEncode.setReagentCode("00"+reagentCode);
                List<DangerousEncode> dangerousEncodes =
                        dangerousEncodeService.selectAll(dangerousEncode,null,null);
 
                String reagentName = dangerousEncodes.get(0).getReagentName();
                String cas = dangerousEncodes.get(0).getCas();
                //危险性质 reagent_character
                String dangerousInfo = dangerousEncodes.get(0).getMemo();
                BaseMeta meta = baseMetaService.getBaseMeta("reagent_character", null, dangerousInfo);
                BaseMeta baseMeta = new BaseMeta();
                if (meta == null){
                 //   baseMeta.setId(IDUtils.uuid());
                    baseMeta.setGroupCode("reagent_character");
                    baseMeta.setValidFlag(ValidFlag.VALID);
                    baseMeta.setMetaValue(dangerousInfo);
                    baseMeta.setMetaKey(dangerousInfo);
                    baseMetaService.insertBaseMeta(baseMeta);
                }else{
                    baseMeta = meta;
                }
 
                //查找是否存在试剂
                List<SysReagent> reagent = sysReagentService
                        .getReagent(reagentName, cas, factory.getId(), specifications.getId(), packing.getId());
 
                    OpeReagentStatus ors = new OpeReagentStatus();
                if (reagent != null &&reagent.size()>0){
                    //存在试剂
                    Long reagentId = reagent.get(0).getId();
                    ors.setReagentId(reagentId);
 
                }else{
                    //试剂不存在
                    SysReagent sr = new SysReagent();
                 //   sr.setId(IDUtils.uuid());
                    sr.setCas(cas);
                    sr.setName(reagentName);
                //    sr.setProductHome(factory.getId());
//                    sr.setReagentFormat(specifications.getId());
//                    sr.setReagentUnit(packing.getId());
//                    //危险性质
//                    sr.setReagentCharacter(baseMeta.getId());
                    //设备生成试剂类型 为3
                    sr.setType(3);
                    sr.setDangerousFlag(DangerousFlag.NORMAL);
                    SysReagent sysReagent = sysReagentService.insertSysReagent(sr);
                    Long reagentId = sysReagent.getId();
                    ors.setReagentId(reagentId);
                }
 
              //  ors.setId(IDUtils.uuid());
                //入仓库
                ors.setStatus(ArrivalStatus.WAREHOUSE.getKey());
                //批号
                ors.setArticleNumber(inWarehouseInfo.getArticleNumber());
                ors.setValidFlag(ValidFlag.VALID.getKey());
                ors.setContainerId(inWarehouseInfo.getContainerId());
                ors.setHouseId(inWarehouseInfo.getWarehouseId());
                ors.setUserId(inWarehouseInfo.getUser());
                ors.setReagentCode(code);
                opeReagentStatusService.insertOpeReagentStatus(ors);
 
                //更新试剂流向
                OpeUseFlow opeUseFlow = new OpeUseFlow();
                //入仓库
                opeUseFlow.setStatus(ArrivalStatus.WAREHOUSE.getKey());
                opeUseFlow.setContainerId(inWarehouseInfo.getContainerId());
                opeUseFlow.setHouseId(inWarehouseInfo.getWarehouseId());
                opeUseFlow.setStoreType(StoreType.DIRECTSTORE.getKey());
                opeUseFlow.setReagentCode(code);
                //持有者
                opeUseFlow.setUserId(inWarehouseInfo.getUser());
                opeUseFlow.setOperateState(OperateStatus.WAREHOUSEIN.getKey());
                opeUseFlowService.insertOpeUseFlow(opeUseFlow);
 
                //更新仓库库存
                // 仓库库存update
                OpeWarehouseReserve opeWarehouseReserve = opeWarehouseReserveService
                        .getOpeWarehouseReserve2(ors.getReagentId(), inWarehouseInfo.getArticleNumber(), inWarehouseInfo.getWarehouseId());
                if (opeWarehouseReserve == null) {
                    opeWarehouseReserve = new OpeWarehouseReserve();
                    opeWarehouseReserve.setReagentId(ors.getReagentId());
                    opeWarehouseReserve.setArticleNumber(inWarehouseInfo.getArticleNumber());
                    opeWarehouseReserve.setReserve(0);
                    opeWarehouseReserve.setWarehouseId(inWarehouseInfo.getWarehouseId());
                    opeWarehouseReserve.setUserId(1L);
                }
                //库存加1
                opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() + 1);
                if (opeWarehouseReserve.getId()!=null) {
                    this.opeWarehouseReserveService.insertOpeWarehouseReserve(opeWarehouseReserve);
                } else {
                    this.opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
                }
 
            });
        }
 
    }
 
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void reagentReceiving(ReagentReceivingDto reagentReceiving) {
 
 
 
        String number = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String receiptNum = "BS" + number;
 
        reagentReceiving.getReagentCode().forEach(code->{
                //
            OpeReagentStatus ors = opeReagentStatusService.getOpeReagentStatusByReagentCode(code);
            //1.更新试剂状态  在仓库-》领用
//            ors.setStatus(ArrivalStatus.PERSONAL);
            ors.setContainerId(reagentReceiving.getLabContainerId());
            ors.setHouseId(reagentReceiving.getLabId());
            ors.setUserId(reagentReceiving.getApplyUserId());
 
            if (opeReagentStatusService.isAllowWarehouseUse(ors)) {
                // 减少库存
                OpeWarehouseReserve opeWarehouseReserve = opeWarehouseReserveService.getOpeWarehouseReserve(
                        ors.getReagent().getId(), ors.getArticleNumber());
                opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() - 1);
                opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
            }
            ors.setStatus(ArrivalStatus.NOREGISTER.getKey());
            opeReagentStatusDao.updateOpeReagentStatusDao(ors);
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setReagentCode(ors.getReagentCode());
            opeUseFlow.setStatus(ors.getStatus());
            opeUseFlow.setHouseId(ors.getHouseId());
            opeUseFlow.setContainerId(ors.getContainerId());
            opeUseFlow.setUserId(ors.getUserId());
            opeUseFlow.setPlace(ors.getPlace());
            opeUseFlow.setRemainder(ors.getRemainder());
            //新增 领用单号
            opeUseFlow.setReceiptNumber(receiptNum);
 
 
            opeUseFlow.setOperateState(OperateStatus.WAREHOUSEOUT.getKey());
            this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
 
        });
 
    }
 
    @Override
    @Transactional
    public void updateSysWarehouse2(SysWarehouse sysWarehouse, SysLaboratory sysLaboratory) {
        //1.0更新自己
        this.updateSysWarehouse(sysWarehouse);
        //2.更新实验室
        sysLaboratory.setInfoCode(sysWarehouse.getInfoCode());
        sysLaboratory.setBarCode(sysWarehouse.getBarCode());
        sysLaboratory.setLocation1(sysWarehouse.getLocation1());
        sysLaboratory.setLocation2(sysWarehouse.getLocation2());
        sysLaboratory.setDepartment(sysWarehouse.getDepartment());
        sysLaboratoryService.updateSysLaboratory(sysLaboratory);
    }
 
    @Override
    public List<Map> getAllWarehouse() {
        return sysWarehouseDao.selectAllWarehouse();
    }
}