package com.cm.central.control.service.socket.exception;

import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.manager.SocketServerManager;
import com.cm.central.control.pojo.bos.SocketClientBO;
import com.cm.central.control.pojo.vos.clientexception.ClientExceptionVO;
import com.cm.central.control.service.clientexception.IClientExceptionService;
import com.cm.central.control.service.socket.BaseSocketService;
import com.cm.socket.consts.ISocketConst;
import com.cm.socket.pojo.Message;
import com.cm.socket.pojo.SocketResult;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ExceptionService
 * @Description: 异常业务
 * @Author: WangGeng
 * @Date: 2020/7/28 2:49 下午
 * @Version: 1.0
 **/
@Service("socketExceptionServiceImpl")
public class SocketExceptionServiceImpl extends BaseSocketService {

    @Autowired
    private IClientExceptionService clientExceptionService;

    @Override
    protected void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage) throws Exception {
        SocketResult<JSONObject> socketResult = JSONObject.parseObject(getDecodeContent(readMessage.getContent()), SocketResult.class);
        SocketServerManager socketServerManager = SocketServerManager.getInstance();
        SocketClientBO socketClientBO = socketServerManager.getOnlineClientByToken(channelHandlerContext.channel().id().asShortText());
        ClientExceptionVO clientExceptionVO = new ClientExceptionVO();
        clientExceptionVO.setClientId(socketClientBO.getClientId());
        clientExceptionVO.setExceptionName(socketResult.getData().getString(ISocketConst.EXCEPTION_MESSAGE));
        clientExceptionVO.setExceptionSummary(socketResult.getData().getString(ISocketConst.EXCEPTION_CONTENT));
        clientExceptionVO.setExceptionType(socketResult.getData().getString(ISocketConst.EXCEPTION_CLASS));
        clientExceptionVO.setIsHandle(0);
        clientExceptionVO.setIsNoticeProjectLeader(0);
        clientExceptionService.saveClientExceptionReturnId(clientExceptionVO);
        clientExceptionService.updateClientExceptionAndNoticeProjectLeaderByProjectLeaderId(socketClientBO.getProjectLeaderId());
    }

    @Override
    public void autoReply(ChannelHandlerContext channelHandlerContext) {

    }
}
