package com.yifu.cloud.plus.v1.ekp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.IcbcApiException;
import com.icbc.api.IcbcConstants;
import com.icbc.api.request.JftApiB2bpayTransqueryRequestV1;
import com.icbc.api.request.JftApiPayB2bpayGenpreorderRequestV1;
import com.icbc.api.response.JftApiB2bpayTransqueryResponseV1;
import com.icbc.api.response.JftApiPayB2bpayGenpreorderResponseV1;
import com.yifu.cloud.plus.v1.ekp.config.IcbcConfigProperties;
import com.yifu.cloud.plus.v1.ekp.entity.EkpSocialInfo;
import com.yifu.cloud.plus.v1.ekp.mapper.EkpSocialInfoMapper;
import com.yifu.cloud.plus.v1.ekp.service.IcbcTransactionFlowQueryService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 聚富通到账通知交易流水查询
 *
 * @author huyc
 * @date 2024-05-23 11:21:56
 */
@Log4j2
@Service
@RequiredArgsConstructor
@EnableConfigurationProperties(IcbcConfigProperties.class)
public class IcbcTransactionFlowQueryServiceImpl extends ServiceImpl<EkpSocialInfoMapper, EkpSocialInfo> implements IcbcTransactionFlowQueryService {

	@Autowired
	private IcbcConfigProperties icbcConfigProperties;

	public R getIcbcTransactionFlow() {
		//应用id 应用方私钥 网关公钥 应用方加密串
		String APP_ID = "11000000000000015602";
		//应用方私钥
		String MY_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCudy1tOyVTSCjJD9ej/IXmIKHTBLJ3FPYyhUnAt65hXgTD7OJso3+1cc+AakLGTLrSm9pxxd2AsUkVbdYFsXyq0rQOkG71ULPrzYI/XfbIcjf8dleFkTlEi+bVNN3J3LzbY4uP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hFWC0lA7rCwLAlGFQQKBgBmpf03hpMEP6xfuWR6banW8ZR/MRUKTteOcPbGn0cIf06JZUV9K7StWkznAnerotcLtMO3LiJrv8GdKsfqquFg+SHyNIgAoa79c6/lZexcfGdPFezehHf48Sf0b632ONRF+kY8VTZ2/PHkRz6G2A8v1Eq4USlJkZi/s1/E+sWQBAoGAMU8mm9SGWka1hPYOI3ONCzeBbRBNulcS7B7luGcEbhPwi0p77xciSUy64GSVjZboD/DX9n3m7rU2GIqr50gkpQuTW9N9PNr7z74Tk36bCp5MpEeybMOgv8hovEQBa5PoTbAm9qMf9Xm1whQXUzhjcWR7vT4qAg1A+QXX58G9EkA=";
		//网关公钥
		String APIGW_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwFgHD4kzEVPdOj03ctKM7KV+16bWZ5BMNgvEeuEQwfQYkRVwI9HFOGkwNTMn5hiJXHnlXYCX+zp5r6R52MY0O7BsTCLT7aHaxsANsvI9ABGx3OaTVlPB59M6GPbJh0uXvio0m1r/lTW3Z60RU6Q3oid/rNhP3CiNgg0W6O3AGqwIDAQAB";
		//应用方加密串
		String AES_Key = "5xGJdh7qb+B95SUoxDlatg==";
		DefaultIcbcClient client = new DefaultIcbcClient(APP_ID, IcbcConstants.SIGN_TYPE_RSA2,
				MY_PRIVATE_KEY, IcbcConstants.CHARSET_UTF8, IcbcConstants.FORMAT_JSON, APIGW_PUBLIC_KEY,
				AES_Key, icbcConfigProperties.getAesKey(), "", "");
		JftApiB2bpayTransqueryRequestV1 request = new JftApiB2bpayTransqueryRequestV1();
		request.setServiceUrl("https://apipcs3.dccnet.com.cn/api/jft/api/b2bpay/transquery/V1");

		JftApiB2bpayTransqueryRequestV1.JftApiB2bpayTransqueryRequestV1Biz bizContent = new
				JftApiB2bpayTransqueryRequestV1.JftApiB2bpayTransqueryRequestV1Biz();
		bizContent.setAppId(APP_ID);
//		bizContent.setOutVendorId("955888");
//		bizContent.setOutUserId("874");
		bizContent.setCardNo("9558830200003786563");
//		bizContent.setPayAccount("0200000309200211917");
		bizContent.setStartTrxDate("20240401");
		bizContent.setEndTrxDate("20240730");
		bizContent.setStartId("1");
		bizContent.setEndId("50");
		request.setBizContent(bizContent);
		JftApiB2bpayTransqueryResponseV1 response;
		try {
			//到账通知交易流水查询
			response = client.execute(request);
			log.info(request.toString());
			log.info(response.toString());
			if ("00".equals(response.getStatus())) {
				return R.ok();
			} else {
				return R.failed();
			}
		} catch (IcbcApiException e) {
			e.printStackTrace();
			return R.ok();
		}
	}

