黎星凯
2024-04-30 3be9cfcb6b59366b936821966d20a998a6895f9f
consum-base/src/main/java/com/consum/base/controller/LWhFormProcureController.java
@@ -1,5 +1,6 @@
package com.consum.base.controller;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
@@ -18,13 +19,19 @@
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;
@@ -32,10 +39,12 @@
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.impl.LWhFormProcureCoreService;
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;
@@ -72,6 +81,8 @@
    private LWhFormProcureCoreService lWhFormProcureCoreService;
    @Resource
    private LWhProcureModelService lWhProcureModelService;
    @Resource
    private BaseCategoryService baseCategoryService;
    /**
     * @Description 新增
@@ -140,8 +151,6 @@
        }
        try {
            Field fieldDatas = GenericPager.class.getDeclaredField("datas");
            // fieldDatas.setAccessible(true);
            // fieldDatas.set(genericPager, result);
            ReflectUtil.setFieldValue(genericPager, fieldDatas, result);
        } catch (Exception e) {
            e.printStackTrace();
@@ -207,7 +216,7 @@
     * 根据id查询详情
     */
    @GetMapping("/detail")
    public ResponseValue getById(Long id) throws IllegalAccessException {
    public ResponseValue getById(Long id) throws Exception {
        if (id == null) {
            return ResponseValue.error("采购单id为空");
        }
@@ -276,11 +285,11 @@
        CommonUtil.copyProperties(formProcureQry, param2);
        formProcureQry = param2;
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser == null) {
        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);
    }
@@ -289,7 +298,7 @@
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "query")})
    @GetMapping("/list/export")
    public void export(Long id, HttpServletResponse response) throws Exception {
    public ResponseValue<String> export(Long id, HttpServletResponse response) throws Exception {
        TemplateExportParams params = new TemplateExportParams("import/采购入库单.xls");
        params.setHeadingStartRow(2);
        FinSysTenantUser sysInfo = this.getSysInfo();
@@ -320,7 +329,93 @@
        map.put("totalAmount", totalAmount);
        Workbook workbook = ExcelExportUtil.exportExcel(params, ProcureExcelTemplate.class, exportList, map);
        downLoadExcel("采购入库单", response, workbook);
        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("导入成功!");
    }
}