package com.hotent.portal.service.impl;

import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageBean;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.SysMessage;
import com.hotent.portal.params.MessaboxVo;
import com.hotent.portal.persistence.manager.MessageReadManager;
import com.hotent.portal.persistence.manager.SysMessageManager;
import com.hotent.portal.service.MessageReceiverService;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.api.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageReceiverImpl implements MessageReceiverService {
    @Resource
    SysMessageManager sysMessageManager;
    @Resource
    IUserService iUserService;
    @Resource
    MessageReadManager messageReadManager;

    @Override
    public PageList<SysMessage> listJson(QueryFilter queryFilter) {
        queryFilter.addFilter("receiverId", ContextUtil.getCurrentUserId(), QueryOP.EQUAL);
        PageList<SysMessage> sysMessageList = sysMessageManager.getMsgByUserId(queryFilter);
        List<SysMessage> list = sysMessageList.getRows();
        PageList<SysMessage> newSysMessageList = new PageList<SysMessage>();
        //消息类型值得转换
//		for (int i = 0; i < list.size(); i++) {
//			SysMessage sysMessage = list.get(i);
//			String messageType= MessageTypeUtil.getValue(sysMessage.getMessageType());
//			sysMessage.setMessageType(messageType);
//			newSysMessageList.getRows().add(sysMessage);
//		}
        newSysMessageList.setRows(list);
        newSysMessageList.setPage(sysMessageList.getPage());
        newSysMessageList.setPageSize(sysMessageList.getPageSize());
        newSysMessageList.setTotal(sysMessageList.getTotal());
        return newSysMessageList;
    }

    @Override
    public PageList<SysMessage> listNoUser(QueryFilter queryFilter) {
        PageList<SysMessage> sysMessageList = sysMessageManager.getMsgListNoUser(queryFilter);
        List<SysMessage> list = sysMessageList.getRows();
        PageList<SysMessage> newSysMessageList = new PageList<SysMessage>();

        newSysMessageList.setRows(list);
        newSysMessageList.setPage(sysMessageList.getPage());
        newSysMessageList.setPageSize(sysMessageList.getPageSize());
        newSysMessageList.setTotal(sysMessageList.getTotal());
        return newSysMessageList;
    }

    @Override
    public MessaboxVo getMessBoxInfo(String account) {
        IUser user = iUserService.getUserByAccount(account);
        int messCount = 0;
        int noReadMessCount = 0;
        if(BeanUtils.isNotEmpty(user)){
            QueryFilter queryFilter = QueryFilter.build().withPage(new PageBean(1,PageBean.WITHOUT_PAGE));
            queryFilter.addFilter("receiverId", ContextUtil.getCurrentUserId(), QueryOP.EQUAL);
            PageList<SysMessage> sysMessageList = sysMessageManager.getMsgByUserId(queryFilter);
            messCount = (int)sysMessageList.getTotal();
            if(messCount>0){
                for (SysMessage message : sysMessageList.getRows()) {
                    if(BeanUtils.isEmpty(message.getReceiveTime())){
                        ++noReadMessCount;
                    }
                }
            }
        }
        return new MessaboxVo(messCount, noReadMessCount);
    }

    @Override
    public CommonResult<String> mark(String ids) {
        String[] aryIds = null;
        if(!StringUtil.isEmpty(ids)){
            aryIds = ids.split(",");
        }
        IUser user = ContextUtil.getCurrentUser();
        //保存到已读表
        for (String string : aryIds) {
            messageReadManager.addMessageRead(string, user);
        }
        return new CommonResult<String>(true,"已读成功","");
    }
}
