package cn.lihongjie.coal.noticeStatus.service;

import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.noticeStatus.dto.CreateNoticeStatusDto;
import cn.lihongjie.coal.noticeStatus.dto.MyNoticeStatusDto;
import cn.lihongjie.coal.noticeStatus.dto.NoticeStatusDto;
import cn.lihongjie.coal.noticeStatus.dto.UpdateNoticeStatusDto;
import cn.lihongjie.coal.noticeStatus.entity.NoticeStatusEntity;
import cn.lihongjie.coal.noticeStatus.mapper.NoticeStatusMapper;
import cn.lihongjie.coal.noticeStatus.repository.NoticeStatusRepository;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@Transactional
public class NoticeStatusService extends BaseService<NoticeStatusEntity, NoticeStatusRepository> {
    @Autowired private NoticeStatusRepository repository;

    @Autowired private NoticeStatusMapper mapper;

    @Autowired private ConversionService conversionService;

    public NoticeStatusDto create(CreateNoticeStatusDto request) {
        NoticeStatusEntity entity = mapper.toEntity(request);

        this.repository.save(entity);
        return getById(entity.getId());
    }

    public NoticeStatusDto update(UpdateNoticeStatusDto request) {
        NoticeStatusEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {
        List<NoticeStatusEntity> allById = this.repository.findAllById(request.getIds());
        allById.forEach(x -> {
            x.setDelete(true);
            x.setDeleteTime(LocalDateTime.now());

        });

        this.repository.saveAll(allById);
    }

    public NoticeStatusDto getById(String id) {
        NoticeStatusEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<NoticeStatusDto> list(CommonQuery query) {
        Page<NoticeStatusEntity> page =
                repository.findAll(
                        query.specification(conversionService),
                        PageRequest.of(
                                query.getPageNo(),
                                query.getPageSize(),
                                Sort.by(query.getOrders())));

        return page.map(this.mapper::toDto);
    }

    public MyNoticeStatusDto getMyNoticeCount() {
        MyNoticeStatusDto entity = repository.userNoticeCount(Ctx.getUserId());
        return entity;
    }

    public void read(IdRequest request) {


        List<NoticeStatusEntity> allById = this.repository.findAllById(request.getIds());
        allById.forEach(x -> {
            x.setRead(true);
            x.setReadTime(LocalDateTime.now());

        });

        this.repository.saveAll(allById);
    }
}
