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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

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

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

@Repository
public class SSJUserMessageQueryDaoImpl implements SSJUserMessageQueryDao {
    @Autowired
    private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findOnlineMessages(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
	 	List<Object> queryParams = new ArrayList<Object>();
	 	selSQL.append("SELECT m.send_user_id,m.type,m.content,m.file_key,m.duration,m.create_time,m.time_millis ");
        selSQL.append("FROM ssj_user_message m ");
        selSQL.append("where (m.send_user_id = ? and m.receive_user_id = ?) or (m.send_user_id = ? and m.receive_user_id = ?) ");
        selSQL.append("and m.create_time < ? ");
        selSQL.append("ORDER BY m.create_time desc,m.num desc");
        queryParams.add(searchParams.get("sendUserId"));
        queryParams.add(searchParams.get("receiveUserId"));
        queryParams.add(searchParams.get("receiveUserId"));
        queryParams.add(searchParams.get("sendUserId"));
        queryParams.add(searchParams.get("maxCreateTime"));
        return dao.findPage(selSQL.toString(), queryParams.toArray(),pageable);
	}
}
