| | |
| | | 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.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.GoodsTemplateCountVO; |
| | | import com.consum.base.pojo.response.GoodsTemplateInfoVO; |
| | | import com.consum.base.pojo.response.LWhFormProcureExtendVO; |
| | | 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.model.po.FinSysTenantUser; |
| | | import com.consum.model.po.LWhFormProcure; |
| | | import com.consum.model.po.LWhFormProcureGoods; |
| | | 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; |
| | |
| | | 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 新增 |
| | |
| | | */ |
| | | @ApiOperation(value = "采购单列表查询", notes = "采购单列表查询") |
| | | @ApiImplicitParams({@ApiImplicitParam(name = "param", value = "采购查询条件", required = true, |
| | | dataType = "FormProcureQryDto", paramType = "query")}) |
| | | dataType = "FormProcureQryDto", paramType = "query")}) |
| | | @GetMapping("/list") |
| | | public ResponseValue queryFormProcureList() { |
| | | FormProcureQry param = CommonUtil.getObjFromReq(FormProcureQry.class); |
| | |
| | | |
| | | // 查询型号数量 |
| | | List<GoodsTemplateCountVO> procureCount = |
| | | lWhProcureModelService.getProcureCountByBusinessId(item.getId()); |
| | | lWhProcureModelService.getProcureCountByBusinessId(item.getId()); |
| | | fromProcureVO.setFromProcureTemplateInfoList(procureCount); |
| | | |
| | | result.add(fromProcureVO); |
| | |
| | | * 根据id查询详情 |
| | | */ |
| | | @GetMapping("/detail") |
| | | public ResponseValue getById(Long id) throws IllegalAccessException { |
| | | public ResponseValue getById(Long id) throws Exception { |
| | | if (id == null) { |
| | | return ResponseValue.error("采购单id为空"); |
| | | } |
| | |
| | | BeanUtils.copyProperties(formProcureGood, goodsTemplateInfoVO); |
| | | |
| | | List<GoodModelInfoDTO> goodsModelInfoList = |
| | | lWhProcureModelService.getGoodsModelListByBusinessId(null, formProcureGood.getId()); |
| | | lWhProcureModelService.getGoodsModelListByBusinessId(null, formProcureGood.getId()); |
| | | if (CollectionUtils.isEmpty(goodsModelInfoList)) { |
| | | continue; |
| | | } |
| | |
| | | goodsModelVO.setUnit(goodModelInfo.getUnit()); |
| | | goodsModelVO.setCounts(goodModelInfo.getCounts()); |
| | | goodsModelVO.setTotalAmount( |
| | | goodModelInfo.getTotalAmount() != null ? goodModelInfo.getTotalAmount().doubleValue() : null); |
| | | goodModelInfo.getTotalAmount() != null ? goodModelInfo.getTotalAmount().doubleValue() : null); |
| | | goodsModelVO.setWorehouseCount(goodModelInfo.getWorehouseCount()); |
| | | goodsModelVO.setPrice(goodModelInfo.getPrice()); |
| | | goodsModelVO.setBaseGoodsModelsId(goodModelInfo.getBaseGoodsModelsId()); |
| | |
| | | |
| | | @ApiOperation(value = "采购单明细查询", notes = "采购单明细查询") |
| | | @ApiImplicitParams({@ApiImplicitParam(name = "param", value = "采购单明细查询", required = true, |
| | | dataType = "FormProcureQryDto", paramType = "query")}) |
| | | dataType = "FormProcureQryDto", paramType = "query")}) |
| | | @GetMapping("detail/list") |
| | | public ResponseValue queryFormProcureDetailList() { |
| | | FormProcureQry formProcureQry = CommonUtil.getObjFromReq(FormProcureQry.class); |
| | |
| | | if (sysInfo == null) { |
| | | return ResponseValue.error("登录用户信息不存在"); |
| | | } |
| | | formProcureQry.setAgencyId(Long.valueOf(sysInfo.getTenantId())); |
| | | // 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")}) |
| | | @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"); |
| | |
| | | } |
| | | |
| | | int countNum = |
| | | exportList.stream().filter(item -> item.getNum() != null).mapToInt(ProcureExcelTemplate::getNum).sum(); |
| | | exportList.stream().filter(item -> item.getNum() != null).mapToInt(ProcureExcelTemplate::getNum).sum(); |
| | | double totalAmount = exportList.stream().filter(export -> export.getTotalAmount() != null) |
| | | .mapToDouble(ProcureExcelTemplate::getAmount).sum(); |
| | | .mapToDouble(ProcureExcelTemplate::getAmount).sum(); |
| | | Optional<ProcureExcelTemplate> first = exportList.stream().findFirst(); |
| | | ProcureExcelTemplate templateExcelExport = first.get(); |
| | | String businessFormCode = templateExcelExport.getBusinessFormCode(); |
| | |
| | | map.put("totalAmount", totalAmount); |
| | | |
| | | Workbook workbook = ExcelExportUtil.exportExcel(params, ProcureExcelTemplate.class, exportList, map); |
| | | String filePath = downLoadExcel("采购入库单", response, workbook); |
| | | 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("导入成功!"); |
| | | } |
| | | |
| | | } |