package com.mortals.xhx.protocol.complex;


import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.xhx.protocol.complex.matter.model.req.DeviceLoginReq;
import com.mortals.xhx.protocol.complex.matter.model.rsp.LoginRspInfo;
import com.mortals.xhx.protocol.complex.matter.service.IComplexMatterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public abstract class AbstractComplexService implements IComplexMatterService {

    //综窗访问地址
    @Value("${complex.host:}")
    protected String host;

    //综窗设备编码 多个逗号分割
    @Value("${complex.deviceCode:''}")
    protected String deviceCode;

    //综窗接件token  deviceCode--对应tokenMap
    public static Map<String, String> tokenMap=new HashMap<>();

    @Override
    public String getToken(String deviceLogo) {
        try {

            String deviceToken = tokenMap.getOrDefault(deviceLogo, "");
            if (ObjectUtils.isEmpty(deviceToken)) {
                DeviceLoginReq loginReq = new DeviceLoginReq();
                loginReq.setDeviceLogo(deviceLogo);
                Rest<LoginRspInfo> loginRspInfoRest = this.deviceLogin(loginReq);
                return loginRspInfoRest.getData().getToken();
            } else {
                return deviceToken;
            }
        } catch (Exception e) {
            log.error("获取TOKEN异常", e);
            throw new AppException(e.getMessage());
        }
    }

}
