package com.njcn.user.controller.app;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.constant.SecurityConstants;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.user.pojo.po.User;
import com.njcn.user.service.IAppUserService;
import com.njcn.web.annotation.ReturnMsg;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/6/7 8:51
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/appUser")
@Api(tags = "App用户信息管理")
@AllArgsConstructor
public class AppUserController extends BaseController {

    private final IAppUserService appUserService;

    /**
     * 获取（登录、注册、忘记密码、重新绑定手机）验证码
     */
    @PostMapping("authCode")
    @OperateInfo
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "验证码类型(0:登录 1:注册 2:修改密码 4:换绑新手机 5.老手机校验 6:忘记密码)", required = true, paramType = "query"),
    })
    @ReturnMsg
    public HttpResult<String> authCode(String phone, String devCode, String type) {
        String methodDescribe = getMethodDescribe("authCode");
        String code = appUserService.setMessage(phone,devCode,type);
        if (Objects.equals(code,"OK")){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "fail", methodDescribe);
        }
    }

    /**
     * 手机app注册
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("register")
    @ApiOperation("App用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query")
    })
    public HttpResult<String> register(@Param("phone") String phone, @Param("code") String code, @Param("devCode") String devCode) {
        String methodDescribe = getMethodDescribe("register");
        LogUtil.njcnDebug(log, "{}，手机号:{},验证码:{},设备码:{}", methodDescribe, phone,code,devCode);
        appUserService.register(phone,code,devCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 手机app已登录修改密码
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("modifyPsd")
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query"),
    })
    public HttpResult<Object> modifyPsd(String userId,String phone, String code,String password, String devCode) {
        String methodDescribe = getMethodDescribe("modifyPsd");
        appUserService.modifyPsd(userId,phone,code,password,devCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 手机app忘记密码-重置密码
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("resetPsd")
    @ApiOperation("重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query")
    })
    public HttpResult<Object> resetPsd(String phone, String code,String password, String devCode, @ApiIgnore HttpServletRequest request) {
        String methodDescribe = getMethodDescribe("resetPsd");
        String ip = request.getHeader(SecurityConstants.REQUEST_HEADER_KEY_CLIENT_REAL_IP);
        appUserService.resetPsd(phone,code,password,devCode,ip);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 手机app确认旧手机验证码
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("confirmCode")
    @ApiOperation("确认旧手机验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query"),
    })
    public HttpResult<Object> confirmCode(String phone,String code,String devCode) {
        String methodDescribe = getMethodDescribe("confirmCode");
        appUserService.confirmCode(phone,code,devCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * app用户手机换绑
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("rebindPhone")
    @ApiOperation("换绑新手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phoneNew", value = "新号码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query")
    })
    public HttpResult<Object> rebindPhone(String userId,String phoneNew,String code,String devCode) {
        String methodDescribe = getMethodDescribe("rebindPhone");
        appUserService.rebindPhone(userId,phoneNew,code,devCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 用户登录成功后，更新手机设备码
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("updateDevCode")
    @ApiOperation("更新用户手机设备码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "devCode", value = "设备码", required = true, paramType = "query")
    })
    public HttpResult<Object> updateDevCode(String userId,String devCode) {
        String methodDescribe = getMethodDescribe("updateDevCode");
        appUserService.updateDevCode(userId,devCode);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 获取业务管理员信息
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("getAdminInfo")
    @ApiOperation("获取业务管理员信息")
    public HttpResult<List<User>> getAdminInfo() {
        String methodDescribe = getMethodDescribe("getAdminInfo");
        List<User> list = appUserService.getAdminInfo();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }
}
