李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.warehouse.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.mes.warehouse.dto.CheckMainDTO;
import com.chinaztt.mes.warehouse.entity.*;
import com.chinaztt.mes.warehouse.mapper.*;
import com.chinaztt.mes.warehouse.service.CheckMainService;
import com.chinaztt.mes.warehouse.util.StockUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * 盘点主表
 *
 * @author sunxl
 * @date 2020-12-01 11:06:00
 */
@AllArgsConstructor
@Service
public class CheckMainServiceImpl extends ServiceImpl<CheckMainMapper, CheckMain> implements CheckMainService {
    private NumberGenerator<CheckMain> numberGenerator;
    private WarehouseCheckLocationMapper warehouseCheckLocationMapper;
    private WarehouseCheckPartMapper warehouseCheckPartMapper;
    private CheckDetailMapper checkDetailMapper;
    private CheckDiffAdjDetailMapper checkDiffAdjDetailMapper;
    private StockMapper stockMapper;
    private StockUtils stockUtils;
    @Override
    public boolean fullSave(CheckMainDTO checkMainDTO) {
        checkMainDTO.setChkNo(numberGenerator.generateNumberWithPrefix(CheckMain.DIGIT, CheckMain.PREFIX, CheckMain::getChkNo));
        baseMapper.insert(checkMainDTO);
        addWarehouseCheck(checkMainDTO);
        checkDetailMapper.getCheckDetail(checkMainDTO);
        return true;
    }
 
    @Override
    public IPage<List<CheckMainDTO>> getCheckMainPage(Page page, QueryWrapper<CheckMainDTO> gen) {
        return baseMapper.getCheckMainPage(page,gen);
    }
 
    @Override
    public CheckMainDTO getCheckMainPageById(Long id) {
        return baseMapper.getCheckMainPageById(id);
    }
 
    @Override
    public void fullDelete(Long id) {
        baseMapper.deleteCheckMainById(id);
    }
 
    @Override
    public boolean updateByCheckMainId(CheckMainDTO checkMainDTO) {
        baseMapper.deleteCheckMain(checkMainDTO.getId());
        baseMapper.updateById(checkMainDTO);
        addWarehouseCheck(checkMainDTO);
        checkDetailMapper.getCheckDetail(checkMainDTO);
        return false;
    }
 
    @Override
    public boolean updateWarehouseCheck(List<Long> ids) {
        for (long id:ids){
            CheckMain checkMain = baseMapper.selectById(id);
            if ("未下发".equals(checkMain.getChkStatus())) {
                checkMain.setChkStatus("已下发");
            }
            else{
                List<CheckDetail> checkDetailList = checkDetailMapper.selectList(Wrappers.<CheckDetail>lambdaQuery().eq(CheckDetail::getWarehouseCheckId,checkMain.getId()));
                for (CheckDetail checkDetail:checkDetailList){
                    if (checkDetail.getStatus().equals("待盘")){
                        throw new RuntimeException("有库存没有盘点");
                    }
                }
                checkMain.setChkStatus("盘点中");
            }
            baseMapper.updateById(checkMain);
        }
        return false;
    }
 
    @Override
    public List<CheckMainDTO> getCheckMainPdaPage( QueryWrapper<CheckMainDTO> gen) {
        return baseMapper.getCheckMainPdaPage(gen);
    }
 
    @Override
    public boolean submitWarehouseCheck(Long id) {
        List<CheckDetail> checkDetailList  = checkDetailMapper.selectList(Wrappers.<CheckDetail>lambdaQuery().eq(CheckDetail::getWarehouseCheckId,id));
        for (CheckDetail checkDetail:checkDetailList) {
            CheckDiffAdjDetail checkDiffAdjDetail = checkDiffAdjDetailMapper.selectOne(Wrappers.<CheckDiffAdjDetail>lambdaQuery().eq(CheckDiffAdjDetail::getCheckDetailId, checkDetail.getId()));
            if (checkDiffAdjDetail==null){
                continue;
            }
            Stock stock = stockMapper.selectOne(Wrappers.<Stock>lambdaQuery().eq(Stock::getPartId,checkDetail.getPartId()).eq(Stock::getLocationId,checkDetail.getLocationId()).eq(Stock::getPartBatchNo,checkDetail.getPartBatchNo()).eq(Stock::getSystemNo,checkDetail.getSystemNo()));
            stockUtils.updateById(stock.getId(), checkDiffAdjDetail.getAdjQty(),  BigDecimal.ZERO, BigDecimal.ZERO,BigDecimal.ZERO, checkDiffAdjDetail.getChkNo(), "CHECK");
        }
        return false;
    }
 
    @Override
    public IPage<List<CheckMainDTO>> getCheckPage(Page page, QueryWrapper<CheckMainDTO> gen) {
        return baseMapper.getCheckPage(page,gen);
    }
 
    public void addWarehouseCheck(CheckMainDTO checkMainDTO){
        if (checkMainDTO.getLocationIds().size()!=0){
            for(Long locationId :checkMainDTO.getLocationIds()) {
                WarehouseCheckLocation warehouseCheckLocation = new WarehouseCheckLocation();
                warehouseCheckLocation.setCheckMainId(checkMainDTO.getId());
                warehouseCheckLocation.setLocationId(locationId);
                warehouseCheckLocationMapper.insert(warehouseCheckLocation);
            }
        }
        if (checkMainDTO.getPartIds().size()!=0){
            for(Long partId :checkMainDTO.getPartIds()) {
                WarehouseCheckPart warehouseCheckPart= new WarehouseCheckPart();
                warehouseCheckPart.setCheckMainId(checkMainDTO.getId());
                warehouseCheckPart.setPartId(partId);
                warehouseCheckPartMapper.insert(warehouseCheckPart);
            }
        }
    }
}