package org.dromara.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.main.domain.MainMessage;
import org.dromara.main.domain.MainPosition;
import org.dromara.main.domain.bo.MainMessageBo;
import org.dromara.main.domain.vo.MainMessageVo;
import org.dromara.main.domain.vo.MainPositionVo;
import org.dromara.main.mapper.MainMessageMapper;
import org.dromara.main.mapper.MainPositionMapper;
import org.dromara.main.service.IMainMessageService;
import org.dromara.system.domain.SysTenant;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.mapper.SysTenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

import static io.smallrye.common.function.FunctionsLogging.log;

/**
 * @author chenYing
 * @date 2026-04-03 14:07:16
 **/

@Service
@RequiredArgsConstructor
@Slf4j

public class MainMessageServiceImpl implements IMainMessageService {

    private final MainMessageMapper baseMapper;
    private final MainPositionMapper positionMapper;
    private final SysTenantMapper tenantMapper;

    @Override
    public List<MainMessageVo> queryList(MainMessageBo bo) {
        LambdaQueryWrapper<MainMessage> lqw = new LambdaQueryWrapper<>();
        // 基础过滤：学生ID
        lqw.eq(bo.getStudentId() != null, MainMessage::getStudentId, bo.getStudentId());
        // 【修复】增加对已读/未读状态的过滤支持
        lqw.eq(bo.getIsRead() != null, MainMessage::getIsRead, bo.getIsRead());
        // 按时间倒序
        lqw.orderByDesc(MainMessage::getCreateTime);
        List<MainMessageVo> message=baseMapper.selectVoList(lqw);

        for (MainMessageVo mainMessageVo : message) {
            if (mainMessageVo.getPositionId() != null) {
                MainPositionVo position = positionMapper.selectVoById(mainMessageVo.getPositionId());
                if (position != null && position.getTenantId() != null) {
                    SysTenant tenant = tenantMapper.selectOne(new LambdaQueryWrapper<SysTenant>()
                            .eq(SysTenant::getTenantId, position.getTenantId()));
                    if (tenant != null) {
                        mainMessageVo.setCompanyName(tenant.getCompanyName());
                    }
                }
            }
        }
        return message;
    }


    @Override
    public MainMessageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public Boolean updateByBo(MainMessageBo bo) {
        MainMessage message=BeanUtil.toBean(bo,MainMessage.class);
        return baseMapper.updateById(message)>0;
    }

    @Override
    public Boolean insertByBo(MainMessageBo bo){
        MainMessage message=BeanUtil.toBean(bo,MainMessage.class);
        return baseMapper.insert(message)>0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteBatchIds(ids)>0;
    }

    @Override
    public Long selectCount(MainMessageBo bo) {
        LambdaQueryWrapper<MainMessage> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getStudentId() != null, MainMessage::getStudentId, bo.getStudentId());
        lqw.eq(bo.getIsRead() != null, MainMessage::getIsRead, bo.getIsRead());
        return baseMapper.selectCount(lqw);
    }
    @Override
    public Boolean markAllAsRead(Long studentId) {
        // 将该学生所有 isRead=0 的记录改为 1
        return baseMapper.update(null,
            new LambdaUpdateWrapper<MainMessage>()
                .eq(MainMessage::getStudentId, studentId)
                .eq(MainMessage::getIsRead, 0)
                .set(MainMessage::getIsRead, 1)
        ) > 0;
    }

    @Override
    public Boolean markRead(Long id) {
        return baseMapper.update(null,
            new LambdaUpdateWrapper<MainMessage>()
                .eq(MainMessage::getId, id)
                .set(MainMessage::getIsRead, 1)
        ) > 0;
    }


}
