jiangping
2023-10-24 349bcef9a96391d22522a3409adf5a89e7e70a95
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
389
390
391
392
393
394
package doumeemes.service.ext.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import doumeemes.core.constants.ResponseStatus;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.DateUtil;
import doumeemes.core.utils.excel.EasyExcelUtil;
import doumeemes.dao.business.WTransferDetailMapper;
import doumeemes.dao.business.WarehouseLocationMapper;
import doumeemes.dao.business.dto.WStockImportDTO;
import doumeemes.dao.business.model.*;
import doumeemes.dao.ext.*;
import doumeemes.dao.ext.beanDto.QueryWStockDto;
import doumeemes.dao.ext.beanDto.QueryWStockHeadNewDTO;
import doumeemes.dao.ext.beanDto.QueryWStockNewDTO;
import doumeemes.dao.ext.dto.*;
import doumeemes.dao.ext.vo.*;
import doumeemes.service.ext.ProceduresExtService;
import doumeemes.service.ext.WStockExtService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.CollectionUtils;
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.web.multipart.MultipartFile;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * 物料库存余额信息Service实现
 * @author 江蹄蹄
 * @date 2022/04/20 10:56
 */
@Service
public class WStockExtServiceImpl implements WStockExtService {
 
    @Autowired
    private WStockExtMapper wStockExtMapper;
    @Autowired
    private MaterialDistributeExtMapper materialDistributeExtMapper;
    @Autowired
    private WarehouseExtMapper warehouseExtMapper;
    @Autowired
    private WarehouseLocationMapper warehouseLocationMapper;
    @Autowired
    private AppliancesExtMapper appliancesExtMapper;
 
