郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package com.gk.firework.Controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.firework.Controller.Base.BaseController;
import com.gk.firework.Domain.*;
import com.gk.firework.Domain.Log.JsonParams;
import com.gk.firework.Domain.Utils.FilterObject;
import com.gk.firework.Domain.Utils.Msg;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Vo.DirectionDetail;
import com.gk.firework.Domain.Vo.FireworkDeal;
import com.gk.firework.Domain.Vo.ProductVo;
import com.gk.firework.Service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.gk.firework.Domain.Enum.ErrorCode.*;
import static com.gk.firework.Domain.Enum.ErrorCode.ERROR_10003;
 
/**
 * @author : jingjy
 * @date : 2021/4/1 10:21
 */
@Api(tags = "出库接口")
@RestController
@RequestMapping("/delivery")
public class DeliveryController extends BaseController {
    @Autowired
    private DeliveryOrderService deliveryOrderService;
    @Autowired
    private DeliveryDetailService deliveryDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private StockService stockService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductLocusService productLocusService;
    @Autowired
    private WarnContentService warnContentService;
 
    /**
     * @param jsonArray 接收json数组格式为:[{"id":"用户ID","rfids":"出库流向码,以逗号进行拼接","time":"出库时间","type":"1或者2;1:正常出库;2:退货出库"}]
     * @return msg
     */
    @ApiOperation(value = "出库",notes = "接收json数组格式为:[{\"id\":\"用户ID\",\"rfids\":\"出库流向码,以逗号进行拼接\",\"time\":\"出库时间\",\"type\":\"1或者2;1:正常出库;2:退货出库\"}]")
    @PostMapping("/out")
    @JsonParams
    public Msg deliveryStock(@RequestBody JSONArray jsonArray){
        Msg msg = new Msg(true);
        if (jsonArray.isEmpty()){
            msg.setCode(ERROR_10001.getCode());
            msg.setMessage(ERROR_10001.getMsg()+":未收到出库信息!");
            return msg;
        }
 
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String userId = jsonObject.getString("id");
            String directionCodeStr = jsonObject.getString("rfids");
            String datetime = jsonObject.getString("time");
            String type = jsonObject.getString("type");
            String cert = jsonObject.getString("cert");
            Date date;
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = dateFormat.parse(datetime);
            } catch (ParseException e) {
                msg.setCode(ERROR_10003.getCode());
                msg.setMessage(ERROR_10003.getMsg()+":日期类型错误!");
                return msg;
            }
 
            if (StringUtils.isBlank(directionCodeStr)){
                msg.setCode(ERROR_10001.getCode());
                msg.setMessage(ERROR_10001.getMsg()+":流向码不能为空");
                return msg;
            }
 
            boolean isUser = userService.checkUserById(userId);
            if (!isUser){
                msg.setCode(ERROR_50001.getCode());
                msg.setMessage(ERROR_50001.getMsg()+":未找到相关用户信息,不能出库!");
                return msg;
            }
            UserInfo userInfo = userService.getById(userId);
 
            List<String> directionCodes = StringUtils.toList(directionCodeStr);
 
            //判断是否存在同时间同操作人出库记录
            boolean exist = deliveryOrderService.isOrderExist(datetime,userInfo);
            if (exist){
                continue;
            }
 
            //出库单创建
            DeliveryOrderInfo deliveryOrderInfo;
            String auth = getAuth();
            auth = StringUtils.isBlank(auth) ? "NOAUTH" : auth;
            deliveryOrderInfo = deliveryOrderService.generateDeliveryOrder(type,date,userInfo,cert,auth);
            List<DeliveryDetailInfo>deliveryDetailInfos = deliveryDetailService.generateDeliveryDetail(deliveryOrderInfo,userInfo,directionCodes);
 
