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
package com.consum.base.controller;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.consum.base.service.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.type.StatesType;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.core.utils.MapUtils;
import com.consum.base.core.utils.MapperUtil;
import com.consum.base.pojo.BaseGoodsTemplateParam;
import com.consum.base.pojo.excel.ImportGoodsInfoTemplate;
import com.consum.base.pojo.response.GoodsTemplateVO;
import com.consum.model.po.BaseCategory;
import com.consum.model.po.BaseGoodsModels;
import com.consum.model.po.BaseGoodsTemplate;
import com.consum.model.po.BaseWarehouse;
import com.consum.model.po.FinSysTenant;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.SDictData;
import com.consum.model.vo.BaseGoodsTemplateVo;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
/**
 * @Description 物品模板
 * @Author 卢庆阳
 * @Date 2023/10/24
 */
@Api(value = "物品模板", tags = "物品模板")
@RestController
@RequestMapping("/pc/base/goods/template")
public class BaseGoodsTemplateController extends BaseController {
 
    @Autowired
    private BaseGoodsTemplateService baseGoodsTemplateService;
 
    @Autowired
    private LWhGoodsService lWhGoodsService;
 
    @Autowired
    private BaseWarehouseService baseWarehouseService;
 
    @Autowired
    private FinSysTenantService finSysTenantService;
 
    @Autowired
    private BaseCategoryService baseCategoryService;
 
    @Autowired
    private BaseGoodsModelsService baseGoodsModelsService;
 
    /**
     * @Description 新增物品模板
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    @PostMapping("/add")
    public ResponseValue add() {
        BaseGoodsTemplateParam param = CommonUtil.getObjFromReqBody(BaseGoodsTemplateParam.class);
        BaseGoodsTemplateParam param2 = new BaseGoodsTemplateParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
 
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (param.getCategoryId() == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        if (param.getCategoryId() == null) {
            return ResponseValue.error("分类为空");
        }
        if (StringUtils.isEmpty(param.getGoodsName())) {
            return ResponseValue.error("物品名称为空");
        }
        // 判断同一分类下的物品名称是否重复
        BaseGoodsTemplate goodsTemplate =
                this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId());
        if (goodsTemplate != null) {
            return ResponseValue.error("物品名称已存在");
        }
 
        int result = this.baseGoodsTemplateService.add(param, sysInfo);
        if (result > 0) {
            return ResponseValue.success(1);
        }
        return ResponseValue.error("新增失败!");
    }
 
    /**
     * @Description 物品模板列表查询
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    @GetMapping("/list")
    public ResponseValue queryList() {
        BaseGoodsTemplateParam param = CommonUtil.getObjFromReq(BaseGoodsTemplateParam.class);
        BaseGoodsTemplateParam param2 = new BaseGoodsTemplateParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
 
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        param.setAgencyId(Long.valueOf(sysInfo.getTenantId()));
        GenericPager<BaseGoodsTemplate> pager = this.baseGoodsTemplateService.queryList(param);
        return ResponseValue.success(pager);
    }
 
    /**
     * 根据物品id查询物品详情
     *
     * @author 卢庆阳
     * @date 2023/9/26
     */
    @GetMapping("/detail")
    public ResponseValue getById(Long id) {
        if (id == null) {
            return ResponseValue.error("物品id为空");
        }
        BaseGoodsTemplateVo vo = this.baseGoodsTemplateService.getById(id);
        if (vo == null) {
            return ResponseValue.error("查询失败!");
        }
        return ResponseValue.success("查询成功!", vo);
    }
 
    /**
     * @Description 编辑
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    @PostMapping("/edit")
    public ResponseValue edit() {
        BaseGoodsTemplateParam param = CommonUtil.getObjFromReqBody(BaseGoodsTemplateParam.class);
        BaseGoodsTemplateParam param2 = new BaseGoodsTemplateParam();
        CommonUtil.copyProperties(param, param2);
        param = param2;
 
        if (StringUtils.isEmpty(param.getGoodsName())) {
            return ResponseValue.error("物品名称为空");
        }
        List<BaseGoodsModels> models = param.getModels();
        if (CollectionUtils.isEmpty(models)) {
            return ResponseValue.error("物品型号为空");
        }
 
        int num = this.baseGoodsTemplateService.updateBaseGoodsTemplate(param);
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("编辑失败!");
    }
 
    /**
     * 修改状态
     *
     * @author 卢庆阳
     * @date 2023/10/25
     * <p>
     * 物品的禁用或删除,不影响已经采购入过库的物品信息。
     * <p>
     */
    @PostMapping("/updStatus")
    public ResponseValue updateStatus() {
        BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class);
        BaseGoodsTemplate param2 = new BaseGoodsTemplate();
        CommonUtil.copyProperties(goodsTemplate, param2);
        goodsTemplate = param2;
 
        if (goodsTemplate.getId() == null || goodsTemplate.getStates() == null) {
            return ResponseValue.error("参数错误");
        }
 