    @Autowired
    private BomDetailExtMapper bomDetailExtMapper;
    @Autowired
    private BomExtMapper bomExtMapper;
    @Autowired
    private ProceduresExtService proceduresExtService;
    @Autowired
    private WTransferDetailExtMapper wTransferDetailExtMapper;
    @Override
    public PageData<WStockExtListVO> findPage(PageWrap<QueryWStockExtDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<WStockExtListVO> result = wStockExtMapper.selectListAll(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
 
    @Override
    public List<WStockExtListVO> getListBynum(QueryWStockExtDTO pageWrap) {
        List<WStockExtListVO> result = wStockExtMapper.selectListAll(pageWrap);
        return result;
    }
 
    @Override
    public PageData<WStockExtListVO> findListPage(PageWrap<QueryWStockDto> pageWrap) {
 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        //只能查看当前根组织的数据
        pageWrap.getModel().setRootDepartId(user.getRootDepartment().getId());
//        if(!Constants.equalsInteger(user.getCurComDepartment().getId(),user.getRootDepartment().getId())){
            //如果当前选择的公司级组织非根组织信息,只能查看当前选择公司级组织数据
            pageWrap.getModel().setDepartId(user.getCurComDepartment().getId());
//        }
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<WStockExtListVO> result = wStockExtMapper.selectAdminList(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
 
 
 
    @Override
    public PageData<WStockExtListVO> choiceStockListPage(PageWrap<QueryWStockDto> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        if(!Objects.isNull(pageWrap.getModel().getWarehouseQualityType())){
            Constants.wareHouse_QualityType w = Constants.wareHouse_QualityType.checkEnum(pageWrap.getModel().getWarehouseQualityType());
            if(w.getType()==3){
                pageWrap.getModel().setWarehouseQualityType(null);
            }else{
                pageWrap.getModel().setWarehouseQualityType(w.getType().toString());
            }
        }
        List<WStockExtListVO> result = wStockExtMapper.choiceStockList(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
 
 
 
    @Override
    public PageData<WStockExtListVO> choiceStockPageByTransfer(PageWrap<QueryWStockHeadNewDTO> pageWrap) {
        List<WTransferDetail> wTransferDetailList = wTransferDetailExtMapper.wTransferDetailListById(pageWrap.getModel().getTransferId());
        if(Objects.isNull(wTransferDetailList)||wTransferDetailList.size()==Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未查询到转库单明细信息");
        }
        List<QueryWStockNewDTO> stockNewDTOList = new ArrayList<>();
        for (WTransferDetail wTransferDetail:wTransferDetailList) {
            QueryWStockNewDTO queryWStockNewDTO = new QueryWStockNewDTO();
            BeanUtils.copyProperties(wTransferDetail,queryWStockNewDTO);
            queryWStockNewDTO.setLocationId(wTransferDetail.getOutLocationId());
            stockNewDTOList.add(queryWStockNewDTO);
        }
        pageWrap.getModel().setStockNewDTOList(stockNewDTOList);
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<WStockExtListVO> result = wStockExtMapper.choiceStockPageByTransfer(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
 
    /**
     * 库存是否满足计划
     * @param  p
     * @return
     */
    @Override
    public   Integer isStockForPlan(PlansExtListVO p){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        QueryBomDetailExtDTO bdparam = initBomParam(user,p);
        ProceduresExtListVO pModel = proceduresExtService.getById(user.getCompany().getId(),p.getProcedureId());
        if(pModel==null){
            return 0;
        }
        //待分配(计划数量-已分配未报工数量-合格数量-不良数量)
        int tNum = p.getNum() - p.getDistributNum();
        QueryWStockExtDTO stock = new QueryWStockExtDTO();
        stock.setDeleted(Constants.ZERO);
        stock.setRootDepartId(p.getRootDepartId());
        stock.setWarehouseId(pModel.getPickingWarehouseId());
        if(bdparam==null){
            //如果没有配置BOM,则投料本身
            stock.setMaterialId(p.getMaterialId());
            //查询库存记录
          List<WStockExtListVO>  stockList = wStockExtMapper.selectAllList( stock);
            if(stockList!=null && stockList.size()>0 &&Constants.formatBigdecimal(stockList.get(0).getNum()).compareTo(new BigDecimal(tNum)) >=0){
                //如果库存不足
                return 1;
            }
        }else{
            //查询bom明细
            List<BomDetailExtListVO> bomDetailList = bomDetailExtMapper.selectList(bdparam);
            if(!CollectionUtils.isEmpty(bomDetailList) ){
               stock.setMaterialIds(getMaterialIds(bomDetailList));
            }
            //查询库存记录
            List<WStockExtListVO>  stockList = wStockExtMapper.selectAllList( stock);
            boolean isFull = true;
            for (BomDetailExtListVO i : bomDetailList) {
                //遍历查询库存是否满足
                BigDecimal  sNum =getStockByMaterialId(i.getMaterialId(),stockList);
                //所需要库存量
                BigDecimal needNum = Constants.formatBigdecimal(i.getNum()).multiply(new BigDecimal(tNum));
                if( sNum.compareTo(needNum) <0){
                    //如果库存不足
                    isFull =false;
                    break;
                }
            }
            return  isFull?1:0;
        }
        return 0;
 
    }
 
    private BigDecimal getStockByMaterialId(Integer materialId, List<WStockExtListVO> stockList) {
        BigDecimal num = new BigDecimal(0);
        if(stockList!=null){
            for(WStockExtListVO s : stockList){
                if(Constants.equalsInteger(s.getMaterialId(),materialId)){
                    num.add(Constants.formatBigdecimal(s.getNum()));
                }
            }
        }
        return num;
    }
 
    private List<Integer> getMaterialIds(List<BomDetailExtListVO> bomDetailList) {
        if(CollectionUtils.isEmpty(bomDetailList) ){
            return  null;
        }
        List<Integer> list = new ArrayList<>();
        bomDetailList. forEach(i->{
            //判断当前状态,根据状态获取明细记录 若是已取消业务,查询 0
         list.add(i.getMaterialId());
        });
        return  list;
    }
 
 
    private QueryBomDetailExtDTO initBomParam(LoginUserInfo user, PlansExtListVO mp)  throws BusinessException{
        QueryBomExtDTO bb = new QueryBomExtDTO();
        bb.setDepartId(mp.getDepartId());
        bb.setDeleted(Constants.ZERO);
        bb.setRootDepartId(user.getRootDepartment().getId());
        bb.setMaterialId(mp.getMaterialId());
        bb.setProcedureId(mp.getProcedureId());
        BomExtListVO versionBom = bomExtMapper.selectByModel( bb);
        if(versionBom == null || StringUtils.isBlank(versionBom.getVersion()) || versionBom.getBomVersionId() == null){
            return  null;
        }
        QueryBomDetailExtDTO  bom = new QueryBomDetailExtDTO();
        bom.setDeleted(Constants.ZERO);
        bom.setBomId(versionBom.getBomVersionId());
        bom.setWorkorderId(mp.getId());
        return bom;
    }
 
 
 
 
 
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    @Override
    public String importStock(MultipartFile file)  {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        //解析excel
        List<WStockImportDTO> list = EasyExcelUtil.importExcel(file, 1, 1, WStockImportDTO.class);
        if(list == null || list.size()==0){
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!");
        }
        String msg = "";
        int index = 1;
        for(WStockImportDTO p : list){
            WStock stock = new WStock();
            stock.setDeleted(Constants.ZERO);
            stock.setRootDepartId(user.getRootDepartment().getId());
            stock.setBatch(p.getBatch());
            stock.setNum(p.getNum());
 
            Appliances appliances = new Appliances();
            appliances.setUpdateTime(DateUtil.getCurrentDate());
            appliances.setUpdateUser(user.getId());
            appliances.setNum(p.getNum());
            appliances.setBatch(p.getBatch());
            //检查数据有效性
            String code =  checkData(p,stock,appliances,index,user);
            if(StringUtils.isNotBlank(code)){
                //如果有篮筐不为空,提示信息有修改
                msg += "【"+code+"】";
            }
            if(stock.getId()!=null){
                stock.setUpdateTime(DateUtil.getCurrentDate());
                stock.setUpdateUser(user.getId());
                wStockExtMapper.updateById(stock);
            }else{
                stock.setCreateTime(DateUtil.getCurrentDate());
                stock.setCreateUser(user.getId());
                wStockExtMapper.insert(stock);
            }
            appliancesExtMapper.updateById(appliances);
 
            index++;
        }
        if(StringUtils.isNotBlank(msg)){
            msg = "你好,部分数据导入成功!其中篮筐"+msg+"非空篮筐,导入不成功!";
        }else{
            msg = "全部导入成功!";
        }
        return msg;
 
    }
 
    /**
     * 数据有效性检查
     * @param p
     * @param index
     * @param user
     * @throws BusinessException
     */
    private String checkData(WStockImportDTO p, WStock stock, Appliances appliances ,int index,LoginUserInfo user) throws BusinessException{
        if(Constants.formatBigdecimal(p.getNum()).compareTo(new BigDecimal(0)) != 1){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【物料数量】数据错误!");
        }
        if(p.getApplianceCode() == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【物料编码】数据错误");
        }
        if(StringUtils.isBlank(p.getMaterialCode())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【物料编码】数据错误!");
        }
        if(StringUtils.isBlank(p.getWarehouse())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【仓库名称】数据错误!");
        }
 
        p.setApplianceCode(p.getApplianceCode().trim());
        p.setMaterialCode(p.getMaterialCode().trim());
        p.setWarehouse(p.getWarehouse().trim());
        Appliances a = new Appliances();
        a.setDeleted(Constants.ZERO);
        a.setCode(p.getApplianceCode());
        a.setRootDepartId(user.getRootDepartment().getId());
        a.setDepartId(user.getCurComDepartment().getId());
        //查询篮筐信息
        a = appliancesExtMapper.selectOne(new QueryWrapper<>(a));
        if(a == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【篮筐编码】数据不存在!");
        }
        if(!Constants.equalsInteger(a.getStatus(),Constants.APPLIANCES_STATUS.empty)){
            return p.getApplianceCode() ;
        }
 
        Warehouse w = new Warehouse();
        w.setDeleted(Constants.ZERO);
        w.setName(p.getWarehouse());
        w.setRootDepartId(user.getRootDepartment().getId());
        w.setDepartId(user.getCurComDepartment().getId());
        //查询篮筐信息
        w = warehouseExtMapper.selectOne(new QueryWrapper<>(w));
        if(w == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【仓库名称】数据不存在!");
        }
        stock.setWarehouseId(w.getId());
 
        if(StringUtils.isNotBlank(p.getLocation())){
            p.setLocation(p.getLocation().trim());
            WarehouseLocation l = new WarehouseLocation();
            l.setDeleted(Constants.ZERO);
            l.setWarehouseId(w.getId());
            l.setUnionName(p.getLocation());
            l.setRootDepartId(user.getRootDepartment().getId());
            //查询篮筐信息
            l = warehouseLocationMapper.selectOne(new QueryWrapper<>(l));
            if(l == null || !Constants.equalsInteger(l.getWarehouseId(),w.getId())){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【库位名称】数据不存在!!");
            }
            stock.setLocationId(l.getId());
            appliances.setLocationId(l.getId());
        }
 
 
        QueryMaterialDistributeExtDTO d = new QueryMaterialDistributeExtDTO();
        d.setDeleted(Constants.ZERO);
        d.setMmodelCode(p.getMaterialCode());
        d.setRootDepartId(user.getRootDepartment().getId());
        d.setDepartId(user.getCurComDepartment().getId());
        //查询产品信息
        MaterialDistributeExtListVO mm = materialDistributeExtMapper.selectByModel(d);
        if(mm == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(index+2)+"】行【产品编码】数据不存在!");
        }
        //存储物料分配表主键
        stock.setMaterialId(mm.getId());
        //单位编码
        stock.setUnitId(mm.getUnitId());
 
        appliances.setMaterialId(mm.getId());
        appliances.setUnitId(mm.getUnitId());
        appliances.setPositionType(Constants.APPLIANCES_POSITION.warehouse);
        appliances.setStatus(Constants.APPLIANCES_STATUS.full);
        appliances.setId(a.getId());
        appliances.setWarehouseId(w.getId());
 
        WStock s1 = new WStock();
        s1.setLocationId(stock.getLocationId());
        s1.setWarehouseId(stock.getWarehouseId());
        s1.setDeleted(Constants.ZERO);
        s1.setMaterialId(stock.getMaterialId());
        s1.setBatch(stock.getBatch());
        s1 = wStockExtMapper.selectOne(new QueryWrapper<>(s1));
        if(s1 !=null){
            stock.setId(s1.getId());
            stock.setNum(Constants.formatBigdecimal(s1.getNum()).add(stock.getNum()));
        }
 
        return null;
    }
}