#
luxiaotao1123
2021-06-09 9512f15494e270f93013540e6862b7ea77a03865
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
package com.zy.asrs.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.StockOutParam;
import com.zy.asrs.mapper.OutStockMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.VersionUtils;
import com.zy.common.model.LocDetlDto;
import com.zy.common.model.OutLocDto;
import com.zy.common.service.CommonService;
import com.zy.common.service.erp.entity.OutStockBillEntry;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
 
@Service("outStockService")
public class OutStockServiceImpl extends ServiceImpl<OutStockMapper, OutStockBillEntry> implements OutStockService{
    // 工作号生成规则默认类型
    private static final int DEFAULT_WORK_NO_TYPE = 0;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private LocNormalService locNormalService;
 
    @Override
    public Page<OutStockBillEntry> queryOutStock(Page<OutStockBillEntry> page) {
        page.setRecords(baseMapper.queryOutStock(page.getCondition()));
        page.setTotal(baseMapper.queryOutStockCount(page.getCondition()));
        return page;
    }
 
    @Override
    public List<LocDetl> queryMatWithLoc(String fbillNo) {
        List<OutStockBillEntry> matList = baseMapper.queryMatnrWithBillNo(fbillNo);
        List<LocDetl> tempLocList = new ArrayList<LocDetl>();
        List<LocDetl> locList = new ArrayList<LocDetl>();
        for (Integer i = 0; i < matList.size(); i++) {
            /*自动根据物料编码选取最优出货顺序(先进先出,靠外货物先出)*/
            tempLocList = baseMapper.queryMatWithLoc(matList.get(i).getFnumber());
            BigDecimal needNum = matList.get(i).getFAuxQty();
            BigDecimal locNum = BigDecimal.ZERO;
            if (tempLocList.size() > 0) {
                for (Integer j = 0; j < tempLocList.size(); j ++) {
                    locNum = locNum.add(new BigDecimal(tempLocList.get(j).getAnfme()));
                    /* 计算需要的数量,符合数量后结束循环 */
                    if (needNum.compareTo(locNum) == 1) {
                        locList.add(tempLocList.get(j));
                    } else {
                        locList.add(tempLocList.get(j));
                        break;
                    }
                }
            }
        }
        return locList;
    }
 
    @Override
    public Integer queryOutStockFInterID(String supplier) {
       return baseMapper.queryOutStockFInterID(supplier);
    }
 
    @Override
    @Transactional
    public void startupFullTakeStore(StockOutParam param, Long userId) {
        // 目标站点状态检测
        BasDevp staNo = basDevpService.checkSiteStatus(param.getOutSite());
        // 获取库位明细
        List<LocDetlDto> locDetlDtos = new ArrayList<>();
        for (StockOutParam.LocDetl paramLocDetl : param.getLocDetls()) {
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount())) {
                LocDetl sqlParam = new LocDetl();
                sqlParam.setLocNo(paramLocDetl.getLocNo());
                sqlParam.setMatnr(paramLocDetl.getMatnr());
                LocDetl one = locDetlService.selectOne(new EntityWrapper<>(sqlParam));
                if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount()));
            }
        }
        if (!locDetlDtos.isEmpty()) {
            // 启动出库开始 101.出库
            stockOut(staNo, locDetlDtos, null, userId, param.getFbillNo());
        } else {
            throw new CoolException("库位物料不存在");
        }
    }
 
