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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package com.consum.base.controller;
 
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.fastjson.JSONObject;
import com.consum.base.core.utils.IdUtil;
import com.consum.base.pojo.*;
import com.consum.base.service.*;
import com.consum.base.util.DateUtil;
import com.consum.model.po.*;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.core.utils.PageUtil;
import com.consum.base.pojo.dto.GoodModelInfoDTO;
import com.consum.base.pojo.excel.ImportProcureOrderTemplate;
import com.consum.base.pojo.excel.ProcureExcelTemplate;
import com.consum.base.pojo.query.FormProcureQry;
import com.consum.base.pojo.response.FormProcureVO;
import com.consum.base.pojo.response.GoodsModelVO;
import com.consum.base.pojo.response.GoodsTemplateCountVO;
import com.consum.base.pojo.response.GoodsTemplateInfoVO;
import com.consum.base.pojo.response.LWhFormProcureExtendVO;
import com.consum.base.service.core.LWhFormProcureCoreService;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.web.ResponseValue;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.ReflectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
/**
 * @ClassName LWhFormProcureController
 * @Date 2023/10/27
 * @Description
 * @Version 1.0
 **/
@Api(value = "仓库表单采购", tags = "仓库表单采购")
@RestController
@RequestMapping("/pc/whForm/procure")
public class LWhFormProcureController extends BaseController {
 
    @Resource
    private LWhFormProcureService lWhFormProcureService;
    @Resource
    private LWhFormProcureGoodsService lWhFormProcureGoodsServiceImpl;
    @Resource
    private LWhFormProcureCoreService lWhFormProcureCoreService;
    @Resource
    private LWhProcureModelService lWhProcureModelService;
    @Resource
    private BaseCategoryService baseCategoryService;
 
    /**
     * 仓库服务
     */
    @Autowired
    private BaseWarehouseService baseWarehouseService;
 
    /**
     * 入库id
     */
    @Autowired
    private BaseGoodsTemplateService baseGoodsTemplateService;
 
    /**
     * 物品model服务
     */
    @Autowired
    private BaseGoodsModelsService baseGoodsModelsService;
 
    /**
     * @Description 新增
     */
    @PostMapping("/add")
    public ResponseValue add() {
        LWhFormProcureParam param = CommonUtil.getObjFromReqBody(LWhFormProcureParam.class);
        LWhFormProcureParam param2 = new LWhFormProcureParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        return this.add(param);
    }
 
    private ResponseValue add(LWhFormProcureParam param) {
        if (param.getWarehouseId() == null) {
            return ResponseValue.error("仓库ID不能为空");
        }
 
        List<LWhFormProcureGoodsInfoParam> procureGoods = param.getProcureGoods();
        if (CollectionUtils.isEmpty(procureGoods)) {
            return ResponseValue.error("采购单不能为空");
        }
        FinSysTenantUser sysTenantUser = this.getSysInfo();
        S_user_core currentUser = this.getCurrentUser();
        lWhFormProcureService.add(param, sysTenantUser, currentUser);
        return ResponseValue.success();
    }
 
