package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.entity.SysMessage;
import com.loan.system.domain.vo.SysMessageVO;
import com.loan.system.repository.MessageRepository;
import com.loan.system.service.MessageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class MessageServiceImpl implements MessageService {
    private final MessageRepository messageRepository;

    @Autowired
    public MessageServiceImpl(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }
    @Override
    public List<SysMessageVO> getMessageByMobile(String mobile) {
        return BeanUtil.copyToList(messageRepository.findByMobileAndIsDelete(mobile,false), SysMessageVO.class);
    }

    @Override
    public List<SysMessageVO> getMessageByRole(String role) {
        return BeanUtil.copyToList(messageRepository.findByUserRoleAndIsDelete(role,false), SysMessageVO.class);
    }

    @Override
    public void addMessage(SysMessage message) {
        message.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        message.setReadStatus(false);
        message.setIsDelete(false);
        messageRepository.save(message);
    }
}
