SZH
2023-03-09 53e7dce8d55487cbac64e4374ec9aa1b52a6c6ed
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
package com.gkhy.safePlatform.equipment.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.equipment.entity.*;
import com.gkhy.safePlatform.equipment.enums.*;
import com.gkhy.safePlatform.equipment.excepiton.EquipmentException;
import com.gkhy.safePlatform.equipment.model.dto.req.MaterialReceiveRecordsBaseReq;
import com.gkhy.safePlatform.equipment.model.dto.req.MaterialReceiveRecordsQuery;
import com.gkhy.safePlatform.equipment.model.dto.req.MaterialReceiveRecordsReq;
import com.gkhy.safePlatform.equipment.model.dto.resp.MaterialReceiveRecordsBaseDto;
import com.gkhy.safePlatform.equipment.model.dto.resp.MaterialReceiveRecordsDto;
import com.gkhy.safePlatform.equipment.service.MaterialReceiveRecordsService;
import com.gkhy.safePlatform.equipment.service.baseService.MaterialReceiveRecordsBaseInfoService;
import com.gkhy.safePlatform.equipment.service.baseService.MaterialReceiveRecordsInfoService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialDetailInfoService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialInfoService;
import io.micrometer.core.instrument.util.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Service
public class MaterialReceiveRecordsServiceImpl implements MaterialReceiveRecordsService {
    @Autowired
    private MaterialReceiveRecordsBaseInfoService recordsBaseInfoService;
    @Autowired
    private MaterialReceiveRecordsInfoService receiveRecordsInfoService;
 
    @Autowired
    private SafeMaterialDetailInfoService safeMaterialDetailInfoService;
 
