@Override
  public GeneratorInstance get(GeneratorInstanceGetRequest request) {
    Long id = request.getId();
    GeneratorInstance generatorInstancePersistence = generatorInstanceRepository.selectById(id);
    if (generatorInstancePersistence == null) {
      throw new AppException("实例不存在");
    }

    GeneratorInstance generatorInstance = new GeneratorInstance();
    generatorInstance.setId(generatorInstancePersistence.getId());
    generatorInstance.setName(generatorInstancePersistence.getName());
    generatorInstance.setCreateDate(generatorInstancePersistence.getCreateDate());
    generatorInstance.setModifyDate(generatorInstancePersistence.getModifyDate());
    generatorInstance.setIsDelete(generatorInstancePersistence.getIsDelete());

    User userPersistence =
        userRepository.selectById(generatorInstancePersistence.getUser().getId());
    generatorInstance.setUser(userPersistence);

    Long generatorId = generatorInstancePersistence.getGenerator().getId();
    Generator generatorPersistence = generatorRepository.selectById(generatorId);
    if (generatorPersistence == null) {
      throw new AppException("生成器不存在");
    }
    generatorInstance.setGenerator(generatorPersistence);

    generatorInstance.setDataModel(generatorInstancePersistence.getDataModel());
    generatorInstance.setVersion(generatorInstancePersistence.getVersion());

    return generatorInstance;
  }
  @Override
  public PagerResult<GeneratorInstance> search(GeneratorInstanceSearchRequest request) {
    Long userId = request.getUserId();
    String name = StringUtils.hasText(request.getName()) ? request.getName() : null;
    List<GeneratorInstance> records =
        generatorInstanceRepository.filter(
            generatorInstance -> {
              if (generatorInstance.getIsDelete()) {
                return false;
              }
              if (name != null) {
                if (!generatorInstance.getName().contains(name)) {
                  return false;
                }
              }
              if (userId != null) {
                if (!userId.equals(generatorInstance.getUser().getId())) {
                  return false;
                }
              }
              return true;
            });
    Integer page = request.getPage();
    Integer pageSize = request.getPageSize();
    Integer fromIndex = (page - 1) * pageSize;
    Integer toIndex = fromIndex + pageSize > records.size() ? records.size() : fromIndex + pageSize;
    List<GeneratorInstance> limitRecords = records.subList(fromIndex, toIndex);
    List<GeneratorInstance> result = new ArrayList<>();
    for (GeneratorInstance g : limitRecords) {
      GeneratorInstance generatorInstance = new GeneratorInstance();
      generatorInstance.setId(g.getId());
      generatorInstance.setName(g.getName());
      generatorInstance.setCreateDate(g.getCreateDate());
      generatorInstance.setModifyDate(g.getModifyDate());
      generatorInstance.setIsDelete(g.getIsDelete());
      User userPersistence = userRepository.selectById(g.getUser().getId());
      generatorInstance.setUser(userPersistence);
      Generator generatorPersistence = generatorRepository.selectById(g.getGenerator().getId());
      if (generatorPersistence == null) {
        throw new AppException("生成器不存在");
      }
      generatorInstance.setGenerator(generatorPersistence);
      generatorInstance.setDataModel(g.getDataModel());
      generatorInstance.setVersion(g.getVersion());
      result.add(generatorInstance);
    }

    String sortField = request.getSortField();
    String sortDirection = request.getSortDirection();
    if ("modifyDate".equals(sortField)) {
      if ("DESC".equalsIgnoreCase(sortDirection)) {
        result.sort(
            (g1, g2) -> (int) (g2.getModifyDate().getTime() - g1.getModifyDate().getTime()));
      }
    }

    return new PagerResult<>(result, (long) records.size());
  }