package com.pj.project.app_user;

import cn.hutool.core.util.StrUtil;
import com.pj.api.consts.FeignFactory;
import com.pj.api.dto.AppUserDto;
import com.pj.api.dto.PeopleDto;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.enummj.DeleteStatus;
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 com.pj.utils.so.SoMap;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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


/**
 * app_user -- 移动端账号(只针对app端使用)
 *
 * @author yzs
 */
@RestController
@RequestMapping("/app/AppUser/")
@Slf4j
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() {
        Map so = new HashMap();
        AppUser user = appUserService.getById(StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        if (user.getUserType() == 1) {
            PeopleDto peopleDto = FeignFactory.levelOneServerInterface.getRpcById(user.getFkId());
            so.put("groupId", peopleDto.getGroupId());
            so.put("groupName", peopleDto.getGroupName());
            so.put("leaderName", peopleDto.getLeaderName());
            so.put("leaderPhone", peopleDto.getLeaderPhone());
        }
        so.put("auth", user.getAuth());
        so.put("face", user.getFace());
        so.put("name", user.getName());
        so.put("phone", user.getPhone());

        return AjaxJson.getSuccessData(so);
    }

    /**
     * 边民首次使用APP人脸认证
     */
    @PostMapping("face")
    public AjaxJson face(@RequestPart MultipartFile file, Integer type) throws Exception {
        appUserService.authFace(StpAPPUserUtil.getAPPLoginInfo().getLoginId(), file, type);
        return AjaxJson.getSuccess();
    }
    @PostMapping("faceTest")
    public AjaxJson faceTest(@RequestPart MultipartFile file, Integer type) throws Exception {
        appUserService.authFace(1L, file, type);
        return AjaxJson.getSuccess();
    }

    @PostMapping("faceImg")
    public AjaxJson faceImg(String img, Integer type)  throws IOException {
        if (StrUtil.isEmpty(img)) return AjaxJson.getError("人脸信息为空!");
        appUserService.authFace(StpAPPUserUtil.getAPPLoginInfo().getLoginId(), img, type);
        return AjaxJson.getSuccess();
    }

    @PostMapping("faceLogin")
    public AjaxJson faceLogin(@RequestPart MultipartFile file) {
        return appUserService.faceLogin(file);
    }


    /**
     * 用户注册
     */
    @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);
    }

    @PostMapping("login/purchaser")
    public AjaxJson purchaserLogin(@Validated @RequestBody LoginDto dto) {
        return appUserService.purchaserLogin(dto);
    }

    /**
     * 用户退出
     */
    @RequestMapping("logout")
    public AjaxJson logout() {
        String logout = appUserService.logout();
        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("密码修改失败!");
    }

    /**
     * 获取银行卡信息
     */
    @RequestMapping("getBankInfo")
    public AjaxJson getBankInfo() {
        SoMap so = appUserService.getBankInfo();
        return AjaxJson.getSuccessData(so);
    }

    /**
     * 获取银行卡信息
     */
    @RequestMapping("saveBankInfo")
    public AjaxJson saveBankInfo() {
        SoMap so = SoMap.getRequestSoMap();
        return AjaxJson.toAjax(appUserService.saveBankInfo(so));
    }

    /*-----               正常业务接口👆👆    不越界     rpc远程调用 👇👇                     -----*/

    /**
     * 收购商认证
     */
    @PostMapping("rpc/getByPhoneAndUserType")
    public AppUserDto getByPhoneAndUserType(@RequestParam("phone") String phone, @RequestParam("userType") Integer userType) {
        return appUserService.getByPhoneAndUserType(phone, userType);
    }

    /**
     * 注销账户
     */
    @RequestMapping("delete")
    public AjaxJson delete(Long id){
        return AjaxJson.getSuccess();
    }


}
