package com.pj.project.app_user;

import com.pj.api.dto.AppUserDto;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.project.app_user.dto.ForgetPasswordDto;
import com.pj.project.app_user.dto.LoginDto;
import com.pj.project.app_user.dto.RegisterDto;
import com.pj.project.app_user.vo.AppUserVo;
import com.pj.utils.sg.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


/**
 * app_user -- 移动端账号(只针对app端使用)
 *
 * @author yzs
 */
@RestController
@RequestMapping("/app/AppUser/")
public class AppUserApiController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    AppUserService appUserService;


    /** 修改个人信息 */
    @RequestMapping("update")
    public AjaxJson update(AppUser a){
        appUserService.update(a);
        return AjaxJson.getSuccess();
    }

    /** 查看个人信息 */
    @RequestMapping("getSelfInfo")
    public AjaxJson getById(){
        AppUserVo a = appUserService.getSelfInfo();
        return AjaxJson.getSuccessData(a);
    }

    /** 获取认证状态 */
    @RequestMapping("getAuth")
    public AjaxJson getAuth(){
        AppUser user=appUserService.getById(StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        Map so=new HashMap();
        so.put("auth",user.getAuth());
        so.put("face",user.getFace());
        return AjaxJson.getSuccessData(so);
    }

    /** 边民首次使用APP人脸认证 */
    @PostMapping("face")
    public AjaxJson face(){
        AppUser user=new AppUser();
        user.setId(StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        user.setFace(1);
        user.setFaceTime(new Date());
        return AjaxJson.toAjax(appUserService.updateById(user));
    }


    /**
     * 用户注册
     */
    @PostMapping("register")
    public AjaxJson register(@Validated RegisterDto registerDto) {
        boolean register = appUserService.register(registerDto);
        if (!register) return AjaxJson.getError("注册失败!");
        return AjaxJson.getSuccess("注册完成!");
    }

    /**
     * 用户登录
     */
    @PostMapping("login")
    public AjaxJson login(@Validated LoginDto dto) {
        return appUserService.login(dto);
    }

    /**
     * 用户退出
     */
    @RequestMapping("logout")
    public AjaxJson logout(@RequestParam("appUserId") Object appUserId) {
        String logout = appUserService.logout(appUserId);
        return AjaxJson.getSuccessData(logout);
    }

    /**
     * 忘记密码
     */
    @RequestMapping("forgetPassword")
    public AjaxJson forgetPassword(@Validated @RequestBody ForgetPasswordDto forgetPasswordDto) {
        return appUserService.forgetPassword(forgetPasswordDto);
    }

    /**
     * 获取短信验证码
     */
    @RequestMapping("getPhoneSmsCode")
    public AjaxJson getPhoneSmsCode(String phone) throws Exception {
        boolean phoneSmsCode = appUserService.getPhoneSmsCode(phone);
        if (phoneSmsCode) return AjaxJson.getSuccess("验证码发送成功!");
        return AjaxJson.getError("验证码发送失败!");
    }

    /**
     * 修改密码
     */
    @RequestMapping("modifyPassword")
    public AjaxJson modifyPassword(@RequestParam("oldPassword") String olePassword,@RequestParam("newPassword")String newPassword,@RequestParam("rePassword")String rePassword) {
        boolean result = appUserService.modifyPassword(olePassword, newPassword,rePassword);
        if (result) return AjaxJson.getSuccess("密码修改成功!");
        return AjaxJson.getError("密码修改失败!");
    }
    /*-----               正常业务接口👆👆    不越界     rpc远程调用 👇👇                     -----*/
    /**
     * 收购商认证
     */
    @RequestMapping("rpc/getByPhoneAndUserType")
    public AppUserDto getByPhoneAndUserType(@RequestParam("phone") String phone,@RequestParam("userType")Integer userType) {
        AppUserDto byPhoneAndUserType = appUserService.getByPhoneAndUserType(phone, userType);
        return byPhoneAndUserType;
    }


}
