package org.aming.web.qq.repository.jdbc.impl;

import org.aming.web.qq.domain.Message;
import org.aming.web.qq.domain.Page;
import org.aming.web.qq.domain.User;
import org.aming.web.qq.repository.jdbc.MessageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Type;
import java.sql.Types;
import java.util.List;

/**
 * @author daming
 * @version 2017/10/5.
 */
@Repository
public class MessageDaoImpl implements MessageDao {

    private JdbcTemplate jdbcTemplate;

    private static final String SQL_SAVE_MESSAGE = " INSERT INTO message (id,sendUserId,receiveUserId,content,sendDate,sendDeleteFlag,receiveDeleteFlag,create_id,creation_date,update_id,update_date) SELECT ?,A.id,B.id,?,?,?,?,'system',NOW(),'system',NOW() FROM `user` A  JOIN USER B ON  B.username = ? WHERE A.username = ? ";
    public int saveMessage(Message message) {
        Object[]  args = new Object[]{
                message.getId(),
                message.getContent(),
                new java.sql.Timestamp(message.getSendDate() != null ? message.getSendDate().getTime() : System.currentTimeMillis()),
                message.getSendDeleteFlag(),
                message.getReceiveDeleteFlag(),
                message.getReceiveUser().getUsername(),
                message.getSendUser().getUsername()
        };
        return jdbcTemplate.update(SQL_SAVE_MESSAGE,args);
    }

    private static final String SQL_GET_MESSAGE = "SELECT id,sendUserId,receiveUserId,content,sendDate,sendDeleteFlag,receiveDeleteFlag FROM message A  WHERE (A.sendUserId = ? AND A.receiveUserId = ?) OR (A.sendUserId = ? AND A.receiveUserId = ?) ORDER BY A.sendDate DESC LIMIT ?,?";
    public List<Message> getMessage(User sendUser, User receiveUser, Page page) {
        Object[] args = new Object[]{
                sendUser.getId(),
                receiveUser.getId(),
                receiveUser.getId(),
                sendUser.getId(),
                page.getCurrentPage(),
                page.getPageSize()
        };
        int[] argTypes = new int[]{
                Types.VARCHAR,
                Types.VARCHAR,
                Types.VARCHAR,
                Types.VARCHAR,
                Types.INTEGER,
                Types.INTEGER
        };
        return jdbcTemplate.query(
                SQL_GET_MESSAGE,
                args,
                argTypes,
                (rs, i) -> {
                    return new Message()
                            .setId(rs.getString("id"))
                            .setSendUser(sendUser)
                            .setReceiveUser(receiveUser)
                            .setContent(rs.getString("content"))
                            .setSendDate(rs.getTimestamp("sendDate"))
                            .setSendDeleteFlag(rs.getInt("sendDeleteFlag"))
                            .setReceiveDeleteFlag(rs.getInt("receiveDeleteFlag"));
                });
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}

@@ -41,16 +41,55 @@
 .impl;%0A%0A
+import com.google.common.collect.Maps;%0A
 import o
@@ -116,24 +116,24 @@
 in.Message;%0A
-
 import org.a
@@ -553,16 +553,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1987,24 +1987,168 @@
 age page) %7B%0A
+        Map%3CString,User%3E map = Maps.newHashMap();%0A        map.put(sendUser.getId(),sendUser);%0A        map.put(receiveUser.getId(),receiveUser);%0A
         Obje
@@ -2914,24 +2914,46 @@
 endUser(
+map.get(rs.getString(%22
 sendUser
 )%0A      
@@ -2940,24 +2940,29 @@
 ng(%22sendUser
+Id%22))
 )%0A          
@@ -2995,16 +2995,38 @@
 iveUser(
+map.get(rs.getString(%22
 receiveU
@@ -3028,16 +3028,21 @@
 eiveUser
+Id%22))
 )%0A      

fix the bug about sendUser and receiveUser