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.material.MaterialInfo;
import com.mortals.xhx.protocol.complex.matter.model.rsp.matter.MatterWorkInfo;
import com.mortals.xhx.protocol.complex.matter.service.IComplexMatterService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

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 ImplementSituationSendTask implements Runnable {

    private ImplementationReq implementationReq;

    private String deviceLogo;

    private IComplexMatterService complexMatterService;

    private ICacheService cacheService;


    public ImplementSituationSendTask(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("ImplementSituationSendTask启动发送");
            Rest<List<MaterialInfo>> rest = complexMatterService.getMaterialByEventAndSituation(deviceLogo, implementationReq);
            cacheService.lpushForTime(KEY_EVENT_IMPLEMENTATION_RESP + implementationReq.getNonce(), REDIS_RESP_TIMEOUT, JSONObject.toJSONString(rest));

        } catch (Exception e) {
            log.error("发送异常：" + e);
        }


    }

}