package com.ruoyi.generator.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.generator.domain.Community.CommunityUserNotification;
import com.ruoyi.generator.mapper.community.CommunityUserNotificationMapper;
import com.ruoyi.generator.vo.CommunityUserNotificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 业务 服务层实现
 *
 * @author ruoyi
 */
@Service
public class CommunityNotificationServiceImpl extends ServiceImpl<CommunityUserNotificationMapper, CommunityUserNotification> implements ICommunityNotificationService  {

    @Autowired
    private CommunityUserNotificationMapper communityUserNotificationMapper;

    /**
     * 获取通知权限
     * @param userId
     * @return 获取通知权限
     */
    @Override
    public List<CommunityUserNotificationVo> selectUserNotification(Long userId) {
        if (Objects.isNull(userId)) {
            userId = SecurityUtils.getLoginUser().getUserId();
        }
        // 查询自己的通知权限
        List<CommunityUserNotification> communityUserNotifications
                = communityUserNotificationMapper
                .selectList(new QueryWrapper<CommunityUserNotification>().eq("user_id", userId));
        // 创建一个用于存放VO的列表
        List<CommunityUserNotificationVo> communityUserNotificationVos = new ArrayList<>();

        // 遍历查询结果，复制属性到 VO 对象
        for (CommunityUserNotification communityUserNotification : communityUserNotifications) {
            CommunityUserNotificationVo communityUserNotificationVo = new CommunityUserNotificationVo();
            BeanUtils.copyProperties(communityUserNotification, communityUserNotificationVo);
            communityUserNotificationVos.add(communityUserNotificationVo);
        }

        // 判断通知权限是否为空
        if (communityUserNotificationVos.isEmpty()) {
            // 如果没有通知权限，插入一条关于 userId 的数据
            CommunityUserNotification newNotification = new CommunityUserNotification();
            newNotification.setUserId(userId);
            newNotification.setIsCircle(false);
            newNotification.setIsLetter(false);
            newNotification.setIsComment(false);
            newNotification.setIsAddAttention(false);
            newNotification.setIsPraiseCollection(false);
            // 设置其他必要的默认值，例如：
            // 使用 MyBatis-Plus 执行插入操作
            communityUserNotificationMapper.insert(newNotification);
            // 将新插入的记录转换为 VO 并返回
            CommunityUserNotificationVo newNotificationVo = new CommunityUserNotificationVo();
            BeanUtils.copyProperties(newNotification, newNotificationVo);
            communityUserNotificationVos.add(newNotificationVo);
        }

        return communityUserNotificationVos;
    }


    /**
     * 更新通知权限
     * @param communityUserNotification
     * @return 返回修改成功的对象
     */
    public List<CommunityUserNotificationVo> modifyUserNotification(CommunityUserNotification communityUserNotification) {
        //获取userId
        Long userId = SecurityUtils.getUserId();
        // 创建更新包装器
        UpdateWrapper<CommunityUserNotification> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);  // 根据 userId 条件
        // 更新字段
        if (communityUserNotification.getIsPraiseCollection() != null) {
            updateWrapper.set("is_praise_collection", communityUserNotification.getIsPraiseCollection());
        }
        if (communityUserNotification.getIsAddAttention() != null) {
            updateWrapper.set("is_add_attention", communityUserNotification.getIsAddAttention());
        }
        if (communityUserNotification.getIsComment() != null) {
            updateWrapper.set("is_comment", communityUserNotification.getIsComment());
        }
        if (communityUserNotification.getIsCircle() != null) {
            updateWrapper.set("is_circle", communityUserNotification.getIsCircle());
        }
        if (communityUserNotification.getIsLetter() != null) {
            updateWrapper.set("is_letter", communityUserNotification.getIsLetter());
        }
        // 执行更新操作
        boolean updated = this.update(updateWrapper);
        if (!updated){
            System.out.println("更新失败");
        }
        // 创建一个用于存放VO的列表
        List<CommunityUserNotificationVo> communityUserNotificationVos = new ArrayList<>();
        CommunityUserNotification updatedUserNotification = this.getOne(new UpdateWrapper<CommunityUserNotification>().eq("user_id", userId));
        // 将新插入的记录转换为 VO 并返回
        CommunityUserNotificationVo newNotificationVo = new CommunityUserNotificationVo();
        BeanUtils.copyProperties(updatedUserNotification, newNotificationVo);
        communityUserNotificationVos.add(newNotificationVo);

        return communityUserNotificationVos;
    }
}
