package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.WeChat;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/wechat")
public class WeChatController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private SysPermissionService permissionService;
    /**
     * 处理微信小程序登录请求
     *
     * @param code 微信小程序登录时获取的 code
     * @return 包含 openid 和  JSON 响应
     */
    @GetMapping("/login")
    public AjaxResult login(@RequestParam("code") String code) {
        Map<String, String> result=null;
        try {
            result=WeChat.getOpenIdAndSessionKey(code);
        } catch (IOException e) {
            throw new RuntimeException("获取 openid 和 session_key 失败", e);
        }

        String openId=result.get("openid");

        //通过openId判断是否绑定用户，若以绑定，返回token，直接登录，若无，则返回提示请绑定手机号
        SysUser sysUser=new SysUser();
        sysUser.setOpenId(openId);
        List<SysUser> lst= userService.selectUserList(sysUser);
        Map<String,String> map=new HashMap<>();
        if(lst!=null&&lst.size()>0){
            //登录并返回token
            SysUser user=lst.get(0);
            String token = loginService.wxlogin(user.getUserName(),"123456");
            map.put("openId",openId);
            map.put("token",token);
            return new AjaxResult(200,"登录成功",map);
        }else{
            map.put("openId",openId);
            map.put("token",null);
            return new AjaxResult(200,"当前用户未绑定手机号，请绑定",map);
        }

    }

    //绑定手机号并登录
    @GetMapping("/bindPhone")
    public AjaxResult bindPhone(@RequestParam("openId") String openId,@RequestParam("phone") String phone) {
        Map<String,String> map=new HashMap<>();
        //通过手机号查询是否有用户
        SysUser sysUser=new SysUser();
        sysUser.setPhonenumber(phone);
        List<SysUser> lst= userService.selectUserList(sysUser);
        if(lst!=null&&lst.size()>0){
            //绑定手机号并登录返回token
            SysUser user=lst.get(0);
            user.setOpenId(openId);
            userService.updateUserOpenId(user);

            String token = loginService.wxlogin(user.getUserName(),"123456");

            map.put("openId",openId);
            map.put("token",token);
        }else{
            return new AjaxResult(500,"当前手机号系统不存在，请确定手机号是否正确或联系管理员",openId);
        }
        return new AjaxResult(200,"登陆成功",map);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        Map<String,Object> map=new HashMap<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        map.put("user", user);
        map.put("roles", roles);
        return new AjaxResult(200,"获取成功",map);
    }
}