        int num = this.baseGoodsTemplateService.updateStatus(goodsTemplate);
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("修改失败!");
    }
 
    /**
     * @Description 根据物品id删除物品
     * @Author 卢庆阳
     * @Date 2023/10/25
     * <p>
     * 物品的禁用或删除,不影响已经采购入过库的物品信息。
     * <p/>
     */
    @DeleteMapping("/del")
    public ResponseValue updateById() {
        BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class);
        BaseGoodsTemplate param2 = new BaseGoodsTemplate();
        CommonUtil.copyProperties(goodsTemplate, param2);
        goodsTemplate = param2;
 
        if (goodsTemplate.getId() == null) {
            return ResponseValue.error("物品id为空");
        }
        int num = this.baseGoodsTemplateService.updateById(goodsTemplate, this.getCurrentUser());
 
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("删除失败!");
    }
 
    /**
     * @Description 查询仓库类型(数据字典)
     * @Author 卢庆阳
     * @Date 2023/10/30
     */
    @GetMapping("/select/classificationCode")
    public ResponseValue queryClassificationCode() {
        List<SDictData> list = this.baseGoodsTemplateService.queryClassificationCode();
        return ResponseValue.success(list);
    }
 
    /**
     * @Description 根据分类id查询物品模板
     * @Author 卢庆阳
     * @Date 2023/10/30
     */
    @GetMapping("/selectByCategoryId")
    public ResponseValue queryByCategoryId(Long categoryId) {
        List<BaseGoodsTemplate> list = this.baseGoodsTemplateService.queryByCategoryId(categoryId);
        return ResponseValue.success(list);
    }
 
    @GetMapping("/query/goodsTemplate")
    public ResponseValue queryGoodsTemplateByCategoryId(Long agencyId, Long categoryId) {
        // 不限制机构
        List<BaseGoodsTemplate> list = baseGoodsTemplateService.queryGoodsTemplateByCategoryId(null, categoryId,
                StatesType.NORMAL.getValue(), null);
        if (list == null) {
            return ResponseValue.error("查询失败!");
        }
        return ResponseValue.success("查询成功!", list);
    }
 
    @ApiOperation(value = "调拨查询机构下所有仓库下的分类模板信息", notes = "调拨查询机构下所有仓库下的分类模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agencyId", value = "父级机构id", required = true, dataType = "java.lang.Long",
                    paramType = "query"),
            @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, dataType = "Long",
                    paramType = "query")})
    @GetMapping("/query/warehouse/goods")
    public ResponseValue queryWarehouseGoods(Long agencyId, Long categoryId) {
 
        // 调拨查询所有仓库物品模板信息
        Map<String, Object> map = new HashMap<>();
        map.put("categoryId", categoryId);
 
        List<BaseWarehouse> baseWarehouseList =
                baseWarehouseService.getBaseWareHouseList(agencyId, StatesType.NORMAL.getValue());
        if (CollectionUtils.isEmpty(baseWarehouseList)) {
            return ResponseValue.error("机构无仓库!");
 
        }
        List<Long> warehouseIdList = baseWarehouseList.stream().map(BaseWarehouse::getId).collect(Collectors.toList());
        map.put("warehouseIdList", warehouseIdList);
 
        String sql =
                "SELECT DISTINCT bgt.id,bgt.GOODS_NAME, CLASSIFICATION type FROM l_wh_goods g LEFT JOIN base_goods_template bgt ON g.BASE_GOODS_TEMPLATE_ID = bgt.id "
                        + "WHERE WAREHOUSE_TYPE = 0 " + "AND WAREHOUSE_ID in (:warehouseIdList) "
                        + "AND CATEGORY_ID = :categoryId "
                        // 1:集采,2:自采
                        + "AND BUY_TYPE =1";
 
        List<GoodsTemplateVO> resultList = Lists.newArrayList();
        List<Map<String, Object>> goodsTemplateList = lWhGoodsService.select(sql, map, new MapperUtil());
        goodsTemplateList.forEach(item -> {
            GoodsTemplateVO goodsTemplateVO = MapUtils.convertMapToObj(item, GoodsTemplateVO.class);
            resultList.add(goodsTemplateVO);
        });
        return ResponseValue.success("查询成功!", resultList);
    }
 
    /**
     * 导入物品数据
     * @param file
     * @return
     */
    @PostMapping("/import")
    public ResponseValue importData(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(), ImportGoodsInfoTemplate.class,
                    new AnalysisEventListener<ImportGoodsInfoTemplate>() {
                        /**
                         * 主数据
                         */
                        final List<BaseGoodsTemplateParam> list = Lists.newArrayList();
 
                        /**
                         * 模块数据
                         */
                        final List<BaseGoodsModels> baseGoodsModelsArrayList = Lists.newArrayList();
 
                        /**
                         * 读取的行号
                         */
                        Integer rowIndex = 0;
 
                        /**
                         * 表头信息
                         * @param headMap
                         * @param context
                         */
                        @Override
                        public void invokeHeadMap(Map headMap, AnalysisContext context) {
                            // 验证表头数量
                            logger.info("解析分发单的表头长度: {}", headMap.size());
                            if (headMap.size() != 7) {
                                throw new ExcelAnalysisException("上传的文件不符!");
                            }
                        }
 
                        /**
                         * 数据处理
                         * @param data
                         * @param analysisContext
                         */
                        @Override
                        public void invoke(ImportGoodsInfoTemplate data, AnalysisContext analysisContext) {
                            rowIndex++;
                            String categoryOne = data.getCategoryOne();
                            String categoryTwo = data.getCategoryTwo();
                            String categoryThree = data.getCategoryThree();
                            if (StrUtil.isEmpty(categoryThree)) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,品类名称不能为空:" + categoryThree);
                            }
                            String goodsName = data.getGoodsName();
                            if (StrUtil.isEmpty(goodsName)) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,品名不能为空:" + goodsName);
                            }
                            String goodModelName = data.getGoodModelName();
                            if (StrUtil.isEmpty(goodModelName)) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,规格型号不能为空:" + goodModelName);
                            }
                            String unit = data.getUnit();
                            if (StrUtil.isEmpty(unit)) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,单位不能为空:" + unit);
                            }
                            //String type = data.getType();
                            String agencyName = data.getAgencyName();
                            if (StrUtil.isEmpty(agencyName)) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,创建机构不能为空:" + agencyName);
                            }
                            BaseCategory baseCategory = baseCategoryService.getByCategoryByName(categoryThree);
                            if (baseCategory == null) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,未查询到此品类名称:" + categoryThree);
                            }
                            Long categoryId = baseCategory.getId();
                            // 判断同一分类下的物品名称是否重复
                            BaseGoodsTemplate goodsTemplate = baseGoodsTemplateService.getByGoodsNameAndCategoryId(goodsName, categoryId);
                            if (goodsTemplate != null) {
                                // 查看此批次和数量
                                BaseGoodsModels baseGoodsModelsAdd = new BaseGoodsModels();
                                baseGoodsModelsAdd.setGoodsTemplatesId(goodsTemplate.getId());
                                baseGoodsModelsAdd.setStates(1);
                                baseGoodsModelsAdd.setModelName(goodModelName);
                                baseGoodsModelsAdd.setUnit(unit);
                                // 判断同一物品模板id下的 规格型号名称是否重复
                                BaseGoodsModels baseGoodsModels = baseGoodsModelsService.getByModelNameAndGoodsTemplatesId(baseGoodsModelsAdd);
                                if (baseGoodsModels == null) {
                                    baseGoodsModelsArrayList.add(baseGoodsModelsAdd);
                                    return;
                                } else {
                                    throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "物品名称:" + goodsName + "已存在,规格:" + goodModelName + ",单位:" + unit);
                                }
                            }
                            Optional<BaseGoodsTemplateParam> optional =
                                    list.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst();
                            Optional<BaseGoodsTemplateParam> optional2 =
                                    list.stream().filter(item -> item.getGoodsName().equals(goodsName)).findFirst();
                            if (optional.isPresent() && optional2.isPresent()) {
                                BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
                                baseGoodsModels.setModelName(goodModelName);
                                baseGoodsModels.setUnit(unit);
                                optional.get().getModels().add(baseGoodsModels);
                            } else {
                                BaseGoodsTemplateParam baseGoodsTemplate = new BaseGoodsTemplateParam();
                                baseGoodsTemplate.setCategoryId(categoryId);
                                baseGoodsTemplate.setGoodsName(goodsName);
                                baseGoodsTemplate.setStates(1);
                                FinSysTenant finSysTenant = finSysTenantService.selectByName(agencyName);
                                if (finSysTenant == null) {
                                    throw new ExcelAnalysisException("第" + rowIndex + "条数据,未查询到此机构:" + agencyName);
                                }
                                baseGoodsTemplate.setAgencyId(finSysTenant.getId());
                                baseGoodsTemplate.setAgencyName(agencyName);
                                List<BaseGoodsModels> models = new ArrayList<>();
                                BaseGoodsModels baseGoodsModels = new BaseGoodsModels();
                                baseGoodsModels.setModelName(goodModelName);
                                baseGoodsModels.setUnit(unit);
                                models.add(baseGoodsModels);
                                baseGoodsTemplate.setModels(models);
                                list.add(baseGoodsTemplate);
                            }
                        }
 
                        /**
                         * 处理后导入数据
                         * @param analysisContext
                         */
                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            // 主数据存储
                            for (BaseGoodsTemplateParam baseGoodsTemplate : list) {
                                baseGoodsTemplateService.add(baseGoodsTemplate, null);
                            }
                            // 模块数据存储
                            for (BaseGoodsModels baseGoodsModels : baseGoodsModelsArrayList) {
                                baseGoodsModelsService.add(baseGoodsModels);
                            }
                        }
 
                        @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("导入成功!");
    }
}