<%_ if (position === 'import') { _%>
    <%_ if (userIdTypeName === 'UUID') { _%>
import java.util.UUID;
    <%_ } _%>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import <%= packageName %>.domain.enumeration.AnnoCategory;
import <%= packageName %>.service.criteria.UserCriteria;
import <%= entityAbsolutePackage %>.repository.AnnouncementRecordRepository;
import <%= packageName %>.domain.enumeration.AnnoSendStatus;
import <%= packageName %>.security.SecurityUtils;
import <%= entityAbsolutePackage %>.domain.AnnouncementRecord;
import java.time.ZonedDateTime;
import <%= packageName %>.web.rest.errors.BadRequestAlertException;
    <%_ if (databaseTypeMongodb) { _%>
import <%= packageName %>.service.dto.AdminUserDTO;
import <%= packageName %>.service.UserService;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Criteria;
    <%_ } _%>
    <%_ if (databaseTypeSql) { _%>
import <%=packageName%>.service.UserQueryService;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'importInterface') { _%>
import <%= packageName %>.domain.enumeration.AnnoCategory;
<%_ } _%>
<%_ if (position === 'extendsImport') { _%>
import <%= entityAbsolutePackage %>.repository.AnnouncementRecordRepository;
    <%_ if (databaseTypeSql) { _%>
import <%= packageName %>.service.UserQueryService;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>

    void release(<%= databaseTypeSql ? 'Long' : 'String' %> id);
    <%_ if (databaseTypeMongodb) { _%>
    Page<AnnouncementDTO> findAllByCategoryAndIdIn(AnnoCategory category, List<String> ids, Pageable page);
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
    @Transactional
    <%_ } _%>
    public void release(<%= databaseTypeSql ? 'Long' : 'String' %> id)<% if (serviceImpl) { %> {
        Announcement announcement = announcementRepository.<% if (databaseTypeSql) { %><%= ormTool === 'hibernate' ? 'getOne' : 'selectById' %>(id);<% } else { %>findById(id).get();<% } %>
        if (announcement != null) {
            announcement
                .sendStatus(AnnoSendStatus.RELEASED)
                .sendTime(ZonedDateTime.now())
                .senderId(SecurityUtils.getCurrentUserId().orElse(null));
            <%= ormTool === 'mybatis' && databaseTypeSql ? '': 'announcement = ' %>announcementRepository.save(announcement);
            List<AnnouncementRecord> records = new ArrayList<>();
            ZonedDateTime sendTime = ZonedDateTime.now();
            <%= databaseTypeSql ? userIdTypeName : 'String' %>[] userIds = {};
            <%_ if (databaseTypeMongodb) { _%>
            Criteria criteria = new Criteria();
            switch (announcement.getReceiverType()) {
                case ALL:
                    return;
                case USER:
                    userIds = announcement.getReceiverIds();
                    break;
                case POSITION:
                    criteria.and("position.$id").in(Arrays.asList(announcement.getReceiverIds()));
                    userIds = userService.getFieldByCriteria(new Query(criteria)).stream().map(AdminUserDTO::getId).collect(Collectors.toList()).toArray(userIds);
                    break;
                case DEPARTMENT:
                    criteria.and("department.$id").in(Arrays.asList(announcement.getReceiverIds()));
                    userIds = userService.getFieldByCriteria(new Query(criteria)).stream().map(AdminUserDTO::getId).collect(Collectors.toList()).toArray(userIds);
                    break;
                case AUTHORITY:
                    criteria.and("authorities.$id").in(Arrays.asList(announcement.getReceiverIds()));
                    userIds = userService.getFieldByCriteria(new Query(criteria)).stream().map(AdminUserDTO::getId).collect(Collectors.toList()).toArray(userIds);
                    break;
            }
            <%_ } else { _%>
            UserCriteria criteria = new UserCriteria();
            List<Long> receiverIds = Arrays.stream(announcement.getReceiverIds().split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
            switch (announcement.getReceiverType()) {
                case ALL:
                    return;
                case USER:
                    userIds = Arrays.stream((announcement.getReceiverIds().split(","))).map(<%= userIdTypeName %>::<%= userIdTypeName === 'UUID' ? 'fromString' : 'valueOf'%>).toArray(<%= userIdTypeName %>[]::new);
                    break;
                case POSITION:
                    criteria.positionId().setIn(receiverIds);
                    userIds = userQueryService.getFieldByCriteria(Long.class, "id", true, criteria).toArray(userIds);
                    break;
                case DEPARTMENT:
                    criteria.departmentId().setIn(receiverIds);
                    userIds = userQueryService.getFieldByCriteria(Long.class, "id", true, criteria).toArray(userIds);
                    break;
                case AUTHORITY:
                    criteria.authoritiesId().setIn(receiverIds);
                    userIds = userQueryService.getFieldByCriteria(Long.class, "id", true, criteria).toArray(userIds);
                    break;
            }
            <%_ } _%>
            <%_ if (ormTool === 'mybatis' && databaseTypeSql) { %>
            for (<%= userIdTypeName %> userId : userIds) {
                announcementRecordRepository.save(new AnnouncementRecord().anntId(announcement.getId()).userId(userId).hasRead(false));
            }
            <%_ } else { _%>
            for (<%= databaseTypeSql ? 'Long' : 'String' %> userId : userIds) {
                records.add(new AnnouncementRecord().anntId(announcement.getId()).userId(userId).hasRead(false));
            }
            announcementRecordRepository.saveAll(records);
            <%_ } _%>
        } else {
            throw new BadRequestAlertException("未找到指定Id的通知", "Announcement", "IdNotFound");
        }
    }<% } else { %>;<% } %>
    <%_ if (databaseTypeMongodb) { _%>

    public Page<AnnouncementDTO> findAllByCategoryAndIdIn(AnnoCategory category, List<String> ids, Pageable page)<% if (serviceImpl) { %> {
        return announcementRepository.findAllByCategoryAndIdIn(category, ids, page).map(announcementMapper::toDto);
    }<% } else { %>;<% } %>
    <%_ } _%>
<%_ } _%>