    @Autowired
    private SafeMaterialInfoService safeMaterialInfoService;
    @Autowired
    private RedissonClient redissonClient;
    @Override
    public SearchResultVO<List<MaterialReceiveRecordsBaseDto>> listByPage(ContextCacheUser currentUser, PageQuery<MaterialReceiveRecordsQuery> pageQuery) {
        MaterialReceiveRecordsQuery query = pageQuery.getSearchParams();
        query.setDepId(currentUser.getDepId());
        Page page = new Page<>(pageQuery.getPageIndex(), pageQuery.getPageSize());
        List<MaterialReceiveRecordsBaseDO> list = recordsBaseInfoService.listByPage(page,query);
        List<MaterialReceiveRecordsInfo> receiveRecordsInfoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            List<Long> idList = list.stream().map(MaterialReceiveRecordsBaseDO::getId).collect(Collectors.toList());
            receiveRecordsInfoList = receiveRecordsInfoService.listByReceiveBaseIds(idList);
        }
        List<MaterialReceiveRecordsBaseDto> receiveRecordsBaseDtoList = new ArrayList<>();
        for(MaterialReceiveRecordsBaseDO receiveRecordsBaseDO:list){
            MaterialReceiveRecordsBaseDto receiveRecordsBaseDto = new MaterialReceiveRecordsBaseDto();
            BeanUtils.copyProperties(receiveRecordsBaseDO,receiveRecordsBaseDto);
            receiveRecordsBaseDto.setStatusName(MaterialReceiveRecordsStatusEnum.getByCode(receiveRecordsBaseDO.getStatus()).getValue());
            //获取该记录关联的物资
            List<MaterialReceiveRecordsInfo> selectList = receiveRecordsInfoList.stream().filter(item -> item.getReceiveBaseId().equals(receiveRecordsBaseDO.getId())).collect(Collectors.toList());
            List<MaterialReceiveRecordsDto> receiveRecordsDtoList = new ArrayList<>();
            for(MaterialReceiveRecordsInfo receiveRecordsInfo:selectList){
                MaterialReceiveRecordsDto receiveRecordsDto = new MaterialReceiveRecordsDto();
                BeanUtils.copyProperties(receiveRecordsInfo,receiveRecordsDto);
                receiveRecordsDto.setMaterialStatusName(MaterialStatusEnum.getByCode(receiveRecordsInfo.getMaterialStatus()).getValue());
                receiveRecordsDto.setRevertStatusName(MaterialRevertStatusEnum.getByCode(receiveRecordsInfo.getRevertStatus()).getValue());
                receiveRecordsDtoList.add(receiveRecordsDto);
            }
            receiveRecordsBaseDto.setMaterialList(receiveRecordsDtoList);
            receiveRecordsBaseDtoList.add(receiveRecordsBaseDto);
        }
        return new SearchResultVO<>(true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                receiveRecordsBaseDtoList,
                ResultCodes.OK);
    }
 
    @Override
    @Transactional
    public void revertMaterial(ContextCacheUser currentUser, MaterialReceiveRecordsBaseReq req) {
        //检查
        List<MaterialReceiveRecordsReq> materialList = req.getMaterialList();
        for(MaterialReceiveRecordsReq receiveRecordsReq:materialList){
            if(null == receiveRecordsReq.getReceiveId()){
                throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL,"领取清单主键不可为空!");
            }
            if(null != receiveRecordsReq.getMaterialStatus()){
                if(null == MaterialStatusEnum.getByCode(receiveRecordsReq.getMaterialStatus())){
                    throw new EquipmentException(EquipmentResultCodes.DATA_ILLEGAL,"物资状态不合法!");
                }else{
                    if(MaterialStatusEnum.OTHER.getCode().equals(receiveRecordsReq.getMaterialStatus()) && StringUtils.isBlank(receiveRecordsReq.getRemark())){
                        throw new EquipmentException(EquipmentResultCodes.DATA_ILLEGAL,"损耗备注不可为空!");
                    }
                }
            }
        }
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_REVERT_MATERIAL");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            //获取该次领取记录
            MaterialReceiveRecordsBaseInfo receiveRecordsBaseInfo = recordsBaseInfoService.queryById(req.getReceiveBaseId());
            if(null == receiveRecordsBaseInfo){
                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"该条记录不存在!");
            }
            if(receiveRecordsBaseInfo.getStatus().equals(MaterialReceiveRecordsStatusEnum.CONSUMABLE_RETURN_NO.getCode())){
                throw new EquipmentException(EquipmentResultCodes.MATERIAL_CONSUMABLE);
            }
            List<Long> idList = materialList.stream().map(MaterialReceiveRecordsReq::getReceiveId).collect(Collectors.toList());
            //检查该次要归还的物资是否有归还或处理过
            List<MaterialReceiveRecordsDO> revertList = receiveRecordsInfoService.getNoReturnRecordByIds(idList);
            if(materialList.size() > revertList.size()){
                throw new EquipmentException(EquipmentResultCodes.MATERIAL_IN_THE_LIBRARY,"已归还或已处理物资不可重复归还处理!");
            }
            List<MaterialReceiveRecordsInfo> updateRecordsList = new ArrayList<>();
            List<SafeMaterialDetailInfo> updateDetailList = new ArrayList<>();
            int revertCount = 0;
            int consumeCount = 0;
            int validCount = 0;
            for(MaterialReceiveRecordsReq receiveRecordsReq:materialList){
                //过滤出该条领取物资
                List<MaterialReceiveRecordsDO> selectList = revertList.stream().filter(item -> item.getId().equals(receiveRecordsReq.getReceiveId())).collect(Collectors.toList());
                MaterialReceiveRecordsDO receiveRecord = selectList.get(0);
                //物资清单
                SafeMaterialDetailInfo safeMaterialDetailInfo = new SafeMaterialDetailInfo();
                safeMaterialDetailInfo.setId(receiveRecord.getSmdId());
                safeMaterialDetailInfo.setWareHousingTime(LocalDateTime.now());
 
 
                //领取清单
                MaterialReceiveRecordsInfo materialReceiveRecordsInfo = new MaterialReceiveRecordsInfo();
                materialReceiveRecordsInfo.setId(receiveRecordsReq.getReceiveId());
                materialReceiveRecordsInfo.setMaterialStatus(receiveRecordsReq.getMaterialStatus());
                materialReceiveRecordsInfo.setRemark(MaterialStatusEnum.getByCode(receiveRecordsReq.getMaterialStatus()).getValue());
                if(MaterialStatusEnum.INTACT.getCode().equals(receiveRecordsReq.getMaterialStatus()) || null == receiveRecordsReq.getMaterialStatus()){
                    materialReceiveRecordsInfo.setRevertStatus(MaterialRevertStatusEnum.REVERT_YES.getCode());
                    safeMaterialDetailInfo.setIrStatus(IssueReceiptEnum.IN_THE_LIBRARY.getCode());
                    revertCount++;
                    if(receiveRecord.getValidStatus().equals(ValidStatusEnum.NO.getCode())){
                        //无效情况
                        validCount++;
                    }
                }else{
                    //损耗
                    materialReceiveRecordsInfo.setRevertStatus(MaterialRevertStatusEnum.DEPLETION.getCode());
                    if (StringUtils.isNotBlank(receiveRecordsReq.getRemark())) {
                        materialReceiveRecordsInfo.setRemark(receiveRecordsReq.getRemark());
                    }
                    safeMaterialDetailInfo.setIrStatus(IssueReceiptEnum.OUT_OF_LIBRARY.getCode());
                    consumeCount++;
                }
                updateRecordsList.add(materialReceiveRecordsInfo);
 
                safeMaterialDetailInfo.setStatus(materialReceiveRecordsInfo.getMaterialStatus());
                safeMaterialDetailInfo.setRemark(materialReceiveRecordsInfo.getRemark());
                updateDetailList.add(safeMaterialDetailInfo);
 
            }
 
            //物资管理
            SafeMaterialBO safeMaterialBO = new SafeMaterialBO();
            safeMaterialBO.setId(receiveRecordsBaseInfo.getSmId());
            safeMaterialBO.setStockCount(revertCount-validCount);
            //领取总记录
            MaterialReceiveRecordsBaseInfo materialReceiveRecordsBaseInfo = new MaterialReceiveRecordsBaseInfo();
            materialReceiveRecordsBaseInfo.setId(req.getReceiveBaseId());
            if(receiveRecordsBaseInfo.getReceiveCount() == receiveRecordsBaseInfo.getConsumeCount()+materialList.size()){
                materialReceiveRecordsBaseInfo.setStatus(MaterialReceiveRecordsStatusEnum.RETURN_COMPLETE.getCode());
            }else{
                materialReceiveRecordsBaseInfo.setStatus(MaterialReceiveRecordsStatusEnum.RETURN_PART.getCode());
            }
            materialReceiveRecordsBaseInfo.setRevertCount(receiveRecordsBaseInfo.getRevertCount()+revertCount);
            materialReceiveRecordsBaseInfo.setConsumeCount(receiveRecordsBaseInfo.getConsumeCount()+consumeCount);
            //修改物资管理库存
            safeMaterialInfoService.updateCountById(safeMaterialBO);
            //修改物资清单状态
            safeMaterialDetailInfoService.updateBatchById(updateDetailList);
            //修改总记录
            recordsBaseInfoService.updateById(materialReceiveRecordsBaseInfo);
            //修改领取清单
            receiveRecordsInfoService.updateBatchById(updateRecordsList);
            //创建成功,释放锁
            lock.unlock();
        }catch (EquipmentException e) {
            e.printStackTrace();
            throw new EquipmentException(e.getCode(), e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
    }
    @Override
    public List<MaterialReceiveRecordsDO> getReceiveRecordsByReceiveUids(MaterialReceiveRecordsBO bo){
        if (CollectionUtils.isEmpty(bo.getReceiveUids())) {
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL,"领取人不可为空!");
        }
        if(CollectionUtils.isEmpty(bo.getSmallClassifyIds())){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL,"物资类型(小类)不可为空!");
        }
        List<MaterialReceiveRecordsDO> list = receiveRecordsInfoService.getReceiveRecordsByReceiveUids(bo);
        return list;
    }
 
    @Override
    public MaterialReceiveRecordsBaseDto queryById(ContextCacheUser currentUser, Long id) {
        if(ObjectUtils.isEmpty(id)){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL,"领取总记录主键不可为空!");
        }
        MaterialReceiveRecordsBaseInfo receiveRecordsBaseInfo = recordsBaseInfoService.queryById(id);
        List<MaterialReceiveRecordsInfo> receiveRecordsInfoList = receiveRecordsInfoService.listByReceiveBaseId(id);
        MaterialReceiveRecordsBaseDto materialReceiveRecordsBaseDto = new MaterialReceiveRecordsBaseDto();
        if(!ObjectUtils.isEmpty(receiveRecordsBaseInfo)){
            BeanUtils.copyProperties(receiveRecordsBaseInfo,materialReceiveRecordsBaseDto);
            List<MaterialReceiveRecordsDto> receiveRecordsDtoList = new ArrayList<>();
            for(MaterialReceiveRecordsInfo receiveRecordsInfo:receiveRecordsInfoList){
                MaterialReceiveRecordsDto receiveRecordsDto = new MaterialReceiveRecordsDto();
                BeanUtils.copyProperties(receiveRecordsInfo,receiveRecordsDto);
                receiveRecordsDto.setMaterialStatusName(MaterialStatusEnum.getByCode(receiveRecordsInfo.getMaterialStatus()).getValue());
                receiveRecordsDto.setRevertStatusName(MaterialRevertStatusEnum.getByCode(receiveRecordsInfo.getRevertStatus()).getValue());
                receiveRecordsDtoList.add(receiveRecordsDto);
            }
            materialReceiveRecordsBaseDto.setMaterialList(receiveRecordsDtoList);
            return materialReceiveRecordsBaseDto;
        }
 
       return null;
    }
 
}