package com.ruoyi.system.service.impl;

import com.google.gson.Gson;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.wxMessage.WeChatMessageSender;
import com.ruoyi.common.wxMessage.WxMpPropertiesConfig;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.WxMsgService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.common.constant.UserConstants.PWD;

@Slf4j
@Service
public class WxMsgServiceImpl implements WxMsgService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    @SneakyThrows
    @Override
    public SysUser creatUser(String code) {
        //获取code
        log.info("获取到微信授权后：code:{}", code);
        //获取用户openid
        Map<String, Object> wxUserInfo = WeChatMessageSender.getWxUserInfo(code);
        String openid = String.valueOf(wxUserInfo.get("openid"));
        if("null".equals(openid)){
            throw new ServiceException("授权码错误，请重新授权");
        }
        //将数据存入redis
        redisCache.setCacheObject(openid, wxUserInfo);
        //查询openid是否在数据库
        SysUser sysUser = userMapper.selectUserByOpenId(openid);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setOpenId(openid);
        }
        return sysUser;
    }

    @Override
    public SysUser gzhLogin(String openId, String phone) {
        //查询账号是否在数据库
        SysUser sysUser = userMapper.selectUserByUserName(phone);
        if (sysUser == null) {
            //创建账号
            //从redis中取出数据
            Map<String, Object> wxUserInfo = redisCache.getCacheObject(openId);
            sysUser = new SysUser();
            sysUser.setUserName(phone);
            sysUser.setPhonenumber(phone);
            sysUser.setNickName(String.valueOf(wxUserInfo.get("nickname")));
            sysUser.setAvatar(String.valueOf(wxUserInfo.get("headimgurl")));
            sysUser.setOpenId(openId);
            sysUser.setPassword(SecurityUtils.encryptPassword(PWD));
            Long[] roleIds = new Long[1];
            roleIds[0] = 102L;
            sysUser.setRoleIds(roleIds);
            userService.insertUser(sysUser);
        }
        return sysUser;
    }
}
