diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/ItemAccountsController.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/ItemAccountsController.java index 82b5b8f..af59e60 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/ItemAccountsController.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/ItemAccountsController.java @@ -159,9 +159,12 @@ public class ItemAccountsController { @GetMapping("/get-import-template") @Operation(summary = "获得导入备件台账模板") public void importTemplate(HttpServletResponse response) throws IOException { - List list = Arrays.asList(); + List list = Arrays.asList(); + Map mapDropDown = new HashMap<>(); + String[] strings = DictFrameworkUtils.dictTypeDictDataValue(DictTypeConstants.ITEM_ACCOUNT_LOCATION_TYPE); + mapDropDown.put(3, strings); // 输出 - ExcelUtils.write(response, "备件台账基本信息导入模板.xls", "备件台账基本信息列表", ItemAccountsExcelVO.class, list); + ExcelUtils.write(response, "备件台账基本信息导入模板.xls", "备件台账基本信息列表", ItemAccountsImportExcelVO.class, list,mapDropDown); } @PostMapping("/import") @@ -177,8 +180,8 @@ public class ItemAccountsController { @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart", required = false, defaultValue = "false") Boolean updatePart) throws Exception { - List list = ExcelUtils.read(file, ItemAccountsExcelVO.class); - List errorList = itemAccountsService.importItemAccountsList(list, mode, updatePart); + List list = ExcelUtils.read(file, ItemAccountsImportExcelVO.class); + List errorList = itemAccountsService.importItemAccountsList(list, mode, updatePart); Map returnMap = new HashMap<>(); returnMap.put("errorCount", errorList.size()); diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsErrorVO.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsErrorVO.java new file mode 100644 index 0000000..91a17ec --- /dev/null +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsErrorVO.java @@ -0,0 +1,17 @@ +package com.win.module.eam.controller.itemaccounts.vo; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class ItemAccountsErrorVO extends ItemAccountsImportExcelVO { + @ExcelProperty(value = "导入状态", index = 0) + private String importStatus; + + @ExcelProperty(value = "导入说明", index = 1) + private String importRemark; +} diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsExcelVO.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsExcelVO.java index 7fb8b1d..4ae6505 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsExcelVO.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsExcelVO.java @@ -2,6 +2,8 @@ package com.win.module.eam.controller.itemaccounts.vo; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; +import com.win.framework.excel.core.annotations.DictFormat; +import com.win.framework.excel.core.convert.DictConvert; import lombok.Data; import java.math.BigDecimal; @@ -18,10 +20,10 @@ public class ItemAccountsExcelVO { @ExcelProperty("备件编号") private String itemNumber; - @ExcelProperty("备件编号") + @ExcelProperty("备件名称") private String itemName; - @ExcelProperty("库区名称") + @ExcelProperty("库区编号") private String areaNumber; @ExcelProperty("库区名称") @@ -35,8 +37,8 @@ public class ItemAccountsExcelVO { @ExcelProperty("库存数量") private BigDecimal qty; - - @ExcelProperty("库存类型") + @DictFormat("item_account_location_type") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中 + @ExcelProperty(value = "库存类型", converter = DictConvert.class) private String type; @ExcelProperty("创建时间") diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsImportExcelVO.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsImportExcelVO.java new file mode 100644 index 0000000..4415f6d --- /dev/null +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/controller/itemaccounts/vo/ItemAccountsImportExcelVO.java @@ -0,0 +1,42 @@ +package com.win.module.eam.controller.itemaccounts.vo; + +import com.alibaba.excel.annotation.ExcelIgnore; +import com.alibaba.excel.annotation.ExcelProperty; +import com.win.framework.excel.core.annotations.DictFormat; +import com.win.framework.excel.core.annotations.ExcelValid; +import com.win.framework.excel.core.convert.DictConvert; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +/** + * 备件台账 导出模版和导入数据 + * + * @author 超级管理员 + */ +@AllArgsConstructor +@NoArgsConstructor +@Accessors(chain = false) // 设置 chain = false,避免用户导入有问题 +@Data +public class ItemAccountsImportExcelVO { + + @ExcelProperty("备件编号") + private String itemNumber; + + @ExcelProperty("库位编号") + private String locationNumber; + + @ExcelProperty("库存数量") + private BigDecimal qty; + + @ExcelProperty(value = "库存类型", converter = DictConvert.class) + @DictFormat("item_account_location_type") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中 + private String type; + + + +} diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/convert/itemaccounts/ItemAccountsConvert.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/convert/itemaccounts/ItemAccountsConvert.java index 4bf7858..916a25c 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/convert/itemaccounts/ItemAccountsConvert.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/convert/itemaccounts/ItemAccountsConvert.java @@ -4,10 +4,7 @@ import java.util.*; import com.win.framework.common.pojo.PageResult; -import com.win.module.eam.controller.itemaccounts.vo.ItemAccountsCreateReqVO; -import com.win.module.eam.controller.itemaccounts.vo.ItemAccountsExcelVO; -import com.win.module.eam.controller.itemaccounts.vo.ItemAccountsRespVO; -import com.win.module.eam.controller.itemaccounts.vo.ItemAccountsUpdateReqVO; +import com.win.module.eam.controller.itemaccounts.vo.*; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; import com.win.module.eam.dal.dataobject.itemaccounts.ItemAccountsDO; @@ -23,6 +20,8 @@ public interface ItemAccountsConvert { ItemAccountsConvert INSTANCE = Mappers.getMapper(ItemAccountsConvert.class); ItemAccountsDO convert(ItemAccountsCreateReqVO bean); + ItemAccountsErrorVO convert(ItemAccountsImportExcelVO bean); + ItemAccountsDO convert1(ItemAccountsImportExcelVO bean); ItemAccountsDO convert(ItemAccountsUpdateReqVO bean); diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/dal/mysql/location/LocationMapper.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/dal/mysql/location/LocationMapper.java index 3273926..3d9bd2b 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/dal/mysql/location/LocationMapper.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/dal/mysql/location/LocationMapper.java @@ -52,6 +52,17 @@ public interface LocationMapper extends BaseMapperX { .orderByDesc(LocationDO::getId)); } + /** + * 根据库位编码查询信息 + * @param number + * @return + */ + default LocationDO getByNumber(@Param("number") String number){ + return selectOne(new LambdaQueryWrapperX() + .eqIfPresent(LocationDO::getNumber, number) + .eqIfPresent(LocationDO::getAvailable, "TRUE")); + }; + /** * 扫库位码获取库位信息以及关联备件信息 * @param number diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsService.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsService.java index 0dc8587..2e986d6 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsService.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsService.java @@ -108,7 +108,7 @@ public interface ItemAccountsService { * @param updatePart 是否支持更新 * @return 导入结果 */ - public List importItemAccountsList(List datas, Integer mode, boolean updatePart); + public List importItemAccountsList(List datas, Integer mode, boolean updatePart); /** * 获得备件台账分页 diff --git a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsServiceImpl.java b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsServiceImpl.java index 3a604c9..ce62751 100644 --- a/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsServiceImpl.java +++ b/win-module-eam/win-module-eam-biz/src/main/java/com/win/module/eam/service/itemaccounts/ItemAccountsServiceImpl.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.win.framework.common.exception.util.ServiceExceptionUtil; import com.win.framework.common.pojo.CustomConditions; import com.win.framework.common.pojo.PageResult; +import com.win.module.eam.controller.item.vo.ItemBaseVO; import com.win.module.eam.controller.itemaccounts.vo.*; import com.win.module.eam.controller.transaction.vo.TransactionCreateReqVO; import com.win.module.eam.convert.itemaccounts.ItemAccountsConvert; @@ -28,6 +29,7 @@ import com.win.module.eam.enums.transaction.TransactionEnum; import com.win.module.eam.service.transaction.TransactionService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; @@ -41,7 +43,6 @@ import java.util.Map; import static com.win.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.win.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static com.win.module.eam.enums.ErrorCodeConstants.*; -import static com.win.module.eam.enums.ErrorCodeConstants.CLASSTYPR_NO_NOMAL; /** * 备件台账 Service 实现类 @@ -340,27 +341,86 @@ public class ItemAccountsServiceImpl implements ItemAccountsService { } @Override - public List importItemAccountsList(List datas, Integer mode, boolean updatePart) { + @Transactional + public List importItemAccountsList(List datas, Integer mode, boolean updatePart) { + //备件编号+库位编码+类型唯一 if (CollUtil.isEmpty(datas)) { - //throw exception(ITEM_ACCOUNTS_IMPORT_LIST_IS_EMPTY); + throw exception(ITEM_NEW_TURN_IN_IMPORT_LIST_IS_EMPTY);//导入数据不能为空 } - List errorList = new ArrayList<>(); -// datas.forEach(item -> { -// if(errorList == null){ -// // 判断如果不存在,在进行插入 -// ItemAccountsDO obj = itemAccountsMapper.selectByCode(item.getCode()); -// if (obj == null&& mode != 3) { -// itemAccountsMapper.insert(ItemAccountsConvert.INSTANCE.convert(item)); -// } -// else if (obj != null && mode != 2) {// 如果存在,判断是否允许更新 -// ItemAccountsDO itemAccountsDO = ItemAccountsConvert.INSTANCE.convert(item); -// itemAccountsDO.setId(obj.getId()); -// itemAccountsMapper.updateById(obj); -// } -// } -// }); + List errorList = new ArrayList<>(); + datas.forEach(item -> { + //判断该备件是否在备件基础数据中无则报错 + validate(item.getItemNumber(),item.getLocationNumber());//校验库位是否被使用 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("number",item.getItemNumber()); + queryWrapper.eq("available","TRUE"); + List itemDOS = itemMapper.selectList(queryWrapper); + if(itemDOS.isEmpty()){ + ItemAccountsErrorVO convert = ItemAccountsConvert.INSTANCE.convert(item); + convert.setImportStatus("失败"); + convert.setImportRemark("该备件在基础数据中不存在"); + errorList.add(convert); + return; + } + //判断该库位是否在备件基础数据中无则报错 + LocationDO locationDO = locationMapper.getByNumber(item.getLocationNumber()); + if(locationDO==null){ + ItemAccountsErrorVO convert = ItemAccountsConvert.INSTANCE.convert(item); + convert.setImportStatus("失败"); + convert.setImportRemark("该库位在基础数据中不存在"); + errorList.add(convert); + return; + } + try { + validate(item.getItemNumber(),item.getLocationNumber());//校验库位是否被使用 + } catch (Exception e) { + ItemAccountsErrorVO convert = ItemAccountsConvert.INSTANCE.convert(item); + convert.setImportStatus("失败"); + convert.setImportRemark("该库位已存在其他类型数据"); + errorList.add(convert); + return; + } + try { + validate(item.getItemNumber(),item.getLocationNumber());//校验库位是否被使用 + } catch (Exception e) { + ItemAccountsErrorVO convert = ItemAccountsConvert.INSTANCE.convert(item); + convert.setImportStatus("失败"); + convert.setImportRemark("该库位已存在其他类型数据"); + errorList.add(convert); + return; + } + try { + if(item.getItemNumber()==null|| item.getItemNumber().trim().isEmpty() || item.getLocationNumber()==null|| item.getLocationNumber().trim().isEmpty() || item.getType()==null|| item.getType().trim().isEmpty()){//备件号为空 + ItemAccountsErrorVO convert = ItemAccountsConvert.INSTANCE.convert(item); + convert.setImportStatus("失败"); + convert.setImportRemark("数据为空"); + errorList.add(convert); + } + ItemAccountsDO itemAccountsDO = validateItemAndTypeAndLocation(item.getItemNumber(), item.getType(), item.getLocationNumber());//查询重复数据 + if(mode != 2){// 如果存在,判断是否允许更新 + ItemAccountsDO itemAccountsDO1 = ItemAccountsConvert.INSTANCE.convert1(item); + itemAccountsDO1.setId(itemAccountsDO.getId()); + itemAccountsMapper.updateById(itemAccountsDO1); + } + if(itemAccountsDO==null&&mode != 3){//数据库中不存在值且不是覆盖走新增 + //根据库位查询库区 + ItemAccountsDO itemAccountsDO1 = ItemAccountsConvert.INSTANCE.convert1(item); + itemAccountsDO1.setAreaNumber(locationDO.getAreaNumber()); + itemAccountsMapper.insert(itemAccountsDO1); + } + } catch (Exception e) { + ItemAccountsErrorVO convert = ItemAccountsConvert.INSTANCE.convert(item); + convert.setImportStatus("失败"); + convert.setImportRemark("数据错误"); + errorList.add(convert); + } + }); + //错误不为空并非部分更新,手工回滚 + if(!errorList.isEmpty() && !updatePart) { + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + } return errorList; }