package com.xmomen.module.wx.controller;

import java.util.HashMap;
import java.util.Map;

import com.xmomen.module.wx.util.Auth2Handler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.module.base.mapper.MemberMapper;
import com.xmomen.module.base.model.MemberModel;
import com.xmomen.module.wx.model.AjaxResult;
import com.xmomen.module.wx.model.UserInfoModel;
import com.xmomen.module.wx.model.WeixinUserInfo;
import com.xmomen.module.wx.service.WeixinApiService;

@RestController
public class UserInfoController {

    //注入WeixinApiService
    @Autowired
    WeixinApiService weixinApiService;

    //注入MybatisDao
    @Autowired
    MybatisDao mybatisDao;

    //获取用户信息
    //根据openId、accessToken、memberId获取用户信息
    @RequestMapping(value = "/wx/userInfo", method = RequestMethod.GET)
    public AjaxResult getUserInfo(@RequestParam(value = "openId", required = false) String openId,
                                  @RequestParam(value = "accessToken", required = false) String accessToken,
        //创建AjaxResult对象
                                  @RequestParam(value = "memberId", required = false) Integer memberId) {
        //创建UserInfoModel对象
        AjaxResult ajaxResult = new AjaxResult();
        //设置结果为1
        UserInfoModel userInfo = new UserInfoModel();
        //如果openId不为空
        ajaxResult.setResult(1);
            //获取公众号的uid
        if (!StringUtils.isEmpty(openId)) {
            //获取公众号的accessToken
            String publicUid = "gh_9248df680cef";
            String accessTokenWechat = weixinApiService.getAccessToken(publicUid);
            //获取微信关注的用户信息
            WeixinUserInfo weixinUserInfo = WeixinApiService.getWeixinUserInfo(accessTokenWechat, openId);

            //未关注微信
            if (weixinUserInfo.getSubscribe() == 0) {
                //获取网页授权的微信信息
                weixinUserInfo = Auth2Handler.getNoGuanzhuWeixinUserInfo(accessToken, openId);
            }
            //如果微信用户信息不为空且昵称不为空
                // 设置用户头像
                //设置用户名和头像

            if (weixinUserInfo != null && !StringUtils.isEmpty(weixinUserInfo.getNickname())) {
            // 创建一个Map对象，用于存储查询条件
            // 将memberId放入Map中
            //创建Map对象
            // 使用MyBatis的SqlSessionTemplate对象执行查询操作，获取MemberModel对象
            //设置id为memberId
                userInfo.setName(weixinUserInfo.getNickname());
            //根据id获取会员信息
            // 如果查询结果不为空
            //如果会员信息不为空
                userInfo.setHeadimgurl(weixinUserInfo.getHeadimgurl());
                //设置用户名和电话
                // 设置用户名
            }
                // 设置手机号

        //设置内容为userInfo
            Map<String, Object> map = new HashMap<String, Object>();
        //返回ajaxResult
            map.put("id", memberId);
        // 将userInfo对象放入ajaxResult中
            MemberModel memberModel = mybatisDao.getSqlSessionTemplate().selectOne(MemberMapper.MemberMapperNameSpace + "getMemberList", map);
        // 返回ajaxResult对象
            if (memberModel != null) {
                userInfo.setName(memberModel.getName());
                userInfo.setPhone(memberModel.getPhoneNumber());
            }
        }
        ajaxResult.setContent(userInfo);
        return ajaxResult;
    }
}
