
package org.tio.sitexxx.im.server.handler.wx.friend;

import org.tio.sitexxx.im.server.handler.wx.WxChatApi;
import org.tio.core.ChannelContext;
import org.tio.jfinal.kit.Ret;
import org.tio.sitexxx.service.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.wx.friend.WxFriendMsgReq;
import org.tio.sitexxx.im.common.bs.wx.friend.WxFriendMsgResp;
import org.tio.sitexxx.im.server.Ims;
import org.tio.sitexxx.im.server.handler.AbsImServerHandler;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.model.main.WxFriendMsg;
import org.tio.sitexxx.service.service.chat.ChatMsgService;
import org.tio.sitexxx.service.service.chat.FriendService;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.sitexxx.service.utils.RetUtils;
import org.tio.sitexxx.service.vo.AppCode;
import org.tio.sitexxx.service.vo.SimpleUser;
import org.tio.utils.json.Json;

import java.util.List;

/**
 * 朋友间的聊天消息请求，wx_friend_msg-- Client-->Server
 * 
 * @author xufei
 * 2020年3月10日 下午3:14:44
 */
@CommandHandler(Command.WxFriendMsgReq)
public class WxFriendMsgReqHandler extends AbsImServerHandler {
	public static final WxFriendMsgReqHandler me = new WxFriendMsgReqHandler();

	/**
	 * 消息获取三种
	 * 1、起始消息为空，起始页
	 * 2、有开始消息，中间页
	 * 3、有结束页，同步断层数据
	 */
	@Override
	public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket, User curr, SimpleUser currSimpleUser) throws Exception {
		WxFriendMsgReq msgReq = Json.toBean(packet.getBodyStr(), WxFriendMsgReq.class);
		Long chatlinkid = msgReq.getChatlinkid();
		Long startmid = msgReq.getStartmid();
		Long endmid = msgReq.getEndmid();
		Long tomid = msgReq.getTomid();
		Ret ret = ChatMsgService.me.p2pMsgList(chatlinkid, curr.getId(), startmid, endmid, tomid);
		if (ret.isFail()) {
			WxChatApi.sendFriendErrorMsg(channelContext, curr.getId(), curr.getId(), chatlinkid, AppCode.FriendErrorCode.SYS_ERROR, RetUtils.getRetMsg(ret));
			return;
		}
		List<WxFriendMsg> data = RetUtils.getOkTData(ret);
		WxFriendMsgResp wxFriendMsgResp = new WxFriendMsgResp();
		wxFriendMsgResp.setData(data);
		wxFriendMsgResp.setChatlinkid(chatlinkid);
		if (data.size() < ConfService.getInt("im.history.chat.count.p2p", FriendService.WX_FRIEND_MSG_LIMIT)) {
			wxFriendMsgResp.setLastPage(true);
		}
		if (data.size() <= 0 && startmid == null && endmid == null) {
			System.out.println("WxFriendMsgReqHandler chatId:"+chatlinkid+", curr:"+curr.getId()+", body:"+packet.getBodyStr());
		}
		ImPacket imPacket = new ImPacket(Command.WxFriendMsgResp, wxFriendMsgResp);
		Ims.send(channelContext, imPacket);
	}
}
