package com.github.wxiaoqi.security.admin.rpc;

import com.ace.cache.annotation.Cache;
import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.admin.rpc.service.AppPermissionService;
import com.github.wxiaoqi.security.admin.rpc.service.PermissionService;
import com.github.wxiaoqi.security.api.vo.authority.PermissionInfo;
import com.github.wxiaoqi.security.api.vo.user.AppUserInfo;
import com.github.wxiaoqi.security.api.vo.user.UserInfo;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.util.process.SystemConfig;
import com.github.wxiaoqi.security.common.util.result.JsonResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author keliii
 */
@RestController
@RequestMapping("api/app")
public class AppUserRest {
    @Autowired
    private AppPermissionService appPermissionService;

//    @Cache(key="permission")
//    @RequestMapping(value = "/permissions", method = RequestMethod.GET)
//    public @ResponseBody
//    List<PermissionInfo> getAllPermission(){
//        return permissionService.getAllPermission();
//    }
//
    @Cache(key="app:permission:u{1}")
    @RequestMapping(value = "/user/un/{username}/permissions", method = RequestMethod.GET)
    public @ResponseBody List<PermissionInfo> getPermissionByUsername(@PathVariable("username") String username){
        return appPermissionService.getPermissionByUsername(username);
    }

    @RequestMapping(value = "/user/validate", method = RequestMethod.POST)
    public @ResponseBody
    AppUserInfo validate(@RequestBody Map<String,String> body){
        return appPermissionService.validate(body.get("username"),body.get("password"));
    }

    /**
     * 发送验证码
     * @param username
     * @param type 0-注册验证，1-找回密码验证，2&3-修改手机号
     * @return
     */
    @RequestMapping(value = "/user/sendsms", method = RequestMethod.POST)
    public @ResponseBody
    JSONObject sendsms(@RequestParam(value="username",defaultValue="")String  username, @RequestParam(value="type",defaultValue="0")Integer type){
        return appPermissionService.sendSMS(username,type);
    }

    /**
     * 注册
     * @param username
     * @param mobilecode
     * @param password
     * @return
     */
    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public @ResponseBody
    JSONObject register(
            @RequestParam(value="username",defaultValue="")String  username,
            @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
            @RequestParam(value="password",defaultValue="")String  password
    ){
        //默认昵称
        String nickname=SystemConfig.USER_NIKENAME_DEFAULT+(int)((Math.random()*9+1)*100000);
        return appPermissionService.register(username, password, SystemConfig.USER_HEADER_URL_DEFAULT,nickname,
                mobilecode, null, null,0);
    }


    /**
     *  微信注册/微信绑定
     * @param username
     * @param mobilecode
     * @param password
     * @param nickname
     * @param headimgurl
     * @param openid
     * @param unionid
     * @param type
     * @return
     */
    @RequestMapping(value = "/user/wxregister", method = RequestMethod.POST)
    public @ResponseBody
    JSONObject wxregister(
            @RequestParam(value="username",defaultValue="")String  username,
            @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
            @RequestParam(value="password",defaultValue="")String  password,
            @RequestParam(value="nickname",defaultValue="")String  nickname,
            @RequestParam(value="headimgurl",defaultValue="")String  headimgurl,
            @RequestParam(value="openid",defaultValue="")String  openid,
            @RequestParam(value="unionid",defaultValue="")String  unionid,
            @RequestParam(value="type",defaultValue="0")Integer  type,
            @RequestParam(value="isQQ",defaultValue="0")Integer  isQQ
    ){
        if(StringUtils.isBlank(headimgurl)){
            headimgurl=SystemConfig.USER_HEADER_URL_DEFAULT;
        }
        if(StringUtils.isBlank(nickname)){
             nickname=SystemConfig.USER_NIKENAME_DEFAULT+(int)((Math.random()*9+1)*100000);
        }
        return appPermissionService.weCahtRegister(username,password,openid, unionid,nickname,headimgurl,type,mobilecode,isQQ);
    }
    /**
     * 手机号码检测是否已绑定
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "/user/checkBindWechat")
    public @ResponseBody JSONObject checkBindWechat(@RequestParam(value="username",defaultValue="")String  username) {
        if (StringUtils.isBlank(username)) {
            return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
        }
        return appPermissionService.checkBindWechat(username);
    }


    @RequestMapping(value = "/user/wxlogin")
    public @ResponseBody JSONObject wxlogin(@RequestParam(value="openid",defaultValue="")String  openid,@RequestParam(value="isQQ",defaultValue="0")Integer isQQ) {
        return appPermissionService.weCahtLogin(openid,isQQ);
    }

    /**
     * 登录
     * @param username
     * @param mobilecode
     * @param password
     * @param type 1-账号密码;2-验证码
     * @return
     */
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public @ResponseBody
    JSONObject login(
            @RequestParam(value="username",defaultValue="")String  username,
            @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
            @RequestParam(value="password",defaultValue="")String  password,
            @RequestParam(value="type",defaultValue="1")Integer  type
    ){
        return appPermissionService.login(username,password,mobilecode,type);
    }

    @RequestMapping(value = "/user/reset", method = RequestMethod.POST)
    public @ResponseBody
    JSONObject login(
            @RequestParam(value="username",defaultValue="")String  username,
            @RequestParam(value="mobilecode",defaultValue="")String mobilecode,
            @RequestParam(value="password",defaultValue="")String  password
    ){
        return appPermissionService.reset(username,mobilecode,password);
    }

    @RequestMapping(value = "/user/authentication", method = RequestMethod.POST)
    public @ResponseBody ObjectRestResponse authentication(
            @RequestParam(value="userId",defaultValue="0")Integer  userId,
            @RequestParam(value="idNumber",defaultValue="")String idNumber,
            @RequestParam(value="status",defaultValue="0")Integer  status
    ){
        return appPermissionService.upAuthentication(userId,idNumber,status);
    }


}
