package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qunzhixinxi.hnqz.admin.api.constant.CacheConstants;
import com.qunzhixinxi.hnqz.admin.api.entity.SysUser;
import com.qunzhixinxi.hnqz.admin.entity.SysUserSub;
import com.qunzhixinxi.hnqz.admin.entity.dto.UserSignCertDTO;
import com.qunzhixinxi.hnqz.admin.enums.GigTypeEnum;
import com.qunzhixinxi.hnqz.admin.service.SysUserService;
import com.qunzhixinxi.hnqz.admin.service.gig.SysUserSignCertService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户签约认证
 *
 * @author lixuesong
 * @date 2022年03月03日 14:34
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/user-sign-cert")
public class SysUserSignCertController {

	private final SysUserSignCertService sysUserSignCertService;

	private final SysUserService sysUserService;

	private final RedisTemplate redisTemplate;

	/**
	 * 保存银行卡信息
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@SysLog("保存银行卡信息")
	@PostMapping("/save-bank-info")
	public R<?> saveBankInfo(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (StrUtil.isBlank(userSignCertDTO.getBankPhone()) || !Validator.isMobile(userSignCertDTO.getBankPhone())) {
			return R.failed("银行预留手机号不正确");
		}
		if (StrUtil.length(userSignCertDTO.getBankCardNumber()) < 16) {
			return R.failed("银行卡号不正确");
		}
		SysUser sysUser = new SysUser();
		sysUser.setUserId(userSignCertDTO.getUserId());
		sysUser.setBankCardNumber(userSignCertDTO.getBankCardNumber());
		sysUser.setBankPhone(userSignCertDTO.getBankPhone());

		return R.ok(sysUserService.updateById(sysUser));
	}

	/**
	 * 保存用户基础信息
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@SysLog("保存用户基础信息")
	@PostMapping("/save-base-info")
	public R<?> saveBaseInfo(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (CollUtil.isEmpty(userSignCertDTO.getGigTypeList())) {
			return R.failed("认证渠道不能为空");
		}
		if (StrUtil.isBlank(userSignCertDTO.getBankPhone()) || !Validator.isMobile(userSignCertDTO.getBankPhone())) {
			return R.failed("银行预留手机号不正确");
		}
		if (StrUtil.length(userSignCertDTO.getBankCardNumber()) < 16) {
			return R.failed("银行卡号不正确");
		}
		if (StrUtil.isNotBlank(userSignCertDTO.getIdCardNumber()) && !Validator.isCitizenId(userSignCertDTO.getIdCardNumber())) {
			return R.failed("身份证号不正确");
		}
		sysUserSignCertService.saveBaseInfo(userSignCertDTO);
		return R.ok();
	}

	/**
	 * 签协议
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@SysLog("签协议")
	@PostMapping("/sign-agreement")
	public R<?> signAgreement(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (CollUtil.isEmpty(userSignCertDTO.getGigTypeList())) {
			return R.failed("认证渠道不能为空");
		}
		if (!userSignCertDTO.getGigTypeList().contains(GigTypeEnum.REN_LI_JIA) && StrUtil.isBlank(userSignCertDTO.getAgreementUrl())) {
			// 除人力家外，其他都做校验
			return R.failed("协议不能为空");
		}
		List<SysUserSub> userSubList = sysUserSignCertService.listUserSignCert(userSignCertDTO.getUserId(), userSignCertDTO.getGigTypeList());
		if (CollUtil.isEmpty(userSubList)) {
			return R.failed("数据异常");
		}
		sysUserSignCertService.signAgreement(userSignCertDTO);

		return R.ok();
	}

	/**
	 * 人身验证
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@SysLog("人身验证")
	@PostMapping("/personal-identity-verification")
	public R<?> personalIdentityVerification(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (!StrUtil.isAllNotBlank(userSignCertDTO.getIdCardFrontUrl(), userSignCertDTO.getIdCardBackUrl(),
				userSignCertDTO.getCertVideo1Url(), userSignCertDTO.getCertVideo2Url())) {
			return R.failed("身份证或视频信息不能为空");
		}
		if (CollUtil.isEmpty(userSignCertDTO.getGigTypeList())) {
			return R.failed("认证渠道不能为空");
		}
		List<SysUserSub> userSubList = sysUserSignCertService.listUserSignCert(userSignCertDTO.getUserId(), userSignCertDTO.getGigTypeList());
		if (CollUtil.isEmpty(userSubList)) {
			return R.failed("数据异常");
		}
		sysUserSignCertService.personalIdentityVerification(userSignCertDTO);

		return R.ok();
	}

	/**
	 * 批量发起用户签约认证
	 *
	 * @param userSignCertDTO
	 * @return R
	 */
	@SysLog("用户签约认证")
	@PostMapping("/batch-channel-cert")
	public R<?> batchChannelCert(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (CollUtil.isEmpty(userSignCertDTO.getGigTypeList())) {
			return R.failed("认证渠道不能为空");
		}
		List<SysUserSub> userSubList = sysUserSignCertService.listUserSignCert(userSignCertDTO.getUserId(), userSignCertDTO.getGigTypeList());
		if (CollUtil.isEmpty(userSubList)) {
			return R.failed("数据异常");
		}
		for (SysUserSub userSub : userSubList) {
			if (userSignCertDTO.getGigTypeList().contains(GigTypeEnum.REN_LI_JIA)) {
				// 人力家不校验
				continue;
			}
			if (StrUtil.isBlank(userSub.getAgreementUrl())) {
				return R.failed("协议不能为空");
			}
			if (!StrUtil.isAllNotBlank(userSub.getIdCardBackUrl(), userSub.getIdCardFrontUrl(),
					userSub.getCertVideo1Url(), userSub.getCertVideo2Url())) {
				return R.failed("身份证或视频信息不能为空");
			}
		}
		sysUserSignCertService.batchChannelCert(userSignCertDTO);

		return R.ok();
	}

	/**
	 * 税邦云发送绑卡验证码
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@SysLog("税邦云发送绑卡验证码")
	@PostMapping("/bind-card-verification")
	public R<?> bindCardVerification(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (StrUtil.isBlank(userSignCertDTO.getBankPhone())) {
			return R.failed("银行预留手机号不能为空");
		}
		if (StrUtil.isBlank(userSignCertDTO.getBankCardNumber())) {
			return R.failed("银行卡号不能为空");
		}
		sysUserSignCertService.bindCardVerification(userSignCertDTO);

		return R.ok();
	}

	/**
	 * 税邦云绑卡确认
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@SysLog("税邦云绑卡确认")
	@PostMapping("/bind-card-confirm")
	public R<?> bindCardConfirm(@RequestBody UserSignCertDTO userSignCertDTO) {
		if (userSignCertDTO.getUserId() == null) {
			return R.failed("userId不能为空");
		}
		if (CollUtil.isEmpty(userSignCertDTO.getGigTypeList())) {
			return R.failed("认证渠道不能为空");
		}
		sysUserSignCertService.bindCardConfirm(userSignCertDTO);

		return R.ok();
	}

	/**
	 * 查询签约认证结果
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/sign-cert-result/{userId}")
	public R<?> getSignCertResult(@PathVariable("userId") Integer userId) {
		String key = CacheConstants.USER_SIGN_CERT_RESPONSE_KEY + userId;
		String response = (String) redisTemplate.opsForValue().get(key);
		if (response == null) {
			return R.failed("没有结果");
		}
		JSONObject jsonResult = JSONUtil.parseObj(response, true);
		return R.ok(jsonResult);
	}
}
