package com.dgtis.dorm.manager.db.service;

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

import javax.annotation.Resource;

import com.dgtis.dorm.manager.db.dao.AlumniFriUserMapper;
import com.dgtis.dorm.manager.db.dao.AlumniUserMessageMapper;
import com.dgtis.dorm.manager.db.domain.AlumniFriUser;
import com.dgtis.dorm.manager.db.domain.AlumniUserMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserMessageService {

	@Resource
	private AlumniUserMessageMapper alumniUserMessageMapper;
	@Autowired
	private AlumniFriUserMapper alumniFriUserMapper;

	/**
	 * 查询发送给我的消息
	 * @param page
	 * @param size
	 * @param sort
	 * @param order
	 * @return
	 */
	public Map<String, Object> findUserMessageList(String userId, Integer page,
			Integer size, String sort, String order) {
		PageHelper.startPage(page, size);
		AlumniUserMessage message = new AlumniUserMessage();
		if(!StringUtils.isEmpty(userId)){
			message.setFkUserId(userId);
        }
        List<Map> list = alumniUserMessageMapper.selectUserMessageAll(message);
        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("items",list);
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());
		return resMap;
	}

	public int insert(AlumniUserMessage record) {
		return alumniUserMessageMapper.insertSelective(record);
	}

	/**
	 * 查询发送给我的私密消息
	 * @param message
	 * @param page
	 * @param size
	 * @return
	 */
	public Map<String, Object> findUserMessage(AlumniUserMessage message, Integer page, Integer size) {
		PageHelper.startPage(page, size);
		List<Map> list = alumniUserMessageMapper.selectUserMessage(message);
		PageInfo pageinfo = new PageInfo(list);
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("items",list);
        resMap.put("total", pageinfo.getTotal());
		return resMap;
	}

	public Map<String, Object> findSystemUserMessageByUserId(String userId, Integer page, Integer size) {
		PageHelper.startPage(page, size);
		List<Map> list = alumniUserMessageMapper.selectSystemUserMessageByUserId(userId);
		PageInfo pageinfo = new PageInfo(list);
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("items",list);
        resMap.put("total", pageinfo.getTotal());
		return resMap;
	}

	public Map<String, Object> findUserMessageDetails(String userId, String fromUserId) {
		alumniUserMessageMapper.updateUserMessageRead(userId, fromUserId);
		List<Map> list = alumniUserMessageMapper.selectUserMessageDetails(userId, fromUserId);
		AlumniFriUser friUser = alumniFriUserMapper.selectFriUser(Integer.valueOf(userId), Integer.valueOf(fromUserId));
		Map<String,Object> resMap = new HashMap<String,Object>();
		if(friUser == null) {
			resMap.put("isAttention", 0);
		} else {
			resMap.put("isAttention", 1);
		}
		resMap.put("items",list);
		return resMap;
	}

	public Map<String, Object> findMySystemMessageDetails(String id) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		AlumniUserMessage message = alumniUserMessageMapper.selectByPrimaryKey(Integer.valueOf(id));
		if(message != null) {
			message.setIsRead(true);
			alumniUserMessageMapper.updateByPrimaryKeySelective(message);
			resMap.put("item", message);
			resMap.put("state", "success");
			resMap.put("massege", "查询成功");
		} else {
			resMap.put("state", "failuren");
			resMap.put("massege", "查询失败");
		}
		return resMap;
	}

	/**
	 * 查询私密消息未读数量
	 * @param userId
	 * @return
	 */
	public Map<String, Object> findMyPrivateMsgCount(String userId) {
		Map<String,Object> resMap = new HashMap<String,Object>();
		//通知消息未读数量
		Integer informCount = alumniUserMessageMapper.findMyMsgCount(userId);
		//私密消息未读数量
		Integer noReadCount = alumniUserMessageMapper.selectPrivateNoReadCount(userId);
		//申请消息待审核数量
		Integer NoNormalCount = alumniUserMessageMapper.selectNoNormalCountByAOU(userId);
		resMap.put("informCount",informCount);
		resMap.put("privateNoReadCount",noReadCount);
		resMap.put("NoNormalCount",NoNormalCount);
		resMap.put("noReadCount", noReadCount+NoNormalCount+informCount);
		return resMap;
	}

	public boolean updateMsg(String id) {
		int flag = alumniUserMessageMapper.updateMsg(id);
		return flag > 0 ? true:false;
	}
}
