package com.sptg.permissions.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.permissions.api.entity.Conf;
import com.sptg.permissions.api.entity.WeiXinConf;
import com.sptg.permissions.biz.mapper.WeiXinConfMapper;
import com.sptg.permissions.biz.service.ConfServiceInterface;
import com.sptg.permissions.biz.service.WeiXinConfInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * title: WeiXinConfServiceImpl
 * package: com.sptg.permissions.biz.security.impl
 * description:
 *
 *
 *
 */
@Service
@RequiredArgsConstructor
public class WeiXinConfServiceImpl extends ServiceImpl<WeiXinConfMapper, WeiXinConf> implements WeiXinConfInterface {

    /**
     * 菜单缓存名称
     */
    private static final String WEI_XIN_CONF_CACHE_NAME = "sptg:permissions:weiXinConf";

    private final RedisTemplate<String,String> redisTemplate;

    @Override
    public WeiXinConf selectEntityByOrgIdAndName(WeiXinConf weiXinConf) {
//          查询缓存
        HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
        Long orgId = weiXinConf.getOrgId();
        String name = weiXinConf.getName();
        String hashKey = orgId + "-" + name;
        String json = hashOperations.get(WEI_XIN_CONF_CACHE_NAME,hashKey);
        if (StringUtils.isEmpty(json)) {
            Wrapper<WeiXinConf> wrapper = new EntityWrapper<>();
            wrapper.eq("l_org_id", orgId);
            wrapper.eq("s_name", name);
            WeiXinConf conf = this.selectOne(wrapper);
            if (null != conf) {
                //  保存到缓存中
                json = JsonUtils.objectToJson(conf);
                hashOperations.put(WEI_XIN_CONF_CACHE_NAME,hashKey,json);
            }
            return conf;
        }
        return JsonUtils.jsonToPojo(json,WeiXinConf.class);
    }

//    @Override
//    public WeiXinConf selectEntityById(Long orgId) {
//        //  查询缓存
//        HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
//        String hashKey = orgId.toString();
//        String json = hashOperations.get(WEI_XIN_CONF_CACHE_NAME,hashKey);
//        if (StringUtils.isEmpty(json)) {
//            WeiXinConf conf = this.selectById(orgId);
//            if (null != conf) {
//                //  保存到缓存中
//                json = JsonUtils.objectToJson(conf);
//                hashOperations.put(WEI_XIN_CONF_CACHE_NAME,hashKey,json);
//            }
//            return conf;
//        }
//        return JsonUtils.jsonToPojo(json,WeiXinConf.class);
//    }
}
