package com.emergency.avenue.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.avenue.admin.api.entity.SysUserMsg;
import com.emergency.avenue.admin.mapper.SysUserMsgMapper;
import com.emergency.avenue.admin.service.SysUserMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 <script type="text/javascript" src="http://cdn.bootcss.com/sockjs-client/1.1.1/sockjs.min.js"></script>
 <script type="text/javascript" src="http://cdn.bootcss.com/stomp.js/2.3.3/stomp.js"></script>
 <script type="text/javascript" src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

 <script type="text/javascript">
 let stompClient = null;
let socket = new SockJS('http://localhost:9999/admin/act/ws');
		stompClient = Stomp.over(socket);
		stompClient.connect({'user-name':'212'}, function(frame) {
		console.log(socket._server);

		stompClient.subscribe('/user/'+socket._server+'/queue/chat', function(chat) {
		console.log(chat.body);
		});

		stompClient.subscribe('/queue/chat', function(chat) {
		console.log(chat.body);
		});
		});

		function sendMsg() {
		stompClient.send("/app/chat", {}, "{'a':'hello'}");
		}

		$(function() {
		$("#send").click(function() {
		sendMsg();
		});
		});
</script>
</body>
</html>
 */
@Service
public class SysUserMsgServiceImpl extends ServiceImpl<SysUserMsgMapper,SysUserMsg> implements SysUserMsgService {

	@Autowired
	public SimpMessagingTemplate template;

	public IPage<SysUserMsg> getPmUserMsg(Page page,SysUserMsg userMsg){
		return baseMapper.getPmUserMsg(page,userMsg);
	}
	public void addOneUserMsg(String createuser,Long touserid,String  msgContent){
		SysUserMsg u=new SysUserMsg();
		u.setCreateTime(LocalDateTime.now());
		u.setCreateBy(createuser);
		u.setMsgContent(msgContent);
		u.setUserId(touserid);
		u.setReadstate(0);
		save(u);
		//通知
		//template.convertAndSend( "/queue/chat","hello" );
		///user/{id}
		template.convertAndSendToUser(touserid.toString(),"/queue/chat",u );
	}


}
