package com.miaxis.app.controller.user;

import com.miaxis.app.file.vo.FileVo;
import com.miaxis.app.user.dto.UserDto;
import com.miaxis.app.user.service.IUserInfoService;
import com.miaxis.app.user.vo.UserVo;
import com.miaxis.common.aliyunOSS.AliyunConfig;
import com.miaxis.common.aliyunOSS.AliyunUpload;
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.enums.PictureTypeEnum;
import com.miaxis.common.exception.CustomException;
import com.miaxis.common.utils.SecurityUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 【用户信息】Controller
 *
 * @author wwl
 * @date 2020-12-23
 */
@RestController
@RequestMapping(Constants.STUDENT_PREFIX+"/user/info")
@Api(tags={"【app-普通用户信息】"})
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));
    }

    /**
     * 删除用户信息
     */
	@DeleteMapping("/{ids}")
    @ApiOperation("删除用户信息")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "用户信息ids参数", required = true)
            @PathVariable Long[] ids
    ){
        return toResponse(userInfoService.deleteUserInfoByIds(ids));
    }

    /**
     * 上传身份证正反面,及人像图片
     * uploadIdCardAndHeadPicture
     */
    @PostMapping(Constants.OPEN_PREFIX + "/uploadIdCardAndHeadPicture")
    @ApiOperation("上传身份证正反面,及人像图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "1:身份证正面 2:身份证反面 3:人脸比对模板(采集人像图)" ,dataType = "String",required = true),
            @ApiImplicitParam(name = "multFile", value = "上传文件", required = true, dataType = "MultipartFile")
    })
    public Response uploadIdCardAndHeadPicture(String type,MultipartFile multFile) throws IOException {
        String prefix = "";
        if (PictureTypeEnum.IDCARDFRONT.getCode().equals(type)){
            prefix = PictureTypeEnum.IDCARDFRONT.getPrefix();
        }else if (PictureTypeEnum.IDCARDFRONT.getCode().equals(type)){
            prefix = PictureTypeEnum.IDCARDREVERSE.getPrefix();
        }else if (PictureTypeEnum.FACES.getCode().equals(type)){
            prefix = PictureTypeEnum.FACES.getPrefix();
        }else if (PictureTypeEnum.FACES.getCode().equals(type)){
            prefix = PictureTypeEnum.FACES.getPrefix();
        }else {
            throw new CustomException("类型错误!");
        }
        String savePath = AliyunConfig.GZPT_USER
                + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/"+ prefix + "/";
        String fileName = System.currentTimeMillis() + ".jpg";//文件名
        String fileUrl = AliyunUpload.uploadForStream(multFile.getInputStream(), savePath, fileName);
        FileVo fileVo = new FileVo();
        fileVo.setFileName(fileName);
        fileVo.setFilePath(savePath+fileName);
        fileVo.setFileUrl(fileUrl);
        return Response.success(fileVo);
    }

    /**
     * 删除文件
     * uploadIdCardAndHeadPicture
     */
    @DeleteMapping(Constants.OPEN_PREFIX + "/cardAndHeadPicture")
    @ApiOperation("删除文件")
    @ApiImplicitParam(name = "filePath",value = "文件路径(filePath)" ,dataType = "String",required = true)
    public Response deleteCardAndHeadPicture(String filePath) {
        boolean b = AliyunUpload.deleteObject(filePath);
        if (!b)return Response.error();
        return Response.success();
    }

    /**
     * 保存用户报名信息
     * 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);
    }

}
