| | |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.core.utils.DateUtil; |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.core.annotation.excel.ExcelImporter; |
| | | import com.doumee.dao.admin.request.DeviceImport; |
| | | import com.doumee.dao.business.*; |
| | | import com.doumee.dao.business.YwDeviceMapper; |
| | | import com.doumee.dao.business.YwDeviceRecordMapper; |
| | | import com.doumee.dao.business.join.MemberJoinMapper; |
| | | import com.doumee.dao.business.model.*; |
| | | import com.doumee.dao.business.vo.YwDeviceCateDataVO; |
| | | import com.doumee.dao.business.vo.YwDeviceDataVO; |
| | | import com.doumee.dao.business.vo.YwDeviceParentCateDataVO; |
| | | import com.doumee.dao.business.vo.YwDeviceStatusDataVO; |
| | | import com.doumee.dao.system.MultifileMapper; |
| | | import com.doumee.dao.system.SystemUserMapper; |
| | | import com.doumee.dao.system.model.Multifile; |
| | | import com.doumee.dao.system.model.SystemUser; |
| | | import com.doumee.service.business.YwDeviceRecordService; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | @Autowired |
| | | private RedisTemplate<String,Object> redisTemplate; |
| | | |
| | | @Autowired |
| | | private CategoryMapper categoryMapper; |
| | | @Autowired |
| | | private YwProjectMapper ywProjectMapper; |
| | | @Autowired |
| | | private YwBuildingMapper ywBuildingMapper; |
| | | @Autowired |
| | | private YwFloorMapper ywFloorMapper; |
| | | @Autowired |
| | | private YwRoomMapper ywRoomMapper; |
| | | @Autowired |
| | | private MemberJoinMapper memberJoinMapper; |
| | | @Autowired |
| | | private SystemUserMapper systemUserMapper; |
| | | |
| | | private static final int IMPORT_EXCEL_ROW_OFFSET = 3; |
| | | |
| | | @Override |
| | | public Integer create(YwDevice ywDevice) { |
| | |
| | | if(ywDeviceMapper.selectCount(new QueryWrapper<YwDevice>().lambda().eq(YwDevice::getIsdeleted,Constants.ZERO).eq(YwDevice::getCode,ywDevice.getCode()))>Constants.ZERO){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"设备编号重复!"); |
| | | } |
| | | ywDevice.setDeviceNum(normalizeDeviceNum(ywDevice.getDeviceNum())); |
| | | ywDevice.setCreateDate(new Date()); |
| | | ywDevice.setCreator(loginUserInfo.getId()); |
| | | ywDevice.setIsdeleted(Constants.ZERO); |
| | |
| | | .ne(YwDevice::getId,ywDevice.getId()))>Constants.ZERO){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"设备编号重复!"); |
| | | } |
| | | ywDevice.setDeviceNum(normalizeDeviceNum(ywDevice.getDeviceNum())); |
| | | ywDevice.setEditDate(new Date()); |
| | | ywDevice.setEditor(loginUserInfo.getId()); |
| | | ywDeviceMapper.updateById(ywDevice); |
| | |
| | | ywDeviceStatusDataVO.setAmount(ywDeviceList.size()); |
| | | ywDeviceStatusDataVO.setWorkAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.ZERO)).collect(Collectors.toList()).size()); |
| | | ywDeviceStatusDataVO.setExceptionAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.ONE)).collect(Collectors.toList()).size()); |
| | | ywDeviceStatusDataVO.setErrAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.THREE)).collect(Collectors.toList()).size()); |
| | | ywDeviceStatusDataVO.setErrAmount(ywDeviceList.stream().filter(i->Objects.nonNull(i.getStatus())&&Constants.equalsInteger(i.getStatus(),Constants.TWO)).collect(Collectors.toList()).size()); |
| | | return ywDeviceStatusDataVO; |
| | | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {BusinessException.class, Exception.class}) |
| | | public String importBatch(MultipartFile file, LoginUserInfo loginUserInfo) { |
| | | List<DeviceImport> dataList; |
| | | try { |
| | | ExcelImporter ie = new ExcelImporter(file, 1, 0); |
| | | dataList = ie.getDataList(DeviceImport.class, null); |
| | | } catch (Exception e) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,导入文件解析失败,请检查表格格式!"); |
| | | } |
| | | if (CollectionUtils.isEmpty(dataList)) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,录入数据为空!"); |
| | | } |
| | | DeviceImportCache cache = buildImportCache(); |
| | | List<YwDevice> insertList = new ArrayList<>(); |
| | | List<YwDevice> updateList = new ArrayList<>(); |
| | | Set<String> fileCodes = new HashSet<>(); |
| | | Date now = new Date(); |
| | | for (int i = 0; i < dataList.size(); i++) { |
| | | DeviceImport row = dataList.get(i); |
| | | if (isImportBlankRow(row)) { |
| | | continue; |
| | | } |
| | | YwDevice device = checkImportRow(row, i, cache, fileCodes, loginUserInfo, now); |
| | | if (device.getId() != null) { |
| | | updateList.add(device); |
| | | } else { |
| | | insertList.add(device); |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(insertList) && CollectionUtils.isEmpty(updateList)) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,录入有效数据为空!"); |
| | | } |
| | | if (!CollectionUtils.isEmpty(insertList)) { |
| | | ywDeviceMapper.insert(insertList); |
| | | } |
| | | for (YwDevice device : updateList) { |
| | | ywDeviceMapper.updateById(device); |
| | | } |
| | | return String.format("操作成功,本次导入 新增%d个设备,更新%d个设备", insertList.size(), updateList.size()); |
| | | } |
| | | |
| | | private boolean isImportBlankRow(DeviceImport row) { |
| | | return StringUtils.isBlank(row.getCode()) |
| | | && StringUtils.isBlank(row.getName()) |
| | | && StringUtils.isBlank(row.getDeviceNum()) |
| | | && StringUtils.isBlank(row.getProjectName()) |
| | | && StringUtils.isBlank(row.getRoomPath()); |
| | | } |
| | | |
| | | private DeviceImportCache buildImportCache() { |
| | | DeviceImportCache cache = new DeviceImportCache(); |
| | | List<Category> categories = categoryMapper.selectList(new QueryWrapper<Category>().lambda() |
| | | .eq(Category::getIsdeleted, Constants.ZERO) |
| | | .eq(Category::getType, Constants.FIVE)); |
| | | Map<Integer, Category> categoryById = categories.stream() |
| | | .collect(Collectors.toMap(Category::getId, c -> c, (a, b) -> a)); |
| | | for (Category category : categories) { |
| | | if (category.getParentId() == null || category.getParentId() <= 0) { |
| | | continue; |
| | | } |
| | | Category parent = categoryById.get(category.getParentId()); |
| | | if (parent == null || StringUtils.isBlank(parent.getName()) || StringUtils.isBlank(category.getName())) { |
| | | continue; |
| | | } |
| | | cache.categoryPathMap.put(parent.getName() + "/" + category.getName(), category.getId()); |
| | | } |
| | | List<YwProject> projects = ywProjectMapper.selectList(new QueryWrapper<YwProject>().lambda() |
| | | .eq(YwProject::getIsdeleted, Constants.ZERO)); |
| | | for (YwProject project : projects) { |
| | | if (StringUtils.isNotBlank(project.getName())) { |
| | | cache.projectNameMap.put(project.getName(), project.getId()); |
| | | } |
| | | } |
| | | List<YwBuilding> buildings = ywBuildingMapper.selectList(new QueryWrapper<YwBuilding>().lambda() |
| | | .eq(YwBuilding::getIsdeleted, Constants.ZERO)); |
| | | for (YwBuilding building : buildings) { |
| | | if (building.getProjectId() != null && StringUtils.isNotBlank(building.getName())) { |
| | | cache.buildingMap.put(building.getProjectId() + "|" + building.getName(), building.getId()); |
| | | } |
| | | } |
| | | List<YwFloor> floors = ywFloorMapper.selectList(new QueryWrapper<YwFloor>().lambda() |
| | | .eq(YwFloor::getIsdeleted, Constants.ZERO)); |
| | | for (YwFloor floor : floors) { |
| | | if (floor.getBuildingId() != null && StringUtils.isNotBlank(floor.getName())) { |
| | | cache.floorMap.put(floor.getBuildingId() + "|" + floor.getName(), floor.getId()); |
| | | } |
| | | } |
| | | List<YwRoom> rooms = ywRoomMapper.selectList(new QueryWrapper<YwRoom>().lambda() |
| | | .eq(YwRoom::getIsdeleted, Constants.ZERO)); |
| | | for (YwRoom room : rooms) { |
| | | if (room.getProjectId() == null || room.getBuildingId() == null || room.getFloor() == null) { |
| | | continue; |
| | | } |
| | | if (StringUtils.isNotBlank(room.getCode())) { |
| | | cache.roomMap.put(roomKey(room.getProjectId(), room.getBuildingId(), room.getFloor(), room.getCode()), room.getId()); |
| | | } |
| | | if (StringUtils.isNotBlank(room.getRoomNum())) { |
| | | cache.roomMap.put(roomKey(room.getProjectId(), room.getBuildingId(), room.getFloor(), room.getRoomNum()), room.getId()); |
| | | } |
| | | } |
| | | List<YwDevice> devices = ywDeviceMapper.selectList(new QueryWrapper<YwDevice>().lambda() |
| | | .eq(YwDevice::getIsdeleted, Constants.ZERO) |
| | | .select(YwDevice::getId, YwDevice::getCode)); |
| | | for (YwDevice device : devices) { |
| | | if (StringUtils.isNotBlank(device.getCode())) { |
| | | cache.existingCodeIdMap.put(device.getCode(), device.getId()); |
| | | } |
| | | } |
| | | List<Member> members = memberJoinMapper.selectJoinList(Member.class, new MPJLambdaWrapper<Member>() |
| | | .select(Member::getId, Member::getName) |
| | | .leftJoin(Company.class, Company::getId, Member::getCompanyId) |
| | | .eq(Member::getIsdeleted, Constants.ZERO) |
| | | .eq(Company::getType, Constants.ONE)); |
| | | if (!CollectionUtils.isEmpty(members)) { |
| | | List<Integer> memberIds = members.stream().map(Member::getId).filter(Objects::nonNull).collect(Collectors.toList()); |
| | | if (!CollectionUtils.isEmpty(memberIds)) { |
| | | List<SystemUser> systemUsers = systemUserMapper.selectList(new QueryWrapper<SystemUser>().lambda() |
| | | .eq(SystemUser::getDeleted, Boolean.FALSE) |
| | | .in(SystemUser::getMemberId, memberIds)); |
| | | Map<Integer, Integer> memberUserMap = systemUsers.stream() |
| | | .filter(u -> u.getMemberId() != null) |
| | | .collect(Collectors.toMap(SystemUser::getMemberId, SystemUser::getId, (a, b) -> a)); |
| | | for (Member member : members) { |
| | | if (StringUtils.isBlank(member.getName())) { |
| | | continue; |
| | | } |
| | | Integer userId = memberUserMap.get(member.getId()); |
| | | if (userId == null) { |
| | | continue; |
| | | } |
| | | if (cache.internalUserNameMap.containsKey(member.getName())) { |
| | | cache.duplicateInternalUserNames.add(member.getName()); |
| | | } |
| | | cache.internalUserNameMap.put(member.getName(), userId); |
| | | } |
| | | } |
| | | } |
| | | return cache; |
| | | } |
| | | |
| | | private String roomKey(Integer projectId, Integer buildingId, Integer floorId, String roomName) { |
| | | return projectId + "|" + buildingId + "|" + floorId + "|" + roomName; |
| | | } |
| | | |
| | | private YwDevice checkImportRow(DeviceImport row, int index, DeviceImportCache cache, Set<String> fileCodes, |
| | | LoginUserInfo loginUserInfo, Date now) { |
| | | int rowNum = index + IMPORT_EXCEL_ROW_OFFSET; |
| | | if (StringUtils.isBlank(row.getCode())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备编号不能为空,请检查表格内容!"); |
| | | } |
| | | if (StringUtils.isBlank(row.getName())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备名称不能为空,请检查表格内容!"); |
| | | } |
| | | Integer deviceNum = parseImportDeviceNum(row.getDeviceNum(), rowNum); |
| | | if (StringUtils.isBlank(row.getProjectName())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行所属项目不能为空,请检查表格内容!"); |
| | | } |
| | | if (StringUtils.isBlank(row.getRoomPath())) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行关联房源不能为空,请检查表格内容!"); |
| | | } |
| | | String code = row.getCode().trim(); |
| | | if (fileCodes.contains(code)) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备编号【" + code + "】重复,请检查表格内容!"); |
| | | } |
| | | fileCodes.add(code); |
| | | Integer existingId = cache.existingCodeIdMap.get(code); |
| | | |
| | | Integer cateId = null; |
| | | if (StringUtils.isNotBlank(row.getCategoryPath())) { |
| | | String categoryPath = row.getCategoryPath().trim(); |
| | | String[] cateParts = categoryPath.split("/"); |
| | | if (cateParts.length < 2 || StringUtils.isBlank(cateParts[1])) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备分类【" + categoryPath + "】格式不正确,请使用一级/二级格式!"); |
| | | } |
| | | cateId = cache.categoryPathMap.get(categoryPath); |
| | | if (cateId == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备分类【" + categoryPath + "】未找到,请检查表格内容!"); |
| | | } |
| | | } |
| | | |
| | | Integer projectId = cache.projectNameMap.get(row.getProjectName().trim()); |
| | | if (projectId == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行所属项目【" + row.getProjectName() + "】未找到,请检查表格内容!"); |
| | | } |
| | | |
| | | String[] roomParts = row.getRoomPath().trim().split("/"); |
| | | if (roomParts.length < 3 || StringUtils.isAnyBlank(roomParts[0], roomParts[1], roomParts[2])) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行关联房源【" + row.getRoomPath() + "】格式不正确,请使用楼宇/楼层/房源格式!"); |
| | | } |
| | | Integer buildingId = cache.buildingMap.get(projectId + "|" + roomParts[0].trim()); |
| | | if (buildingId == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行关联房源【" + row.getRoomPath() + "】未找到,请检查表格内容!"); |
| | | } |
| | | Integer floorId = cache.floorMap.get(buildingId + "|" + roomParts[1].trim()); |
| | | if (floorId == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行关联房源【" + row.getRoomPath() + "】未找到,请检查表格内容!"); |
| | | } |
| | | Integer roomId = cache.roomMap.get(roomKey(projectId, buildingId, floorId, roomParts[2].trim())); |
| | | if (roomId == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行关联房源【" + row.getRoomPath() + "】未找到,请检查表格内容!"); |
| | | } |
| | | |
| | | Integer status = parseImportStatus(row.getStatusText(), rowNum); |
| | | |
| | | Integer userId = resolveInternalUserId(row.getAdminUserName(), rowNum, "设备管理员", cache); |
| | | Integer maintenanceUserId = resolveInternalUserId(row.getMaintenanceUserName(), rowNum, "维保负责人", cache); |
| | | |
| | | YwDevice device = new YwDevice(); |
| | | device.setCode(code); |
| | | device.setName(row.getName().trim()); |
| | | device.setDeviceNum(deviceNum); |
| | | device.setCateId(cateId); |
| | | device.setModelNo(StringUtils.trimToNull(row.getModelNo())); |
| | | device.setUserId(userId); |
| | | device.setAddr(StringUtils.trimToNull(row.getAddr())); |
| | | device.setProjectId(projectId); |
| | | device.setBuildingId(buildingId); |
| | | device.setFloorId(floorId); |
| | | device.setRoomId(roomId); |
| | | device.setBuyDate(parseImportDate(row.getBuyDate(), rowNum, "购入时间")); |
| | | device.setStatus(status); |
| | | device.setContent(StringUtils.trimToNull(row.getContent())); |
| | | device.setSupplier(StringUtils.trimToNull(row.getSupplier())); |
| | | device.setSupplierLinker(StringUtils.trimToNull(row.getSupplierLinker())); |
| | | device.setSupplierPhone(StringUtils.trimToNull(row.getSupplierPhone())); |
| | | device.setMaintenanceUserId(maintenanceUserId); |
| | | device.setMaintenanceOverDate(parseImportDate(row.getMaintenanceOverDate(), rowNum, "维保到期日")); |
| | | device.setMaintenanceContent(StringUtils.trimToNull(row.getMaintenanceContent())); |
| | | if (existingId != null) { |
| | | device.setId(existingId); |
| | | device.setEditor(loginUserInfo.getId()); |
| | | device.setEditDate(now); |
| | | } else { |
| | | device.setCreator(loginUserInfo.getId()); |
| | | device.setCreateDate(now); |
| | | device.setIsdeleted(Constants.ZERO); |
| | | } |
| | | return device; |
| | | } |
| | | |
| | | private Integer normalizeDeviceNum(Integer deviceNum) { |
| | | if (deviceNum == null || deviceNum <= 0) { |
| | | return 1; |
| | | } |
| | | return deviceNum; |
| | | } |
| | | |
| | | private Integer parseImportDeviceNum(String deviceNumText, int rowNum) { |
| | | if (StringUtils.isBlank(deviceNumText)) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备数量不能为空,请检查表格内容!"); |
| | | } |
| | | String text = deviceNumText.trim(); |
| | | if (StringUtils.endsWith(text, ".0")) { |
| | | text = StringUtils.substringBefore(text, ".0"); |
| | | } |
| | | try { |
| | | double value = Double.parseDouble(text); |
| | | if (value <= 0 || value != Math.floor(value)) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备数量必须为正整数,请检查表格内容!"); |
| | | } |
| | | return (int) value; |
| | | } catch (NumberFormatException e) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备数量必须为正整数,请检查表格内容!"); |
| | | } |
| | | } |
| | | |
| | | private Integer resolveInternalUserId(String userName, int rowNum, String fieldLabel, DeviceImportCache cache) { |
| | | if (StringUtils.isBlank(userName)) { |
| | | return null; |
| | | } |
| | | String name = userName.trim(); |
| | | if (cache.duplicateInternalUserNames.contains(name)) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行" + fieldLabel + "【" + name + "】存在重名,请检查表格内容!"); |
| | | } |
| | | Integer userId = cache.internalUserNameMap.get(name); |
| | | if (userId == null) { |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行" + fieldLabel + "【" + name + "】未找到,请检查表格内容!"); |
| | | } |
| | | return userId; |
| | | } |
| | | |
| | | private Integer parseImportStatus(String statusText, int rowNum) { |
| | | if (StringUtils.isBlank(statusText)) { |
| | | return Constants.ZERO; |
| | | } |
| | | String text = statusText.trim(); |
| | | if ("正常".equals(text)) { |
| | | return Constants.ZERO; |
| | | } |
| | | if ("损坏".equals(text)) { |
| | | return Constants.ONE; |
| | | } |
| | | if ("报废".equals(text)) { |
| | | return Constants.TWO; |
| | | } |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + rowNum + "行设备状态【" + statusText + "】不正确,仅支持正常/损坏/报废!"); |
| | | } |
| | | |
| | | private Date parseImportDate(String dateText, int rowNum, String fieldLabel) { |
| | | if (StringUtils.isBlank(dateText)) { |
| | | return null; |
| | | } |
| | | String text = dateText.trim(); |
| | | if (StringUtils.endsWith(text, ".0")) { |
| | | text = StringUtils.substringBefore(text, ".0"); |
| | | } |
| | | Date date = DateUtil.parseFromFormats(text); |
| | | if (date != null) { |
| | | return date; |
| | | } |
| | | try { |
| | | return new SimpleDateFormat("yyyy-MM-dd").parse(text); |
| | | } catch (Exception ignored) { |
| | | // try excel serial number below |
| | | } |
| | | try { |
| | | return org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.parseDouble(text)); |
| | | } catch (Exception ignored) { |
| | | // fall through to error |
| | | } |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), |
| | | "对不起,第" + rowNum + "行" + fieldLabel + "【" + dateText + "】格式不正确,请使用yyyy-MM-dd格式!"); |
| | | } |
| | | |
| | | private static class DeviceImportCache { |
| | | private final Map<String, Integer> categoryPathMap = new HashMap<>(); |
| | | private final Map<String, Integer> projectNameMap = new HashMap<>(); |
| | | private final Map<String, Integer> buildingMap = new HashMap<>(); |
| | | private final Map<String, Integer> floorMap = new HashMap<>(); |
| | | private final Map<String, Integer> roomMap = new HashMap<>(); |
| | | private final Map<String, Integer> existingCodeIdMap = new HashMap<>(); |
| | | private final Map<String, Integer> internalUserNameMap = new HashMap<>(); |
| | | private final Set<String> duplicateInternalUserNames = new HashSet<>(); |
| | | } |
| | | |
| | | } |
| | | |