
package com.ms.biz.common;

import com.jinritemai.cloud.base.api.BaseRequest;
import com.jinritemai.cloud.base.core.util.AuthThreadLocalUtil;
import com.ms.biz.bo.ShopBO;
import com.ms.biz.service.Shop1688DsAuthService;
import com.ms.biz.service.ShopService;
import com.ms.biz.tool.SystemTool;
import com.ms.dal.entity.Shop;
import com.ms.dal.entity.Shop1688DsAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * SPI基础类
 */
@Slf4j
public class SPIBaseService {
    @Autowired
    private Shop1688DsAuthService shop1688DsAuthService;

    @Autowired
    private ShopService shopService;

    protected Long shopId;
    protected ShopBO shopInfo;
    protected String authCode;

    public void initHandle(BaseRequest req) {
        shopId = Long.valueOf(req.getAuthId());

        // 本地开发环境
        if(SystemTool.isDevEnv()){
            AuthThreadLocalUtil.set(req.getAuthId());
        }

        // TODO 后面可以redis缓存，避免每次都查库
        Shop shop = shopService.getDetailById(shopId);
        shopInfo = shopService.getShopInfoByShop(shop);
    }
    public String getAuthCode() {
        Shop1688DsAuth shop1688DsAuth = shop1688DsAuthService.getDetailByShopId(shopId);
        if (shop1688DsAuth != null) {
            authCode = shop1688DsAuth.getAuthCode();
            log.info(authCode);
            return authCode;
        } else {
            return null;
        }
    }
}
