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

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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
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 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 (null == thirdPartInfoVos) {
            thirdPartInfoVos = mongoTemplate.find(Query.query(Criteria.where("uid").is(uid).and("state").is(1)),
                    AdamThirdPartInfoVo.class, AdamThirdPartInfoVo.class.getSimpleName());

            redisUtil.hset(AdamRedisConst.INFO_THIRD_PARTY, uid, thirdPartInfoVos);
        }
        return thirdPartInfoVos;
    }

    @Override
    public AdamThirdPartInfoVo queryByOpenId(String openId, String platform) {
        return mongoTemplate.findOne(Query.query(Criteria.where("openId").is(openId)
                        .and("platform").is(platform).and("state").is(1)),
                AdamThirdPartInfoVo.class, AdamThirdPartInfoVo.class.getSimpleName());
    }
}