//    @Override
    @Transactional
    public void stockOut(BasDevp staNo, List<LocDetlDto> locDetlDtos, Integer ioType, Long userId, String fbillNo) {
        // 合并同类项
        Set<String> locNos = new HashSet<>();
        locDetlDtos.forEach(dto -> locNos.add(dto.getLocDetl().getLocNo()));
        List<OutLocDto> dtos = new ArrayList<>();
        for (String locNo : locNos) {
            List<LocDetlDto> list = new ArrayList<>();
            Iterator<LocDetlDto> iterator = locDetlDtos.iterator();
            while (iterator.hasNext()) {
                LocDetlDto dto = iterator.next();
                if (locNo.equals(dto.getLocDetl().getLocNo())) {
                    list.add(dto);
                    iterator.remove();
                }
            }
            dtos.add(new OutLocDto(locNo, list));
        }
        // 生成工作档
        for (OutLocDto dto : dtos) {
            // 判断入出库类型:101.全板出库 or 103.拣料出库
            if (ioType == null) {
                ioType = dto.isAll() ? 101 : 103;
            }
            // 获取库位
            LocMast locMast = locMastService.selectById(dto.getLocNo());
            // 获取路径
            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                    .eq("type_no", ioType)
                    .eq("stn_no", staNo.getDevNo())
                    .eq("crn_no", locMast.getCrnNo());
            StaDesc staDesc = staDescService.selectOne(wrapper);
            if (Cools.isEmpty(staDesc)) {
                throw new CoolException("出库路径不存在");
            }
            // 生成工作号
            int workNo = commonService.getWorkNo(DEFAULT_WORK_NO_TYPE);
            // 生成工作档
            WrkMast wrkMast = new WrkMast();
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(new Date());
            wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID
            wrkMast.setIoType(ioType); // 入出库状态
            wrkMast.setIoPri(13D); // 优先级:13
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站
            wrkMast.setStaNo(staDesc.getStnNo()); // 目标站
            wrkMast.setSourceLocNo(dto.getLocNo()); // 源库位
            wrkMast.setBarcode(locMast.getBarcode()); // 条码
            wrkMast.setFullPlt("Y"); // 满板:Y
            wrkMast.setPicking("N"); // 拣料
            wrkMast.setExitMk("N"); // 退出
            wrkMast.setEmptyMk("N"); // 空板
            wrkMast.setLinkMis("N");
            wrkMast.setAppeUser(userId); // 操作人员数据
            wrkMast.setAppeTime(new Date());
            wrkMast.setModiUser(userId);
            wrkMast.setModiTime(new Date());
            if (!wrkMastService.insert(wrkMast)) {
                throw new CoolException("保存工作档失败,出库库位号:"+dto.getLocNo());
            }
            // 生成工作档明细
            for (LocDetlDto detlDto : dto.getLocDetlDtos()) {
                // 出库时,数量为0的直接忽略
                if (detlDto.getCount()==null || detlDto.getCount() <= 0.0D) {continue;}
                WrkDetl wrkDetl = new WrkDetl();
                wrkDetl.setWrkNo(workNo);
                wrkDetl.setIoTime(new Date());
                Double anfme = ioType==101?detlDto.getLocDetl().getAnfme():detlDto.getCount();
                wrkDetl.setAnfme(anfme); // 数量
                VersionUtils.setWrkDetl(wrkDetl, detlDto.getLocDetl()); // 版本控制
                wrkDetl.setAppeTime(new Date());
                wrkDetl.setAppeUser(userId);
                wrkDetl.setModiTime(new Date());
                wrkDetl.setModiUser(userId);
                wrkDetl.setSupplier(fbillNo);
                wrkDetl.setMemo(detlDto.getLocDetl().getMemo());
                if (!wrkDetlService.insert(wrkDetl)) {
                    throw new CoolException("保存工作档明细失败");
                }
            }
            // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中
            locMast = locMastService.selectById(dto.getLocNo());
            if (locMast.getLocSts().equals("F")) {
                locMast.setLocSts(ioType==101?"R":"P");
                locMast.setModiUser(userId);
                locMast.setModiTime(new Date());
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("预约库位状态失败,库位号:"+dto.getLocNo());
                }
            } else {
                throw new CoolException(dto.getLocNo() + "库位不是在库状态");
            }
        }
        // todo:luxiaotao
        // 同一列的同时出库,则优先出浅库位
    }
}