package cn.superdesk.uniorder.dubbo.consumer.websocket;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzScheduleTaskStatementOfAccount;
import cn.superdesk.uniorder.dubbo.consumer.websocket.biz.dto.W;
import cn.superdesk.uniorder.dubbo.consumer.websocket.biz.dto.WrapperBodySysQuartzScheduleTaskStatementOfAccountRunTask;
import cn.superdesk.uniorder.dubbo.facade.websocket.DubboWebSocketService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Description: webSocket
 * @Author: linjinyu
 * @CreateDate: 2021/1/28 下午12:46
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/1/28 下午12:46
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class WebSocketDubboCustomerService {

    @Reference(version = "1.0.0", timeout = 10000, retries = -1)
    private DubboWebSocketService dubboWebSocketService;

    /****************************************** 基础方法 start ******************************************/

    public String sendGet(String token, String message) throws Exception {
//		log.info("\nWebSocketDubboCustomerService.sendGet \ntoken : {}\nmessage : {}", token, message);
        return dubboWebSocketService.sendGet(token, message);
    }

    public String sendPost(String token, String body) throws Exception {

//		log.info("\nWebSocketDubboCustomerService.sendPost \ntoken : {}\nbody : {}", token, body);

        String result = "";

        try {
            result = dubboWebSocketService.sendPost(token, body);
        } catch (java.lang.reflect.InvocationTargetException e) {
//			e.printStackTrace();
            // TODO 再优化
            log.error("\nWebSocketDubboCustomerService.sendPost 发送失败 \ntoken : {}\nbody : {}", token, body);
        }

        return result;
    }

    public String broadcast(String body) throws Exception {
//        log.info("\nWebSocketDubboCustomerService.broadcast \nmessage : {}", body);
        return dubboWebSocketService.broadcast(body);
    }

    public String getClientsNum() {
        return dubboWebSocketService.getClientsNum();
    }

    public String sendPostAnother(String token, String body) throws Exception {
        return dubboWebSocketService.sendPostAnother(token, body);
    }

    public String broadcastAnother(String body) throws Exception {
        return dubboWebSocketService.broadcastAnother(body);
    }

    /****************************************** 基础方法   end ******************************************/


    /****************************************** 业务方法 mark by linjinyu at 20210220 start ******************************************/


    public void processWrapperBodySysQuartzScheduleTaskStatementOfAccountRunTask(String identifier, SysQuartzScheduleTaskStatementOfAccount entity) {

        W<WrapperBodySysQuartzScheduleTaskStatementOfAccountRunTask> message = new W<>();

        message.setAction(WrapperBodySysQuartzScheduleTaskStatementOfAccountRunTask.ACTION);
        message.setIdentifier(identifier);
        message.setBody(WrapperBodySysQuartzScheduleTaskStatementOfAccountRunTask
                .builder()
                .sysQuartzScheduleTaskStatementOfAccount(entity)
                .build());

        try {
            sendPost(identifier, AnJsonUtils.toJson(message));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /****************************************** 业务方法 mark by linjinyu at 20210220 start ******************************************/
}
