package com.mortals.xhx.thread;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.busiz.req.accept.ComplexAcceptReq;
import com.mortals.xhx.common.utils.AbstractThread;
import com.mortals.xhx.protocol.complex.matter.model.req.ImplementationReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import static com.mortals.xhx.common.key.RedisKey.KEY_COMPLEX_REQ;


/**
 * 发送线程
 *
 * @author zxfei
 */
@Slf4j
@Service
@Order(value = 1)
public class SendThread extends AbstractThread {

    @Autowired
    private ICacheService cacheService;

    @Value("${complex.deviceCode:''}")
    protected String deviceCode;

    @Override
    protected int getSleepTime() {
        return 100;
    }

    @Override
    protected void process() throws Exception {
        // log.info("send thread process");
        String reqStr = cacheService.lpop(KEY_COMPLEX_REQ, String.class);
        if (!ObjectUtils.isEmpty(reqStr)) {
            JSONObject reqJson = JSONObject.parseObject(reqStr);
            String urlPath = reqJson.getString("urlPath");
            //根据url路由断言匹配发送 后续通过动态路由分发
            if ("eventImplementation/list".equals(urlPath)) {
                //查询事项
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                ImplementListSendTask implementListSendTask = new ImplementListSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementListSendTask);
            } else if ("event-implementation/get-by-id".equals(urlPath)) {
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                ImplementDetailSendTask implementSendTask = new ImplementDetailSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementSendTask);
            } else if ("bus-situation-material/getSituationMaterialTreeByEventIds".equals(urlPath)) {
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                ImplementSituationSendTask implementSendTask = new ImplementSituationSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementSendTask);
            } else if ("consulting-service/getMaterialByEventAndSituation".equals(urlPath)) {
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                EventAndSituationSendTask implementSendTask = new EventAndSituationSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementSendTask);
            } else if ("material/findList".equals(urlPath)) {
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                ImplementMaterialSendTask implementSendTask = new ImplementMaterialSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementSendTask);
            } else if ("event-implementation/getDynamicFormByEventIds".equals(urlPath)) {
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                ImplementDynamicFormSendTask implementSendTask = new ImplementDynamicFormSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementSendTask);
            } else if ("common/queryDict".equals(urlPath)) {
                ImplementationReq implementationReq = JSONObject.parseObject(reqStr, ImplementationReq.class);
                DictSendTask implementSendTask = new DictSendTask(deviceCode, implementationReq);
                ThreadPool.getInstance().execute(implementSendTask);
            } else if ("self-device-info/acceptHandling".equals(urlPath)) {
                ComplexAcceptReq complexAcceptReq = JSONObject.parseObject(reqStr, ComplexAcceptReq.class);
                ImplementAcceptSendTask acceptSendTask = new ImplementAcceptSendTask(deviceCode, complexAcceptReq);
                ThreadPool.getInstance().execute(acceptSendTask);
            }


        }

    }


    @Override
    protected void threadClosed() {
        // TODO Auto-generated method stub
    }


}
