package com.xinelu.applet.utils;

import com.xinelu.common.config.AppletChatConfig;
import com.xinelu.common.config.NurseAppletChatConfig;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.entity.AppletAccessToken;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.AppletChatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Description 获取小程序AccessToken公共方法
 * @Author 纪寒
 * @Date 2023-02-27 18:02:05
 * @Version 1.0
 */
@Component
public class AppletAccessTokenUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private AppletChatConfig appletChatConfig;
    @Resource
    private NurseAppletChatConfig nurseAppletChatConfig;

    /**
     * 返回成功状态码
     */
    private static final int SUCCESS_CODE = 0;

    /**
     * 获取小程序AccessToken方法
     *
     * @return 小程序的AccessToken
     */
    public String getAppletAccessToken() {
        String accessToken;
        String accessTokenKey = Constants.NURSE_STATION_APPLET_ACCESS_TOKEN + "accessToken";
        //从Redis中取出accessToken
        Object object = redisTemplate.opsForValue().get(accessTokenKey);
        if (Objects.isNull(object)) {
            //没有，获取accessToken
            AppletAccessToken appletAccessToken = AppletChatUtil.getAppletAccessToken(appletChatConfig.getAppletId(), appletChatConfig.getSecret());
            if (Objects.isNull(appletAccessToken)) {
                throw new ServiceException("获取微信小程序accessToken信息失败");
            }
            if (Objects.nonNull(appletAccessToken.getErrcode()) && appletAccessToken.getErrcode() != SUCCESS_CODE) {
                throw new ServiceException("获取微信小程序accessToken信息失败，失败信息为：" + appletAccessToken.getErrmsg(), 201);
            }
            if (StringUtils.isBlank(appletAccessToken.getAccessToken())) {
                throw new ServiceException("accessToken信息为空");
            }
            //存入Redis中
            redisTemplate.opsForValue().set(accessTokenKey, appletAccessToken.getAccessToken(), 3600, TimeUnit.SECONDS);
            accessToken = appletAccessToken.getAccessToken();
        } else {
            accessToken = (String) object;
        }
        return accessToken;
    }

    /**
     * 护理员小程序获取小程序AccessToken方法
     *
     * @return 小程序的AccessToken
     */
    public String getPersonAppletAccessToken() {
        String accessToken;
        String accessTokenKey = Constants.NURSE_STATION_PERSON_APPLET_ACCESS_TOKEN + "accessToken";
        //从Redis中取出accessToken
        Object object = redisTemplate.opsForValue().get(accessTokenKey);
        if (Objects.isNull(object)) {
            //没有，获取accessToken
            AppletAccessToken appletAccessToken = AppletChatUtil.getAppletAccessToken(nurseAppletChatConfig.getAppletId(), nurseAppletChatConfig.getSecret());
            if (Objects.isNull(appletAccessToken)) {
                throw new ServiceException("获取微信小程序accessToken信息失败");
            }
            if (Objects.nonNull(appletAccessToken.getErrcode()) && appletAccessToken.getErrcode() != SUCCESS_CODE) {
                throw new ServiceException("获取微信小程序accessToken信息失败，失败信息为：" + appletAccessToken.getErrmsg(), 201);
            }
            if (StringUtils.isBlank(appletAccessToken.getAccessToken())) {
                throw new ServiceException("accessToken信息为空");
            }
            //存入Redis中
            redisTemplate.opsForValue().set(accessTokenKey, appletAccessToken.getAccessToken(), 3600, TimeUnit.SECONDS);
            accessToken = appletAccessToken.getAccessToken();
        } else {
            accessToken = (String) object;
        }
        return accessToken;
    }

}