    /**
     * @Description 列表查询
     */
    @ApiOperation(value = "采购单列表查询", notes = "采购单列表查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "param", value = "采购查询条件", required = true,
            dataType = "FormProcureQryDto", paramType = "query")})
    @GetMapping("/list")
    public ResponseValue queryFormProcureList() {
        FormProcureQry param = CommonUtil.getObjFromReq(FormProcureQry.class);
        FormProcureQry param2 = new FormProcureQry();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        /*当前登录人只能看到自己机构下的列表*/
        FinSysTenantUser sysInfo = this.getSysInfo();
        String tenantId = sysInfo.getTenantId();
        if (param.getAgencyId() == null) {
            param.setAgencyId(Long.valueOf(tenantId));
        }
        PageUtil genericPager = lWhFormProcureService.queryFormProcureList(param);
        List<LWhFormProcure> data = genericPager.getDatas();
        ArrayList<FormProcureVO> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(data)) {
            data.forEach(item -> {
                FormProcureVO fromProcureVO = new FormProcureVO();
                BeanUtils.copyProperties(item, fromProcureVO);
 
                // 查询型号数量
                List<GoodsTemplateCountVO> procureCount =
                        lWhProcureModelService.getProcureCountByBusinessId(item.getId());
                fromProcureVO.setFromProcureTemplateInfoList(procureCount);
 
                result.add(fromProcureVO);
            });
        }
        try {
            Field fieldDatas = GenericPager.class.getDeclaredField("datas");
            ReflectUtil.setFieldValue(genericPager, fieldDatas, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        genericPager.setDatas(result);
        return ResponseValue.success(genericPager);
    }
 
    /**
     * @Description 编辑
     */
    @PostMapping("/edit")
    public ResponseValue edit() {
        LWhFormProcureParam param = CommonUtil.getObjFromReqBody(LWhFormProcureParam.class);
        LWhFormProcureParam param2 = new LWhFormProcureParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
        ResponseValue delFlag = delById(param.getId());
        if (delFlag.getCode() == ResponseValue.CODE_SUCCESS) {
            return this.add(param);
        }
        return ResponseValue.error("编辑失败!");
    }
 
    /**
     * @Description 根据id删除
     */
    @DeleteMapping("/del")
    public ResponseValue delById(Long id) {
        if (id == null) {
            return ResponseValue.error("参数不能为空!");
        }
        LWhFormProcure lWhFormProcure = lWhFormProcureService.get(new LWhFormProcure(id));
        if (lWhFormProcure == null) {
            return ResponseValue.error("删除失败!");
        }
        if (lWhFormProcure.getStates() != 1) {
            return ResponseValue.error("待入库状态才能删除!");
        }
        int num = lWhFormProcureService.delete(new LWhFormProcure(id));
        if (num == 0) {
            return ResponseValue.error("删除失败!");
        }
        lWhFormProcureGoodsServiceImpl.update("delete from L_WH_FORM_PROCURE_GOODS where WH_FORM_PROCURE_ID=" + id);
 
        lWhProcureModelService.update("delete from L_WH_PROCURE_MODEL where BUSINESS_TYPE =1 and BUSINESS_ID=" + id);
        return ResponseValue.success(1);
    }
 
    /**
     * 入库操作
     */
    @PostMapping("/income")
    public ResponseValue income(Long id) {
        String errMsg = lWhFormProcureCoreService.doProcure(id, getCurrentUser());
        if (errMsg == null) {
            return ResponseValue.success();
        }
        return ResponseValue.error(errMsg);
    }
 
    /**
     * 根据id查询详情
     */
    @GetMapping("/detail")
    public ResponseValue getById(Long id) throws Exception {
        if (id == null) {
            return ResponseValue.error("采购单id为空");
        }
        LWhFormProcure lWhFormProcure = lWhFormProcureService.get(new LWhFormProcure(id));
        if (lWhFormProcure == null) {
            return ResponseValue.error("采购单不存在");
        }
        LWhFormProcureExtendVO lWhFormProcureExtendVO = new LWhFormProcureExtendVO();
        BeanUtils.copyProperties(lWhFormProcure, lWhFormProcureExtendVO);
 
        // 物品
        LWhFormProcureGoods lWhFormProcureGoods = new LWhFormProcureGoods();
        lWhFormProcureGoods.setWhFormProcureId(id);
        List<LWhFormProcureGoods> formProcureGoods = lWhFormProcureGoodsServiceImpl.select(lWhFormProcureGoods);
 
        List<GoodsTemplateInfoVO> goodsTemplateInfoList = new ArrayList<>();
        for (LWhFormProcureGoods formProcureGood : formProcureGoods) {
            GoodsTemplateInfoVO goodsTemplateInfoVO = new GoodsTemplateInfoVO();
            BeanUtils.copyProperties(formProcureGood, goodsTemplateInfoVO);
 
            List<GoodModelInfoDTO> goodsModelInfoList =
                    lWhProcureModelService.getGoodsModelListByBusinessId(null, formProcureGood.getId());
            if (CollectionUtils.isEmpty(goodsModelInfoList)) {
                continue;
            }
            GoodModelInfoDTO goodModelInfoDTO = goodsModelInfoList.stream().findFirst().orElse(null);
            if (goodModelInfoDTO != null) {
                goodsTemplateInfoVO.setBaseCategoryName(goodModelInfoDTO.getCategoryName());
            }
            List<GoodsModelVO> goodsModelList = Lists.newArrayList();
            for (GoodModelInfoDTO goodModelInfo : goodsModelInfoList) {
                GoodsModelVO goodsModelVO = getGoodsModelVO(goodModelInfo);
                goodsModelList.add(goodsModelVO);
            }
 
            ReflectUtil.setFieldValue(goodsTemplateInfoVO, "models", goodsModelList);
 
            goodsTemplateInfoList.add(goodsTemplateInfoVO);
        }
        lWhFormProcureExtendVO.setProcureGoods(goodsTemplateInfoList);
        return ResponseValue.success("查询成功!", lWhFormProcureExtendVO);
    }
 
    private GoodsModelVO getGoodsModelVO(GoodModelInfoDTO goodModelInfo) {
        GoodsModelVO goodsModelVO = new GoodsModelVO();
 
        goodsModelVO.setId(goodModelInfo.getId());
        goodsModelVO.setBaseGoodsModelsName(goodModelInfo.getBaseGoodsModelsName());
        goodsModelVO.setUnit(goodModelInfo.getUnit());
        goodsModelVO.setCounts(goodModelInfo.getCounts());
        goodsModelVO.setTotalAmount(
                goodModelInfo.getTotalAmount() != null ? goodModelInfo.getTotalAmount().doubleValue() : null);
        goodsModelVO.setWorehouseCount(goodModelInfo.getWorehouseCount());
        goodsModelVO.setPrice(goodModelInfo.getPrice());
        goodsModelVO.setBaseGoodsModelsId(goodModelInfo.getBaseGoodsModelsId());
        return goodsModelVO;
    }
 
    @ApiOperation(value = "采购单明细查询", notes = "采购单明细查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "param", value = "采购单明细查询", required = true,
            dataType = "FormProcureQryDto", paramType = "query")})
    @GetMapping("detail/list")
    public ResponseValue queryFormProcureDetailList() {
        FormProcureQry formProcureQry = CommonUtil.getObjFromReq(FormProcureQry.class);
        FormProcureQry param2 = new FormProcureQry();
        CommonUtil.copyProperties(formProcureQry, param2);
        formProcureQry = param2;
 
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        // formProcureQry.setAgencyId(Long.valueOf(sysInfo.getTenantId()));
        GenericPager genericPager = lWhFormProcureService.queryFormProcureDetailList(formProcureQry);
        return ResponseValue.success(genericPager);
    }
 
    @ApiOperation(value = "采购单导出", notes = "采购单导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "query")})
    @GetMapping("/list/export")
    public ResponseValue<String> export(Long id, HttpServletResponse response) throws Exception {
        TemplateExportParams params = new TemplateExportParams("import/采购入库单.xls");
        params.setHeadingStartRow(2);
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
 
        List<ProcureExcelTemplate> exportList = lWhFormProcureService.getExportList(id);
        if (CollectionUtils.isEmpty(exportList)) {
            throw new RuntimeException("数据为空");
        }
 
        int countNum =
                exportList.stream().filter(item -> item.getNum() != null).mapToInt(ProcureExcelTemplate::getNum).sum();
        double totalAmount = exportList.stream().filter(export -> export.getTotalAmount() != null)
                .mapToDouble(ProcureExcelTemplate::getAmount).sum();
        Optional<ProcureExcelTemplate> first = exportList.stream().findFirst();
        ProcureExcelTemplate templateExcelExport = first.get();
        String businessFormCode = templateExcelExport.getBusinessFormCode();
        Long createTime = templateExcelExport.getCreateTime();
        String operatorName = templateExcelExport.getOperatorName();
 
        Map<String, Object> map = new HashMap<>();
        map.put("code", businessFormCode);
        map.put("date", DateUtils.toShowDate(createTime));
        map.put("name", operatorName);
        map.put("countNum", countNum);
        map.put("totalAmount", totalAmount);
 
        Workbook workbook = ExcelExportUtil.exportExcel(params, ProcureExcelTemplate.class, exportList, map);
        String filePath = downLoadExcel("采购入库单", workbook);
        return ResponseValue.success("导出成功", filePath);
 
    }
 
    /**
     * 采购单导入
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "采购单导入", notes = "采购单导入")
    @PostMapping("/import")
    public ResponseValue upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        // 文件格式校验
//        if (!".xls".endsWith(originalFilename)) {
//            return ResponseValue.error("文件格式有误!");
//        }
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("当前登录用户为空");
        }
        try {
            EasyExcelFactory.read(file.getInputStream(), ImportProcureOrderTemplate.class,
                    new AnalysisEventListener<ImportProcureOrderTemplate>() {
                        /**
                         * 自采采购单的内层数据
                         */
                        final List<LWhFormProcureGoodsInfoParam> zcList = Lists.newArrayList();
 
                        /**
                         * 集采采购单的内层数据
                         */
                        final List<LWhFormProcureGoodsInfoParam> jcList = Lists.newArrayList();
 
                        /**
                         * 采购单的主数据
                         */
                        final LWhFormProcureParam lWhFormProcureParam = new LWhFormProcureParam();
 
                        /**
                         * 第几条数据
                         */
                        Integer index = 0;
 
                        /**
                         * 表头信息
                         * @param headMap
                         * @param context
                         */
                        @Override
                        public void invokeHeadMap(Map headMap, AnalysisContext context) {
                            // 验证表头数量
                            logger.info("解析分发单的表头长度: {}", headMap.size());
                            if (headMap.size() != 12) {
                                throw new ExcelAnalysisException("上传的文件不符!");
                            }
                        }
 
                        /**
                         * 数据处理
                         * @param data
                         * @param analysisContext
                         */
                        @Override
                        public void invoke(ImportProcureOrderTemplate data, AnalysisContext analysisContext) {
                            index++;
                            String categoryOne = data.getCategoryOne();
                            String categoryTwo = data.getCategoryTwo();
                            String categoryThree = data.getCategoryThree();
                            if (StrUtil.isEmpty(categoryThree)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,品类名称不能为空:" + categoryThree);
                            }
                            String goodsName = data.getGoodsName();
                            if (StrUtil.isEmpty(goodsName)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,品名不能为空:" + goodsName);
                            }
                            String goodModelName = data.getGoodModelName();
                            if (StrUtil.isEmpty(goodModelName)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,规格型号不能为空:" + goodModelName);
                            }
                            String unit = data.getUnit();
                            if (StrUtil.isEmpty(unit)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,单位不能为空:" + unit);
                            }
                            String type = data.getType();
                            //String agencyName = data.getAgencyName();
                            String warehouseName = data.getWarehouseName();
                            if (StrUtil.isEmpty(warehouseName)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,所在仓库不能为空:" + warehouseName);
                            }
                            String supplierName = data.getSupplierName();
                            if (StrUtil.isEmpty(supplierName)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,供货商不能为空:" + supplierName);
                            }
 
                            String price = data.getPrice();
                            if (StrUtil.isEmpty(price)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,单价(元)不能为空:" + price);
                            }
                            String num = data.getNum();
                            if (StrUtil.isEmpty(num)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,库存数量不能为空:" + num);
                            }
                            // 物品类型(自采集采)
                            String goodsType = data.getGoodsType();
                            if (StrUtil.isEmpty(goodsType)) {
                                throw new ExcelAnalysisException("第" + index + "条数据,物品类型不能为空:" + goodsType);
                            }
 
                            // 第一次进来把外层的值设置一下
                            if (lWhFormProcureParam.getAgencyId() == null) {
                                // [通过仓库名,查找对应的仓库id]
                                FinSysTenantUser currentUser = getSysInfo();
                                BaseWarehouseParam baseWarehouseParam = new BaseWarehouseParam();
                                baseWarehouseParam.setStates(1);
                                baseWarehouseParam.setWarehouseName(warehouseName);
                                GenericPager<BaseWarehouse> pager = baseWarehouseService.queryList(baseWarehouseParam, currentUser);
                                List<BaseWarehouse> datas = pager.getDatas();
                                Long ckId = null;
                                if (!CollectionUtil.isEmpty(datas)) {
                                    BaseWarehouse baseWarehouse = datas.get(0);
                                    ckId = baseWarehouse.getId();
                                } else {
                                    // 报错 仓库不存在
                                    throw new ExcelAnalysisException("第" + index + "条数据,仓库不存在:" + warehouseName);
                                }
                                lWhFormProcureParam.setProcureDoc("");
                                lWhFormProcureParam.setWarehouseId(ckId);
                                lWhFormProcureParam.setProcureTime(DateUtil.getNowDate() + "");
                                lWhFormProcureParam.setBuyType(2);
                            }
                            // 查询分类id
                            BaseCategory baseCategory = baseCategoryService.getByCategoryByName(categoryThree);
                            if (baseCategory == null) {
                                throw new ExcelAnalysisException("第" + index + "条数据,此分类不存在:" + categoryThree);
                            }
                            Long categoryId = baseCategory.getId();
                            // 不同采集类型,不同筛选
                            Optional<LWhFormProcureGoodsInfoParam> optional = null;
                            if ("自采".equals(goodsType)) {
                                optional = zcList.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst();
                            } else if ("集采".equals(goodsType)) {
                                optional = jcList.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst();
                            }
 
                            Optional<LWhFormProcureGoodsInfoParam> optional1 = null;
                            if ("自采".equals(goodsType)) {
                                optional1 = zcList.stream().filter(item -> item.getGoodsTemplateName().equals(goodsName)).findFirst();
                            } else if ("集采".equals(goodsType)) {
                                optional1 = jcList.stream().filter(item -> item.getGoodsTemplateName().equals(goodsName)).findFirst();
                            }
 
                            // 品类 和 商品名完全一样才加入
                            if (optional.isPresent() && optional1.isPresent()) {
                                LWhProcureModelParam lWhProcureModelParam = new LWhProcureModelParam();
                                lWhProcureModelParam.setCounts(StrUtil.isEmpty(num) ? 0 : Integer.valueOf(num));
                                lWhProcureModelParam.setPrice(new BigDecimal(price).multiply(new BigDecimal("100")).longValue());
                                lWhProcureModelParam.setBaseUnit(unit);
 
 
                                // 数据第二层-物品的id
                                BaseGoodsTemplate goodsTemplate = baseGoodsTemplateService.getByGoodsNameAndCategoryId(goodsName, categoryId);
                                if (goodsTemplate == null) {
                                    throw new ExcelAnalysisException("第" + index + "条数据,此物品不存在:" + goodsName);
                                }
                                BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
                                baseGoodsModels.setModelName(goodModelName);
                                baseGoodsModels.setGoodsTemplatesId(goodsTemplate.getId());
                                BaseGoodsModels byModelNameAndGoodsTemplatesId = baseGoodsModelsService.getByModelNameAndGoodsTemplatesId(baseGoodsModels);
                                if (byModelNameAndGoodsTemplatesId == null) {
                                    throw new ExcelAnalysisException("第" + index + "条数据" + "品名:" + goodsName + ",规格型号:" + goodModelName + "未找到");
                                }
                                lWhProcureModelParam.setBaseGoodsModelsId(byModelNameAndGoodsTemplatesId.getId());
                                optional.get().getModels().add(lWhProcureModelParam);
                            } else {
                                LWhFormProcureGoodsInfoParam lWhFormProcureGoodsInfoParam = new LWhFormProcureGoodsInfoParam();
                                // 数据第二层-物品三级分类第三级的id
                                lWhFormProcureGoodsInfoParam.setCategoryId(categoryId);
                                // 数据第二层-物品的id
                                BaseGoodsTemplate goodsTemplate = baseGoodsTemplateService.getByGoodsNameAndCategoryId(goodsName, categoryId);
                                if (goodsTemplate == null) {
                                    throw new ExcelAnalysisException("第" + index + "条数据,此物品不存在:" + goodsName);
                                }
                                lWhFormProcureGoodsInfoParam.setBaseGoodsTemplateId(goodsTemplate.getId());
                                // 数据第二层-供应商
                                lWhFormProcureGoodsInfoParam.setSupplier(supplierName);
                                // 数据第二层-物品名
                                lWhFormProcureGoodsInfoParam.setGoodsTemplateName(goodsName);
                                // 数据第三层数据
                                List<LWhProcureModelParam> lWhProcureModelParams = new ArrayList<>();
                                LWhProcureModelParam lWhProcureModelParam = new LWhProcureModelParam();
                                lWhProcureModelParam.setCounts(StrUtil.isEmpty(num) ? 0 : Integer.valueOf(num));
                                BigDecimal multiply = new BigDecimal(price).multiply(new BigDecimal("100"));
                                lWhProcureModelParam.setPrice(multiply.longValue());
                                lWhProcureModelParam.setBaseUnit(unit);
                                BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
                                baseGoodsModels.setModelName(goodModelName);
                                baseGoodsModels.setGoodsTemplatesId(goodsTemplate.getId());
                                BaseGoodsModels byModelNameAndGoodsTemplatesId = baseGoodsModelsService.getByModelNameAndGoodsTemplatesId(baseGoodsModels);
                                if (byModelNameAndGoodsTemplatesId == null) {
                                    throw new ExcelAnalysisException("第" + index + "条数据" + "品名:" + goodsName + ",规格型号:" + goodModelName + "未找到");
                                }
                                lWhProcureModelParam.setBaseGoodsModelsId(byModelNameAndGoodsTemplatesId.getId());
                                lWhProcureModelParams.add(lWhProcureModelParam);
                                lWhFormProcureGoodsInfoParam.setModels(lWhProcureModelParams);
                                // 不同采集类型到不同list
                                if ("自采".equals(goodsType)) {
                                    zcList.add(lWhFormProcureGoodsInfoParam);
                                } else if ("集采".equals(goodsType)) {
                                    jcList.add(lWhFormProcureGoodsInfoParam);
                                }
                            }
                        }
 
                        /**
                         * 处理后的数据导入
                         * @param analysisContext
                         */
                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            lWhFormProcureParam.setProcureGoods(zcList);
                            // 采购单-自采
                            long zcId = IdUtil.generateId();
                            lWhFormProcureParam.setId(zcId);
                            String zcJsonString = JSONObject.toJSONString(lWhFormProcureParam);
                            logger.info("导入采购单(自采)json数据 --------------------");
                            logger.info(zcJsonString);
                            add(lWhFormProcureParam);
 
                            // 采购单-集采
                            long jcId = IdUtil.generateId();
                            lWhFormProcureParam.setId(jcId);
                            lWhFormProcureParam.setBuyType(1);
                            lWhFormProcureParam.setProcureGoods(jcList);
                            String jcJsonString = JSONObject.toJSONString(lWhFormProcureParam);
                            logger.info("导入采购单(集采)json数据 --------------------");
                            logger.info(jcJsonString);
                            add(lWhFormProcureParam);
                            // 自采和集采入库
                            income(zcId);
                            income(jcId);
                        }
 
                        @Override
                        public void onException(Exception exception, AnalysisContext analysisContext) throws Exception {
                            if (exception instanceof ExcelDataConvertException) {
                                ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
                                Integer row = excelDataConvertException.getRowIndex() + 1;
                                Integer column = excelDataConvertException.getColumnIndex() + 1;
                                throw new ExcelAnalysisException("第" + row + "行,第" + column + "列解析异常,请正确填写");
                            } else {
                                throw new ExcelAnalysisException(exception.getMessage());
                            }
                        }
                    }).sheet(0).doRead();
        }catch (ExcelAnalysisException e) {
            return ResponseValue.error(e.getMessage());
        }catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseValue.error("系统错误");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseValue.error("系统错误");
        }
        return ResponseValue.success("导入成功!");
    }
 
}