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

    Long userId = request.getAuthentication().getUserId();
    if (!userId.equals(generatorInstancePersistence.getUser().getId())) {
      throw new AppException("权限不足");
    }
    generatorInstanceRepository.delete(generatorInstancePersistence);

    Long rootDataModelId = generatorInstancePersistence.getDataModel().getId();
    DataModel rootDataModePersistence = dataModelRepository.selectById(rootDataModelId);
    if (rootDataModePersistence == null) {
      throw new AppException("rootDataMode不存在");
    }
    dataModelRepository.delete(rootDataModePersistence);

    Generator generatorPersistence =
        generatorRepository.selectById(generatorInstancePersistence.getGenerator().getId());
    generatorPersistence.setInstanceCount(generatorPersistence.getInstanceCount() - 1);
    generatorRepository.update(generatorPersistence);

    return generatorInstancePersistence;
  }
  @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;
  }