package com.hfkj.service.impl;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsMerKeyMapper;
import com.hfkj.service.BsMerKeyService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @className: BsMerKeyServiceImpl
 * @author: HuRui
 * @date: 2023/5/23
 **/
@Service("merKeyService")
public class BsMerKeyServiceImpl implements BsMerKeyService {

    @Resource
    private BsMerKeyMapper merKeyMapper;

    @Override
    public Map<String, Object> getDetailByMerNo(String merNo) {
        Map<String, Object> key = merKeyMapper.queryKeyByMerNo(merNo);
        if (key == null) {
            throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_KEY_ERR, "");
        }
        return key;
    }

    @Override
    @Cached(cacheType = CacheType.REMOTE, name="MER_KEY:", key = "#merNo")
    public String getKeyByMerNo(String merNo) {
        return MapUtils.getString(getDetailByMerNo(merNo), "merKey");
    }
}
