package com.mortals.xhx.thread;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.xhx.protocol.complex.matter.model.req.ImplementationReq;
import com.mortals.xhx.protocol.complex.matter.model.rsp.implement.ImplementDetailInfo;
import com.mortals.xhx.protocol.complex.matter.model.rsp.work.WorkInfo;
import com.mortals.xhx.protocol.complex.matter.service.IComplexMatterService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import static com.mortals.xhx.common.key.Constant.REDIS_RESP_TIMEOUT;
import static com.mortals.xhx.common.key.RedisKey.KEY_EVENT_IMPLEMENTATION_RESP;

@Slf4j
public class ImplementDynamicFormSendTask implements Runnable {

    private ImplementationReq implementationReq;

    private String deviceLogo;

    private IComplexMatterService complexMatterService;

    private ICacheService cacheService;


    public ImplementDynamicFormSendTask(String deviceLogo, ImplementationReq implementationReq) {
        this.implementationReq = implementationReq;
        this.deviceLogo = deviceLogo;
        complexMatterService = SpringUtils.getBean(IComplexMatterService.class);
        cacheService = SpringUtils.getBean(ICacheService.class);
    }

    @Override
    public void run() {
        try {
            log.info("ImplementDynamicFormSendTask启动发送");
            Rest<WorkInfo> rest = complexMatterService.getWorkMatter(deviceLogo, implementationReq.getEventIdList());
            cacheService.lpushForTime(KEY_EVENT_IMPLEMENTATION_RESP + implementationReq.getNonce(), REDIS_RESP_TIMEOUT, JSONObject.toJSONString(rest));
        } catch (Exception e) {
            log.error("发送异常：" + e);
        }


    }

}