package com.qunzhixinxi.hnqz.admin.service.gig.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qunzhixinxi.hnqz.admin.api.constant.CacheConstants;
import com.qunzhixinxi.hnqz.admin.api.constant.MqConstants;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.api.entity.SysUser;
import com.qunzhixinxi.hnqz.admin.config.TaxNotifyConfig;
import com.qunzhixinxi.hnqz.admin.config.UpmsConfig;
import com.qunzhixinxi.hnqz.admin.entity.SysDeptSub;
import com.qunzhixinxi.hnqz.admin.entity.SysUserSub;
import com.qunzhixinxi.hnqz.admin.entity.dto.UserSignCertDTO;
import com.qunzhixinxi.hnqz.admin.entity.model.GigBaseInfoModel;
import com.qunzhixinxi.hnqz.admin.entity.model.GigInfoModel;
import com.qunzhixinxi.hnqz.admin.entity.model.GigUserInfoModel;
import com.qunzhixinxi.hnqz.admin.entity.vo.SysUserSubVO;
import com.qunzhixinxi.hnqz.admin.enums.GigCallBackStatus;
import com.qunzhixinxi.hnqz.admin.enums.GigChannelEnum;
import com.qunzhixinxi.hnqz.admin.enums.GigTypeEnum;
import com.qunzhixinxi.hnqz.admin.enums.LockEnum;
import com.qunzhixinxi.hnqz.admin.enums.MemberIDCardType;
import com.qunzhixinxi.hnqz.admin.enums.MemberType;
import com.qunzhixinxi.hnqz.admin.enums.OladingCertStatus;
import com.qunzhixinxi.hnqz.admin.enums.PayChannel;
import com.qunzhixinxi.hnqz.admin.enums.RenlijiaCertStatus;
import com.qunzhixinxi.hnqz.admin.enums.SubjectTypeEnum;
import com.qunzhixinxi.hnqz.admin.enums.YeeCertStatus;
import com.qunzhixinxi.hnqz.admin.service.SysDeptService;
import com.qunzhixinxi.hnqz.admin.service.SysDeptSubService;
import com.qunzhixinxi.hnqz.admin.service.SysUserService;
import com.qunzhixinxi.hnqz.admin.service.SysUserSubService;
import com.qunzhixinxi.hnqz.admin.service.gig.SysUserSignCertService;
import com.qunzhixinxi.hnqz.common.core.entity.BaseMap;
import com.qunzhixinxi.hnqz.common.rabbitmq.client.RabbitMqClient;
import com.qunzhixinxi.hnqz.common.security.service.HnqzUser;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import com.qunzhixinxi.hnqz.common.taxhelper.enums.TaxHelperCertStatus;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 用户签约认证服务
 *
 * @author lixuesong
 * @date 2022年03月03日 14:38
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserSignCertServiceImpl implements SysUserSignCertService {

	private final SysUserService sysUserService;

	private final SysUserSubService sysUserSubService;

	private final SysDeptSubService sysDeptSubService;

	private final SysDeptService sysDeptService;

	private final RabbitMqClient rabbitMqClient;

	private final UpmsConfig upmsConfig;

	private final TaxNotifyConfig taxNotifyConfig;

	private final RedisTemplate redisTemplate;

	/**
	 * 查询用户签约认证信息
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public List<SysUserSub> listUserSignCert(Integer userId) {
		List<SysUserSub> userSubList = sysUserSubService.list(Wrappers.<SysUserSub>lambdaQuery()
				.eq(SysUserSub::getUserId, userId));
		return userSubList;
	}

	/**
	 * 查询用户签约认证信息
	 *
	 * @param userId
	 * @param gigTypeList
	 * @return
	 */
	@Override
	public List<SysUserSub> listUserSignCert(Integer userId, List<GigTypeEnum> gigTypeList) {
		List<SysUserSub> userSubList = sysUserSubService.list(Wrappers.<SysUserSub>lambdaQuery()
				.eq(SysUserSub::getUserId, userId)
				.in(SysUserSub::getGigType, gigTypeList));
		return userSubList;
	}

	/**
	 * 查询用户签约认证信息
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public List<SysUserSubVO> listUserSignCertForApi(Integer userId) {
		List<SysUserSub> userSubList = sysUserSubService.list(Wrappers.<SysUserSub>lambdaQuery()
				.eq(SysUserSub::getUserId, userId));
		if (CollUtil.isNotEmpty(userSubList)) {
			List<SysUserSubVO> resultList = new ArrayList<>();
			userSubList.forEach(sysUserSub -> {
				SysUserSubVO userSubVO = BeanUtil.copyProperties(sysUserSub, SysUserSubVO.class, "certStatus");
				switch (sysUserSub.getGigType()) {
					case REN_LI_JIA:
						userSubVO.setCertStatus(RenlijiaCertStatus.resolve(sysUserSub.getCertStatus()).name());
						break;
					case TAX_HELPOR:
						userSubVO.setCertStatus(TaxHelperCertStatus.resolve(sysUserSub.getCertStatus()).name());
						break;
					case YEE:
						userSubVO.setCertStatus(YeeCertStatus.resolve(String.valueOf(sysUserSub.getCertStatus())).name());
						break;
					case OLADING:
						userSubVO.setCertStatus(OladingCertStatus.resolve(String.valueOf(sysUserSub.getCertStatus())).name());
						break;
					default:
				}

				resultList.add(userSubVO);
			});
			return resultList;
		}
		return new ArrayList<>();
	}

	/**
	 * 保存用户基础信息
	 *
	 * @param userSignCertDTO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveBaseInfo(UserSignCertDTO userSignCertDTO) {
		HnqzUser user = SecurityUtils.getUser();
		Integer operateUserId = user.getId();
		// 查询user
		SysUser queryUser = sysUserService.getById(userSignCertDTO.getUserId());

		if (StrUtil.isNotBlank(userSignCertDTO.getIdCardNumber()) || StrUtil.isNotBlank(userSignCertDTO.getBankPhone())
				|| StrUtil.isNotBlank(userSignCertDTO.getBankCardNumber())) {
			// 更新用户信息
			SysUser updateUser = new SysUser();
			updateUser.setUserId(userSignCertDTO.getUserId());
			updateUser.setIdCardNumber(userSignCertDTO.getIdCardNumber());
			updateUser.setBankPhone(userSignCertDTO.getBankPhone());
			updateUser.setBankCardNumber(userSignCertDTO.getBankCardNumber());
			sysUserService.updateById(updateUser);
		}
		// 更新用户签约认证主体信息
		userSignCertDTO.getGigTypeList().forEach(gigType -> {
			// 税邦云单独设置渠道类型
			GigChannelEnum gigChannel;
			if (GigTypeEnum.TAX_HELPOR.equals(gigType)) {
				// 读取用户所认证的企业的结算渠道
				SysDeptSub deptSub = sysDeptSubService.getOne(Wrappers.<SysDeptSub>lambdaQuery()
						.eq(SysDeptSub::getDeptId, queryUser.getDeptId())
						.eq(SysDeptSub::getSubjectType, GigTypeEnum.TAX_HELPOR.getCode())
						.eq(SysDeptSub::getEnableFlag, SubjectTypeEnum.ENABLE_FLAG_TRUE.getCode()));
				if (deptSub != null) {
					gigChannel = GigChannelEnum.resolve(Integer.parseInt(deptSub.getSubjectType()));
				} else {
					gigChannel = GigChannelEnum.DEFAULT;
				}
			} else {
				gigChannel = GigChannelEnum.DEFAULT;
			}
			SysUserSub queryUserSub = sysUserSubService.getOne(Wrappers.<SysUserSub>lambdaQuery()
					.eq(SysUserSub::getUserId, userSignCertDTO.getUserId())
					.eq(SysUserSub::getGigType, gigType));
			if (queryUserSub != null) {
				SysUserSub updateUserSub = new SysUserSub();
				updateUserSub.setId(queryUserSub.getId());
				updateUserSub.setCallbackStatus(GigCallBackStatus.NOT_SUBMIT);
				updateUserSub.setGigChannel(gigChannel);
				updateUserSub.setBankCardNumber(userSignCertDTO.getBankCardNumber());
				updateUserSub.setUpdateTime(LocalDateTime.now());
				updateUserSub.setUpdateUser(operateUserId);
				sysUserSubService.updateById(updateUserSub);
			} else {
				SysUserSub userSub = new SysUserSub();
				userSub.setUserId(userSignCertDTO.getUserId());
				userSub.setDeptId(queryUser.getDeptId());
				userSub.setGigType(gigType);
				userSub.setGigChannel(gigChannel);
				userSub.setCallbackStatus(GigCallBackStatus.NOT_SUBMIT);
				userSub.setBankCardNumber(userSignCertDTO.getBankCardNumber());
				userSub.setBankPhone(userSignCertDTO.getBankPhone());
				userSub.setCreateUser(operateUserId);
				userSub.setCreateTime(LocalDateTime.now());
				sysUserSubService.save(userSub);
			}
		});
	}

	/**
	 * 签协议
	 *
	 * @param userSignCertDTO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void signAgreement(UserSignCertDTO userSignCertDTO) {
		LambdaUpdateWrapper<SysUserSub> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(SysUserSub::getUserId, userSignCertDTO.getUserId());
		if (CollUtil.isNotEmpty(userSignCertDTO.getGigTypeList())) {
			updateWrapper.in(SysUserSub::getGigType, userSignCertDTO.getGigTypeList());
		}
		updateWrapper.set(SysUserSub::getAgreementUrl, userSignCertDTO.getAgreementUrl());
		updateWrapper.set(SysUserSub::getUpdateUser, SecurityUtils.getUser().getId());
		updateWrapper.set(SysUserSub::getUpdateTime, LocalDateTime.now());

		sysUserSubService.update(updateWrapper);
	}

	/**
	 * 人身验证
	 *
	 * @param userSignCertDTO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void personalIdentityVerification(UserSignCertDTO userSignCertDTO) {
		LambdaUpdateWrapper<SysUserSub> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(SysUserSub::getUserId, userSignCertDTO.getUserId());
		if (CollUtil.isNotEmpty(userSignCertDTO.getGigTypeList())) {
			updateWrapper.in(SysUserSub::getGigType, userSignCertDTO.getGigTypeList());
		}
		if (StrUtil.isNotBlank(userSignCertDTO.getAgreementUrl())) {
			updateWrapper.set(SysUserSub::getAgreementUrl, userSignCertDTO.getAgreementUrl());
		}
		updateWrapper.set(SysUserSub::getIdCardFrontUrl, userSignCertDTO.getIdCardFrontUrl());
		updateWrapper.set(SysUserSub::getIdCardBackUrl, userSignCertDTO.getIdCardBackUrl());
		updateWrapper.set(SysUserSub::getCertVideo1Url, userSignCertDTO.getCertVideo1Url());
		updateWrapper.set(SysUserSub::getCertVideo2Url, userSignCertDTO.getCertVideo2Url());
		updateWrapper.set(SysUserSub::getUpdateUser, SecurityUtils.getUser().getId());
		updateWrapper.set(SysUserSub::getUpdateTime, LocalDateTime.now());

		sysUserSubService.update(updateWrapper);
	}

	/**
	 * 批量发起用户签约认证
	 *
	 * @param userSignCertDTO
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean batchChannelCert(UserSignCertDTO userSignCertDTO) {
		Integer userId = userSignCertDTO.getUserId();
		// 根据用户id查询用户信息、认证信息
		SysUser sysUser = sysUserService.getById(userId);
		if (sysUser == null) {
			return false;
		}
		// 查询dept
		SysDept dept = sysDeptService.getById(sysUser.getDeptId());
		// 查询当前用户关联的userSub
		LambdaQueryWrapper<SysUserSub> userSubWrapper = Wrappers.lambdaQuery();
		userSubWrapper.eq(SysUserSub::getUserId, userId);
		if (CollUtil.isNotEmpty(userSignCertDTO.getGigTypeList())) {
			userSubWrapper.in(SysUserSub::getGigType, userSignCertDTO.getGigTypeList());
		}
		List<SysUserSub> userSubList = sysUserSubService.list(userSubWrapper);

		if (CollUtil.isNotEmpty(userSubList)) {
			// 封装请求数据
			userSubList.stream().filter(sysUserSub -> {
				// 过滤掉认证成功的数据
				if (GigTypeEnum.TAX_HELPOR.equals(sysUserSub.getGigType())
						&& TaxHelperCertStatus.CHECKED_UNBIND.getCode() != sysUserSub.getCertStatus()
						&& TaxHelperCertStatus.CERT.getCode() != sysUserSub.getCertStatus()) {
					return true;
				} else if (GigTypeEnum.YEE.equals(sysUserSub.getGigType())
						&& !YeeCertStatus.SIGNED.getCode().equals(String.valueOf(sysUserSub.getCertStatus()))) {
					return true;
				} else if (GigTypeEnum.OLADING.equals(sysUserSub.getGigType())
						&& !OladingCertStatus.CERT.getCode().equals(String.valueOf(sysUserSub.getCertStatus()))) {
					return true;
				} else if (GigTypeEnum.REN_LI_JIA.equals(sysUserSub.getGigType())
						&& RenlijiaCertStatus.CER_STATUS_1.getCode() != sysUserSub.getCertStatus()) {
					return true;
				}
				return false;
			}).forEach(userSub -> {
				// 发起前置判断
				boolean preSignCert = preSignCert(sysUser, userSub);
				if (!preSignCert) {
					return;
				}
				// 请求流水号
				String requestId = RandomStringUtils.randomAlphabetic(16);
				// 封装请求参数
				GigInfoModel infoModel = buildChannelCertModel(sysUser, userSub, dept.getTaxCode(), requestId);
				// 更新为进行中状态
				SysUserSub updateUserSub = new SysUserSub();
				updateUserSub.setId(userSub.getId());
				updateUserSub.setRequestId(requestId);
				updateUserSub.setCallbackStatus(GigCallBackStatus.IN_PROGRESS);
				sysUserSubService.updateById(updateUserSub);
				// 发送mq
				BaseMap gigInfo = new BaseMap();
				gigInfo.put(userSub.getGigType().name(), infoModel);
				rabbitMqClient.sendMessage(MqConstants.DIRECT_MODE_GIG_SIGN_CERT_QUEUE, JSONUtil.toJsonStr(gigInfo));

			});
			return true;
		}
		return false;
	}

	/**
	 * 签约认证前的前置判断
	 *
	 * @param sysUser
	 * @param userSub
	 */
	private boolean preSignCert(SysUser sysUser, SysUserSub userSub) {
		boolean flag = true;
		switch (userSub.getGigType()) {
			case OLADING:
				// 如果存在相同身份证信息的用户，并且是签约或认证过，则不发起，直接返回签约认证状态
				List<SysUser> existUserList = sysUserService.list(Wrappers.<SysUser>lambdaQuery()
						.eq(SysUser::getRealname, sysUser.getRealname())
						.eq(SysUser::getIdCardNumber, sysUser.getIdCardNumber())
						.eq(SysUser::getLockFlag, LockEnum.UN_LOCK.val())
						.ne(SysUser::getUserId, sysUser.getUserId()));
				if (CollUtil.isNotEmpty(existUserList)) {
					List<Integer> existUserIdList = existUserList.stream()
							.map(SysUser::getUserId)
							.collect(Collectors.toList());
					List<SysUserSub> existUserSubList = sysUserSubService.list(Wrappers.<SysUserSub>lambdaQuery()
							.in(SysUserSub::getUserId, existUserIdList)
							.in(SysUserSub::getCertStatus, Integer.valueOf(OladingCertStatus.CERT.getCode()))
							.eq(SysUserSub::getCallbackStatus, GigCallBackStatus.RETURNED)
							.eq(SysUserSub::getGigType, GigTypeEnum.OLADING));
					if (CollUtil.isNotEmpty(existUserSubList)) {
						flag = false;
						SysUserSub existUserSub = existUserSubList.get(0);
						// 更新状态
						SysUserSub updateUserSub = new SysUserSub();
						updateUserSub.setId(userSub.getId());
						updateUserSub.setCertStatus(existUserSub.getCertStatus());
						updateUserSub.setCallbackStatus(GigCallBackStatus.RETURNED);
						updateUserSub.setUpdateTime(LocalDateTime.now());
						updateUserSub.setUpdateUser(0);
						sysUserSubService.updateById(updateUserSub);
						// 结果放到redis
						String key = CacheConstants.USER_SIGN_CERT_RESPONSE_KEY + userSub.getUserId();
						Map<String, String> subMap = new HashMap<>();
						subMap.put("certStatus", OladingCertStatus.resolve(String.valueOf(existUserSub.getCertStatus())).name());
						Map<String, Object> result = MapUtil.newHashMap(1);
						result.put("OLADING", subMap);
						redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(result), 1, TimeUnit.MINUTES);
					}
				}
				break;
			case REN_LI_JIA:
				// 人力家直接调用原有发短信接口
				SysDept dept = new SysDept();
				dept.setDeptId(sysUser.getDeptId());
				Map<String, String> renLiJiaResult = sysUserService.certByRenLiJia(sysUser, dept);
				// 更新状态
				SysUserSub updateUserSub = new SysUserSub();
				updateUserSub.setId(userSub.getId());
				Integer renLijiaCertStatus =
						Optional.ofNullable(sysUser.getRljCertStatus()).orElseGet(RenlijiaCertStatus.CER_STATUS_4::getCode);
				// 结果状态
				RenlijiaCertStatus certStatus = RenlijiaCertStatus.resolve(renLijiaCertStatus);
				updateUserSub.setCertStatus(sysUser.getRljCertStatus());
				// 发起成功或认证成功，才修改CallbackStatus状态
				if (RenlijiaCertStatus.CER_STATUS_1.equals(certStatus) || RenlijiaCertStatus.CER_STATUS_3.equals(certStatus)) {
					updateUserSub.setCallbackStatus(GigCallBackStatus.IN_PROGRESS);
				} else {
					updateUserSub.setCallbackStatus(GigCallBackStatus.RETURNED);
				}
				updateUserSub.setCertRemark(renLiJiaResult.get("msg"));
				updateUserSub.setUpdateTime(LocalDateTime.now());
				updateUserSub.setUpdateUser(0);
				sysUserSubService.updateById(updateUserSub);
				// 结果放到redis
				String key = CacheConstants.USER_SIGN_CERT_RESPONSE_KEY + userSub.getUserId();
				Map<String, String> subMap = new HashMap<>();
				subMap.put("certStatus", certStatus.name());
				Map<String, Object> result = MapUtil.newHashMap(1);
				result.put("REN_LI_JIA", subMap);
				redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(result), 1, TimeUnit.MINUTES);
				// 默认返回false，不触发发送mq
				flag = false;
			default:
		}

		return flag;
	}

	/**
	 * 封装签约认证的请求参数
	 *
	 * @param sysUser
	 * @param userSub
	 * @param entCode
	 * @param requestId
	 * @return
	 */
	private GigInfoModel buildChannelCertModel(SysUser sysUser, SysUserSub userSub, String entCode, String requestId) {
		// 基础信息
		GigBaseInfoModel baseInfoModel = new GigBaseInfoModel();
		baseInfoModel.setRequestId(requestId);
		baseInfoModel.setGigType(userSub.getGigType());
		baseInfoModel.setGigChannel(userSub.getGigChannel());
		// 用户信息
		GigUserInfoModel userInfoModel = new GigUserInfoModel();
		userInfoModel.setUserId(sysUser.getUserId());
		userInfoModel.setName(sysUser.getRealname());
		userInfoModel.setUsername(sysUser.getUsername());
		userInfoModel.setEntCode(entCode);
		userInfoModel.setDeptId(sysUser.getDeptId());
		userInfoModel.setIdCard(sysUser.getIdCardNumber());
		userInfoModel.setIdCardType(MemberIDCardType.ID_CARD);
		userInfoModel.setMemberType(MemberType.YAOYI_EXCLUSIVE_PERSONNEL);
		userInfoModel.setEmail("gig_service@yaoyi.net"); // TODO
		userInfoModel.setAddress("北京市-房山区-长阳镇");
		userInfoModel.setPayChannel(PayChannel.BANK_CARD);
		userInfoModel.setBankPhone(userSub.getBankPhone());
		userInfoModel.setBankCard(userSub.getBankCardNumber());
		userInfoModel.setIdCardFront(StrUtil.isBlank(userSub.getIdCardFrontUrl())
				? null : userSub.getIdCardFrontUrl().startsWith("https")
				? userSub.getIdCardFrontUrl() : upmsConfig.getClientUrl() + userSub.getIdCardFrontUrl());
		userInfoModel.setIdCardBack(StrUtil.isBlank(userSub.getIdCardBackUrl())
				? null : userSub.getIdCardBackUrl().startsWith("https")
				? userSub.getIdCardBackUrl() : upmsConfig.getClientUrl() + userSub.getIdCardBackUrl());
		userInfoModel.setCertVideo1(StrUtil.isBlank(userSub.getCertVideo1Url())
				? null : userSub.getCertVideo1Url().startsWith("https")
				? userSub.getCertVideo1Url() : upmsConfig.getClientUrl() + userSub.getCertVideo1Url());
		userInfoModel.setCertVideo2(StrUtil.isBlank(userSub.getCertVideo2Url())
				? null : userSub.getCertVideo2Url().startsWith("https")
				? userSub.getCertVideo2Url() : upmsConfig.getClientUrl() + userSub.getCertVideo2Url());
		userInfoModel.setSignImageUrl(StrUtil.isBlank(userSub.getAgreementUrl())
				? null : userSub.getAgreementUrl().startsWith("https")
				? userSub.getAgreementUrl() : upmsConfig.getClientUrl() + userSub.getAgreementUrl());

		GigInfoModel infoModel = new GigInfoModel();
		infoModel.setBaseInfo(baseInfoModel);
		infoModel.setUserInfo(userInfoModel);

		return infoModel;
	}

	/**
	 * 税邦云发送绑卡验证码
	 *
	 * @param userSignCertDTO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void bindCardVerification(UserSignCertDTO userSignCertDTO) {
		SysUserSub userSub = sysUserSubService.getOne(Wrappers.<SysUserSub>lambdaQuery()
				.eq(SysUserSub::getUserId, userSignCertDTO.getUserId())
				.eq(SysUserSub::getGigType, GigTypeEnum.TAX_HELPOR));
		if (userSub == null) {
			throw new RuntimeException("数据异常");
		}
		// 查询user
		SysUser sysUser = sysUserService.getById(userSignCertDTO.getUserId());
		// 查询dept
		SysDept dept = sysDeptService.getById(sysUser.getDeptId());
		// 基础信息
		GigBaseInfoModel baseInfoModel = new GigBaseInfoModel();
		baseInfoModel.setGigType(userSub.getGigType());
		baseInfoModel.setGigChannel(userSub.getGigChannel());
		baseInfoModel.setBankCardOpt("captcha");
		baseInfoModel.setNotifyUrl(taxNotifyConfig.getNotifyUrl());
		// 用户信息
		GigUserInfoModel userInfoModel = new GigUserInfoModel();
		userInfoModel.setName(sysUser.getRealname());
		userInfoModel.setUsername(sysUser.getUsername());
		userInfoModel.setEntCode(dept.getTaxCode());
		userInfoModel.setIdCard(sysUser.getIdCardNumber());
		userInfoModel.setBankPhone(userSignCertDTO.getBankPhone());
		userInfoModel.setBankCard(userSignCertDTO.getBankCardNumber());

		GigInfoModel infoModel = new GigInfoModel();
		infoModel.setBaseInfo(baseInfoModel);
		infoModel.setUserInfo(userInfoModel);
		BaseMap gigInfo = new BaseMap();
		gigInfo.put(userSub.getGigType().name(), infoModel);
		// 发送mq
		rabbitMqClient.sendMessage(MqConstants.DIRECT_MODE_GIG_BANK_CARD_QUEUE, JSONUtil.toJsonStr(gigInfo));

	}

	/**
	 * 绑卡确认
	 *
	 * @param userSignCertDTO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void bindCardConfirm(UserSignCertDTO userSignCertDTO) {
		List<SysUserSub> userSubList = sysUserSubService.list(Wrappers.<SysUserSub>lambdaQuery()
				.eq(SysUserSub::getUserId, userSignCertDTO.getUserId())
				.in(SysUserSub::getGigType, userSignCertDTO.getGigTypeList()));
		if (CollUtil.isEmpty(userSubList)) {
			throw new RuntimeException("数据异常");
		}
		BaseMap gigInfo = new BaseMap();
		userSubList.stream()
				// 人力家暂过滤掉
				.filter(sysUserSub -> !GigTypeEnum.REN_LI_JIA.equals(sysUserSub.getGigType()))
				.forEach(userSub -> {
					// 新银行卡号缓存到redis
					String cacheKey = String.format("%s_%s_%s", CacheConstants.BIND_CARD_CONFIRM_NEW_BANK_CARD, userSub.getUserId(), userSub.getGigType().name());
					redisTemplate.opsForValue().set(cacheKey, userSignCertDTO);

					// 查询user
					SysUser sysUser = sysUserService.getById(userSignCertDTO.getUserId());
					// 查询dept
					SysDept dept = sysDeptService.getById(sysUser.getDeptId());
					// 封装请求数据
					if (GigTypeEnum.TAX_HELPOR.equals(userSub.getGigType())) {
						userSub.setBankPhone(userSignCertDTO.getBankPhone());
						userSub.setBankCardNumber(userSignCertDTO.getBankCardNumber());
						gigInfo.put(userSub.getGigType().name(),
								this.buildTaxHelperBindCardConfirmModel(sysUser, userSub, dept.getTaxCode(), userSignCertDTO.getBankCardNumber(), userSignCertDTO.getVerifyCode()));
					} else if (GigTypeEnum.YEE.equals(userSub.getGigType())) {
						gigInfo.put(userSub.getGigType().name(),
								this.buildYeeBindCardConfirmModel(sysUser, userSub, dept.getTaxCode(), userSignCertDTO.getBankCardNumber()));
					} else if (GigTypeEnum.OLADING.equals(userSub.getGigType())) {
						gigInfo.put(userSub.getGigType().name(),
								this.buildOladingBindCardConfirmModel(sysUser, userSub, dept.getTaxCode(), userSignCertDTO.getBankCardNumber()));
					}
					// 更新为进行中状态
					SysUserSub updateUserSub = new SysUserSub();
					updateUserSub.setId(userSub.getId());
					updateUserSub.setCallbackStatus(GigCallBackStatus.IN_PROGRESS);
					updateUserSub.setUpdateTime(LocalDateTime.now());
					updateUserSub.setUpdateUser(0);
					sysUserSubService.updateById(updateUserSub);
				});

		if (CollUtil.isNotEmpty(gigInfo)) {
			// 发送mq
			rabbitMqClient.sendMessage(MqConstants.DIRECT_MODE_GIG_BANK_CARD_QUEUE, JSONUtil.toJsonStr(gigInfo));
		}
	}

	/**
	 * 封装税邦云绑卡model
	 *
	 * @param sysUser
	 * @param userSub
	 * @param taxCode
	 * @param newBankCard
	 * @param verifyCode
	 * @return
	 */
	private GigInfoModel buildTaxHelperBindCardConfirmModel(SysUser sysUser, SysUserSub userSub, String taxCode, String newBankCard, String verifyCode) {
		// 基础信息
		GigBaseInfoModel baseInfoModel = new GigBaseInfoModel();
		baseInfoModel.setGigType(userSub.getGigType());
		baseInfoModel.setGigChannel(userSub.getGigChannel());
		baseInfoModel.setCaptcha(verifyCode);
		baseInfoModel.setNotifyUrl(taxNotifyConfig.getNotifyUrl());
		// 用户信息
		GigUserInfoModel userInfoModel = new GigUserInfoModel();
		userInfoModel.setName(sysUser.getRealname());
		userInfoModel.setUsername(sysUser.getUsername());
		userInfoModel.setEntCode(taxCode);
		userInfoModel.setIdCard(sysUser.getIdCardNumber());
		userInfoModel.setBankPhone(userSub.getBankPhone());
		userInfoModel.setBankCard(userSub.getBankCardNumber());
		userInfoModel.setNewBankCard(newBankCard);
		userInfoModel.setAddress("北京市-房山区-长阳镇");

		GigInfoModel infoModel = new GigInfoModel();
		infoModel.setBaseInfo(baseInfoModel);
		infoModel.setUserInfo(userInfoModel);

		return infoModel;
	}

	/**
	 * 封装易联换绑model
	 *
	 * @param sysUser
	 * @param userSub
	 * @param taxCode
	 * @param newBankCard
	 * @return
	 */
	private GigInfoModel buildYeeBindCardConfirmModel(SysUser sysUser, SysUserSub userSub, String taxCode, String newBankCard) {
		// 基础信息
		GigBaseInfoModel baseInfoModel = new GigBaseInfoModel();
		baseInfoModel.setGigChannel(userSub.getGigChannel());
		baseInfoModel.setGigType(userSub.getGigType());
		// 用户信息
		GigUserInfoModel userInfoModel = new GigUserInfoModel();
		userInfoModel.setName(sysUser.getRealname());
		userInfoModel.setUsername(sysUser.getUsername());
		userInfoModel.setEntCode(taxCode);
		userInfoModel.setIdCard(sysUser.getIdCardNumber());
		userInfoModel.setBankPhone(userSub.getBankPhone());
		userInfoModel.setBankCard(userSub.getBankCardNumber());
		userInfoModel.setNewBankCard(newBankCard);
		userInfoModel.setAddress("北京市-房山区-长阳镇");

		GigInfoModel infoModel = new GigInfoModel();
		infoModel.setBaseInfo(baseInfoModel);
		infoModel.setUserInfo(userInfoModel);

		return infoModel;
	}

	/**
	 * 封装自由职家换绑model
	 *
	 * @param sysUser
	 * @param userSub
	 * @param taxCode
	 * @param newBankCard
	 * @return
	 */
	private GigInfoModel buildOladingBindCardConfirmModel(SysUser sysUser, SysUserSub userSub, String taxCode, String newBankCard) {
		// 基础信息
		GigBaseInfoModel baseInfoModel = new GigBaseInfoModel();
		baseInfoModel.setGigChannel(userSub.getGigChannel());
		baseInfoModel.setGigType(userSub.getGigType());
		// 用户信息
		GigUserInfoModel userInfoModel = new GigUserInfoModel();
		userInfoModel.setName(sysUser.getRealname());
		userInfoModel.setUsername(sysUser.getUsername());
		userInfoModel.setEntCode(taxCode);
		userInfoModel.setIdCard(sysUser.getIdCardNumber());
		userInfoModel.setBankPhone(userSub.getBankPhone());
		userInfoModel.setBankCard(userSub.getBankCardNumber());
		userInfoModel.setNewBankCard(newBankCard);
		userInfoModel.setAddress("北京市-房山区-长阳镇"); // TODO

		GigInfoModel infoModel = new GigInfoModel();
		infoModel.setBaseInfo(baseInfoModel);
		infoModel.setUserInfo(userInfoModel);

		return infoModel;
	}
}
