package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.test.domain.Message;
import com.ruoyi.test.domain.Vo.MessageVo;
import com.ruoyi.test.mapper.MessageMapper;
import com.ruoyi.test.seriver.IMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public int addMessage(MessageVo messageVo) {
        Message message = new Message();
        BeanUtils.copyProperties(messageVo,message);
        String teahcerName = iSysUserService.selectUserById(message.getTeacherId()).getUserName();
        String source = "教师"+teahcerName+"提醒你请尽快完成"+messageVo.getName();
        message.setSource(source);
        return messageMapper.addMessage(message);
    }

    @Override
    public List<Message> selectById(Long id) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getStudentId, id) // 按照学生 ID 查询
                    .orderBy(true, true, Message::getTf)  // 按照 tf 字段的值进行降序排列
                    .orderByDesc(Message::getCreatetime); // 如果 tf 相同，再按照 createtime 字段降序排序

        return messageMapper.selectList(queryWrapper);
    }

    @Override
    public Integer updateMessage(Long id) {
        return messageMapper.updateMessage(id);
    }


}
