package com.ssj.dao.sys.schoolmatecircle.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ssj.dao.sys.schoolmatecircle.dao.SSJUserFriendQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class SSJUserFriendQueryDaoImpl implements SSJUserFriendQueryDao {
    @Autowired
    private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findFriends(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t.id,t.sex,t.user_name,t.photo,t.easemob_name,count(if(m.is_read=0,true,null)) as unreadnum FROM tb_user t ");
		selSql.append("LEFT JOIN ssj_user_message m ON m.send_user_id = t.id and m.receive_user_id = ? ");
		selSql.append("where t.id in (SELECT friend_user_id AS user_id FROM ssj_user_friend WHERE owner_user_id = ? ");
		selSql.append("UNION ALL ");
		selSql.append("SELECT owner_user_id AS user_id FROM ssj_user_friend WHERE friend_user_id = ?) ");
		selSql.append("GROUP BY t.id ORDER BY unreadnum desc");
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	@Override
	public List<Map<String, Object>> findParents(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.id,t.sex,v.parent_role,t.photo,t.easemob_name,count(if(m.is_read=0,true,null)) as unreadnum from tb_user t ");
		selSql.append("LEFT JOIN tb_lib_vip v ON t.id = v.user_id ");
		selSql.append("LEFT JOIN ssj_user_message m ON m.send_user_id = v.user_id and m.receive_user_id = v.child_user_id ");
		selSql.append("where v.child_user_id = ?");
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	@Override
	public List<Map<String, Object>> friendsApplyInfo(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.id,t.sex,t.user_name,t.photo,t.easemob_name,s.`status` from ssj_user_friend_apply s ");
		selSql.append("LEFT JOIN tb_user t ON s.owner_user_id = t.id ");
		selSql.append("where s.friend_user_id = ?");
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> historyFriendsApply(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.id,t.sex,t.user_name,t.photo,t.easemob_name,s.`status` from ssj_user_friend_apply s ");
		selSql.append("LEFT JOIN tb_user t ON s.friend_user_id = t.id ");
		selSql.append("where s.owner_user_id = ?");
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> searchFriends(String userId,String friendUserName) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select DISTINCT t.id,t.sex,t.user_name,t.photo,t.easemob_name,s.`status` from tb_user t ");
		selSql.append("LEFT JOIN ssj_user_friend_apply s ON ");
		selSql.append("(s.owner_user_id = ? and t.id = s.friend_user_id) or ");
		selSql.append("(s.friend_user_id = ? and t.id = s.owner_user_id) ");
		selSql.append("where t.id != ? and t.user_name like CONCAT('%', ?, '%')");
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(friendUserName);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findOtherParents(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.id,t.sex,p.role_name,t.photo,t.easemob_name,count(if(m.is_read=0,true,null)) as unreadnum from tb_user t ");
		selSql.append("LEFT JOIN ssj_parent_role p ON t.id = p.role_user_id ");
		selSql.append("LEFT JOIN tb_lib_vip v ON v.id = p.vip_id  ");
		selSql.append("LEFT JOIN ssj_user_message m ON m.send_user_id = p.role_user_id and m.receive_user_id = v.child_user_id ");
		selSql.append("where v.child_user_id = ? GROUP BY p.role_user_id");
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
}