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

import org.tio.core.ChannelContext;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.server.utils.ImUtils;
import org.tio.sitexxx.service.Command;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.model.main.UserActionLog;
import org.tio.sitexxx.service.vo.SimpleUser;
import org.tio.utils.json.Json;

/**
 * 
 * @author tanyaowu 
 * 2016年12月29日 下午10:16:51
 */
@CommandHandler(Command.UserActionLogReq)
public class UserActionLogReqHandler implements ImServerHandler {
	@Override
	public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket) throws Exception {
		User curr = ImUtils.getUser(channelContext);

		//path actiontype
		UserActionLog userActionLog = Json.toBean(packet.getBodyStr(), UserActionLog.class);
		userActionLog.setActiondesc(UserActionLog.Action.getName(userActionLog.getActiontype()));
		userActionLog.setInitpath(userActionLog.getPath()); //先这么做
		userActionLog.setSession(ImUtils.getTokenByToken(channelContext));

		if (curr != null) {
			userActionLog.setMayuid(curr.getId());
			userActionLog.setUid(curr.getId());
		} else {
			SimpleUser fromUser = ImUtils.getHandshakeSimpleUser(channelContext);

			if (fromUser != null) {
				if (fromUser.getMay() != null) {
					userActionLog.setMayuid(fromUser.getMay().getI());
				} else {
					userActionLog.setMayuid(fromUser.getI());
				}

				userActionLog.setUid(fromUser.getI());
			}
		}

		userActionLog.save();
	}
}
