黎星凯
2024-04-30 3be9cfcb6b59366b936821966d20a998a6895f9f
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
package com.consum.base.controller;
 
import java.io.IOException;
import java.lang.reflect.Field;
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 org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
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.LWhFormProcureGoodsInfoParam;
import com.consum.base.pojo.LWhFormProcureParam;
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.BaseCategoryService;
import com.consum.base.service.LWhFormProcureGoodsService;
import com.consum.base.service.LWhFormProcureService;
import com.consum.base.service.LWhProcureModelService;
import com.consum.base.service.core.LWhFormProcureCoreService;
import com.consum.model.po.BaseCategory;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.LWhFormProcure;
import com.consum.model.po.LWhFormProcureGoods;
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;
 
    /**
     * @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);
 
    }
 
    @ApiOperation(value = "采购单导入", notes = "采购单导入")
    @PostMapping("/import")
    public ResponseValue upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if (!".xls".endsWith(originalFilename)) {
            return ResponseValue.error("文件格式有误!");
        }
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("当前登录用户为空");
        }
        EasyExcelFactory.read(file.getInputStream(), ImportProcureOrderTemplate.class,
            new AnalysisEventListener<ImportProcureOrderTemplate>() {
                List<LWhFormProcureParam> list = Lists.newArrayList();
 
                @Override
                public void invoke(ImportProcureOrderTemplate data, AnalysisContext analysisContext) {
                    String categoryOne = data.getCategoryOne();
                    String categoryTwo = data.getCategoryTwo();
                    String categoryThree = data.getCategoryThree();
                    String goodsName = data.getGoodsName();
                    String goodModelName = data.getGoodModelName();
                    String unit = data.getUnit();
                    String type = data.getType();
                    String agencyName = data.getAgencyName();
                    String warehouseName = data.getWarehouseName();
                    String supplierName = data.getSupplierName();
                    String price = data.getPrice();
                    String num = data.getNum();
 
                    BaseCategory baseCategory = baseCategoryService.getByCategoryByName(categoryThree);
                    Long categoryId = baseCategory.getId();
 
                    // Optional<BaseGoodsTemplateParam> optional =
                    // list.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst();
                    //
                    // if (optional.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);
                    // 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);
                    // }
                }
 
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    for (LWhFormProcureParam procureParam : list) {
                        // lWhFormProcureService.add(procureParam, null);
                    }
                }
 
                @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 RuntimeException("第" + row + "行,第" + column + "列解析异常,请正确填写");
                    } else {
                        throw new RuntimeException(exception.getMessage());
                    }
                }
            }).sheet(0).doRead();
 
        return ResponseValue.success("导入成功!");
    }
 
}