package com.sckw.payment.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.payment.dao.KwpWalletRelationMapper;
import com.sckw.payment.model.KwpWalletRelation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */
@Service
@Slf4j
public class KwpWalletRelationService {
    @Resource
    private KwpWalletRelationMapper relationMapper;
    private final ConcurrentHashMap<Long, String> map = new ConcurrentHashMap<>(12);

    public String getRelation(Long entId) {
        String uid = map.get(entId);
        if (StringUtils.isNotBlank(uid)) {
            log.debug("从缓存中加载企业用户关系：{}", JSONObject.toJSONString(map));
            return uid;
        }
        KwpWalletRelation kwpWalletRelation = relationMapper.selectOne(new LambdaQueryWrapper<KwpWalletRelation>().eq(KwpWalletRelation::getEntId, entId).eq(KwpWalletRelation::getDelFlag, 0).last("limit 1"));
        if (Objects.isNull(kwpWalletRelation)) {
            return null;
        }
        map.put(entId, kwpWalletRelation.getUid());
        return kwpWalletRelation.getUid();
    }


}
