package org.springblade.flow.utils;

import cn.hutool.core.util.IdUtil;
import org.springblade.common.cache.PlatformCache;
import org.springblade.common.config.entity.PlatformConfig;
import org.springblade.common.config.yeepay.OpenProduct;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.common.YeepayApiConstant;
import org.springblade.yeePay.entity.saas.InvitationDto;
import org.springblade.yeePay.entity.saas.MicroInvitationDto;
import org.springblade.yeePay.service.YeepayCommonService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description: 构建InvitationDto
 * @since: 9/23/21 -- 2:35 PM
 */
public class MicroInvitationBuilder {

	private static YeepayCommonService yeepayCommonService;
	private static YeePayConst yeePayConst;

	static {
		yeepayCommonService = SpringUtil.getBean(YeepayCommonService.class);
		yeePayConst = SpringUtil.getBean(YeePayConst.class);
	}


	/**
	 * 封装进件信息
	 */
	public static MicroInvitationDto build(Audit audit) {

		MicroInvitationDto microInvitationDto = MicroInvitationDto.builder().build();

		microInvitationDto.setRequestNo(IdUtil.simpleUUID());

		//商户信息
		MicroInvitationDto.MerchantSubjectInfo merchantSubjectInfo = microInvitationDto.new MerchantSubjectInfo();
		merchantSubjectInfo.setShortName(audit.getShortName());
		merchantSubjectInfo.setSignName(audit.getLegalPerson());
		microInvitationDto.setMerchantSubjectInfo(merchantSubjectInfo);

		//法人信息
		MicroInvitationDto.MerchantCorporationInfo merchantCorporationInfo = microInvitationDto.new MerchantCorporationInfo();
		String legalLicenceBackUrl = yeepayCommonService.addFileToYeepay(audit.getIdCardContrary());
		String legalLicenceFontUrl = yeepayCommonService.addFileToYeepay(audit.getIdCardFront());
		merchantCorporationInfo.setLegalLicenceFrontUrl(legalLicenceFontUrl);
		merchantCorporationInfo.setLegalLicenceNo(audit.getLegalLicenceNo());
		merchantCorporationInfo.setLegalLicenceBackUrl(legalLicenceBackUrl);
		merchantCorporationInfo.setLegalLicenceType(audit.getLegalLicenceType());
		merchantCorporationInfo.setMobile(audit.getContactMobile());
		microInvitationDto.setMerchantCorporationInfo(merchantCorporationInfo);

		//经营地址
		MicroInvitationDto.BusinessAddressInfo businessAddressInfo = microInvitationDto.new BusinessAddressInfo();
		businessAddressInfo.setAddress(audit.getAddress());
		businessAddressInfo.setCity(audit.getCity());
		businessAddressInfo.setDistrict(audit.getDistrict());
		businessAddressInfo.setProvince(audit.getProvince());
		microInvitationDto.setBusinessAddressInfo(businessAddressInfo);

		//结算账户信息
		MicroInvitationDto.SettlementAccountInfo settlementAccountInfo = microInvitationDto.new SettlementAccountInfo();
		settlementAccountInfo.setBankAccountType(audit.getBankAccountType());
		settlementAccountInfo.setBankCardNo(audit.getBankCardNo());
		settlementAccountInfo.setBankCode(audit.getBankCode());
//		settlementAccountInfo.setSettlementDirection(YeepayApiConstant.settlementDirection.ACCOUNT.name());
		microInvitationDto.setAccountInfo(settlementAccountInfo);

		//添加开通产品信息
		List<InvitationDto.ProductInfo> productInfos = new ArrayList<>(32);
		Arrays.stream(OpenProduct.MICRO_PRODUCTS).forEach(item -> {
			InvitationDto.ProductInfo productInfo = PlatformCache.productInfo(item);
			productInfo.setProductName(null);
			productInfos.add(productInfo);
		});
		microInvitationDto.setProductInfo(productInfos);

		//产品资质信息
		MicroInvitationDto.ProductQualificationInfo productQualificationInfo = microInvitationDto.new ProductQualificationInfo();
		productQualificationInfo.setPaymentScene("GYSDK");
		microInvitationDto.setProductQualificationInfo(productQualificationInfo);

		microInvitationDto.setParentMerchantNo(yeePayConst.getPlatformServiceNo());

		return microInvitationDto;
	}
}
