<%_ if (position === 'import') { _%>
    <%_ if (userIdTypeName === 'UUID') { _%>
import java.util.UUID;
    <%_ } _%>
import <%= packageName %>.security.SecurityUtils;
import java.time.ZonedDateTime;
import <%= packageName %>.system.service.dto.AnnouncementDTO;
    <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
    <%_ } _%>
    <%_ if (databaseTypeMongodb) { _%>
import <%= entityAbsolutePackage %>.domain.enumeration.AnnoSendStatus;
import <%= entityAbsolutePackage %>.domain.enumeration.ReceiverType;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'importInterface') { _%>
import java.time.ZonedDateTime;
import <%= packageName %>.system.service.dto.AnnouncementDTO;
    <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    <%_ } _%>
    <%_ if (databaseTypeMongodb) { _%>
import <%= entityAbsolutePackage %>.domain.enumeration.AnnoSendStatus;
import <%= entityAbsolutePackage %>.domain.enumeration.ReceiverType;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>

    void setRead(<%= databaseTypeSql ? 'Long' : 'String'%> anntId);
    <%_ if (databaseTypeSql) { _%>

    void updateRecord(List<AnnouncementDTO> announcementDTOS);
    <%_ } else { _%>

    void updateRecord();
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'method') { _%>
    <%_ if (databaseTypeSql) { _%>
        <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
    @Transactional
        <%_ } _%>
    public void updateRecord(List<AnnouncementDTO> announcementDTOS)<% if (serviceImpl) { %> {
        <%= user.primaryKey.type%> userId = SecurityUtils.getCurrentUserId().orElseThrow(<% if (javaVersion === '8') { %>() -> new NoSuchElementException("No value present")<% } %>);
        announcementDTOS.forEach(announcementDTO -> {
            Long anntId = announcementDTO.getId();
            <%_ if (ormTool === 'hibernate') { _%>
            if (!announcementRecordRepository.exists(Example.of(
                new AnnouncementRecord().anntId(announcementDTO.getId()).userId(userId)))) {
                announcementRecordRepository.save(new AnnouncementRecord().anntId(anntId).userId(userId));
            }
            <%_ } else { _%>
            if (announcementRecordRepository.selectCount(
                new LambdaQueryWrapper<AnnouncementRecord>()
                    .eq(AnnouncementRecord::getAnntId, announcementDTO.getId())
                    .eq(AnnouncementRecord::getUserId, userId)
            ) == 0) {
                announcementRecordRepository.save(new AnnouncementRecord().anntId(anntId).userId(userId));
            }
            <%_ } _%>
        });
    }<% } else { %>;<% }%>
    <%_ } else { _%>
    public void updateRecord()<% if (serviceImpl) { %> {
        Criteria acriteria = new Criteria();
        acriteria.and("send_status").is(AnnoSendStatus.RELEASED).andOperator(Criteria.where("receiver_type").is(ReceiverType.ALL));
        List<AnnouncementRecord> announcementRecords = mongoTemplate.find(new Query(acriteria), AnnouncementRecord.class);
        String userId = SecurityUtils.getCurrentUserId().orElseThrow(<% if (javaVersion === '8') { %>() -> new NoSuchElementException("No value present")<% } %>);
        announcementRecords.forEach(
            record -> {
                String anntId = record.getId();
                if (
                    !announcementRecordRepository.exists(
                        Example.of(new AnnouncementRecord().anntId(record.getId()).userId(userId))
                    )
                ) {
                    announcementRecordRepository.save(new AnnouncementRecord().anntId(anntId).userId(userId));
                }
            }
        );
    }<% } else { %>;<% }%>
    <%_ } _%>

    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
    @Transactional
    <%_ } _%>
    public void setRead(<%= databaseTypeSql ? 'Long' : 'String'%> anntId)<% if (serviceImpl) { %> {
        <%= user.primaryKey.type%> userId = SecurityUtils.getCurrentUserId().orElseThrow(<% if (javaVersion === '8') { %>() -> new NoSuchElementException("No value present")<% } %>);
        <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
        this.update(new LambdaUpdateWrapper<AnnouncementRecord>()
            .set(AnnouncementRecord::getHasRead, true)
            .set(AnnouncementRecord::getReadTime, ZonedDateTime.now())
            .eq(AnnouncementRecord::getAnntId, anntId)
            .eq(AnnouncementRecord::getUserId, userId)
        );
        <%_ } else { %>
        Optional<AnnouncementRecord> one = announcementRecordRepository.findOne(Example.of(new AnnouncementRecord().anntId(anntId).userId(userId)));
        if (one.isPresent()) {
            one.get().hasRead(true).readTime(ZonedDateTime.now());
            announcementRecordRepository.save(one.get());
        }
        <%_ } _%>
    }<% } else { %>;<% }%>
<%_ } _%>
