<%
const pageUtil = ormTool !== 'mybatis' ? 'PaginationUtil': 'IPageUtil';
const pageGetContent = ormTool !== 'mybatis' ? 'getContent': 'getRecords';
const pageName = ormTool !== 'mybatis' ? 'Page': 'IPage';
%>
<%_ if (position === 'extendsImport') { _%>
    <%_ if (databaseTypeSql) { _%>
import <%= packageName %>.system.service.AnnouncementRecordQueryService;
    <%_ } _%>
import <%= packageName %>.system.service.AnnouncementRecordService;
<%_ } _%>
<%_ if (position === 'import') { _%>
import java.util.stream.Collectors;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.domain.enumeration.AnnoCategory;
import <%= packageName %>.domain.enumeration.AnnoSendStatus;
import <%= packageName %>.domain.enumeration.ReceiverType;
    <%_ if (databaseTypeSql) { _%>
import <%= entityAbsolutePackage %>.service.AnnouncementRecordQueryService;
import <%= entityAbsolutePackage %>.service.criteria.AnnouncementRecordCriteria;
    <%_ } _%>
    <%_ if (databaseTypeMongodb) { _%>
import <%= entityAbsolutePackage %>.domain.AnnouncementRecord;
    <%_ } _%>
import <%= entityAbsolutePackage %>.service.AnnouncementRecordService;
import <%= entityAbsolutePackage %>.service.dto.AnnouncementRecordDTO;
import <%= entityAbsolutePackage %>.domain.Announcement;
import java.util.concurrent.atomic.AtomicReference;
import java.time.ZonedDateTime;
    <%_ if (!(ormTool === 'mybatis' && databaseTypeSql)) { _%>
import org.springframework.data.domain.PageImpl;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    /**
     * {@code GET  /announcements/current-user/unread/{category}} : get the  announcements of unread for current user.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the announcementDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/announcements/current-user/unread/{category}")
    public ResponseEntity<List<AnnouncementDTO>> getUnReadAnnouncements(@PathVariable AnnoCategory category, Pageable page) {
        <%_ if (databaseTypeSql) { _%>
        AnnouncementCriteria acriteria = new AnnouncementCriteria();
        acriteria.sendStatus().setEquals(AnnoSendStatus.RELEASED);
        acriteria.receiverType().setEquals(ReceiverType.ALL);
        announcementRecordService.updateRecord(announcementQueryService.findByCriteria(acriteria));
        AnnouncementRecordCriteria criteria = new AnnouncementRecordCriteria();
        criteria.userId().setEquals(SecurityUtils.getCurrentUserId().orElseThrow(<% if (javaVersion === '8') { %>() -> new NoSuchElementException("No value present")<% } %>));
        criteria.hasRead().setEquals(false);
        <%= pageName%><AnnouncementDTO> byCriteria;
        List<Long> ids = announcementRecordQueryService.findByCriteria(criteria).stream().map(AnnouncementRecordDTO::getAnntId).collect(Collectors.toList());
        if (!ids.isEmpty()) {
            AnnouncementCriteria announcementCriteria = new AnnouncementCriteria();
            announcementCriteria.id().setIn(ids);
            announcementCriteria.category().setEquals(category);
            byCriteria = announcementQueryService.findByCriteria(announcementCriteria, <% if (ormTool === 'hibernate') { %>page<% } else { %>PageableUtils.toPage(page)<% } %>);
        } else {
            <%_ if (ormTool === 'hibernate') { _%>
            byCriteria = new PageImpl<>(new ArrayList<>(), page, 0);
            <%_ } else { _%>
            byCriteria= new Page<>(0, page.getPageSize(), 0);
            <%_ } _%>
        }
        <%_ } else { _%>
        announcementRecordService.updateRecord();
        Page<AnnouncementDTO> byCriteria;
        List<String> ids = announcementRecordService.findAllByExample(
            Example.of(new AnnouncementRecord().userId(SecurityUtils.getCurrentUserId().get()).hasRead(false),ExampleMatcher.matchingAll()), Pageable.unpaged())
            .stream()
            .map(AnnouncementRecordDTO::getAnntId)
            .collect(Collectors.toList());
        if (!ids.isEmpty()) {
            byCriteria = announcementService.findAllByCategoryAndIdIn(category, ids, page);
        } else {
            byCriteria = new PageImpl<>(new ArrayList<>(), page, 0);
        }
        <%_ } _%>
        HttpHeaders headers = <%= pageUtil%>.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), byCriteria);
        return ResponseEntity.ok().headers(headers).body(byCriteria.<%= pageGetContent%>());
    }

    /**
     * {@code PUT  /announcements/:id} : release the "id" announcement.
     *
     * @param id the id of the announcementDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the announcementDTO, or with status {@code 404 (Not Found)}.
     */
    @PutMapping("/announcements/{id}/release")
    public ResponseEntity<AnnouncementDTO> releaseAnnouncement(@PathVariable <%= databaseTypeSql ? 'Long' : 'String' %> id) {
        log.debug("REST request to get Announcement : {}", id);
        announcementService.release(id);
        Optional<AnnouncementDTO> announcementDTO = announcementService.findOne(id);
        return ResponseUtil.wrapOrNotFound(announcementDTO);
    }

    /**
     * {@code PUT  /announcements/:id/read} : Updates an existing announcementRecord to read by current user.
     *
     * @param id the id of the announcementId to read.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated announcementRecordDTO,
     * or with status {@code 400 (Bad Request)} if the announcementRecordDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the announcementRecordDTO couldn't be updated.
     */
    @PutMapping("/announcements/{id}/read")
    public ResponseEntity<Void> updateAnnouncementRecordRead(@PathVariable(value = "id", required = false) final <%= databaseTypeSql ? 'Long' : 'String' %> id ) {
        log.debug("REST request to update AnnouncementRecord : {}", id);
        announcementRecordService.setRead(id);
        return ResponseEntity.ok().build();
    }
<%_ } _%>
