package com.zcxk.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.Dto.MessageStatic;
import com.zcxk.Dto.RequsetMessageQuery;
import com.zcxk.client.UserCenterClient;
import com.zcxk.dao.MessageMapper;
import com.zcxk.entity.Message;
import com.zcxk.model.LoginUser;
import com.zcxk.service.MessageService;
import com.zcxk.util.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * (Message表)服务实现类
 *
 * @author makejava
 * @since 2020-11-12 10:53:25
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserCenterClient userCenterClient;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Message selectById(Integer id) {
        return this.messageMapper.selectById(id);
    }


    /**
     * 查询所有
     *
     * @return 实例对象的集合
     */
    @Override
    public List<Message> selectAll() {
        return this.messageMapper.selectAll();
    }

    /**
     * 根据条件查询
     *
     * @return 实例对象的集合
     */
    @Override
    public List<Message> selectList(Message message) {
        return this.messageMapper.selectList(message);
    }

    /**
     * 新增数据
     *
     * @param message 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Message message) {
        return this.messageMapper.insert(message);
    }

    /**
     * 批量新增
     *
     * @param messages 实例对象的集合
     * @return 生效的条数
     */
    @Override
    public int batchInsert(List<Message> messages) {
        return this.messageMapper.batchInsert(messages);
    }

    /**
     * 修改数据
     *
     * @param message 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Message message) {

        return this.messageMapper.update(message);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Integer id) {
        return this.messageMapper.deleteById(id);
    }

    /**
     * 查询总数据数
     *
     * @return 数据总数
     */
    @Override
    public int count() {
        return this.messageMapper.count();
    }

    /**
     * 分页查询
     *
     * @return 对象列表
     */
    @Override
    public IPage<Message> selectPage(Message message, IPage<Message> page) {
        message.setUserId(UserUtil.getCurrentUser().getId());
        IPage<Message> messageIPage = this.messageMapper.selectPage(page, message);
        messageIPage.getRecords().forEach(newMessage->{
            if(StringUtils.isNoneEmpty(newMessage.getPath())&&
                    StringUtils.isNoneEmpty(newMessage.getUrl())){
                newMessage.setUrl(newMessage.getPath()+newMessage.getUrl());
            }

        });
        return messageIPage;
    }

    @Override
    public int updateMultiple(RequsetMessageQuery requsetMessageQuery) {
        requsetMessageQuery.setUserId(UserUtil.getCurrentUser().getId());
        if(requsetMessageQuery.getIds().size()==0){
           requsetMessageQuery.setIds(null);

        }

        messageMapper.updateMultiple(requsetMessageQuery);
        return 0;
    }

    @Override
    public List<MessageStatic> queryUnreadMessageStatic() {
        LoginUser currentUser = UserUtil.getCurrentUser();
        List<MessageStatic> messageStatics = messageMapper.queryUnreadMessageStatic(currentUser.getId());
        List<String>appIds=currentUser.getAppIds();
        if(appIds!=null&&appIds.size()>0){

            List<MessageStatic> userMessageTypeByApp =
                    messageMapper.getUserMessageTypeByApp(appIds);
            Map<Integer,MessageStatic>staticMap=new HashMap<>();
            messageStatics.forEach(messageStatic -> {
                staticMap.put(messageStatic.getTypeId(),messageStatic);
            });

            userMessageTypeByApp.forEach(messageType->{
                MessageStatic messageStatic = staticMap.get(messageType.getTypeId());
                if(messageStatic!=null){
                    messageType.setCn(messageStatic.getCn());
                }

            });
            messageStatics=userMessageTypeByApp;
        }

        return messageStatics;
    }

    @Override
    public List<Message> queryLastMessage(int num, Integer type) {
       ;

        return messageMapper.queryLastMessage(num, UserUtil.getCurrentUser().getId(),type);
    }

    @Override
    public int judgingTheMessageJumpPermission(String url, String appId) {
        int result=-1;
        Map<String,String> urls = userCenterClient.getUserMenuByUid(appId);
        if(urls.containsKey(url)){
            result=0;
        }

        return result;
    }
}