package com.qmth.themis.exam.websocketTemplete;

import com.alibaba.fastjson.JSONObject;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.WebsocketDto;
import com.qmth.themis.business.enums.WebsocketTypeEnum;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Objects;

/**
 * @Description: 考生端mobile websocket模版
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/24
 */
@Component
public class WebSocketMobileMessageTemplete {
    private final static Logger log = LoggerFactory.getLogger(WebSocketMobileMessageTemplete.class);

    /**
     * 状态同步
     *
     * @param body
     * @param time
     * @return
     */
    public WebsocketDto syncStatus(String body, Long time) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        log.info("syncStatus jsonObject:{}", jsonObject.toJSONString());
        if (Objects.isNull(jsonObject.get(SystemConstant.RECORD_ID)) || Objects.equals(jsonObject.get(SystemConstant.RECORD_ID), "")) {
            throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
        }
        return this.syncAck(body, time);
    }

    /**
     * 同步确认
     *
     * @param body
     * @param time
     * @return
     */
    public WebsocketDto syncAck(String body, Long time) {
        return new WebsocketDto(WebsocketTypeEnum.convertToName(Thread.currentThread().getStackTrace()[1].getMethodName()), Collections.singletonMap(SystemConstant.ACK_MESSAGE, time));
    }
}
