package com.fjhx.victoriatourist.service.jd.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fjhx.victoriatourist.entity.jd.vo.JdInfoVo;
import com.fjhx.victoriatourist.service.jd.JdInfoService;
import com.fjhx.victoriatourist.service.jd.constants.JdParamConstant;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class JdClientFactory {
    @Resource
    JdInfoService jdInfoService;
    private JdClient jdClient = null;


    public JdClient getJdClient() {
        if(ObjectUtil.isNull(jdClient)){
            JdInfoVo jdInfoVo = jdInfoService.getOne();
            if(StrUtil.isBlank(jdInfoVo.getAccessToken())){
                throw new RuntimeException("京东accessToken为空");
            }
            jdClient = new DefaultJdClient(JdParamConstant.SERVER_URL, jdInfoVo.getAccessToken(), JdParamConstant.appKey, JdParamConstant.appSecret);
        }
        return jdClient;
    }
}