package com.ichangzuo.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ichangzuo.system.mapper.MessageMapper;
import com.ichangzuo.system.model.entity.Message;
import com.ichangzuo.system.model.query.MessagePageQuery;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IService<Message> {
    public IPage<Message> listPage(MessagePageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Message> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper<Message>();
        Integer type = queryParams.getType();
        Long uid = queryParams.getUserId();
        if(type==0) {
            lqw.eq(Message::getType, 0);
            if(uid!=null && uid>0){
                lqw.lt(Message::getState, 2);
            }
        }
        else {
            if (uid == null || uid == 0) { //全部
                lqw.eq(Message::getType, 1);
            } else {
                lqw.eq(Message::getType, 1)
                    .eq(Message::getUserId, uid)
                    .lt(Message::getState, 2)
                    .orderByAsc(Message::getState);
            }
        }

        lqw.orderByDesc(Message::getMsgId);

        return baseMapper.selectPage(page, lqw);
    }

    public List<Message> getLatest(Long userId){
        //公告
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper<Message>()
                .eq(Message::getType, 0)
                .lt(Message::getState, 2)
                .orderByDesc(Message::getMsgId)
                .last("limit 5");
        List<Message> list = baseMapper.selectList(lqw);
        //未读消息
        lqw = new LambdaQueryWrapper<Message>()
                .eq(Message::getUserId, userId)
                .eq(Message::getType, 1)
                .eq(Message::getState, 0)
                .orderByDesc(Message::getMsgId)
                .last("limit 5");

        list.addAll(baseMapper.selectList(lqw));

        return list;
    }

    public boolean setByIds(String idsStr, Integer set){
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "消息号列表为空");
        List<Integer> ids = Arrays.stream(idsStr.split(","))
                .map(Integer::parseInt)
                .toList();
        Integer now = (int) (System.currentTimeMillis()/1000);
        boolean result = true;

        for(Integer id: ids){
            result = update(new LambdaUpdateWrapper<Message>()
                    .eq(Message::getMsgId, id)
                    .set(Message::getState, set)
                    .set(set==2 ? Message::getDeleteTime : Message::getReadTime, now));

            if(!result) break;
        }
        return result;
    }

    public void sendUserMessage(Long toId, String title, String content){
        Message msg = new Message();
        msg.setUserId(toId);
        msg.setTitle(title);
        msg.setContent(content);
        msg.setType(1);
        msg.setSendTime((int) (System.currentTimeMillis()/1000));

        save(msg);
    }

    public boolean saveMessage(Message message){
        Integer msgId = message.getMsgId();
        if(msgId==null){
            message.setSendTime((int) (System.currentTimeMillis()/1000));
            message.setState(0);
        }

        return saveOrUpdate(message);
    }
}
