package com.ruoyi.web.userLogin.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.userLogin.service.IWechatUserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @version : V1.0
 * @ClassName: LoginController
 * @Description: 用户登录
 * @Auther: JiangYuQi
 * @Date: 2020/7/7 18:03
 */
@RestController
@RequestMapping("/wechat")
public class WechatUserLoginController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IWechatUserLoginService wechatUserLoginService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), null,
                null);
        ajax.put(Constants.TOKEN, token);
        ajax.put("data",wechatUserLoginService.findUserInfo(loginBody.getUsername(),loginBody.getOpenId()));
        return ajax;
    }


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/openIdLogin")
    public AjaxResult openIdLogin(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, "");
        Map<String,Object> data = wechatUserLoginService.getLoginUserByOpenId(loginBody.getOpenId());
        if(data!=null){
            ajax.put("data",data);
            return ajax;
        }else {
            return error();
        }
    }

    /**
     * 查询出openid
     * @param code
     * @param appId
     * @return
     */
    @GetMapping("/getOpenId")
    public AjaxResult getOpenId(String code,String appId){
        return success(wechatUserLoginService.getOpenId(code,appId));
    }

    /**
     * 根据用户查询菜单信息
     * @param username
     * @return
     */
    @GetMapping("/selectRoleMenuList")
    public AjaxResult selectRoleMenuList(String username){
        return success(wechatUserLoginService.selectRoleMenuList(username));
    }

    /**
     *  查询登录用户的组织架构
     * @param map
     * @return
     */
    @RequestMapping("/selectDeptAll")
    public AjaxResult selectDeptAll(@RequestParam Map<String,Object> map){
        return success(wechatUserLoginService.getDeptAll(map));
    }

    /**
     * @Description: 修改密码
     * @Param: [map]
     * @return: java.lang.Object
     * @Author: wxw
     * @Date: 2020/7/7 19:15
     */
    @RequestMapping(value = "updatePassword",method = RequestMethod.POST)
    public Object updatePassword(@RequestParam Map<String,Object> map){
        return wechatUserLoginService.updataUser(map);
    }

}
