package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.system.message.MessageService;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static com.mortals.xhx.common.key.Constant.SEND_INTEVEL;

@Component
@Slf4j
public class DeviceSendThirdPartyService implements IApplicationStartedService {

    @Autowired
    private SendTaskThreadPool sendTaskThreadPool;

    protected Boolean stopped = false;
    @Autowired
    private ICacheService cacheService;

    @Autowired
    private MessageService messageService;


    @Value("${thirdPartyPath:/inter/device/deviceIn}")
    public String thirdPartyPath;

    @Override
    public void start() {
        log.info("初始化发送线程数量");
        //启动短信发送响应更新线程
        sendTaskThreadPool.execute(() -> {
            int waitTime = SEND_INTEVEL;
            while (!stopped) {
                try {
//                    DeviceReq deviceReq = cacheService.lpop(KEY_DEVICE_THIRDPARTY_QUEUE, DeviceReq.class);
//                    if (!ObjectUtils.isEmpty(deviceReq)) {
//                        String phpInUrl = GlobalSysInfo.getParamValue(PARAM_SERVER_PHP_IN_HTTP_URL, "http://172.15.28.116:8090");
//                        ApiResp<String> resp = messageService.sendThirdParty(UrlBuilder.of(phpInUrl).addPath(thirdPartyPath).build(), deviceReq);
//                        log.info("sendThirty resp ==>{}", JSON.toJSONString(resp));
//                    }

//                    try {
//                        Thread.sleep(waitTime);
//                    } catch (InterruptedException e2) {
//                    }

                } catch (Exception e) {
                    log.error("异常", e);

                    try {
                        Thread.sleep(waitTime);
                    } catch (InterruptedException e2) {
                    }
                }
            }
        });

    }

    @Override
    public void stop() {
        log.info("停止服务..");
        this.stopped = true;
    }

    @Override
    public int getOrder() {
        return 50;
    }

}
