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.domain.Community.CommunityUserPrivacy;
import com.ruoyi.generator.mapper.community.CommunityUserNotificationMapper;
import com.ruoyi.generator.mapper.community.CommunityUserPrivacyMapper;
import com.ruoyi.generator.vo.CommunityUserNotificationVo;
import com.ruoyi.generator.vo.CommunityUserPrivacyVo;
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 CommunityPrivacyServiceImpl extends ServiceImpl<CommunityUserPrivacyMapper, CommunityUserPrivacy> implements ICommunityPrivacyService  {


    @Autowired
    private CommunityUserPrivacyMapper communityUserPrivacyMapper;

    /**
     * 获取隐私权限
     * @param userId
     * @return 获取隐私权限
     */
    @Override
    public List<CommunityUserPrivacyVo> selectUserPrivacy(Long userId) {
        if (Objects.isNull(userId)) {
            userId = SecurityUtils.getLoginUser().getUserId();
        }
        // 查询自己的隐私权限
        List<CommunityUserPrivacy> communityUserPrivacies
                = communityUserPrivacyMapper
                .selectList(new QueryWrapper<CommunityUserPrivacy>().eq("user_id", userId));
        // 创建一个用于存放VO的列表
        List<CommunityUserPrivacyVo> communityUserPrivacyVos = new ArrayList<>();

        // 遍历查询结果，复制属性到 VO 对象
        for (CommunityUserPrivacy communityUserPrivacy : communityUserPrivacies) {
            CommunityUserPrivacyVo communityUserPrivacyVo = new CommunityUserPrivacyVo();
            BeanUtils.copyProperties(communityUserPrivacy, communityUserPrivacyVo);
            communityUserPrivacyVos.add(communityUserPrivacyVo);
        }

        // 判断隐私权限是否为空
        if (communityUserPrivacyVos.isEmpty()) {
            // 如果没有隐私权限，插入一条关于 userId 的数据
            CommunityUserPrivacy newPrivacy = new CommunityUserPrivacy();
            newPrivacy.setUserId(userId);
            newPrivacy.setIsComment(false);
            newPrivacy.setIsCircle(false);
            newPrivacy.setIsWorks(false);
            newPrivacy.setIsCollection(false);
            newPrivacy.setIsLike(false);
            newPrivacy.setIsCompilation(false);
            newPrivacy.setIsAccounts(false);
            newPrivacy.setIsNames(false);
            // 设置其他必要的默认值，例如：
            // 使用 MyBatis-Plus 执行插入操作
            communityUserPrivacyMapper.insert(newPrivacy);
            // 将新插入的记录转换为 VO 并返回
            CommunityUserPrivacyVo newPrivacyVo = new CommunityUserPrivacyVo();
            BeanUtils.copyProperties(newPrivacy, newPrivacyVo);
            communityUserPrivacyVos.add(newPrivacyVo);
        }

        return communityUserPrivacyVos;
    }

    /**
     * 更新隐私权限
     * @param communityUserPrivacy
     * @return 返回修改成功的对象
     */
    @Override
    public List<CommunityUserPrivacyVo> modifyUserPrivacy(CommunityUserPrivacy communityUserPrivacy) {
        // 获取 userId
        Long userId = SecurityUtils.getUserId();

        // 创建更新包装器
        UpdateWrapper<CommunityUserPrivacy> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId); // 根据 userId 条件

        // 更新字段
        if (communityUserPrivacy.getIsComment() != null) {
            updateWrapper.set("is_comment", communityUserPrivacy.getIsComment());
        }
        if (communityUserPrivacy.getIsCircle() != null) {
            updateWrapper.set("is_circle", communityUserPrivacy.getIsCircle());
        }
        if (communityUserPrivacy.getIsWorks() != null) {
            updateWrapper.set("is_works", communityUserPrivacy.getIsWorks());
        }
        if (communityUserPrivacy.getIsCollection() != null) {
            updateWrapper.set("is_collection", communityUserPrivacy.getIsCollection());
        }
        if (communityUserPrivacy.getIsLike() != null) {
            updateWrapper.set("is_like", communityUserPrivacy.getIsLike());
        }
        if (communityUserPrivacy.getIsCompilation() != null) {
            updateWrapper.set("is_compilation", communityUserPrivacy.getIsCompilation());
        }
        if (communityUserPrivacy.getIsAccounts() != null) {
            updateWrapper.set("is_accounts", communityUserPrivacy.getIsAccounts());
        }
        if (communityUserPrivacy.getIsNames() != null) {
            updateWrapper.set("is_names", communityUserPrivacy.getIsNames());
        }

        // 执行更新操作
        boolean updated = this.update(updateWrapper);
        if (!updated) {
            System.out.println("更新失败");
        }

        // 创建一个用于存放VO的列表
        List<CommunityUserPrivacyVo> communityUserPrivacyVos = new ArrayList<>();
        CommunityUserPrivacy updatedUserPrivacy = this.getOne(new UpdateWrapper<CommunityUserPrivacy>().eq("user_id", userId));

        // 将新插入的记录转换为 VO 并返回
        CommunityUserPrivacyVo newPrivacyVo = new CommunityUserPrivacyVo();
        BeanUtils.copyProperties(updatedUserPrivacy, newPrivacyVo);
        communityUserPrivacyVos.add(newPrivacyVo);

        return communityUserPrivacyVos;
    }
}
