package com.zhongzheng.controller.wx;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zhongzheng.common.constant.Constants;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.domain.entity.SysMenu;
import com.zhongzheng.common.core.domain.entity.SysUser;
import com.zhongzheng.common.core.domain.model.LoginBody;
import com.zhongzheng.common.core.domain.model.LoginUser;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.web.service.*;
import com.zhongzheng.modules.system.service.ISysMenuService;
import com.zhongzheng.modules.user.entity.ClientLoginUser;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.vo.UserVo;
import com.zhongzheng.modules.wx.bo.WxLoginBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags ="微信登录用户操作管理")
@ApiSupport(order = 2)
@RestController
public class WxLoginController
{
    @Autowired
    private WxLoginService wxLoginService;

    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private  IUserService iUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("测试登录")
    @PostMapping("/testLogin")
    public AjaxResult testLogin(@RequestBody WxLoginBody loginBody)
    {
        Map<String,String> map = wxLoginService.test_login();
        return AjaxResult.success(map);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody WxLoginBody loginBody)
    {
        Map<String,String> map = wxLoginService.login(loginBody);
        return AjaxResult.success(map);
    }

    /**
     * 公众号登录方法
     *
     * @param loginBody 登公众号录信息
     * @return 结果
     */
    @ApiOperation("公众号登录")
    @PostMapping("/gzh_login")
    public AjaxResult gzh_login(@RequestBody WxLoginBody loginBody)
    {
        Map<String,String> map = wxLoginService.gzh_login(loginBody);
        return AjaxResult.success(map);
    }

    @ApiOperation("刷新令牌")
    @GetMapping("/refreshToken/{unionId}")
    public AjaxResult refreshToken(@PathVariable("unionId") String unionId)
    {
        String token = wxLoginService.refreshToken(unionId);
        Map<String,Object> map = new HashMap<>();
        map.put(Constants.TOKEN, token);
        return AjaxResult.success(map);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("登录用户信息")
    @GetMapping("getInfo")
    public AjaxResult<UserVo> getInfo()
    {
        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        UserVo vo = iUserService.queryById(loginUser.getUser().getUserId());
        vo.setNull();
        return AjaxResult.success(vo);
    }

}