            deliveryOrderService.save(deliveryOrderInfo);
            deliveryDetailService.saveBatch(deliveryDetailInfos);
            //扣库存
            stockService.deliveryByDetail(date,deliveryDetailInfos,userInfo);
        }
        return msg;
    }
 
    @ApiOperation(value = "快速出库",response = Msg.class)
    @PostMapping("/deliveryQuick")
    @JsonParams
    public Msg deliveryQuick(@RequestBody JSONArray jsonArray){
        Msg msg = new Msg(true);
        if (jsonArray.size() > 0)
        {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String userId = jsonObject.getString("id");
                String directionCodeFirst = jsonObject.getString("rfidfirst");
                String directionCodeLast = jsonObject.getString("rfidlast");
                String datetime = jsonObject.getString("time");
                String transport = jsonObject.getString("cert");
                //type 1.常规出库; 2.退货出库
                String type = "1";
                Date date;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    date = dateFormat.parse(datetime);
                } catch (ParseException e) {
                    return new Msg(ERROR_10003,"出库日期类型错误");
                }
                if (!(FireworkDeal.is22Characters(directionCodeFirst) && FireworkDeal.is22Characters(directionCodeLast))){
                    return new Msg(ERROR_10004,"流向码首尾不符合规则,无法快速出库");
                }
                boolean isUser = userService.checkUserById(userId);
                if (!isUser){
                    return new Msg(ERROR_50001,"未找到相关用户信息,不能出库");
                }
                UserInfo userInfo = userService.getById(userId);
                //判断是否存在同时间同操作人入库记录
                boolean exist = deliveryOrderService.isOrderExist(datetime,userInfo);
                if (exist){ continue; }
 
                String auth = getAuth();
                auth = StringUtils.isBlank(auth) ? "NOAUTH" : auth;
                DeliveryOrderInfo orderInfo = deliveryOrderService.generateDeliveryOrder(type,date,userInfo,transport, auth);
                List<DeliveryDetailInfo>detailInfos = new ArrayList<>();
                List<ProductLocusInfo>locusInfos = new ArrayList<>();
                List<ProductVo> productVos = new ArrayList<>();
                DirectionDetail detailFirst = FireworkDeal.dealDirectionCode(directionCodeFirst);
                DirectionDetail detailLast = FireworkDeal.dealDirectionCode(directionCodeLast);
                if (!detailFirst.getItemCode().equals(detailLast.getItemCode())){
                    return new Msg(ERROR_10004,"首尾商品编码不一致,无法快速出库");
                }
                if (Integer.parseInt(detailFirst.getSerialNo()) > Integer.parseInt(detailLast.getSerialNo())){
                    return new Msg(ERROR_10004,"首位商品序号大于末位,无法快速出库");
                }
                ProductVo productVo = productService.selectVoByDirection(directionCodeFirst);
                if (productVo == null){
                    return new Msg(ERROR_50001,"未找到相关产品信息,不能出库");
                }
 
                Integer firstNo = Integer.parseInt(detailFirst.getSerialNo());
                Integer lastNo = Integer.parseInt(detailLast.getSerialNo());
                int perBoxNum = Integer.parseInt(detailFirst.getBoxNo());
                int boxNum = ((lastNo-firstNo)/perBoxNum)+1;
                int totalNum = 0;
 
                for (int j=1;j<= boxNum;j++){
                    String dire = detailFirst.getItemCode()+detailFirst.getDateCode()+ String.format("%05d", firstNo)+String.format("%03d", perBoxNum);
                    DeliveryDetailInfo detailInfo = new DeliveryDetailInfo();
                    detailInfo.setDeliverycode(orderInfo.getOrdercode());
                    detailInfo.setDirectioncode(dire);
                    detailInfo.setItemcode(productVo.getItemCode());
                    detailInfo.setItemname(productVo.getName());
                    detailInfo.setCreateat(new Date());
                    detailInfo.setCreateby(userInfo.getUsername());
                    detailInfo.setNum(perBoxNum);
                    detailInfos.add(detailInfo);
                    ProductLocusInfo productLocusInfo = new ProductLocusInfo(dire,new Date(),date, userInfo.getCompany(),
                            null,ProductLocusInfo.DELIVERY_STATUS,productVo.getBoxNumber().toString());
                    locusInfos.add(productLocusInfo);
                    firstNo+=perBoxNum;
                    totalNum += perBoxNum;
                }
 
                FireworkDeal.getProductVos(directionCodeFirst,detailFirst,detailLast,productVos,productVo);
                orderInfo.setBoxnum(totalNum);
                deliveryOrderService.save(orderInfo);
                deliveryDetailService.saveBatch(detailInfos);
                productLocusService.insertBatch(locusInfos);
                stockService.deliveryByDetail(date,detailInfos,userInfo);
            }
        }
        return msg;
    }
 
    /**
     * 退货出库
     */
    @PostMapping("/entryReturn")
    @ApiOperation(value = "退货出库", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filter.code", type = "JSONObject",
                    value = " JSONObject{\"datetime\":\"时间\",\"data\":[{\"directionCode\":\"流向码\"},{\"directionCode\":\"流向码\"}]}"),
    })
    @JsonParams
    public Msg getProductListByCodes(@RequestBody JSONObject object) {
        JSONArray jsonArray = object.getJSONArray("data");
        String datetime = object.getString("datetime");
        Date date;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(datetime);
        } catch (ParseException e) {
            return new Msg(ERROR_10003,"日期类型错误!");
        }
        //生成出库单号,类型,退货出库
        DeliveryOrderInfo deliveryOrderInfo;
        UserInfo userInfo = userService.getById(getUser().getId());
        String auth = getAuth();
        auth = StringUtils.isBlank(auth) ? "NOAUTH" : auth;
 
        deliveryOrderInfo = deliveryOrderService.generateDeliveryOrder(FireworkDeal.OTHER_DELIVERY,date,userInfo, null, auth);
        List<DeliveryDetailInfo>deliveryDetailInfos = new ArrayList<>();
        List<StockInfo>stockInfos = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            // 遍历 jsonArray 数组,把每一个对象转成 json 对象
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String directionCode = jsonObject.getString("directionCode");
            DirectionDetail directionDetail = FireworkDeal.dealDirectionCode(directionCode);
            ProductInfo productInfo = productService.selectByDirection(directionDetail.getItemCode());
 
            // 生成出库详情,库存OWNER设置为空
            DeliveryDetailInfo deliveryDetailInfo = new DeliveryDetailInfo();
            deliveryDetailInfo.setDirectioncode(directionCode);
            deliveryDetailInfo.setDeliverycode(deliveryOrderInfo.getOrdercode());
            deliveryDetailInfo.setItemcode(directionDetail.getItemCode());
            deliveryDetailInfo.setItemname(productInfo.getName());
            deliveryDetailInfo.setCreateat(new Date());
            deliveryDetailInfo.setCreateby(getUser().getUsername());
            deliveryDetailInfos.add(deliveryDetailInfo);
            StockInfo stockInfo = stockService.selectStockByDirection(directionCode);
            if (stockInfo == null){
                return new Msg(ERROR_50001,"库存信息未找到");
            }
            stockInfo.setOwner("");
            stockInfo.setModifiedby(userInfo.getUsername());
            stockInfo.setModifieddate(date);
            stockInfos.add(stockInfo);
        }
 
        deliveryOrderService.save(deliveryOrderInfo);
        deliveryDetailService.saveBatch(deliveryDetailInfos);
        stockService.updateStocks(stockInfos,userInfo);
        return success();
    }
 
 
    /**
    * @Description: 出库查询
    * @date 2021/4/15 12:05
    */
    @PostMapping("/outbound-query")
    @ApiOperation(value = "出库查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),
            @ApiImplicitParam(name = "filter.province", value = "省"),
            @ApiImplicitParam(name = "filter.city", value = "市"),
            @ApiImplicitParam(name = "filter.district", value = "区"),
            @ApiImplicitParam(name = "filter.street", value = "街道"),
            @ApiImplicitParam(name = "filter.committee", value = "委员会"),
            @ApiImplicitParam(name = "filter.starttime", value = "开始时间"),
            @ApiImplicitParam(name = "filter.endtime", value = "结束时间"),
            @ApiImplicitParam(name = "filter.transportcert", value = "运输证号码"),
            @ApiImplicitParam(name = "filter.enterprisename", value = "入库企业名称"),
            @ApiImplicitParam(name = "filter.safetysupervision", value = "安全监管分类"),
            @ApiImplicitParam(name = "filter.code", value = "单号"),
    })
    Object getOutboundInfo(@RequestBody FilterObject jsonFilter) {
        Integer pageIndex = jsonFilter.getPageIndex();
        Integer pageSize = jsonFilter.getPageSize();
        IPage page =  deliveryOrderService.selectPage(new Page<>(pageIndex, pageSize), jsonFilter.getFilter(), getUser());
        return success(page);
    }
 
    @PostMapping("/stock/outbound-query")
    @ApiOperation(value = "出库查询", httpMethod = "POST")
    Object getStockOutboundInfo(@RequestBody FilterObject jsonFilter) {
        Integer pageIndex = jsonFilter.getPageIndex();
        Integer pageSize = jsonFilter.getPageSize();
        IPage page =  deliveryOrderService.selectStockPage(new Page<>(pageIndex, pageSize), jsonFilter.getFilter(), getUser());
        return success(page);
    }
 
    /**
    * @Description: 出库导出
    * @date 2021/4/19 17:24
    */
    /**
     * @Description: 出库查询
     * @date 2021/4/15 12:05
     */
    @PostMapping("/export/outbound")
    @ApiOperation(value = "出库查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),
            @ApiImplicitParam(name = "filter.province", value = "省"),
            @ApiImplicitParam(name = "filter.city", value = "市"),
            @ApiImplicitParam(name = "filter.district", value = "区"),
            @ApiImplicitParam(name = "filter.street", value = "街道"),
            @ApiImplicitParam(name = "filter.committee", value = "委员会"),
            @ApiImplicitParam(name = "filter.starttime", value = "开始时间"),
            @ApiImplicitParam(name = "filter.endtime", value = "结束时间"),
            @ApiImplicitParam(name = "filter.transportcert", value = "运输证号码"),
            @ApiImplicitParam(name = "filter.enterprisename", value = "入库企业名称"),
    })
    Object exportOutbound(@RequestBody FilterObject jsonFilter) {
        List<DeliveryOrderInfo> list =  deliveryOrderService.selectExportList(jsonFilter.getFilter(), getUser());
        return success(list);
    }
 
 
    /**
     * @Description: 根据{出库单号}查询出库明细
     * @date 2021/4/15 12:05
     */
    @PostMapping("/outbound-detail")
    @ApiOperation(value = "出库单明细", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),
            @ApiImplicitParam(name = "filter.code", value = "入库单号code"),
    })
    Object getOutboundDetail(@RequestBody FilterObject jsonFilter) {
        Integer pageIndex = jsonFilter.getPageIndex();
        Integer pageSize = jsonFilter.getPageSize();
        IPage page =  deliveryDetailService.selectDetailPage(new Page<>(pageIndex, pageSize), jsonFilter.getFilter());
        return success(page);
    }
 
    /**
     * @Description: 根据{出库单code}查询出库明细并导出
     * @date 2021/4/15 11:09
     */
    @GetMapping("/export/outbound-detail")
    @ApiOperation(value = "出库单明细导出", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "出库单号code"),
    })
    Object getOutboundDetail(@RequestParam String code) {
        List<Map> detailExport = deliveryDetailService.selectDetailExport(code);
        return success(detailExport);
    }
 
    @GetMapping("/cert")
    @ApiOperation(value = "根据运输证查询出库明细", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cert", value = "运输证号"),
    })
    Msg getDeliveryDetailByCert(@RequestParam String cert){
        if (StringUtils.isBlank(cert)){
            return new Msg(ERROR_10001.getCode(),ERROR_10001.getMsg());
        }
        Map<String,List>map = new HashMap<>();
        List<DeliveryOrderInfo> deliveryOrderInfos = deliveryOrderService.getDeliveryOrderByCert(cert);
        if (deliveryOrderInfos == null || deliveryOrderInfos.size() == 0){
            return new Msg(ERROR_50001.getCode(),ERROR_50001.getMsg()+":没有找到对应的出库记录!");
        }
        List<DeliveryDetailInfo>deliveryDetailInfoList = new ArrayList<>();
        for (DeliveryOrderInfo deliveryOrderInfo : deliveryOrderInfos){
            List<DeliveryDetailInfo>deliveryDetailInfos = deliveryDetailService.getDetailByCode(deliveryOrderInfo.getOrdercode());
            deliveryDetailInfoList.addAll(deliveryDetailInfos);
        }
        List<ProductVo>productVos = deliveryOrderService.getProductVosByCert(cert);
        map.put("detail",deliveryDetailInfoList);
        map.put("product",productVos);
 
        return success(map);
    }
 
    @ApiOperation(value = "批量退库")
    @PostMapping("/returnBatch")
    @JsonParams
    public List<Msg> returnBatch(@RequestBody JSONArray array) {
        List<Msg>msgList = new ArrayList<>();
        if (array.size() < 1) {
            return null;
        }
        for (int j = 0; j < array.size(); j++) {
            // 遍历 jsonArray 数组,把每一个对象转成 json 对象
            JSONObject object = array.getJSONObject(j);
            JSONArray jsonArray = object.getJSONArray("data");
            String datetime = object.getString("datetime");
            Date date;
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = dateFormat.parse(datetime);
            } catch (ParseException e) {
                msgList.add(new Msg(ERROR_10003, "日期类型错误!"));
                continue;
            }
            //生成出库单号,类型,退货出库
            DeliveryOrderInfo deliveryOrderInfo;
            UserInfo userInfo = userService.getById(getUser().getId());
            String auth = getAuth();
            auth = StringUtils.isBlank(auth) ? "NOAUTH" : auth;
 
            deliveryOrderInfo = deliveryOrderService.generateDeliveryOrder(FireworkDeal.OTHER_DELIVERY, date, userInfo, null, auth);
            List<DeliveryDetailInfo> deliveryDetailInfos = new ArrayList<>();
            List<StockInfo> stockInfos = new ArrayList<>();
            StringBuilder stringBuilder = new StringBuilder();
            boolean flag = false;
            for (int i = 0; i < jsonArray.size(); i++) {
                // 遍历 jsonArray 数组,把每一个对象转成 json 对象
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String directionCode = jsonObject.getString("directionCode");
                DirectionDetail directionDetail = FireworkDeal.dealDirectionCode(directionCode);
                ProductInfo productInfo = productService.selectByDirection(directionDetail.getItemCode());
 
                // 生成出库详情,库存OWNER设置为空
                DeliveryDetailInfo deliveryDetailInfo = new DeliveryDetailInfo();
                deliveryDetailInfo.setDirectioncode(directionCode);
                deliveryDetailInfo.setDeliverycode(deliveryOrderInfo.getOrdercode());
                deliveryDetailInfo.setItemcode(directionDetail.getItemCode());
                deliveryDetailInfo.setItemname(productInfo.getName());
                deliveryDetailInfo.setCreateat(new Date());
                deliveryDetailInfo.setCreateby(getUser().getUsername());
                deliveryDetailInfos.add(deliveryDetailInfo);
                StockInfo stockInfo = stockService.selectStockByDirection(directionCode);
                if (stockInfo == null) {
                    stringBuilder.append(directionCode).append("库存信息未找到");
                    flag = true;
                    continue;
                }
                stockInfo.setOwner("");
                stockInfos.add(stockInfo);
            }
            if (flag){
                msgList.add(new Msg(ERROR_50001,stringBuilder.toString()));
                continue;
            }
 
            deliveryOrderService.save(deliveryOrderInfo);
            deliveryDetailService.saveBatch(deliveryDetailInfos);
            stockService.updateStocks(stockInfos, getUser());
            msgList.add(new Msg(SUCCESS));
        }
        return msgList;
    }
 
}