class CharacterQueryService {

    /**
     * Logger.
     */
    private static final Logger LOGGER = LogManager.getLogger(CharacterQueryService.class);

    /**
     * Character repository.
     */
    @Inject
    private CharacterRepository characterRepository;

    /**
     * Language service.
     */
    @Inject
    private LangPropsService langPropsService;

    /**
     * Gets total character count.
     *
     * @return total character count
     */
    public int getTotalCharacterCount() {
        return langPropsService.get("characters").length();
    }

    /**
     * Gets all written character count.
     *
     * @return all written character count
     */
    public int getWrittenCharacterCount() {<FILL_FUNCTION_BODY>}

    /**
     * Gets all written characters.
     *
     * <p>
     * <b>Note</b>: Just for testing.
     * </p>
     *
     * @return all written characters
     */
    public Set<JSONObject> getWrittenCharacters() {
        try {
            return new HashSet<>(characterRepository.getList(new Query()));
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Gets characters failed", e);
            return Collections.emptySet();
        }
    }

    /**
     * Gets written character count of a user specified by the given user id.
     *
     * @param userId the given user id
     * @return user written character count
     */
    public int getWrittenCharacterCount(final String userId) {
        final Query query = new Query().setFilter(new PropertyFilter(
                org.b3log.symphony.model.Character.CHARACTER_USER_ID, FilterOperator.EQUAL, userId));

        try {
            return (int) characterRepository.count(query);
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Counts user written characters failed", e);

            return 0;
        }
    }

    /**
     * Gets an unwritten character.
     *
     * @param userId the specified user id
     * @return character
     */
    public String getUnwrittenCharacter(final String userId) {
        final String ret = getUnwrittenCharacterRandom(userId);
        if (StringUtils.isNotBlank(ret)) {
            return ret;
        }

        return getUnwrittenCharacterOneByOne(userId);
    }

    /**
     * Gets an unwritten character (strategy: One By One).
     *
     * @param userId the specified user id
     * @return character
     */
    private String getUnwrittenCharacterOneByOne(final String userId) {
        final String characters = langPropsService.get("characters");

        int index = 0;
        while (true) {
            if (index > characters.length()) {
                return null; // All done
            }

            final String ret = StringUtils.trim(characters.substring(index, index + 1));

            index++;

            final Query query = new Query();
            query.setFilter(CompositeFilterOperator.and(
                    new PropertyFilter(org.b3log.symphony.model.Character.CHARACTER_USER_ID, FilterOperator.EQUAL, userId),
                    new PropertyFilter(org.b3log.symphony.model.Character.CHARACTER_CONTENT, FilterOperator.EQUAL, ret)
            ));

            try {
                if (characterRepository.count(query) > 0) {
                    continue;
                }

                return ret;
            } catch (final RepositoryException e) {
                LOGGER.log(Level.ERROR, "Gets an unwritten character for user [id=" + userId + "] failed", e);
            }
        }
    }

    /**
     * Gets an unwritten character (strategy: Random).
     *
     * @param userId the specified user id
     * @return character
     */
    private String getUnwrittenCharacterRandom(final String userId) {
        final String characters = langPropsService.get("characters");

        final int maxRetries = 7;
        int retries = 0;

        while (retries < maxRetries) {
            retries++;

            final int index = RandomUtils.nextInt(0, characters.length());
            final String ret = StringUtils.trim(characters.substring(index, index + 1));

            final Query query = new Query();
            query.setFilter(CompositeFilterOperator.and(
                    new PropertyFilter(org.b3log.symphony.model.Character.CHARACTER_USER_ID, FilterOperator.EQUAL, userId),
                    new PropertyFilter(org.b3log.symphony.model.Character.CHARACTER_CONTENT, FilterOperator.EQUAL, ret)
            ));

            try {
                if (characterRepository.count(query) > 0) {
                    continue;
                }

                return ret;
            } catch (final RepositoryException e) {
                LOGGER.log(Level.ERROR, "Gets an unwritten character failed", e);
            }
        }

        return null;
    }

    /**
     * Gets an unwritten character.
     *
     * @return character
     */
    public String getUnwrittenCharacter() {
        final String ret = getUnwrittenCharacterRandom();
        if (StringUtils.isNotBlank(ret)) {
            return ret;
        }

        return getUnwrittenCharacterOneByOne();
    }

    /**
     * Gets an unwritten character (strategy: Random).
     *
     * @return character
     */
    private String getUnwrittenCharacterRandom() {
        final String characters = langPropsService.get("characters");

        final int maxRetries = 7;
        int retries = 0;

        while (retries < maxRetries) {
            retries++;

            final int index = RandomUtils.nextInt(0, characters.length());
            final String ret = StringUtils.trim(characters.substring(index, index + 1));

            final Query query = new Query().setFilter(
                    new PropertyFilter(org.b3log.symphony.model.Character.CHARACTER_CONTENT, FilterOperator.EQUAL, ret));

            try {
                if (characterRepository.count(query) > 0) {
                    continue;
                }

                return ret;
            } catch (final RepositoryException e) {
                LOGGER.log(Level.ERROR, "Gets an unwritten character failed", e);
            }
        }

        return null;
    }

    /**
     * Gets an unwritten character (strategy: One By One).
     *
     * @return character
     */
    private String getUnwrittenCharacterOneByOne() {
        final String characters = langPropsService.get("characters");

        int index = 0;
        while (true) {
            if (index > characters.length()) {
                return null; // All done
            }

            final String ret = StringUtils.trim(characters.substring(index, index + 1));

            index++;

            final Query query = new Query().setFilter(
                    new PropertyFilter(org.b3log.symphony.model.Character.CHARACTER_CONTENT, FilterOperator.EQUAL, ret));

            try {
                if (characterRepository.count(query) > 0) {
                    continue;
                }

                return ret;
            } catch (final RepositoryException e) {
                LOGGER.log(Level.ERROR, "Gets an unwritten character failed", e);
            }
        }
    }
}


        try {
            final List<JSONObject> result = characterRepository.select("select count(DISTINCT characterContent) from "
                    + characterRepository.getName());
            if (null == result || result.isEmpty()) {
                return 0;
            }

            return result.get(0).optInt("count(DISTINCT characterContent)");
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Counts characters failed", e);

            return 0;
        }
    
<no_super_class>