@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());
  }
  @Override
  public GeneratorInstance create(GeneratorInstanceCreateRequest request) {
    Long userId = request.getAuthentication().getUserId();

    Long generatorId = request.getGeneratorId();
    Generator generatorPersistence = generatorRepository.selectById(generatorId);
    if (generatorPersistence == null) {
      throw new AppException("生成器不存在");
    }

    if (!generatorPersistence.getIsApplied()
        && !generatorPersistence.getDeveloper().getId().equals(userId)) {
      generatorPersistence.setIsApplied(true);
    }
    generatorPersistence.setInstanceCount(generatorPersistence.getInstanceCount() + 1);
    generatorRepository.update(generatorPersistence);

    GeneratorInstance generatorInstance = new GeneratorInstance();
    generatorInstance.setId(idWorker.nextId());
    generatorInstance.setName(request.getName());
    generatorInstance.setCreateDate(new Date());
    generatorInstance.setModifyDate(new Date());
    generatorInstance.setIsDelete(false);
    Generator generator = new Generator();
    generator.setId(generatorPersistence.getId());
    generatorInstance.setGenerator(generator);
    User user = new User();
    user.setId(userId);
    generatorInstance.setUser(user);
    generatorInstance.setVersion(generatorPersistence.getVersion());

    DataModel dataModel = new DataModel();
    dataModel.setId(idWorker.nextId());
    dataModel.setCreateDate(new Date());
    dataModel.setModifyDate(new Date());
    dataModel.setIsDelete(false);
    GeneratorInstance g = new GeneratorInstance();
    g.setId(generatorInstance.getId());
    dataModel.setGeneratorInstance(g);
    generator.setId(generatorPersistence.getId());
    dataModel.setGenerator(generator);
    dataModel.setUser(user);
    dataModelRepository.insert(dataModel);

    DataModel dm = new DataModel();
    dm.setId(dataModel.getId());
    generatorInstance.setDataModel(dm);
    generatorInstanceRepository.insert(generatorInstance);

    return generatorInstance;
  }