package bus.service.impl;

import bus.model.SnowflakeUtil;
import bus.model.dto.BNoticeInfoDto;
import bus.model.dto.BNoticeUserDto;
import bus.model.dto.BNoticeUserPageDto;
import bus.model.po.BNoticeInfoPo;
import bus.model.vo.BNoticeUserVo;
import bus.model.po.BNoticeUserPo;
import bus.mapper.BNoticeUserMapper;
import bus.mapper.BNoticeInfoMapper;

import bus.service.BNoticeUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcas.common.sso.model.ExtInfo;
import com.orcas.iso.config.common.user.CurrentUserHolder;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.ArrayList;

/**
 * @Program: bus
 * @Description: 描述
 * @Author: zy
 * @Date: 2025-03-10 22:38:55
 **/
@Service
public class BNoticeUserServiceImpl extends ServiceImpl<BNoticeUserMapper,BNoticeUserPo> implements BNoticeUserService {

    @Autowired
    private BNoticeUserMapper bNoticeUserMapper;
    @Autowired
    private SnowflakeUtil snowflakeUtil;
    @Autowired
    private BNoticeInfoMapper bNoticeInfoMapper;


    @Override
    public void addNoticeUser(BNoticeUserDto dto) {
        ExtInfo extInfo = CurrentUserHolder.get();
        String userId = extInfo.getUserId();
        QueryWrapper<BNoticeUserPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("notice_id", dto.getNoticeId());
        if (bNoticeUserMapper.selectList(queryWrapper) != null) {
            return;
        }
        BNoticeUserPo po = new BNoticeUserPo();
        po.setId(snowflakeUtil.snowflakeId());
        po.setUserId(userId);
        po.setNoticeId(dto.getNoticeId());
        save(po);
    }

    @Override
    public void addNoticeUserAll() {
        // 获取当前用户信息
        ExtInfo extInfo = CurrentUserHolder.get();
        String userId = extInfo.getUserId();
        
        // 获取所有需要添加的通知用户列表
        List<BNoticeUserPo> noticeUserList = bNoticeUserMapper.selectList(null);
        
        // 批量添加通知用户
        for (BNoticeUserPo noticeUser : noticeUserList) {
            // 检查用户是否已经添加过该通知
            BNoticeUserPo existPo = bNoticeUserMapper.selectOne(
                new QueryWrapper<BNoticeUserPo>()
                    .eq("user_id", userId)
                    .eq("notice_id", noticeUser.getNoticeId())
            );
            
            // 如果不存在，则添加
            if (existPo == null) {
                BNoticeUserPo po = new BNoticeUserPo();
                po.setId(snowflakeUtil.snowflakeId());
                po.setUserId(userId);
                po.setNoticeId(noticeUser.getNoticeId());
                save(po);
            }
        }
    }

    @Override
    public List<BNoticeInfoDto> getNoticeInfoList() {
        // 获取当前用户信息
        ExtInfo extInfo = CurrentUserHolder.get();
        String userId = extInfo.getUserId();
        // 使用SQL直接查询带有已读状态的公告列表
        return bNoticeInfoMapper.getNoticeInfoList(userId);
    }
}
