package com.cyksj.web.controller.user;

import cn.hutool.core.util.StrUtil;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.dto.UserWxIndentDto;
import com.cyksj.model.entity.UserCertRecord;
import com.cyksj.model.entity.UserStudentCertRecord;
import com.cyksj.model.request.UserIndentCertReq;
import com.cyksj.model.response.WxCertAuthTokenResp;
import com.cyksj.redis.RedisService;
import com.cyksj.service.user.UserIdentCertService;
import com.cyksj.web.util.StpUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 项目名: yhlxj2
 * 文件名: UserCertController
 * 创建者: JavaZou
 * 创建时间:2024/2/21 10:13
 */
@RequestMapping("/applets/user/cert")
@RestController
@RequiredArgsConstructor
public class UserCertController {

	private final UserIdentCertService userIdentCertService;

	private final RedisService redisService;


	/**
	 * 用户是否已授权认证
	 */
	@GetMapping("/check/record")
	public Result<Boolean> checkCertRecord() {
		long userId = StpUserUtil.getLoginIdAsLong();
		Boolean flag = userIdentCertService.checkCertRecord(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(flag);
	}

	/**
	 * 非微信端 用户身份认证 建立微信用户绑定关系
	 */
	@PostMapping("/bind")
	@Deprecated
	public Result<String> bindWxUser() {
		long userId = StpUserUtil.getLoginIdAsLong();
		String state = userIdentCertService.bindWxUser(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(state);
	}


	/**
	 * 用户微信H5认证
	 */
	@PostMapping("/auth")
	public Result<WxCertAuthTokenResp> wxH5IndentCertAuth(@RequestBody UserIndentCertReq indentCertReq) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		indentCertReq.setUserId(userId);
		WxCertAuthTokenResp resp = userIdentCertService.wxH5IndentCert(indentCertReq);
		return GatewayResponse.SUCCESS.newBuilder().toResult(resp);
	}

	/**
	 * 用户微信H5认证结果查询
	 */
	@GetMapping("/auth/result/{mappingId}")
	public void wxH5IndentCertAuthResult(@PathVariable String mappingId, HttpServletResponse response) throws Exception {
		String redirectParams = redisService.getStr(mappingId);
		if (StrUtil.isNotBlank(redirectParams)) {
			UserWxIndentDto userWxIndentDto = Jsons.parseObject(redirectParams, UserWxIndentDto.class);
			try {
				userIdentCertService.wxH5IndentCertResult(userWxIndentDto);
				String url = userWxIndentDto.getRedirectUrl();
				response.sendRedirect(url);
			} catch (Exception e) {
				response.sendRedirect(userWxIndentDto.getErrorUrl());
			} finally {
				redisService.del(mappingId);
			}
		}
	}

	/**
	 * 查看实名认证信息
	 */
	@GetMapping("/get/info")
	public Result<UserCertRecord> getUserCertRecord(String mappingId) throws Exception {
		long userId = StpUserUtil.getLoginIdAsLong();
		UserCertRecord record = userIdentCertService.getUserCertRecord(userId, mappingId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(record);
	}

	/**
	 * 查看大学生认证信息
	 */
	@GetMapping("/get/student/info")
	public Result<UserStudentCertRecord> getUserStudentCertRecord() {
		long userId = StpUserUtil.getLoginIdAsLong();
		UserStudentCertRecord record = userIdentCertService.getUserStudentCertRecord(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(record);
	}
}
