package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunwang.education.entity.MpInnerMsg;
import com.xunwang.education.mapper.MpInnerMsgMapper;
import com.xunwang.education.service.MpInnerMsgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Long
 * @since 2023-03-05
 */
@Service
public class MpInnerMsgServiceImpl extends ServiceImpl<MpInnerMsgMapper, MpInnerMsg> implements MpInnerMsgService {

    @Override
    public HashMap<String, Object> selectMsgPage(String uid, String pageNum, String pageSize) {
        if (StringUtils.isAnyBlank(uid, pageNum, pageSize)) {
            throw new RuntimeException("请求参数为空");
        }
        Page<MpInnerMsg> msgPage = new Page<>(Long.parseLong(pageNum), Long.parseLong(pageSize));
        LambdaQueryWrapper<MpInnerMsg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MpInnerMsg::getUid, uid);
        queryWrapper.orderByDesc(MpInnerMsg::getDatetime);
        IPage<MpInnerMsg> msgIPage = baseMapper.selectPage(msgPage, queryWrapper);
        List<MpInnerMsg> list = msgIPage.getRecords();
        long total = msgIPage.getTotal();
        for (MpInnerMsg mpInnerMsg : list) {
            if ("未读".equals(mpInnerMsg.getStatus())) {
                mpInnerMsg.setPosition("left");
            } else if ("已读".equals(mpInnerMsg.getStatus())){
                mpInnerMsg.setPosition("none");
            }
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("list", list);
        hashMap.put("count", total);
        return hashMap;
    }
}
