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 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 data = genericPager.getDatas(); ArrayList result = new ArrayList<>(); if (!CollectionUtils.isEmpty(data)) { data.forEach(item -> { FormProcureVO fromProcureVO = new FormProcureVO(); BeanUtils.copyProperties(item, fromProcureVO); // 查询型号数量 List 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 formProcureGoods = lWhFormProcureGoodsServiceImpl.select(lWhFormProcureGoods); List goodsTemplateInfoList = new ArrayList<>(); for (LWhFormProcureGoods formProcureGood : formProcureGoods) { GoodsTemplateInfoVO goodsTemplateInfoVO = new GoodsTemplateInfoVO(); BeanUtils.copyProperties(formProcureGood, goodsTemplateInfoVO); List 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 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 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 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 first = exportList.stream().findFirst(); ProcureExcelTemplate templateExcelExport = first.get(); String businessFormCode = templateExcelExport.getBusinessFormCode(); Long createTime = templateExcelExport.getCreateTime(); String operatorName = templateExcelExport.getOperatorName(); Map 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() { List 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 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 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("导入成功!"); } }