	public R saveIcbcManagerCard() {
		//应用id 应用方私钥 网关公钥 应用方加密串
		String APP_ID = "11000000000000015602";
		//应用方私钥
		String MY_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCudy1tOyVTSCjJD9ej/IXmIKHTBLJ3FPYyhUnAt65hXgTD7OJso3+1cc+AakLGTLrSm9pxxd2AsUkVbdYFsXyq0rQOkG71ULPrzYI/XfbIcjf8dleFkTlEi+bVNN3J3LzbY4uPhPztDbkcwBoawl1jhcGTkFdIUCfJ8s9zXAAKgHjeNLXwrdnfvlTn8sfJX3JvrQ9W1Ji2NhCzAg7/fgJ9wAmS+aWKKBuc3ge5DQYcTtIe8hBeKSh6WZkjdUOja6ytOwEGCA3KiCVTw42rP/njrMAR3rh2ptXQtlLekMIEtiN3QAnIWp8IDg8uBEKr0aye6m/IfKMNpbbyaKqaeBUhAgMBAAECggEAOp0rk+ElHx/FJBfAeQWj7rbT51NSqhNOy5ZgOvD7ExdFPpXdVGZxx4HfFuOcX4bz1UIrV8IEMvJc/WgTWYJRwgpcF/CKdryQVg6LTcLB4IIPMTCJIwSxWsgt2z+Rq/oPMR32i1dAqlvL707S9l6KhZ8wc+UwjcjwNTbwX29vTBdiTsFS6QJBaUytu+nHoHvVrpg4botifiokHR1JX0hWjxP/Dh29aooaIbSu80A2sRPLIvRtCzxGjfcZP+H1ZtE439SCguuI+pOqWWLmzUV743P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+zp5r6R52MY0O7BsTCLT7aHaxsANsvI9ABGx3OaTVlPB59M6GPbJh0uXvio0m1r/lTW3Z60RU6Q3oid/rNhP3CiNgg0W6O3AGqwIDAQAB";
		//应用方加密串
		String AES_Key = "5xGJdh7qb+B95SUoxDlatg==";
		DefaultIcbcClient client = new DefaultIcbcClient(APP_ID, IcbcConstants.SIGN_TYPE_RSA2,
				MY_PRIVATE_KEY, IcbcConstants.CHARSET_UTF8, IcbcConstants.FORMAT_JSON,
				APIGW_PUBLIC_KEY, IcbcConstants.ENCRYPT_TYPE_AES, AES_Key, null, null);
		JftApiPayB2bpayGenpreorderRequestV1 request = new JftApiPayB2bpayGenpreorderRequestV1();
		request.setServiceUrl("https://apipcs3.dccnet.com.cn/api/jft/api/pay/b2bpay/genpreorder/V1");
		JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz bizContent =
				new JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz();
		bizContent.setAppId(APP_ID);
		bizContent.setOutOrderId("202406200000066688");
		bizContent.setPayMethod("05");
		bizContent.setMultiRecFlag("0");
		bizContent.setStlFlag("0");
		bizContent.setAsynFlag("0");
		bizContent.setOutVendorId("158742714");
		bizContent.setOutUserId("0202401200000000039");
//		bizContent.setRecAcctId("123654123");
//		bizContent.setPayerAcctId("1001765234");
		bizContent.setTradeTime("2024-06-21 10:41:56");
		bizContent.setTrxChannel("01");
		bizContent.setTrxIp("172.16.24.98");
		bizContent.setPayModeTool("5");
		bizContent.setPayPhoneno("18856151558");
		bizContent.setPayerType("1");
		bizContent.setOrderAmount("2.00");
		bizContent.setPayAmount("2.00");
		bizContent.setFloatRate("888");
		bizContent.setProvince("安徽");
		bizContent.setCity("合肥市");
		bizContent.setCounty("包河区");
		bizContent.setEmail("123456@icbc.com");
		bizContent.setPhone("1388888888");
		bizContent.setAddress("安徽");
		bizContent.setPost("5168845");
		bizContent.setPayRem("试一下");
		bizContent.setOrderRem("食品");
		bizContent.setAddRemark("食品");
		bizContent.setPayerName("aigc科技集团");
		bizContent.setPayerAcctNum("0200062009212528888");
		bizContent.setCurrency("001");
		bizContent.setPayerWalletId("1234567890123456789012");
		bizContent.setPayerWalletName("XXXXX有限公司");
		bizContent.setNotifyUrl("http://www.xxx.com");
		bizContent.setJumpUrl("http://www.xxx.com");
		JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.Good
				goodsInfo = new JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.Good();
		goodsInfo.setGoodsSeqno("1");
		goodsInfo.setGoodsName("牛肉干");
		goodsInfo.setGoodsNum("10");
		goodsInfo.setGoodsAmt("10");
		goodsInfo.setWeight("肉干");
		goodsInfo.setPrice("50");
		goodsInfo.setSellerAddress("13866668");
		goodsInfo.setSellerName("牛大");
		goodsInfo.setUnits("斤");
		goodsInfo.setOutVendorId("158742714");
//		JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.Good
//				goodsInfo2 = new JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.Good();
//		goodsInfo2.setGoodsSeqno("2");
//		goodsInfo2.setGoodsName("商品");
//		goodsInfo2.setGoodsNum("10");
//		goodsInfo2.setGoodsAmt("10");
//		goodsInfo2.setWeight("商品规格");
//		goodsInfo2.setPrice("50");
//		goodsInfo2.setSellerAddress("13866668");
//		goodsInfo2.setSellerName("张三");
//		goodsInfo2.setUnits("斤");
//		goodsInfo2.setOutVendorId("158742714");
		List<JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.Good> goodList = new ArrayList<>();
		goodList.add(goodsInfo);
//		goodList.add(goodsInfo2);
		bizContent.setGoodsInfo(goodList);
		JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.RecInfo
				recInfo1 = new JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.RecInfo();
		recInfo1.setOutVendorId("158742714");
		recInfo1.setRecAcctId("123654123");
		recInfo1.setTrxAmount("2.00");
		recInfo1.setOrderAmount("2.00");
//		JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.RecInfo
//				recInfo2 = new JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.RecInfo();
//		recInfo2.setOutVendorId("158742714");
//		recInfo2.setRecAcctId("04187335");
//		recInfo2.setTrxAmount("1.00");
//		recInfo2.setOrderAmount("1.00");
		List<JftApiPayB2bpayGenpreorderRequestV1.JftApiPayB2bpayGenpreorderRequestV1Biz.RecInfo> recList = new ArrayList<>();
		recList.add(recInfo1);
//		recList.add(recInfo2);
		bizContent.setRecList(recList);
		bizContent.setSummary("测试一下");
		bizContent.setPurpose("测试一下");
		request.setBizContent(bizContent);
		try {
			JftApiPayB2bpayGenpreorderResponseV1 responseV1 = client.execute(request);
			if ("00".equals(responseV1.getStatus())) {
				return R.ok();
			} else {
				return R.failed();
			}
		} catch (IcbcApiException e) {
			e.printStackTrace();
			return R.ok();
		}
	}
}
