| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemSaveReqVO; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemTypeDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.md.item.MesMdItemMapper; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeListReqVO; |
| | | import cn.iocoder.yudao.module.mes.enums.md.MesMdItemTypeEnum; |
| | | |
| | | import cn.iocoder.yudao.module.mes.enums.wm.BarcodeBizTypeEnum; |
| | |
| | | return item; |
| | | } |
| | | |
| | | } |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesMdItemImportRespVO importItemList(List<MesMdItemImportExcelVO> importList, Boolean updateSupport) { |
| | | MesMdItemImportRespVO respVO = MesMdItemImportRespVO.empty(); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | return respVO; |
| | | } |
| | | // 预加载计量单位、物料分类,按名称建索引,避免逐行查库 |
| | | Map<String, Long> unitMeasureMap = new HashMap<>(); |
| | | unitMeasureService.getUnitMeasureListByStatus(CommonStatusEnum.ENABLE.getStatus()) |
| | | .forEach(unit -> unitMeasureMap.put(unit.getName().trim(), unit.getId())); |
| | | Map<String, Long> itemTypeMap = new HashMap<>(); |
| | | itemTypeService.getItemTypeList(new MesMdItemTypeListReqVO()) |
| | | .forEach(type -> itemTypeMap.put(type.getName().trim(), type.getId())); |
| | | |
| | | int index = 0; |
| | | for (MesMdItemImportExcelVO row : importList) { |
| | | index++; |
| | | String key = StrUtil.isNotBlank(row.getCode()) ? row.getCode().trim() : "第 " + index + " 行"; |
| | | // 1. 基础校验 |
| | | if (StrUtil.isBlank(row.getCode())) { |
| | | respVO.getFailureCodes().put(key, "物料编码不能为空"); |
| | | continue; |
| | | } |
| | | if (StrUtil.isBlank(row.getName())) { |
| | | respVO.getFailureCodes().put(key, "物料名称不能为空"); |
| | | continue; |
| | | } |
| | | String code = row.getCode().trim(); |
| | | String unitName = StrUtil.trim(row.getUnitMeasureName()); |
| | | Long unitMeasureId = unitName == null ? null : unitMeasureMap.get(unitName); |
| | | if (unitMeasureId == null) { |
| | | respVO.getFailureCodes().put(key, "计量单位[" + unitName + "]不存在"); |
| | | continue; |
| | | } |
| | | String typeName = StrUtil.trim(row.getItemTypeName()); |
| | | Long itemTypeId = typeName == null ? null : itemTypeMap.get(typeName); |
| | | if (itemTypeId == null) { |
| | | respVO.getFailureCodes().put(key, "物料分类[" + typeName + "]不存在"); |
| | | continue; |
| | | } |
| | | |
| | | // 2. 组装保存对象(颜色一并写入) |
| | | MesMdItemSaveReqVO saveReqVO = new MesMdItemSaveReqVO(); |
| | | saveReqVO.setCode(code); |
| | | saveReqVO.setName(row.getName().trim()); |
| | | saveReqVO.setSpecification(StrUtil.trim(row.getSpecification())); |
| | | saveReqVO.setColor(StrUtil.trim(row.getColor())); |
| | | saveReqVO.setUnitMeasureId(unitMeasureId); |
| | | saveReqVO.setItemTypeId(itemTypeId); |
| | | saveReqVO.setSafeStockFlag(row.getSafeStockFlag()); |
| | | saveReqVO.setMinStock(row.getMinStock()); |
| | | saveReqVO.setMaxStock(row.getMaxStock()); |
| | | saveReqVO.setHighValue(row.getHighValue()); |
| | | saveReqVO.setBatchFlag(row.getBatchFlag()); |
| | | saveReqVO.setRemark(StrUtil.trim(row.getRemark())); |
| | | |
| | | // 3. 新增或更新,单行失败不影响整批 |
| | | MesMdItemDO existItem = itemMapper.selectByCode(code); |
| | | try { |
| | | if (existItem == null) { |
| | | createItem(saveReqVO); |
| | | respVO.getCreateCodes().add(code); |
| | | } else if (!Boolean.TRUE.equals(updateSupport)) { |
| | | respVO.getFailureCodes().put(key, "物料编码已存在(勾选「是否覆盖」可更新)"); |
| | | } else { |
| | | saveReqVO.setId(existItem.getId()); |
| | | saveReqVO.setMdmItemId(existItem.getMdmItemId()); |
| | | updateItem(saveReqVO); |
| | | respVO.getUpdateCodes().add(code); |
| | | } |
| | | } catch (Exception e) { |
| | | respVO.getFailureCodes().put(key, e.getMessage() == null ? "导入失败" : e.getMessage()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | } |