package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUserFollow;
import com.ruoyi.system.domain.SysUserLike;
import com.ruoyi.system.domain.dto.SysNotificationDto;
import com.ruoyi.system.domain.dto.WorkLikeDto;
import com.ruoyi.system.domain.vo.UserMessageVo;
import com.ruoyi.system.service.ISysUserFollowService;
import com.ruoyi.system.service.ISysUserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserNotificationMapper;
import com.ruoyi.system.domain.SysUserNotification;
import com.ruoyi.system.service.ISysUserNotificationService;

/**
 * 用户消息通知Service业务层处理
 * 
 * @author haotian
 * @date 2024-12-02
 */
@Service
public class SysUserNotificationServiceImpl implements ISysUserNotificationService 
{
    @Autowired
    private SysUserNotificationMapper sysUserNotificationMapper;

    @Autowired
    private ISysUserFollowService userFollowService;

    @Autowired
    private ISysUserLikeService userLikeService;

    /**
     * 查询用户消息通知
     * 
     * @param id 用户消息通知主键
     * @return 用户消息通知
     */
    @Override
    public SysUserNotification selectSysUserNotificationById(Long id)
    {
        return sysUserNotificationMapper.selectSysUserNotificationById(id);
    }

    /**
     * 查询用户消息通知列表
     * 
     * @param sysUserNotification 用户消息通知
     * @return 用户消息通知
     */
    @Override
    public List<SysUserNotification> selectSysUserNotificationList(SysUserNotification sysUserNotification)
    {
        return sysUserNotificationMapper.selectSysUserNotificationList(sysUserNotification);
    }

    /**
     * 新增用户消息通知
     * 
     * @param sysUserNotification 用户消息通知
     * @return 结果
     */
    @Override
    public int insertSysUserNotification(SysUserNotification sysUserNotification)
    {
        sysUserNotification.setCreateTime(DateUtils.getNowDate());
        return sysUserNotificationMapper.insertSysUserNotification(sysUserNotification);
    }

    /**
     * 修改用户消息通知
     * 
     * @param sysUserNotification 用户消息通知
     * @return 结果
     */
    @Override
    public int updateSysUserNotification(SysUserNotification sysUserNotification)
    {
        sysUserNotification.setUpdateTime(DateUtils.getNowDate());
        return sysUserNotificationMapper.updateSysUserNotification(sysUserNotification);
    }

    /**
     * 批量删除用户消息通知
     * 
     * @param ids 需要删除的用户消息通知主键
     * @return 结果
     */
    @Override
    public int deleteSysUserNotificationByIds(Long[] ids)
    {
        return sysUserNotificationMapper.deleteSysUserNotificationByIds(ids);
    }

    /**
     * 删除用户消息通知信息
     * 
     * @param id 用户消息通知主键
     * @return 结果
     */
    @Override
    public int deleteSysUserNotificationById(Long id)
    {
        return sysUserNotificationMapper.deleteSysUserNotificationById(id);
    }

    @Override
    public List<SysUserNotification> unreadNotifiaction(Long userId)
    {
        return sysUserNotificationMapper.unreadNotifiaction(userId);
    }


    // 获取指定用户的id对应的消息
    @Override
    public UserMessageVo getUserMessages(Long userId)
    {
        UserMessageVo userMessageVo = new UserMessageVo();

        SysUserFollow followed = new SysUserFollow();
        followed.setFollowedUserId(userId);

        //获取关注我的相关信息
        userMessageVo.setFollowedByList(userFollowService.selectSysUserFollowList(followed));

        //我关注的相关信息
        SysUserFollow following = new SysUserFollow();
        following.setUserId(userId);
        userMessageVo.setFollowingList(userFollowService.selectSysUserFollowList(following));

        //点赞我的列表, 查询所有给我的作品点赞的信息
        userMessageVo.setLikedByList(userLikeService.selectSysUserLikeByUserIdAndTargetId(userId));

        //我点赞的列表
        SysUserLike myLike = new SysUserLike();
        myLike.setUserId(userId);
        userMessageVo.setMyLikeList(userLikeService.selectSysUserLikeList(myLike));

        // 未读消息列表

        List<SysUserNotification> unreadNotifications = sysUserNotificationMapper.unreadNotifiaction(userId);


        userMessageVo.setLatestNotifications(unreadNotifications);


        // 未读消息总数
        userMessageVo.setUnreadCount(Long.valueOf(unreadNotifications.size()));

        return userMessageVo;




    }

    /*
     * 查看新增关注数
     * */
    @Override
    public Long getAddFollowerCount(Long userId){
        return sysUserNotificationMapper.getAddFollowerCount(userId);
    }

    /*
     * 查看新增点赞数
     * */
    @Override
    public Long getAddLikeCount(Long userId){
        return sysUserNotificationMapper.getAddLikeCount(userId);
    }

    /*
     * 读消息
     * */
    @Override
    public Long readNotifiaction(Long userId, String type, String status){
        Date now = DateUtils.getNowDate();
        return sysUserNotificationMapper.readNotifiaction(userId, type, status, now);
    }

    /*
     * 消息--查看系统消息
     * */
    @Override
    public List<SysNotificationDto> getSystemMessage(Long userId){
        return sysUserNotificationMapper.getSystemMessage(userId);
    }

    /*
     * 消息--查看点赞消息列表
     * */
    @Override
    public List<WorkLikeDto> getWorkLikeList(Long userId){
        return sysUserNotificationMapper.getWorkLikeList(userId);
    }

    /*
     * 注销账户是 删除通知数据
     * */
    @Override
    public int deleteSysUserNotificationBySenderId(Long userId){
        return sysUserNotificationMapper.deleteSysUserNotificationBySenderId(userId);
    }

    /**
     * 取消关注时删除关注通知
     *
     */
    @Override
    public int deleteSysUserNotificationBySenderIdAndUserId(Long userId, Long followedUserId, String type){
        return sysUserNotificationMapper.deleteSysUserNotificationBySenderIdAndUserId(userId, followedUserId, type);
    }
}
