package com.miaxis.app.controller.user;

import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.domain.entity.SysUser;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.utils.SecurityUtils;
import com.miaxis.user.dto.UserDto;
import com.miaxis.user.service.IUserInfoService;
import com.miaxis.user.vo.UserVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【用户信息】Controller
 *
 * @author wwl
 * @date 2020-12-23
 */
@RestController
@RequestMapping(Constants.STUDENT_PREFIX+"/user/info")
@Api(tags={"【小程序-普通用户信息】"})
public class UserInfoController extends BaseController{

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 查询用户信息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询用户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<UserInfo> list(@ModelAttribute UserInfo userInfo){
        startPage();
        List<UserInfo> list = userInfoService.list();
        return toResponsePageInfo(list);
    }


    /**
     * 获取用户id获取详细信息
     */
    @GetMapping(value = "/ids/{id}")
    @ApiOperation("获取用户信息详细信息")
    public Response<UserInfo> getInfo(
            @ApiParam(name = "id", value = "用户信息参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(userInfoService.selectUserInfoById(id));
    }


    /**
     * 根据电话获取用户信息详细信息
     */
    @GetMapping(value = "mobles/{mobile}")
    @ApiOperation("获取用户信息详细信息")
    public Response<UserInfo> getInfo(
            @ApiParam(name = "mobile", value = "用户电话", required = true)
            @PathVariable("mobile") String mobile
    ){
        return Response.success(userInfoService.selectUserInfoByMoble(mobile));
    }


    /**
     * 修改用户信息
     */
    @PutMapping
    @ApiOperation("修改用户信息")
    public Response<Integer> edit(@RequestBody UserInfo userInfo){
        return toResponse(userInfoService.updateUserInfo(userInfo));
    }


    /**
     * 保存用户报名信息
     * uploadIdCardAndHeadPicture
     */
    @PostMapping(Constants.OPEN_PREFIX + "/saveUserInformation")
    @ApiOperation("保存用户报名信息")
    public Response saveUserInformation(UserDto userDto) {

        return userInfoService.saveUserInformation(userDto);
    }

    /**
     * 注册报名获取短信验证码
     * @return
     */
    @ApiOperation("注册报名获取短信验证码")
    @ApiImplicitParam(name = "phone",value = "手机号码" ,dataType = "String",required = true)
    @PostMapping(value = Constants.OPEN_PREFIX + "/sendSignUpVerificationCode")
    public Response sendSignUpVerificationCode(String phone){

        return userInfoService.sendSignUpVerificationCode(phone);
    }

    /**
     * 修改手机号获取短信验证码
     * @return
     */
    @ApiOperation("修改手机号获取短信验证码")
    @ApiImplicitParam(name = "phone",value = "手机号码" ,dataType = "String",required = true)
    @PostMapping(value = "/sendModifyVerificationCode")
    public Response sendModifyVerificationCode(String phone){
        //获取当前用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        return userInfoService.sendModifyVerificationCode(user,phone);
    }


    /**
     * 修改用户手机号
     */
    @PostMapping("/modifyPhoneNumber")
    @ApiOperation("修改用户手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smsCodeType,", value = "login:用户登录验证码、modify:修改手机验证码、signUp:注册报名验证码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "verificationCode", value = "短信验证码", required = true, dataType = "String")
    })
    public Response modifyPhoneNumber(String phone,String verificationCode){
        //获取当前用户
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //修改手机号
        return userInfoService.modifyPhoneNumber(phone,verificationCode,user);
    }

    /**
     * 查询我的报名信息
     */
    @GetMapping("/getMyRegistrationInformation")
    @ApiOperation("查询我的报名信息")
    @ApiImplicitParam(name = "userId",value = "用户id" ,dataType = "String",required = true)
    public Response<UserVo> getMyRegistrationInformation(String userId){

        return userInfoService.getMyRegistrationInformation(userId);
    }

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
    }

}
