#
Junjie
2024-01-08 986390a2a8d5e7cff2f522daa9251bf41fda7c1b
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
package com.zy.asrs.common.openapi.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.common.domain.dto.DetlDto;
import com.zy.asrs.common.openapi.entity.dto.OrderListDto;
import com.zy.asrs.common.openapi.entity.param.*;
import com.zy.asrs.common.openapi.service.ApiService;
import com.zy.asrs.common.wms.entity.*;
import com.zy.asrs.common.wms.service.*;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.DateUtils;
import com.zy.asrs.framework.common.SnowflakeIdWorker;
import com.zy.asrs.framework.exception.CoolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service("apiService")
public class ApiServiceImpl implements ApiService {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private MatService matService;
    @Autowired
    private OrderSettleService orderSettleService;
    @Autowired
    private TagService tagService;
 
    @Override
    public void generateOrderPakIn(GenerateOrderPakInParam param) {
        Order order = orderService.selectByNo(param.getOrderNo(), param.getHostId());
        if (!Cools.isEmpty(order)) {
            throw new CoolException(param.getOrderNo() + "单据已存在,请勿重复提交");
        }
        DocType docType = docTypeService.getOne(new LambdaQueryWrapper<DocType>().eq(DocType::getDocId, param.getOrderType()));
        if (docType == null) {
            throw new CoolException("订单类型不存在");
        }
        Date now = new Date();
        // 单据主档
        order = new Order(
                String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                param.getOrderNo(),    // 订单编号
                DateUtils.convert(now),    // 单据日期
                docType.getDocId(),    // 单据类型
                null,    // 项目编号
                null,    //
                null,    // 调拨项目编号
                null,    // 初始票据号
                null,    // 票据号
                null,    // 客户编号
                null,    // 客户
                null,    // 联系方式
                null,    // 操作人员
                null,    // 合计金额
                null,    // 优惠率
                null,    // 优惠金额
                null,    // 销售或采购费用合计
                null,    // 实付金额
                null,    // 付款类型
                null,    // 业务员
                null,    // 结算天数
                null,    // 邮费支付类型
                null,    // 邮费
                null,    // 付款时间
                null,    // 发货时间
                null,    // 物流名称
                null,    // 物流单号
                1L,    // 订单状态
                1,    // 状态
                9527L,    // 添加人员
                now,    // 添加时间
                9527L,    // 修改人员
                now,    // 修改时间
                null,    // 备注
                param.getHostId()
        );
        if (!orderService.save(order)) {
            throw new CoolException("生成单据主档失败,请联系管理员");
        }
        List<GenerateOrderMatParam> mats = param.getMats();
        for (GenerateOrderMatParam matParam : mats) {
            Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, matParam.getMatnr()).eq(Mat::getHostId, param.getHostId()));
            if (Cools.isEmpty(mat)) {
                throw new CoolException(matParam.getMatnr() + "编号商品检索失败,请先添加商品");
            }
            OrderDetl orderDetl = new OrderDetl();
            orderDetl.sync(mat);
            orderDetl.setBatch(matParam.getBatch());
            orderDetl.setAnfme(matParam.getAnfme());
            orderDetl.setOrderId(order.getId());
            orderDetl.setOrderNo(order.getOrderNo());
            orderDetl.setCreateBy(9527L);
            orderDetl.setCreateTime(now);
            orderDetl.setUpdateBy(9527L);
            orderDetl.setUpdateTime(now);
            orderDetl.setStatus(1);
            orderDetl.setQty(0.0D);
            if (!orderDetlService.save(orderDetl)) {
                throw new CoolException("生成单据明细失败,请联系管理员");
            }
        }
    }
 
    @Override
    public void generateOrderPakOut(GenerateOrderPakOutParam param) {
        Order order = orderService.selectByNo(param.getOrderNo(), param.getHostId());
        // 如果单据不存在则添加;如果单据存在,作业中无法修改,反之则修改单据
        if (!Cools.isEmpty(order)) {
            if (order.getSettle() > 1L) {
                throw new CoolException(param.getOrderNo() + "正在出库,无法修改单据");
            }
            orderService.removeById(order.getId());
        }
        DocType docType = docTypeService.getOne(new LambdaQueryWrapper<DocType>().eq(DocType::getDocId, param.getOrderType()));
        if (docType == null) {
            throw new CoolException("订单类型不存在");
        }
        Date now = new Date();
        // 单据主档
        order = new Order(
                String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                param.getOrderNo(),    // 订单编号
                DateUtils.convert(now),    // 单据日期
                docType.getDocId(),    // 单据类型
                null,    // 项目编号
                null,    //
                null,    // 调拨项目编号
                null,    // 初始票据号
                null,    // 票据号
                null,    // 客户编号
                null,    // 客户
                null,    // 联系方式
                null,    // 操作人员
                null,    // 合计金额
                null,    // 优惠率
                null,    // 优惠金额
                null,    // 销售或采购费用合计
                null,    // 实付金额
                null,    // 付款类型
                null,    // 业务员
                null,    // 结算天数
                null,    // 邮费支付类型
                null,    // 邮费
                null,    // 付款时间
                null,    // 发货时间
                null,    // 物流名称
                null,    // 物流单号
                1L,    // 订单状态
                1,    // 状态
                9527L,    // 添加人员
                now,    // 添加时间
                9527L,    // 修改人员
                now,    // 修改时间
                null,    // 备注
                param.getHostId() //仓库ID
        );
        if (!orderService.save(order)) {
            throw new CoolException("生成单据主档失败,请联系管理员");
        }
        List<GenerateOrderMatParam> mats = param.getMats();
        for (GenerateOrderMatParam matParam : mats) {
            Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, matParam.getMatnr()).eq(Mat::getHostId, param.getHostId()));
            if (Cools.isEmpty(mat)) {
                throw new CoolException(matParam.getMatnr() + "编号商品检索失败,请先添加商品");
            }
            OrderDetl orderDetl = new OrderDetl();
            orderDetl.sync(mat);
            orderDetl.setBatch(matParam.getBatch());
            orderDetl.setAnfme(matParam.getAnfme());
            orderDetl.setOrderId(order.getId());
            orderDetl.setOrderNo(order.getOrderNo());
            orderDetl.setCreateBy(9527L);
            orderDetl.setCreateTime(now);
            orderDetl.setUpdateBy(9527L);
            orderDetl.setUpdateTime(now);
            orderDetl.setStatus(1);
            orderDetl.setQty(0.0D);
            orderDetl.setHostId(param.getHostId());
            if (!orderDetlService.save(orderDetl)) {
                throw new CoolException("生成单据明细失败,请联系管理员");
            }
        }
    }
 
    @Override
    public List<DocType> getOrderType() {
        List<DocType> list = docTypeService.list();
        return list;
    }
 
    @Override
    public List<OrderSettle> getOrderStatus() {
        return orderSettleService.list();
    }
 
    @Override
    public List<OrderListDto> getOrderList(OrderListParam param, Long hostId) {
        ArrayList<OrderListDto> listDtos = new ArrayList<>();
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>().eq(Order::getHostId, hostId);
        if (!Cools.isEmpty(param)) {
            if (!Cools.isEmpty(param.getOrderType())) {
                wrapper.eq(Order::getDocType, param.getOrderType());
            }
            if (!Cools.isEmpty(param.getOrderNo())) {
                wrapper.eq(Order::getOrderNo, param.getOrderNo());
            }
            if (!Cools.isEmpty(param.getStatus())) {
                wrapper.eq(Order::getStatus, param.getStatus());
            }
        }
        List<Order> list = orderService.list(wrapper);
        for (Order order : list) {
            List<OrderDetl> orderDetls = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getHostId, hostId).eq(OrderDetl::getOrderId, order.getId()));
            OrderListDto dto = new OrderListDto();
            dto.setOrder(order);
            dto.setOrderDetls(orderDetls);
            listDtos.add(dto);
        }
        return listDtos;
    }
 
    @Override
    @Transactional
    public int createMat(CreateMatParam param) {
        int count = 0;
        Long hostId = param.getHostId();
        for (MatParam matParam : param.getMats()) {
            //获取分类
            Tag tag = tagService.getTag(matParam.getFirstTag(), matParam.getSecondTag(), hostId);
 
            matService.remove(new LambdaQueryWrapper<Mat>()
                    .eq(Mat::getMatnr, matParam.getMatnr())
                    .eq(Mat::getHostId, hostId));
            Mat mat = new Mat();
            mat.setMatnr(matParam.getMatnr());//物料号
            mat.setHostId(hostId);//仓库ID
            mat.setTagId(tag.getId());//分类ID
            if (!Cools.isEmpty(matParam.getMaktx())) {
                mat.setMaktx(matParam.getMaktx());
            }
            if (!Cools.isEmpty(matParam.getMaktx())) {
                mat.setMaktx(matParam.getMaktx());
            }
            if (!Cools.isEmpty(matParam.getName())) {
                mat.setName(matParam.getName());
            }
            if (!Cools.isEmpty(matParam.getSpecs())) {
                mat.setSpecs(matParam.getSpecs());
            }
            if (!Cools.isEmpty(matParam.getModel())) {
                mat.setModel(matParam.getModel());
            }
            if (!Cools.isEmpty(matParam.getColor())) {
                mat.setColor(matParam.getColor());
            }
            if (!Cools.isEmpty(matParam.getBrand())) {
                mat.setBrand(matParam.getBrand());
            }
            if (!Cools.isEmpty(matParam.getUnit())) {
                mat.setUnit(matParam.getUnit());
            }
            if (!Cools.isEmpty(matParam.getPrice())) {
                mat.setPrice(matParam.getPrice());
            }
            if (!Cools.isEmpty(matParam.getSku())) {
                mat.setSku(matParam.getSku());
            }
            if (!Cools.isEmpty(matParam.getUnits())) {
                mat.setUnits(matParam.getUnits());
            }
            if (!Cools.isEmpty(matParam.getBarcode())) {
                mat.setBarcode(matParam.getBarcode());
            }
            if (!Cools.isEmpty(matParam.getOrigin())) {
                mat.setOrigin(matParam.getOrigin());
            }
            if (!Cools.isEmpty(matParam.getManu())) {
                mat.setManu(matParam.getManu());
            }
            if (!Cools.isEmpty(matParam.getManuDate())) {
                mat.setManuDate(matParam.getManuDate());
            }
            if (!Cools.isEmpty(matParam.getItemNum())) {
                mat.setItemNum(matParam.getItemNum());
            }
            if (!Cools.isEmpty(matParam.getWeight())) {
                mat.setWeight(matParam.getWeight());
            }
            if (!Cools.isEmpty(matParam.getLength())) {
                mat.setLength(matParam.getLength());
            }
            if (!Cools.isEmpty(matParam.getVolume())) {
                mat.setVolume(matParam.getVolume());
            }
            if (!Cools.isEmpty(matParam.getThreeCode())) {
                mat.setThreeCode(matParam.getThreeCode());
            }
            if (!Cools.isEmpty(matParam.getSupp())) {
                mat.setSupp(matParam.getSupp());
            }
            if (!Cools.isEmpty(matParam.getSuppCode())) {
                mat.setSuppCode(matParam.getSuppCode());
            }
            if (!Cools.isEmpty(matParam.getDeadTime())) {
                mat.setDeadTime(matParam.getDeadTime());
            }
            if (!Cools.isEmpty(matParam.getMemo())) {
                mat.setMemo(matParam.getMemo());
            }
            if (!Cools.isEmpty(matParam.getStatus())) {
                mat.setStatus(matParam.getStatus());
            }else {
                mat.setStatus(1);
            }
 
            if (matService.save(mat)) {
                count++;
            }
        }
        return count;
    }
}