package com.liquidnet.service.adam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.service.adam.constant.AdamRedisConst;
import com.liquidnet.service.adam.dto.vo.AdamThirdPartInfoVo;
import com.liquidnet.service.adam.entity.AdamThirdParty;
import com.liquidnet.service.adam.mapper.AdamThirdPartyMapper;
import com.liquidnet.service.adam.service.IAdamThirdPartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 用户第三方账号信息 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-05-10
 */
@Service
public class AdamThirdPartyServiceImpl extends ServiceImpl<AdamThirdPartyMapper, AdamThirdParty> implements IAdamThirdPartyService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<AdamThirdPartInfoVo> queryByUid(String uid) {
        List<AdamThirdPartInfoVo> thirdPartInfoVos = (List<AdamThirdPartInfoVo>) redisUtil.hget(AdamRedisConst.INFO_THIRD_PARTY, uid);
        if (CollectionUtils.isEmpty(thirdPartInfoVos)) {
            thirdPartInfoVos = mongoTemplate.find(Query.query(Criteria.where("uid").is(uid).and("state").is(1)),
                    AdamThirdPartInfoVo.class);

            if (!CollectionUtils.isEmpty(thirdPartInfoVos)) redisUtil.hset(AdamRedisConst.INFO_THIRD_PARTY, uid, thirdPartInfoVos);
        }
        return thirdPartInfoVos;
    }

    public AdamThirdPartInfoVo queryByUidAndPlatform(String uid, String platform) {
        List<AdamThirdPartInfoVo> voList = this.queryByUid(uid);
        if (!CollectionUtils.isEmpty(voList)) {
            for (AdamThirdPartInfoVo vo : voList) {
                if (platform.equals(vo.getPlatform())) {
                    return vo;
                }
            }
        }
        return null;
    }
}
